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
Ibrahim Numanagić 5de233a64e
Dynamic Polymorphism ()
* Use Static[] for static inheritance

* Support .seq extension

* Fix 

* Polymorphic typechecking; vtables [wip]

* v-table dispatch [wip]

* vtable routing [wip; bug]

* vtable routing [MVP]

* Fix texts

* Add union type support

* Update FAQs

* Clarify

* Add BSL license

* Add makeUnion

* Add IR UnionType

* Update union representation in LLVM

* Update README

* Update README.md

* Update README

* Update README.md

* Add benchmarks

* Add more benchmarks and README

* Add primes benchmark

* Update benchmarks

* Fix cpp

* Clean up list

* Update faq.md

* Add binary trees benchmark

* Add fannkuch benchmark

* Fix paths

* Add PyPy

* Abort on fail

* More benchmarks

* Add cpp word_count

* Update set_partition cpp

* Add nbody cpp

* Add TAQ cpp; fix word_count timing

* Update CODEOWNERS

* Update README

* Update README.md

* Update CODEOWNERS

* Fix bench script

* Update binary_trees.cpp

* Update taq.cpp

* Fix primes benchmark

* Add mandelbrot benchmark

* Fix OpenMP init

* Add Module::unsafeGetUnionType

* UnionType [wip] [skip ci]

* Integrate IR unions and Union

* UnionType refactor [skip ci]

* Update README.md

* Update docs

* UnionType [wip] [skip ci]

* UnionType and automatic unions

* Add Slack

* Update faq.md

* Refactor types

* New error reporting [wip]

* New error reporting [wip]

* peglib updates [wip] [skip_ci]

* Fix parsing issues

* Fix parsing issues

* Fix error reporting issues

* Make sure random module matches Python

* Update releases.md

* Fix tests

* Fix 

* Fix 

* Fix 

* Fix 

* Fix ; Fix ; Fix ; Fix 

* Fix collection extension methods

* Fix 

* Handle *args/**kwargs with Callable[]; Fix 

* Fix 

* Fix Ptr.__sub__; Fix polymorphism issues

* Add typeinfo

* clang-format

* Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support

* Use CPM for semver and toml++

* Remove extension check

* Revamp str methods

* Update str.zfill

* Fix thunk crashes [wip] [skip_ci]

* Fix str.__reversed__

* Fix count_with_max

* Fix vtable memory allocation issues

* Add poly AST tests

* Use PDQsort when stability does not matter

* Fix dotted imports; Fix  issues

* Fix kwargs passing to Python

* Fix 

* Fix 

* Add isinstance support for unions; Union methods return Union type if different

* clang-format

* Nicely format error tracebacks

* Fix build issues; clang-format

* Fix OpenMP init

* Fix OpenMP init

* Update README.md

* Fix tests

* Update license [skip ci]

* Update license [ci skip]

* Add copyright header to all source files

* Fix super(); Fix error recovery in ClassStmt

* Clean up whitespace [ci skip]

* Use Python 3.9 on CI

* Print info in random test

* Fix single unions

* Update random_test.codon

* Fix polymorhic thunk instantiation

* Fix random test

* Add operator.attrgetter and operator.methodcaller

* Add code documentation

* Update documentation

* Update README.md

* Fix tests

* Fix random init

Co-authored-by: A. R. Shajii <ars@ars.me>
2022-12-04 19:45:21 -05:00

120 lines
3.3 KiB
Python

import random as R
import time
import sys
from copy import copy
seed = int(time.time())
# sys.stderr.write('seed: ' + str(seed) + '\n')
R.seed(seed)
@test
def test_rnd_result(name, results, invariant):
print(name)
for a in results:
assert invariant(a)
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__)
@test
def test_shuffle(v):
s = copy(v)
R.shuffle(s)
assert sorted(v) == sorted(s)
test_shuffle(list(range(100)))
@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)
test_sample(100, 5)
test_sample(100, 100)
test_sample(100, 0)
from python import random as Rpy
@test
def test_vs_python(*args, seed, method: Static[str], T: type = float):
print(seed, method, args)
R1 = R.Random(seed)
R2 = Rpy.Random(seed)
N = 50
A1 = [T(getattr(R1, method)(*args)) for _ in range(N)]
A2 = [T.__from_py__(getattr(R2, method)(*args).p) for _ in range(N)]
assert A1 == A2
test_vs_python(-10, 10, seed=22, method='randrange', T=int)
test_vs_python(-10, 10, 3, seed=33, method='randrange', T=int)
test_vs_python(-10, 10, seed=44, method='randint', T=int)
test_vs_python(20, seed=55, method='getrandbits', T=int)
test_vs_python(32, seed=55, method='getrandbits', T=int)
test_vs_python(40, seed=55, method='getrandbits', T=int)
test_vs_python(63, seed=55, method='getrandbits', T=int)
test_vs_python(seed=0, method='random')
test_vs_python(-12.5, 101.2, seed=1, method='uniform')
test_vs_python(-13, 5.5, 0, seed=2, method='triangular')
#test_vs_python(1.0, 2, seed=3, method='betavariate') # different in older Python versions
test_vs_python(0.3, seed=4, method='expovariate')
#test_vs_python(1.0, 2, seed=5, method='gammavariate') # different in older Python versions
test_vs_python(1.0, 2.0, seed=-101, method='gauss')
test_vs_python(1.0, 2.0, seed=-102, method='lognormvariate')
test_vs_python(1.0, 2.0, seed=-103, method='normalvariate')
test_vs_python(1.0, 2.0, seed=0xffffffff, method='vonmisesvariate')
test_vs_python(1.0, seed=0xffffffff-1, method='paretovariate')
test_vs_python(1.0, 2.0, seed=0, method='weibullvariate')
@test
def test_state():
r = R.Random(1234)
state = r.getstate()
N = 100
A1 = [r.random() for _ in range(N)]
B1 = [r.gauss() for _ in range(N)]
r.setstate(state)
A2 = [r.random() for _ in range(N)]
B2 = [r.gauss() for _ in range(N)]
r.seed(1234)
A3 = [r.random() for _ in range(N)]
B3 = [r.gauss() for _ in range(N)]
assert A1 == A2 == A3
assert B1 == B2 == B3
test_state()