test.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. from ctypes import *
  2. from random import *
  3. import math
  4. import os
  5. import platform
  6. import time
  7. import gc
  8. from enum import Enum
  9. import argparse
  10. parser = argparse.ArgumentParser(
  11. description = "Odin core:math/big test suite",
  12. epilog = "By default we run regression and random tests with preset parameters.",
  13. formatter_class = argparse.ArgumentDefaultsHelpFormatter,
  14. )
  15. #
  16. # Normally, we report the number of passes and fails. With this option set, we exit at first fail.
  17. #
  18. parser.add_argument(
  19. "-exit-on-fail",
  20. help = "Exit when a test fails",
  21. action = "store_true",
  22. )
  23. #
  24. # We skip randomized tests altogether if this is set.
  25. #
  26. no_random = parser.add_mutually_exclusive_group()
  27. no_random.add_argument(
  28. "-no-random",
  29. help = "No random tests",
  30. action = "store_true",
  31. )
  32. #
  33. # Normally we run a given number of cycles on each test.
  34. # Timed tests budget 1 second per 20_000 bits instead.
  35. #
  36. # For timed tests we budget a second per `n` bits and iterate until we hit that time.
  37. #
  38. timed_or_fast = no_random.add_mutually_exclusive_group()
  39. timed_or_fast.add_argument(
  40. "-timed",
  41. type = bool,
  42. default = False,
  43. help = "Timed tests instead of a preset number of iterations.",
  44. )
  45. parser.add_argument(
  46. "-timed-bits",
  47. type = int,
  48. metavar = "BITS",
  49. default = 20_000,
  50. help = "Timed tests. Every `BITS` worth of input is given a second of running time.",
  51. )
  52. #
  53. # For normal tests (non-timed), `-fast-tests` cuts down on the number of iterations.
  54. #
  55. timed_or_fast.add_argument(
  56. "-fast-tests",
  57. help = "Cut down on the number of iterations of each test",
  58. action = "store_true",
  59. )
  60. args = parser.parse_args()
  61. EXIT_ON_FAIL = args.exit_on_fail
  62. #
  63. # How many iterations of each random test do we want to run?
  64. #
  65. BITS_AND_ITERATIONS = [
  66. ( 120, 10_000),
  67. ( 1_200, 1_000),
  68. ( 4_096, 100),
  69. (12_000, 10),
  70. ]
  71. if args.fast_tests:
  72. for k in range(len(BITS_AND_ITERATIONS)):
  73. b, i = BITS_AND_ITERATIONS[k]
  74. BITS_AND_ITERATIONS[k] = (b, i // 10 if i >= 100 else 5)
  75. if args.no_random:
  76. BITS_AND_ITERATIONS = []
  77. #
  78. # Where is the DLL? If missing, build using: `odin build . -build-mode:shared`
  79. #
  80. if platform.system() == "Windows":
  81. LIB_PATH = os.getcwd() + os.sep + "big.dll"
  82. elif platform.system() == "Linux":
  83. LIB_PATH = os.getcwd() + os.sep + "big.so"
  84. elif platform.system() == "Darwin":
  85. LIB_PATH = os.getcwd() + os.sep + "big.dylib"
  86. else:
  87. print("Platform is unsupported.")
  88. exit(1)
  89. TOTAL_TIME = 0
  90. UNTIL_TIME = 0
  91. UNTIL_ITERS = 0
  92. def we_iterate():
  93. if args.timed:
  94. return TOTAL_TIME < UNTIL_TIME
  95. else:
  96. global UNTIL_ITERS
  97. UNTIL_ITERS -= 1
  98. return UNTIL_ITERS != -1
  99. #
  100. # Error enum values
  101. #
  102. class Error(Enum):
  103. Okay = 0
  104. Out_Of_Memory = 1
  105. Invalid_Pointer = 2
  106. Invalid_Argument = 3
  107. Unknown_Error = 4
  108. Max_Iterations_Reached = 5
  109. Buffer_Overflow = 6
  110. Integer_Overflow = 7
  111. Division_by_Zero = 8
  112. Math_Domain_Error = 9
  113. Unimplemented = 127
  114. #
  115. # Disable garbage collection
  116. #
  117. gc.disable()
  118. #
  119. # Set up exported procedures
  120. #
  121. try:
  122. l = cdll.LoadLibrary(LIB_PATH)
  123. except:
  124. print("Couldn't find or load " + LIB_PATH + ".")
  125. exit(1)
  126. def load(export_name, args, res):
  127. export_name.argtypes = args
  128. export_name.restype = res
  129. return export_name
  130. #
  131. # Result values will be passed in a struct { res: cstring, err: Error }
  132. #
  133. class Res(Structure):
  134. _fields_ = [("res", c_char_p), ("err", c_uint64)]
  135. initialize_constants = load(l.test_initialize_constants, [], c_uint64)
  136. print("initialize_constants: ", initialize_constants())
  137. error_string = load(l.test_error_string, [c_byte], c_char_p)
  138. add = load(l.test_add, [c_char_p, c_char_p], Res)
  139. sub = load(l.test_sub, [c_char_p, c_char_p], Res)
  140. mul = load(l.test_mul, [c_char_p, c_char_p], Res)
  141. sqr = load(l.test_sqr, [c_char_p ], Res)
  142. div = load(l.test_div, [c_char_p, c_char_p], Res)
  143. # Powers and such
  144. int_log = load(l.test_log, [c_char_p, c_longlong], Res)
  145. int_pow = load(l.test_pow, [c_char_p, c_longlong], Res)
  146. int_sqrt = load(l.test_sqrt, [c_char_p ], Res)
  147. int_root_n = load(l.test_root_n, [c_char_p, c_longlong], Res)
  148. # Logical operations
  149. int_shl_digit = load(l.test_shl_digit, [c_char_p, c_longlong], Res)
  150. int_shr_digit = load(l.test_shr_digit, [c_char_p, c_longlong], Res)
  151. int_shl = load(l.test_shl, [c_char_p, c_longlong], Res)
  152. int_shr = load(l.test_shr, [c_char_p, c_longlong], Res)
  153. int_shr_signed = load(l.test_shr_signed, [c_char_p, c_longlong], Res)
  154. int_factorial = load(l.test_factorial, [c_uint64], Res)
  155. int_gcd = load(l.test_gcd, [c_char_p, c_char_p], Res)
  156. int_lcm = load(l.test_lcm, [c_char_p, c_char_p], Res)
  157. def test(test_name: "", res: Res, param=[], expected_error = Error.Okay, expected_result = "", radix=16):
  158. passed = True
  159. r = None
  160. err = Error(res.err)
  161. if err != expected_error:
  162. error_loc = res.res.decode('utf-8')
  163. error = "{}: {} in '{}'".format(test_name, err, error_loc)
  164. if len(param):
  165. error += " with params {}".format(param)
  166. print(error, flush=True)
  167. passed = False
  168. elif err == Error.Okay:
  169. r = None
  170. try:
  171. r = res.res.decode('utf-8')
  172. r = int(res.res, radix)
  173. except:
  174. pass
  175. if r != expected_result:
  176. error = "{}: Result was '{}', expected '{}'".format(test_name, r, expected_result)
  177. if len(param):
  178. error += " with params {}".format(param)
  179. print(error, flush=True)
  180. passed = False
  181. if EXIT_ON_FAIL and not passed: exit(res.err)
  182. return passed
  183. def arg_to_odin(a):
  184. if a >= 0:
  185. s = hex(a)[2:]
  186. else:
  187. s = '-' + hex(a)[3:]
  188. return s.encode('utf-8')
  189. def test_add(a = 0, b = 0, expected_error = Error.Okay):
  190. args = [arg_to_odin(a), arg_to_odin(b)]
  191. res = add(*args)
  192. expected_result = None
  193. if expected_error == Error.Okay:
  194. expected_result = a + b
  195. return test("test_add", res, [a, b], expected_error, expected_result)
  196. def test_sub(a = 0, b = 0, expected_error = Error.Okay):
  197. args = [arg_to_odin(a), arg_to_odin(b)]
  198. res = sub(*args)
  199. expected_result = None
  200. if expected_error == Error.Okay:
  201. expected_result = a - b
  202. return test("test_sub", res, [a, b], expected_error, expected_result)
  203. def test_mul(a = 0, b = 0, expected_error = Error.Okay):
  204. args = [arg_to_odin(a), arg_to_odin(b)]
  205. try:
  206. res = mul(*args)
  207. except OSError as e:
  208. print("{} while trying to multiply {} x {}.".format(e, a, b))
  209. if EXIT_ON_FAIL: exit(3)
  210. return False
  211. expected_result = None
  212. if expected_error == Error.Okay:
  213. expected_result = a * b
  214. return test("test_mul", res, [a, b], expected_error, expected_result)
  215. def test_sqr(a = 0, b = 0, expected_error = Error.Okay):
  216. args = [arg_to_odin(a)]
  217. try:
  218. res = sqr(*args)
  219. except OSError as e:
  220. print("{} while trying to square {}.".format(e, a))
  221. if EXIT_ON_FAIL: exit(3)
  222. return False
  223. expected_result = None
  224. if expected_error == Error.Okay:
  225. expected_result = a * a
  226. return test("test_sqr", res, [a], expected_error, expected_result)
  227. def test_div(a = 0, b = 0, expected_error = Error.Okay):
  228. args = [arg_to_odin(a), arg_to_odin(b)]
  229. try:
  230. res = div(*args)
  231. except OSError as e:
  232. print("{} while trying divide to {} / {}.".format(e, a, b))
  233. if EXIT_ON_FAIL: exit(3)
  234. return False
  235. expected_result = None
  236. if expected_error == Error.Okay:
  237. #
  238. # We don't round the division results, so if one component is negative, we're off by one.
  239. #
  240. if a < 0 and b > 0:
  241. expected_result = int(-(abs(a) // b))
  242. elif b < 0 and a > 0:
  243. expected_result = int(-(a // abs((b))))
  244. else:
  245. expected_result = a // b if b != 0 else None
  246. return test("test_div", res, [a, b], expected_error, expected_result)
  247. def test_log(a = 0, base = 0, expected_error = Error.Okay):
  248. args = [arg_to_odin(a), base]
  249. res = int_log(*args)
  250. expected_result = None
  251. if expected_error == Error.Okay:
  252. expected_result = int(math.log(a, base))
  253. return test("test_log", res, [a, base], expected_error, expected_result)
  254. def test_pow(base = 0, power = 0, expected_error = Error.Okay):
  255. args = [arg_to_odin(base), power]
  256. res = int_pow(*args)
  257. expected_result = None
  258. if expected_error == Error.Okay:
  259. if power < 0:
  260. expected_result = 0
  261. else:
  262. # NOTE(Jeroen): Don't use `math.pow`, it's a floating point approximation.
  263. # Use built-in `pow` or `a**b` instead.
  264. expected_result = pow(base, power)
  265. return test("test_pow", res, [base, power], expected_error, expected_result)
  266. def test_sqrt(number = 0, expected_error = Error.Okay):
  267. args = [arg_to_odin(number)]
  268. try:
  269. res = int_sqrt(*args)
  270. except OSError as e:
  271. print("{} while trying to sqrt {}.".format(e, number))
  272. if EXIT_ON_FAIL: exit(3)
  273. return False
  274. expected_result = None
  275. if expected_error == Error.Okay:
  276. if number < 0:
  277. expected_result = 0
  278. else:
  279. expected_result = int(math.isqrt(number))
  280. return test("test_sqrt", res, [number], expected_error, expected_result)
  281. def root_n(number, root):
  282. u, s = number, number + 1
  283. while u < s:
  284. s = u
  285. t = (root-1) * s + number // pow(s, root - 1)
  286. u = t // root
  287. return s
  288. def test_root_n(number = 0, root = 0, expected_error = Error.Okay):
  289. args = [arg_to_odin(number), root]
  290. res = int_root_n(*args)
  291. expected_result = None
  292. if expected_error == Error.Okay:
  293. if number < 0:
  294. expected_result = 0
  295. else:
  296. expected_result = root_n(number, root)
  297. return test("test_root_n", res, [number, root], expected_error, expected_result)
  298. def test_shl_digit(a = 0, digits = 0, expected_error = Error.Okay):
  299. args = [arg_to_odin(a), digits]
  300. res = int_shl_digit(*args)
  301. expected_result = None
  302. if expected_error == Error.Okay:
  303. expected_result = a << (digits * 60)
  304. return test("test_shl_digit", res, [a, digits], expected_error, expected_result)
  305. def test_shr_digit(a = 0, digits = 0, expected_error = Error.Okay):
  306. args = [arg_to_odin(a), digits]
  307. res = int_shr_digit(*args)
  308. expected_result = None
  309. if expected_error == Error.Okay:
  310. if a < 0:
  311. # Don't pass negative numbers. We have a shr_signed.
  312. return False
  313. else:
  314. expected_result = a >> (digits * 60)
  315. return test("test_shr_digit", res, [a, digits], expected_error, expected_result)
  316. def test_shl(a = 0, bits = 0, expected_error = Error.Okay):
  317. args = [arg_to_odin(a), bits]
  318. res = int_shl(*args)
  319. expected_result = None
  320. if expected_error == Error.Okay:
  321. expected_result = a << bits
  322. return test("test_shl", res, [a, bits], expected_error, expected_result)
  323. def test_shr(a = 0, bits = 0, expected_error = Error.Okay):
  324. args = [arg_to_odin(a), bits]
  325. res = int_shr(*args)
  326. expected_result = None
  327. if expected_error == Error.Okay:
  328. if a < 0:
  329. # Don't pass negative numbers. We have a shr_signed.
  330. return False
  331. else:
  332. expected_result = a >> bits
  333. return test("test_shr", res, [a, bits], expected_error, expected_result)
  334. def test_shr_signed(a = 0, bits = 0, expected_error = Error.Okay):
  335. args = [arg_to_odin(a), bits]
  336. res = int_shr_signed(*args)
  337. expected_result = None
  338. if expected_error == Error.Okay:
  339. expected_result = a >> bits
  340. return test("test_shr_signed", res, [a, bits], expected_error, expected_result)
  341. def test_factorial(n = 0, expected_error = Error.Okay):
  342. args = [n]
  343. res = int_factorial(*args)
  344. expected_result = None
  345. if expected_error == Error.Okay:
  346. expected_result = math.factorial(n)
  347. return test("test_factorial", res, [n], expected_error, expected_result)
  348. def test_gcd(a = 0, b = 0, expected_error = Error.Okay):
  349. args = [arg_to_odin(a), arg_to_odin(b)]
  350. res = int_gcd(*args)
  351. expected_result = None
  352. if expected_error == Error.Okay:
  353. expected_result = math.gcd(a, b)
  354. return test("test_gcd", res, [a, b], expected_error, expected_result)
  355. def test_lcm(a = 0, b = 0, expected_error = Error.Okay):
  356. args = [arg_to_odin(a), arg_to_odin(b)]
  357. res = int_lcm(*args)
  358. expected_result = None
  359. if expected_error == Error.Okay:
  360. expected_result = math.lcm(a, b)
  361. return test("test_lcm", res, [a, b], expected_error, expected_result)
  362. # TODO(Jeroen): Make sure tests cover edge cases, fast paths, and so on.
  363. #
  364. # The last two arguments in tests are the expected error and expected result.
  365. #
  366. # The expected error defaults to None.
  367. # By default the Odin implementation will be tested against the Python one.
  368. # You can override that by supplying an expected result as the last argument instead.
  369. TESTS = {
  370. test_add: [
  371. [ 1234, 5432],
  372. ],
  373. test_sub: [
  374. [ 1234, 5432],
  375. ],
  376. test_mul: [
  377. [ 1234, 5432],
  378. [ 0xd3b4e926aaba3040e1c12b5ea553b5, 0x1a821e41257ed9281bee5bc7789ea7 ],
  379. [ 1 << 21_105, 1 << 21_501 ],
  380. ],
  381. test_sqr: [
  382. [ 5432],
  383. [ 0xd3b4e926aaba3040e1c12b5ea553b5 ],
  384. ],
  385. test_div: [
  386. [ 54321, 12345],
  387. [ 55431, 0, Error.Division_by_Zero],
  388. [ 12980742146337069150589594264770969721, 4611686018427387904 ],
  389. [ 831956404029821402159719858789932422, 243087903122332132 ],
  390. ],
  391. test_log: [
  392. [ 3192, 1, Error.Invalid_Argument],
  393. [ -1234, 2, Error.Math_Domain_Error],
  394. [ 0, 2, Error.Math_Domain_Error],
  395. [ 1024, 2],
  396. ],
  397. test_pow: [
  398. [ 0, -1, Error.Math_Domain_Error ], # Math
  399. [ 0, 0 ], # 1
  400. [ 0, 2 ], # 0
  401. [ 42, -1,], # 0
  402. [ 42, 1 ], # 1
  403. [ 42, 0 ], # 42
  404. [ 42, 2 ], # 42*42
  405. ],
  406. test_sqrt: [
  407. [ -1, Error.Invalid_Argument, ],
  408. [ 42, Error.Okay, ],
  409. [ 12345678901234567890, Error.Okay, ],
  410. [ 1298074214633706907132624082305024, Error.Okay, ],
  411. [ 686885735734829009541949746871140768343076607029752932751182108475420900392874228486622313727012705619148037570309621219533087263900443932890792804879473795673302686046941536636874184361869252299636701671980034458333859202703255467709267777184095435235980845369829397344182319113372092844648570818726316581751114346501124871729572474923695509057166373026411194094493240101036672016770945150422252961487398124677567028263059046193391737576836378376192651849283925197438927999526058932679219572030021792914065825542626400207956134072247020690107136531852625253942429167557531123651471221455967386267137846791963149859804549891438562641323068751514370656287452006867713758971418043865298618635213551059471668293725548570452377976322899027050925842868079489675596835389444833567439058609775325447891875359487104691935576723532407937236505941186660707032433807075470656782452889754501872408562496805517394619388777930253411467941214807849472083814447498068636264021405175653742244368865090604940094889189800007448083930490871954101880815781177612910234741529950538835837693870921008635195545246771593130784786737543736434086434015200264933536294884482218945403958647118802574342840790536176272341586020230110889699633073513016344826709214, Error.Okay, ],
  412. ],
  413. test_root_n: [
  414. [ 1298074214633706907132624082305024, 2, Error.Okay, ],
  415. ],
  416. test_shl_digit: [
  417. [ 3192, 1 ],
  418. [ 1298074214633706907132624082305024, 2 ],
  419. [ 1024, 3 ],
  420. ],
  421. test_shr_digit: [
  422. [ 3680125442705055547392, 1 ],
  423. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  424. [ 219504133884436710204395031992179571, 2 ],
  425. ],
  426. test_shl: [
  427. [ 3192, 1 ],
  428. [ 1298074214633706907132624082305024, 2 ],
  429. [ 1024, 3 ],
  430. ],
  431. test_shr: [
  432. [ 3680125442705055547392, 1 ],
  433. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  434. [ 219504133884436710204395031992179571, 2 ],
  435. ],
  436. test_shr_signed: [
  437. [ -611105530635358368578155082258244262, 12 ],
  438. [ -149195686190273039203651143129455, 12 ],
  439. [ 611105530635358368578155082258244262, 12 ],
  440. [ 149195686190273039203651143129455, 12 ],
  441. ],
  442. test_factorial: [
  443. [ 6_000 ], # Regular factorial, see cutoff in common.odin.
  444. [ 12_345 ], # Binary split factorial
  445. ],
  446. test_gcd: [
  447. [ 23, 25, ],
  448. [ 125, 25, ],
  449. [ 125, 0, ],
  450. [ 0, 0, ],
  451. [ 0, 125,],
  452. ],
  453. test_lcm: [
  454. [ 23, 25,],
  455. [ 125, 25, ],
  456. [ 125, 0, ],
  457. [ 0, 0, ],
  458. [ 0, 125,],
  459. ],
  460. }
  461. if not args.fast_tests:
  462. TESTS[test_factorial].append(
  463. # This one on its own takes around 800ms, so we exclude it for FAST_TESTS
  464. [ 10_000 ],
  465. )
  466. total_passes = 0
  467. total_failures = 0
  468. #
  469. # test_shr_signed also tests shr, so we're not going to test shr randomly.
  470. #
  471. RANDOM_TESTS = [
  472. test_add, test_sub, test_mul, test_sqr, test_div,
  473. test_log, test_pow, test_sqrt, test_root_n,
  474. test_shl_digit, test_shr_digit, test_shl, test_shr_signed,
  475. test_gcd, test_lcm,
  476. ]
  477. SKIP_LARGE = [
  478. test_pow, test_root_n, # test_gcd,
  479. ]
  480. SKIP_LARGEST = []
  481. # Untimed warmup.
  482. for test_proc in TESTS:
  483. for t in TESTS[test_proc]:
  484. res = test_proc(*t)
  485. if __name__ == '__main__':
  486. print("\n---- math/big tests ----")
  487. print()
  488. max_name = 0
  489. for test_proc in TESTS:
  490. max_name = max(max_name, len(test_proc.__name__))
  491. fmt_string = "{name:>{max_name}}: {count_pass:7,} passes and {count_fail:7,} failures in {timing:9.3f} ms."
  492. fmt_string = fmt_string.replace("{max_name}", str(max_name))
  493. for test_proc in TESTS:
  494. count_pass = 0
  495. count_fail = 0
  496. TIMINGS = {}
  497. for t in TESTS[test_proc]:
  498. start = time.perf_counter()
  499. res = test_proc(*t)
  500. diff = time.perf_counter() - start
  501. TOTAL_TIME += diff
  502. if test_proc not in TIMINGS:
  503. TIMINGS[test_proc] = diff
  504. else:
  505. TIMINGS[test_proc] += diff
  506. if res:
  507. count_pass += 1
  508. total_passes += 1
  509. else:
  510. count_fail += 1
  511. total_failures += 1
  512. print(fmt_string.format(name=test_proc.__name__, count_pass=count_pass, count_fail=count_fail, timing=TIMINGS[test_proc] * 1_000))
  513. for BITS, ITERATIONS in BITS_AND_ITERATIONS:
  514. print()
  515. print("---- math/big with two random {bits:,} bit numbers ----".format(bits=BITS))
  516. print()
  517. #
  518. # We've already tested up to the 10th root.
  519. #
  520. TEST_ROOT_N_PARAMS = [2, 3, 4, 5, 6]
  521. for test_proc in RANDOM_TESTS:
  522. if BITS > 1_200 and test_proc in SKIP_LARGE: continue
  523. if BITS > 4_096 and test_proc in SKIP_LARGEST: continue
  524. count_pass = 0
  525. count_fail = 0
  526. TIMINGS = {}
  527. UNTIL_ITERS = ITERATIONS
  528. if test_proc == test_root_n and BITS == 1_200:
  529. UNTIL_ITERS /= 10
  530. UNTIL_TIME = TOTAL_TIME + BITS / args.timed_bits
  531. # We run each test for a second per 20k bits
  532. index = 0
  533. while we_iterate():
  534. a = randint(-(1 << BITS), 1 << BITS)
  535. b = randint(-(1 << BITS), 1 << BITS)
  536. if test_proc == test_div:
  537. # We've already tested division by zero above.
  538. bits = int(BITS * 0.6)
  539. b = randint(-(1 << bits), 1 << bits)
  540. if b == 0:
  541. b == 42
  542. elif test_proc == test_log:
  543. # We've already tested log's domain errors.
  544. a = randint(1, 1 << BITS)
  545. b = randint(2, 1 << 60)
  546. elif test_proc == test_pow:
  547. b = randint(1, 10)
  548. elif test_proc == test_sqrt:
  549. a = randint(1, 1 << BITS)
  550. b = Error.Okay
  551. elif test_proc == test_root_n:
  552. a = randint(1, 1 << BITS)
  553. b = TEST_ROOT_N_PARAMS[index]
  554. index = (index + 1) % len(TEST_ROOT_N_PARAMS)
  555. elif test_proc == test_shl_digit:
  556. b = randint(0, 10);
  557. elif test_proc == test_shr_digit:
  558. a = abs(a)
  559. b = randint(0, 10);
  560. elif test_proc == test_shl:
  561. b = randint(0, min(BITS, 120));
  562. elif test_proc == test_shr_signed:
  563. b = randint(0, min(BITS, 120));
  564. else:
  565. b = randint(0, 1 << BITS)
  566. res = None
  567. start = time.perf_counter()
  568. res = test_proc(a, b)
  569. diff = time.perf_counter() - start
  570. TOTAL_TIME += diff
  571. if test_proc not in TIMINGS:
  572. TIMINGS[test_proc] = diff
  573. else:
  574. TIMINGS[test_proc] += diff
  575. if res:
  576. count_pass += 1; total_passes += 1
  577. else:
  578. count_fail += 1; total_failures += 1
  579. print(fmt_string.format(name=test_proc.__name__, count_pass=count_pass, count_fail=count_fail, timing=TIMINGS[test_proc] * 1_000))
  580. print()
  581. print("---- THE END ----")
  582. print()
  583. print(fmt_string.format(name="total", count_pass=total_passes, count_fail=total_failures, timing=TOTAL_TIME * 1_000))
  584. if total_failures:
  585. exit(1)