1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/test/stdlib/random_test.codon

65 lines
1.4 KiB
Python
Raw Normal View History

2021-09-27 14:02:44 -04:00
import random as R
import time
import sys
seed = int(time.time())
# sys.stderr.write('seed: ' + str(seed) + '\n')
R.seed(seed)
2022-01-24 11:32:33 +01:00
2021-09-27 14:02:44 -04:00
@test
def test_rnd_result(name, results, invariant):
print name
for a in results:
assert invariant(a)
2022-01-24 11:32:33 +01:00
test_rnd_result(
"randrange", [R.randrange(10) for _ in range(100)], range(10).__contains__
)
test_rnd_result(
"randrange", [R.randrange(5, 20) for _ in range(100)], range(5, 20).__contains__
)
test_rnd_result(
"randrange",
[R.randrange(9, 99, 3) for _ in range(100)],
range(9, 99, 3).__contains__,
)
test_rnd_result(
"randint", [R.randint(5, 20) for _ in range(100)], range(5, 20 + 1).__contains__
)
population = list("ABCDEFGHIJKLMNOP")
test_rnd_result(
"choice", [R.choice(population) for _ in range(100)], population.__contains__
)
test_rnd_result(
"choice", [R.choice(population) for _ in range(100)], population.__contains__
)
test_rnd_result("choices", R.choices(population), population.__contains__)
test_rnd_result("choices", R.choices(population, k=100), population.__contains__)
2021-09-27 14:02:44 -04:00
@test
def test_shuffle(v):
s = copy(v)
R.shuffle(s)
assert sorted(v) == sorted(s)
2022-01-24 11:32:33 +01:00
2021-09-27 14:02:44 -04:00
test_shuffle(list(range(100)))
2022-01-24 11:32:33 +01:00
2021-09-27 14:02:44 -04:00
@test
def test_sample(n: int, k: int):
s = R.sample(list(range(n)), k=k)
assert len(s) == k
assert len(set(s)) == len(s)
for a in s:
assert a in range(n)
2022-01-24 11:32:33 +01:00
2021-09-27 14:02:44 -04:00
test_sample(100, 5)
test_sample(100, 100)
test_sample(100, 0)