Repository navigation
Expand file tree
/
Copy pathrandom_test.go
More file actions
743 lines (731 loc) · 16.9 KB
/
Copy pathrandom_test.go
File metadata and controls
743 lines (731 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package random_test
import (
"testing"
itn "github.com/1set/starlet/internal"
"github.com/1set/starlet/lib/random"
"go.starlark.net/starlark"
)
func TestLoadModule_Random(t *testing.T) {
var (
repeatTimes = 20
one = starlark.MakeInt(1)
two = starlark.MakeInt(2)
three = starlark.MakeInt(3)
)
tests := []struct {
name string
script string
wantErr string
checkResult func(res starlark.Value) bool
}{
{
name: `nil choice`,
script: itn.HereDoc(`
load('random', 'choice')
choice()
`),
wantErr: `random.choice: missing argument for seq`,
},
{
name: `no choice`,
script: itn.HereDoc(`
load('random', 'choice')
choice([])
`),
wantErr: `cannot choose from an empty sequence`,
},
{
name: `invalid choice`,
script: itn.HereDoc(`
load('random', 'choice')
choice(123)
`),
wantErr: `random.choice: for parameter seq: got int, want starlark.Indexable`,
},
{
name: `one choice`,
script: itn.HereDoc(`
load('random', 'choice')
val = choice([1])
`),
checkResult: func(res starlark.Value) bool {
return res.(starlark.Int) == one
},
},
{
name: `two choices`,
script: itn.HereDoc(`
load('random', 'choice')
val = choice([1, 2])
`),
checkResult: func(res starlark.Value) bool {
val := res.(starlark.Int)
return val == one || val == two
},
},
{
name: `duplicate choices`,
script: itn.HereDoc(`
load('random', 'choice')
val = choice([1, 1, 2, 3, 3, 2, 1])
`),
checkResult: func(res starlark.Value) bool {
val := res.(starlark.Int)
return val == one || val == two || val == three
},
},
{
name: `same choices`,
script: itn.HereDoc(`
load('random', 'choice')
val = choice((3, 3, 3, 3, 3, 3, 3, 3, 3))
`),
checkResult: func(res starlark.Value) bool {
return res.(starlark.Int) == three
},
},
// choices
{
name: "choices with no args",
script: itn.HereDoc(`
load('random', 'choices')
choices()
`),
wantErr: `random.choices: missing argument for population`,
},
{
name: "choices with invalid type",
script: itn.HereDoc(`
load('random', 'choices')
choices(123)
`),
wantErr: `random.choices: for parameter population: got int, want starlark.Indexable`,
},
{
name: "choices with empty population",
script: itn.HereDoc(`
load('random', 'choices')
choices([])
`),
wantErr: `population is empty`,
},
{
name: "choices with mismatch weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=[1, 2])
`),
wantErr: `the number of weights does not match the population`,
},
{
name: "choices with mismatch cum_weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], cum_weights=[1, 2])
`),
wantErr: `the number of weights does not match the population`,
},
{
name: "choices with both weights and cum_weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=[1, 1, 1], cum_weights=[1, 2, 3])
`),
wantErr: `cannot specify both weights and cumulative weights`,
},
{
name: "choices with negative weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=[1, -10, 1])
`),
wantErr: `total of weights must be greater than zero`,
},
{
name: "choices with decreasing cum_weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], cum_weights=[3, 2, 2])
`),
wantErr: `cumulative weights must be non-decreasing`,
},
{
name: "choices with non-number weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=["A", "B", "C"])
`),
wantErr: `weights must be numeric`,
},
{
name: "choices with non-number cum_weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], cum_weights=[1, 2, "three"])
`),
wantErr: `weights must be numeric`,
},
{
name: "choices with zero weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=[0, 0, 0])
`),
wantErr: `total of weights must be greater than zero`,
},
{
name: "choices with nan weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=[1, 2, float('nan')])
`),
wantErr: `total of weights must be finite`,
},
{
name: "choices with inf weights",
script: itn.HereDoc(`
load('random', 'choices')
choices([1, 2, 3], weights=[1, 2, float('inf')])
`),
wantErr: `total of weights must be finite`,
},
{
name: "choices without weights",
script: itn.HereDoc(`
load('random', 'choices')
print(choices([1, 2, 3]))
`),
},
{
name: "choices weights",
script: itn.HereDoc(`
load('random', 'choices')
print(choices((1, 2, 3), weights=[1, 1.5, 5]))
`),
},
{
name: "choices cum_weights",
script: itn.HereDoc(`
load('random', 'choices')
print(choices([1, 2, 3], cum_weights=[1, 2, 8]))
`),
},
{
name: "choices indexable",
script: itn.HereDoc(`
load('random', 'choices')
choices(range(3), cum_weights=[0, 0, 8])
`),
},
{
name: "choices with k<=0",
script: itn.HereDoc(`
load('random', 'choices')
a = choices([1, 2, 3], k=0)
assert.eq(a, [])
b = choices([1, 2, 3], k=-7)
assert.eq(b, [])
`),
},
{
name: "choices with k = 2",
script: itn.HereDoc(`
load('random', 'choices')
a = choices([1, 2, 3], k=2)
assert.eq(len(a), 2)
`),
},
{
// k sizes the result slice; a huge k would OOM the host
name: "choices with k over the cap",
script: itn.HereDoc(`
load('random', 'choices')
a = choices([1, 2, 3], k=1099511627776)
`),
wantErr: `random.choices: k is too large`,
},
{
name: "choices weights has 0",
script: itn.HereDoc(`
load('random', 'choices')
a = choices([1, 2, 3], weights=[0, 1, 0])
assert.eq(a, [2])
`),
},
{
name: "choices cum_weights has 0",
script: itn.HereDoc(`
load('random', 'choices')
a = choices([1, 2, 3, 4, 5], cum_weights=[0, 0, 1, 1, 1])
assert.eq(a, [3])
`),
},
// shuffle
{
name: "shuffle with invalid type",
script: itn.HereDoc(`
load('random', 'shuffle')
x = 123
shuffle(x)
`),
wantErr: `random.shuffle: for parameter seq: got int, want starlark.HasSetIndex`,
},
{
name: "shuffle with immutable type",
script: itn.HereDoc(`
load('random', 'shuffle')
x = (1, 2, 3)
shuffle(x)
`),
wantErr: `random.shuffle: for parameter seq: got tuple, want starlark.HasSetIndex`,
},
{
name: "shuffle frozen",
script: itn.HereDoc(`
load('random', 'shuffle')
load('freeze.star', 'freeze')
x = [1, 2, 3]
freeze(x)
shuffle(x)
`),
wantErr: `cannot assign to element of frozen list`,
},
{
name: "shuffle with empty type",
script: itn.HereDoc(`
load('random', 'shuffle')
val = []
shuffle(val)
`),
checkResult: func(res starlark.Value) bool {
return res.(*starlark.List).Len() == 0
},
},
{
name: "shuffle with one element",
script: itn.HereDoc(`
load('random', 'shuffle')
val = [1]
shuffle(val)
`),
checkResult: func(res starlark.Value) bool {
l := res.(*starlark.List)
return l.Len() == 1 && l.Index(0) == one
},
},
{
name: "shuffle with two elements",
script: itn.HereDoc(`
load('random', 'shuffle')
val = [2, 3]
shuffle(val)
`),
checkResult: func(res starlark.Value) bool {
l := res.(*starlark.List)
return l.Len() == 2 && ((l.Index(0) == two && l.Index(1) == three) || (l.Index(0) == three && l.Index(1) == two))
},
},
{
name: "shuffle with mutable type",
script: itn.HereDoc(`
load('random', 'shuffle')
val = [1, 2, 3]
shuffle(val)
print(val)
`),
checkResult: func(res starlark.Value) bool {
val := res.(*starlark.List)
return val.Index(0) == one || val.Index(0) == two || val.Index(0) == three
},
},
{
name: "randint with less than 2 args",
script: itn.HereDoc(`
load('random', 'randint')
randint()
`),
wantErr: `random.randint: missing argument for a`,
},
{
name: "randint with more than 2 args",
script: itn.HereDoc(`
load('random', 'randint')
randint(1, 2, 3)
`),
wantErr: `random.randint: got 3 arguments, want at most 2`,
},
{
name: "randint with invalid type",
script: itn.HereDoc(`
load('random', 'randint')
randint(1, '2')
`),
wantErr: `random.randint: for parameter b: got string, want int`,
},
{
name: "randint with invalid range",
script: itn.HereDoc(`
load('random', 'randint')
randint(2, 1)
`),
wantErr: `a must be less than or equal to b`,
},
{
name: "randint with equal range",
script: itn.HereDoc(`
load('random', 'randint')
val = randint(1, 1)
`),
checkResult: func(res starlark.Value) bool {
return res.(starlark.Int) == one
},
},
{
name: "randint with range 1",
script: itn.HereDoc(`
load('random', 'randint')
val = randint(1, 2)
`),
checkResult: func(res starlark.Value) bool {
return res.(starlark.Int) == one || res.(starlark.Int) == two
},
},
{
name: "randint with range 2",
script: itn.HereDoc(`
load('random', 'randint')
val = randint(1, 3)
print(val)
`),
checkResult: func(res starlark.Value) bool {
return res.(starlark.Int) == one || res.(starlark.Int) == two || res.(starlark.Int) == three
},
},
{
name: "randbytes with less than 1 args",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes()
assert.eq(len(x), 10)
`),
},
{
name: "randbytes with invalid args",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes(-2)
assert.eq(len(x), 10)
y = randbytes(0)
assert.eq(len(y), 10)
`),
},
// n went straight into make(): a huge-but-int64 n OOM-killed the host,
// and a value past int64 made Int64() undefined (negative -> make panic,
// wrapped -> silently empty). All three are now bounded errors.
{
name: "randbytes with n over the cap",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes(1099511627776)
`),
wantErr: `random.randbytes: n is too large`,
},
{
name: "randbytes with n beyond int64",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes(18446744073709551616)
`),
wantErr: `random.randbytes: n is too large`,
},
{
name: "randstr with n over the cap",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr('abc', 1099511627776)
`),
wantErr: `random.randstr: n is too large`,
},
{
name: "randb32 with n over the cap",
script: itn.HereDoc(`
load('random', 'randb32')
x = randb32(1099511627776)
`),
wantErr: `random.randb32: n is too large`,
},
{
name: "randbytes at the cap still works",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes(1048576)
assert.eq(len(x), 1048576)
`),
},
{
name: "randbytes with invalid type",
script: itn.HereDoc(`
load('random', 'randbytes')
randbytes('1')
`),
wantErr: `random.randbytes: for parameter n: got string, want int`,
},
{
name: "randbytes with 1",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes(1)
assert.eq(len(x), 1)
print(x)
`),
},
{
name: "randbytes with 20",
script: itn.HereDoc(`
load('random', 'randbytes')
x = randbytes(20)
assert.eq(len(x), 20)
`),
},
{
name: "randstr with less than 1 args",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr()
`),
wantErr: `random.randstr: missing argument for chars`,
},
{
name: "randstr with invalid args",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr(123)
`),
wantErr: `random.randstr: for parameter chars: got int, want string`,
},
{
name: "randstr with invalid N",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr('abc', -2)
assert.eq(len(x), 10)
y = randstr('abc', 0)
assert.eq(len(y), 10)
`),
},
{
name: "randstr with empty chars",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr('', 10)
`),
wantErr: `chars must not be empty`,
},
{
name: "randstr with n=1",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr('我爱你', 1)
assert.eq(len(x), 3)
cs = ["我", "爱", "你"]
assert.true(x in cs)
print(x)
`),
},
{
name: "randstr with same chars",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr('AAA', 10)
assert.eq(len(x), 10)
assert.eq(x, 'AAAAAAAAAA')
`),
},
{
name: "randstr for unicode",
script: itn.HereDoc(`
load('random', 'randstr')
x = randstr('你好', 2)
assert.eq(len(x), 6)
cs = ["你好", "好你", "好好", "你你"]
assert.true(x in cs)
print(x)
print(randstr("abcdefghijklmnopqrstuvwxyz", 10))
`),
},
{
name: "random",
script: itn.HereDoc(`
load('random', 'random')
val = random()
print(val)
`),
checkResult: func(res starlark.Value) bool {
f := res.(starlark.Float)
return f >= 0 && f < 1
},
},
{
name: "random with invalid args",
script: itn.HereDoc(`
load('random', 'random')
random(1)
`),
wantErr: `random.random: got 1 arguments, want 0`,
},
{
name: "uniform with less than 2 args",
script: itn.HereDoc(`
load('random', 'uniform')
uniform()
`),
wantErr: `random.uniform: missing argument for a`,
},
{
name: "uniform with more than 2 args",
script: itn.HereDoc(`
load('random', 'uniform')
uniform(1, 2, 3)
`),
wantErr: `random.uniform: got 3 arguments, want at most 2`,
},
{
name: "uniform with invalid type",
script: itn.HereDoc(`
load('random', 'uniform')
uniform('1', '2')
`),
wantErr: `random.uniform: for parameter a: got string, want float`,
},
{
name: "uniform with int",
script: itn.HereDoc(`
load('random', 'uniform')
val = uniform(1, 2)
`),
checkResult: func(res starlark.Value) bool {
f := res.(starlark.Float)
return f >= 1 && f < 2
},
},
{
name: "uniform with float",
script: itn.HereDoc(`
load('random', 'uniform')
val = uniform(1.0, 2.0)
`),
checkResult: func(res starlark.Value) bool {
f := res.(starlark.Float)
return f >= 1 && f < 2
},
},
{
name: "uniform with equal range",
script: itn.HereDoc(`
load('random', 'uniform')
val = uniform(1, 1)
`),
checkResult: func(res starlark.Value) bool {
f := res.(starlark.Float)
return f == 1
},
},
{
name: "uniform with reversed range",
script: itn.HereDoc(`
load('random', 'uniform')
val = uniform(2, 1)
`),
checkResult: func(res starlark.Value) bool {
f := res.(starlark.Float)
return f >= 1 && f < 2
},
},
{
name: "uuid",
script: itn.HereDoc(`
load('random', 'uuid')
val = uuid()
print(val)
assert.eq(len(val), 36)
assert.eq(len(val.replace("-", "")), 32)
`),
},
{
name: "uuid with invalid args",
script: itn.HereDoc(`
load('random', 'uuid')
uuid(2)
`),
wantErr: `random.uuid: got 1 arguments, want 0`,
},
{
name: "randb32 with 0 or 1 args",
script: itn.HereDoc(`
load('random', 'randb32')
x = randb32()
assert.eq(len(x), 10)
y = randb32(6)
assert.eq(len(y), 6)
`),
},
{
name: "randb32 with incorrect args",
script: itn.HereDoc(`
load('random', 'randb32')
x = randb32(-2)
assert.eq(len(x), 10)
y = randb32(0)
assert.eq(len(y), 10)
`),
},
{
name: "randb32 with invalid type",
script: itn.HereDoc(`
load('random', 'randb32')
randb32('1')
`),
wantErr: `random.randb32: for parameter n: got string, want int`,
},
{
name: "randb32 with sep",
script: itn.HereDoc(`
load('random', 'randb32')
x = randb32(20, 5)
assert.eq(len(x), 20+3)
assert.eq(x[5], '-')
assert.eq(len(x.split('-')), 4)
y = randb32(20, 0)
assert.eq(len(y), 20)
z = randb32(20, -1)
assert.eq(len(z), 20)
w = randb32(20, 20)
assert.eq(len(w), 20)
t = randb32(20, 21)
assert.eq(len(t), 20)
u = randb32(20, 22)
v = randb32(20, 1)
assert.eq(len(v), 20+19)
assert.eq(v[1], '-')
assert.eq(len(v.split('-')), 20)
`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for i := 1; i <= repeatTimes; i++ {
res, err := itn.ExecModuleWithErrorTest(t, random.ModuleName, random.LoadModule, tt.script, tt.wantErr, nil)
if (err != nil) != (tt.wantErr != "") {
t.Errorf("random(%q) expects error = '%v', actual error = '%v', result = %v", tt.name, tt.wantErr, err, res)
return
}
if tt.wantErr != "" {
return
}
if tt.checkResult != nil && !tt.checkResult(res["val"]) {
t.Errorf("random(%q) got unexpected result, actual result = %v", tt.name, res)
}
}
})
}
}