test.py 22 KB

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