test.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. print("Factorial:", number)
  352. args = [number]
  353. try:
  354. res = int_factorial(*args)
  355. except OSError as e:
  356. print("{} while trying to factorial {}.".format(e, number))
  357. if EXIT_ON_FAIL: exit(3)
  358. return False
  359. expected_result = None
  360. if expected_error == Error.Okay:
  361. expected_result = math.factorial(number)
  362. return test("test_factorial", res, [number], expected_error, expected_result)
  363. def test_gcd(a = 0, b = 0, expected_error = Error.Okay):
  364. args = [arg_to_odin(a), arg_to_odin(b)]
  365. res = int_gcd(*args)
  366. expected_result = None
  367. if expected_error == Error.Okay:
  368. expected_result = math.gcd(a, b)
  369. return test("test_gcd", res, [a, b], expected_error, expected_result)
  370. def test_lcm(a = 0, b = 0, expected_error = Error.Okay):
  371. args = [arg_to_odin(a), arg_to_odin(b)]
  372. res = int_lcm(*args)
  373. expected_result = None
  374. if expected_error == Error.Okay:
  375. expected_result = math.lcm(a, b)
  376. return test("test_lcm", res, [a, b], expected_error, expected_result)
  377. def test_is_square(a = 0, b = 0, expected_error = Error.Okay):
  378. args = [arg_to_odin(a)]
  379. res = is_square(*args)
  380. expected_result = None
  381. if expected_error == Error.Okay:
  382. expected_result = str(math.isqrt(a) ** 2 == a) if a > 0 else "False"
  383. return test("test_is_square", res, [a], expected_error, expected_result)
  384. # TODO(Jeroen): Make sure tests cover edge cases, fast paths, and so on.
  385. #
  386. # The last two arguments in tests are the expected error and expected result.
  387. #
  388. # The expected error defaults to None.
  389. # By default the Odin implementation will be tested against the Python one.
  390. # You can override that by supplying an expected result as the last argument instead.
  391. TESTS = {
  392. test_add: [
  393. [ 1234, 5432],
  394. ],
  395. test_sub: [
  396. [ 1234, 5432],
  397. ],
  398. test_mul: [
  399. [ 1234, 5432],
  400. [ 0xd3b4e926aaba3040e1c12b5ea553b5, 0x1a821e41257ed9281bee5bc7789ea7 ],
  401. [ 1 << 21_105, 1 << 21_501 ],
  402. ],
  403. test_sqr: [
  404. [ 5432],
  405. [ 0xd3b4e926aaba3040e1c12b5ea553b5 ],
  406. ],
  407. test_div: [
  408. [ 54321, 12345],
  409. [ 55431, 0, Error.Division_by_Zero],
  410. [ 12980742146337069150589594264770969721, 4611686018427387904 ],
  411. [ 831956404029821402159719858789932422, 243087903122332132 ],
  412. ],
  413. test_log: [
  414. [ 3192, 1, Error.Invalid_Argument],
  415. [ -1234, 2, Error.Math_Domain_Error],
  416. [ 0, 2, Error.Math_Domain_Error],
  417. [ 1024, 2],
  418. ],
  419. test_pow: [
  420. [ 0, -1, Error.Math_Domain_Error ], # Math
  421. [ 0, 0 ], # 1
  422. [ 0, 2 ], # 0
  423. [ 42, -1,], # 0
  424. [ 42, 1 ], # 1
  425. [ 42, 0 ], # 42
  426. [ 42, 2 ], # 42*42
  427. ],
  428. test_sqrt: [
  429. [ -1, Error.Invalid_Argument, ],
  430. [ 42, Error.Okay, ],
  431. [ 12345678901234567890, Error.Okay, ],
  432. [ 1298074214633706907132624082305024, Error.Okay, ],
  433. [ 686885735734829009541949746871140768343076607029752932751182108475420900392874228486622313727012705619148037570309621219533087263900443932890792804879473795673302686046941536636874184361869252299636701671980034458333859202703255467709267777184095435235980845369829397344182319113372092844648570818726316581751114346501124871729572474923695509057166373026411194094493240101036672016770945150422252961487398124677567028263059046193391737576836378376192651849283925197438927999526058932679219572030021792914065825542626400207956134072247020690107136531852625253942429167557531123651471221455967386267137846791963149859804549891438562641323068751514370656287452006867713758971418043865298618635213551059471668293725548570452377976322899027050925842868079489675596835389444833567439058609775325447891875359487104691935576723532407937236505941186660707032433807075470656782452889754501872408562496805517394619388777930253411467941214807849472083814447498068636264021405175653742244368865090604940094889189800007448083930490871954101880815781177612910234741529950538835837693870921008635195545246771593130784786737543736434086434015200264933536294884482218945403958647118802574342840790536176272341586020230110889699633073513016344826709214, Error.Okay, ],
  434. ],
  435. test_root_n: [
  436. [ 1298074214633706907132624082305024, 2, Error.Okay, ],
  437. ],
  438. test_shl_digit: [
  439. [ 3192, 1 ],
  440. [ 1298074214633706907132624082305024, 2 ],
  441. [ 1024, 3 ],
  442. ],
  443. test_shr_digit: [
  444. [ 3680125442705055547392, 1 ],
  445. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  446. [ 219504133884436710204395031992179571, 2 ],
  447. ],
  448. test_shl: [
  449. [ 3192, 1 ],
  450. [ 1298074214633706907132624082305024, 2 ],
  451. [ 1024, 3 ],
  452. ],
  453. test_shr: [
  454. [ 3680125442705055547392, 1 ],
  455. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  456. [ 219504133884436710204395031992179571, 2 ],
  457. ],
  458. test_shr_signed: [
  459. [ -611105530635358368578155082258244262, 12 ],
  460. [ -149195686190273039203651143129455, 12 ],
  461. [ 611105530635358368578155082258244262, 12 ],
  462. [ 149195686190273039203651143129455, 12 ],
  463. ],
  464. test_factorial: [
  465. [ 6_000 ], # Regular factorial, see cutoff in common.odin.
  466. [ 12_345 ], # Binary split factorial
  467. ],
  468. test_gcd: [
  469. [ 23, 25, ],
  470. [ 125, 25, ],
  471. [ 125, 0, ],
  472. [ 0, 0, ],
  473. [ 0, 125,],
  474. ],
  475. test_lcm: [
  476. [ 23, 25,],
  477. [ 125, 25, ],
  478. [ 125, 0, ],
  479. [ 0, 0, ],
  480. [ 0, 125,],
  481. ],
  482. test_is_square: [
  483. [ 12, ],
  484. [ 92232459121502451677697058974826760244863271517919321608054113675118660929276431348516553336313179167211015633639725554914519355444316239500734169769447134357534241879421978647995614218985202290368055757891124109355450669008628757662409138767505519391883751112010824030579849970582074544353971308266211776494228299586414907715854328360867232691292422194412634523666770452490676515117702116926803826546868467146319938818238521874072436856528051486567230096290549225463582766830777324099589751817442141036031904145041055454639783559905920619197290800070679733841430619962318433709503256637256772215111521321630777950145713049902839937043785039344243357384899099910837463164007565230287809026956254332260375327814271845678201, ]
  485. ],
  486. }
  487. if not args.fast_tests:
  488. TESTS[test_factorial].append(
  489. # This one on its own takes around 800ms, so we exclude it for FAST_TESTS
  490. [ 10_000 ],
  491. )
  492. total_passes = 0
  493. total_failures = 0
  494. #
  495. # test_shr_signed also tests shr, so we're not going to test shr randomly.
  496. #
  497. RANDOM_TESTS = [
  498. test_add, test_sub, test_mul, test_sqr, test_div,
  499. test_log, test_pow, test_sqrt, test_root_n,
  500. test_shl_digit, test_shr_digit, test_shl, test_shr_signed,
  501. test_gcd, test_lcm, test_is_square,
  502. ]
  503. SKIP_LARGE = [
  504. test_pow, test_root_n, # test_gcd,
  505. ]
  506. SKIP_LARGEST = []
  507. # Untimed warmup.
  508. for test_proc in TESTS:
  509. for t in TESTS[test_proc]:
  510. res = test_proc(*t)
  511. if __name__ == '__main__':
  512. print("\n---- math/big tests ----")
  513. print()
  514. max_name = 0
  515. for test_proc in TESTS:
  516. max_name = max(max_name, len(test_proc.__name__))
  517. fmt_string = "{name:>{max_name}}: {count_pass:7,} passes and {count_fail:7,} failures in {timing:9.3f} ms."
  518. fmt_string = fmt_string.replace("{max_name}", str(max_name))
  519. for test_proc in TESTS:
  520. count_pass = 0
  521. count_fail = 0
  522. TIMINGS = {}
  523. for t in TESTS[test_proc]:
  524. start = time.perf_counter()
  525. res = test_proc(*t)
  526. diff = time.perf_counter() - start
  527. TOTAL_TIME += diff
  528. if test_proc not in TIMINGS:
  529. TIMINGS[test_proc] = diff
  530. else:
  531. TIMINGS[test_proc] += diff
  532. if res:
  533. count_pass += 1
  534. total_passes += 1
  535. else:
  536. count_fail += 1
  537. total_failures += 1
  538. print(fmt_string.format(name=test_proc.__name__, count_pass=count_pass, count_fail=count_fail, timing=TIMINGS[test_proc] * 1_000))
  539. for BITS, ITERATIONS in BITS_AND_ITERATIONS:
  540. print()
  541. print("---- math/big with two random {bits:,} bit numbers ----".format(bits=BITS))
  542. print()
  543. #
  544. # We've already tested up to the 10th root.
  545. #
  546. TEST_ROOT_N_PARAMS = [2, 3, 4, 5, 6]
  547. for test_proc in RANDOM_TESTS:
  548. if BITS > 1_200 and test_proc in SKIP_LARGE: continue
  549. if BITS > 4_096 and test_proc in SKIP_LARGEST: continue
  550. count_pass = 0
  551. count_fail = 0
  552. TIMINGS = {}
  553. UNTIL_ITERS = ITERATIONS
  554. if test_proc == test_root_n and BITS == 1_200:
  555. UNTIL_ITERS /= 10
  556. UNTIL_TIME = TOTAL_TIME + BITS / args.timed_bits
  557. # We run each test for a second per 20k bits
  558. index = 0
  559. while we_iterate():
  560. a = randint(-(1 << BITS), 1 << BITS)
  561. b = randint(-(1 << BITS), 1 << BITS)
  562. if test_proc == test_div:
  563. # We've already tested division by zero above.
  564. bits = int(BITS * 0.6)
  565. b = randint(-(1 << bits), 1 << bits)
  566. if b == 0:
  567. b == 42
  568. elif test_proc == test_log:
  569. # We've already tested log's domain errors.
  570. a = randint(1, 1 << BITS)
  571. b = randint(2, 1 << 60)
  572. elif test_proc == test_pow:
  573. b = randint(1, 10)
  574. elif test_proc == test_sqrt:
  575. a = randint(1, 1 << BITS)
  576. b = Error.Okay
  577. elif test_proc == test_root_n:
  578. a = randint(1, 1 << BITS)
  579. b = TEST_ROOT_N_PARAMS[index]
  580. index = (index + 1) % len(TEST_ROOT_N_PARAMS)
  581. elif test_proc == test_shl_digit:
  582. b = randint(0, 10);
  583. elif test_proc == test_shr_digit:
  584. a = abs(a)
  585. b = randint(0, 10);
  586. elif test_proc == test_shl:
  587. b = randint(0, min(BITS, 120))
  588. elif test_proc == test_shr_signed:
  589. b = randint(0, min(BITS, 120))
  590. elif test_proc == test_is_square:
  591. a = randint(0, 1 << BITS)
  592. else:
  593. b = randint(0, 1 << BITS)
  594. res = None
  595. start = time.perf_counter()
  596. res = test_proc(a, b)
  597. diff = time.perf_counter() - start
  598. TOTAL_TIME += diff
  599. if test_proc not in TIMINGS:
  600. TIMINGS[test_proc] = diff
  601. else:
  602. TIMINGS[test_proc] += diff
  603. if res:
  604. count_pass += 1; total_passes += 1
  605. else:
  606. count_fail += 1; total_failures += 1
  607. print(fmt_string.format(name=test_proc.__name__, count_pass=count_pass, count_fail=count_fail, timing=TIMINGS[test_proc] * 1_000))
  608. print()
  609. print("---- THE END ----")
  610. print()
  611. print(fmt_string.format(name="total", count_pass=total_passes, count_fail=total_failures, timing=TOTAL_TIME * 1_000))
  612. if total_failures:
  613. exit(1)