test.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. is_square = load(l.test_is_square, [c_char_p ], Res)
  158. def test(test_name: "", res: Res, param=[], expected_error = Error.Okay, expected_result = "", radix=16):
  159. passed = True
  160. r = None
  161. err = Error(res.err)
  162. if err != expected_error:
  163. error_loc = res.res.decode('utf-8')
  164. error = "{}: {} in '{}'".format(test_name, err, error_loc)
  165. if len(param):
  166. error += " with params {}".format(param)
  167. print(error, flush=True)
  168. passed = False
  169. elif err == Error.Okay:
  170. r = None
  171. try:
  172. r = res.res.decode('utf-8')
  173. r = int(res.res, radix)
  174. except:
  175. pass
  176. if r != expected_result:
  177. error = "{}: Result was '{}', expected '{}'".format(test_name, r, expected_result)
  178. if len(param):
  179. error += " with params {}".format(param)
  180. print(error, flush=True)
  181. passed = False
  182. if EXIT_ON_FAIL and not passed: exit(res.err)
  183. return passed
  184. def arg_to_odin(a):
  185. if a >= 0:
  186. s = hex(a)[2:]
  187. else:
  188. s = '-' + hex(a)[3:]
  189. return s.encode('utf-8')
  190. def test_add(a = 0, b = 0, expected_error = Error.Okay):
  191. args = [arg_to_odin(a), arg_to_odin(b)]
  192. res = add(*args)
  193. expected_result = None
  194. if expected_error == Error.Okay:
  195. expected_result = a + b
  196. return test("test_add", res, [a, b], expected_error, expected_result)
  197. def test_sub(a = 0, b = 0, expected_error = Error.Okay):
  198. args = [arg_to_odin(a), arg_to_odin(b)]
  199. res = sub(*args)
  200. expected_result = None
  201. if expected_error == Error.Okay:
  202. expected_result = a - b
  203. return test("test_sub", res, [a, b], expected_error, expected_result)
  204. def test_mul(a = 0, b = 0, expected_error = Error.Okay):
  205. args = [arg_to_odin(a), arg_to_odin(b)]
  206. try:
  207. res = mul(*args)
  208. except OSError as e:
  209. print("{} while trying to multiply {} x {}.".format(e, a, b))
  210. if EXIT_ON_FAIL: exit(3)
  211. return False
  212. expected_result = None
  213. if expected_error == Error.Okay:
  214. expected_result = a * b
  215. return test("test_mul", res, [a, b], expected_error, expected_result)
  216. def test_sqr(a = 0, b = 0, expected_error = Error.Okay):
  217. args = [arg_to_odin(a)]
  218. try:
  219. res = sqr(*args)
  220. except OSError as e:
  221. print("{} while trying to square {}.".format(e, a))
  222. if EXIT_ON_FAIL: exit(3)
  223. return False
  224. expected_result = None
  225. if expected_error == Error.Okay:
  226. expected_result = a * a
  227. return test("test_sqr", res, [a], expected_error, expected_result)
  228. def test_div(a = 0, b = 0, expected_error = Error.Okay):
  229. args = [arg_to_odin(a), arg_to_odin(b)]
  230. try:
  231. res = div(*args)
  232. except OSError as e:
  233. print("{} while trying divide to {} / {}.".format(e, a, b))
  234. if EXIT_ON_FAIL: exit(3)
  235. return False
  236. expected_result = None
  237. if expected_error == Error.Okay:
  238. #
  239. # We don't round the division results, so if one component is negative, we're off by one.
  240. #
  241. if a < 0 and b > 0:
  242. expected_result = int(-(abs(a) // b))
  243. elif b < 0 and a > 0:
  244. expected_result = int(-(a // abs((b))))
  245. else:
  246. expected_result = a // b if b != 0 else None
  247. return test("test_div", res, [a, b], expected_error, expected_result)
  248. def test_log(a = 0, base = 0, expected_error = Error.Okay):
  249. args = [arg_to_odin(a), base]
  250. res = int_log(*args)
  251. expected_result = None
  252. if expected_error == Error.Okay:
  253. expected_result = int(math.log(a, base))
  254. return test("test_log", res, [a, base], expected_error, expected_result)
  255. def test_pow(base = 0, power = 0, expected_error = Error.Okay):
  256. args = [arg_to_odin(base), power]
  257. res = int_pow(*args)
  258. expected_result = None
  259. if expected_error == Error.Okay:
  260. if power < 0:
  261. expected_result = 0
  262. else:
  263. # NOTE(Jeroen): Don't use `math.pow`, it's a floating point approximation.
  264. # Use built-in `pow` or `a**b` instead.
  265. expected_result = pow(base, power)
  266. return test("test_pow", res, [base, power], expected_error, expected_result)
  267. def test_sqrt(number = 0, expected_error = Error.Okay):
  268. args = [arg_to_odin(number)]
  269. try:
  270. res = int_sqrt(*args)
  271. except OSError as e:
  272. print("{} while trying to sqrt {}.".format(e, number))
  273. if EXIT_ON_FAIL: exit(3)
  274. return False
  275. expected_result = None
  276. if expected_error == Error.Okay:
  277. if number < 0:
  278. expected_result = 0
  279. else:
  280. expected_result = int(math.isqrt(number))
  281. return test("test_sqrt", res, [number], expected_error, expected_result)
  282. def root_n(number, root):
  283. u, s = number, number + 1
  284. while u < s:
  285. s = u
  286. t = (root-1) * s + number // pow(s, root - 1)
  287. u = t // root
  288. return s
  289. def test_root_n(number = 0, root = 0, expected_error = Error.Okay):
  290. args = [arg_to_odin(number), root]
  291. res = int_root_n(*args)
  292. expected_result = None
  293. if expected_error == Error.Okay:
  294. if number < 0:
  295. expected_result = 0
  296. else:
  297. expected_result = root_n(number, root)
  298. return test("test_root_n", res, [number, root], expected_error, expected_result)
  299. def test_shl_digit(a = 0, digits = 0, expected_error = Error.Okay):
  300. args = [arg_to_odin(a), digits]
  301. res = int_shl_digit(*args)
  302. expected_result = None
  303. if expected_error == Error.Okay:
  304. expected_result = a << (digits * 60)
  305. return test("test_shl_digit", res, [a, digits], expected_error, expected_result)
  306. def test_shr_digit(a = 0, digits = 0, expected_error = Error.Okay):
  307. args = [arg_to_odin(a), digits]
  308. res = int_shr_digit(*args)
  309. expected_result = None
  310. if expected_error == Error.Okay:
  311. if a < 0:
  312. # Don't pass negative numbers. We have a shr_signed.
  313. return False
  314. else:
  315. expected_result = a >> (digits * 60)
  316. return test("test_shr_digit", res, [a, digits], expected_error, expected_result)
  317. def test_shl(a = 0, bits = 0, expected_error = Error.Okay):
  318. args = [arg_to_odin(a), bits]
  319. res = int_shl(*args)
  320. expected_result = None
  321. if expected_error == Error.Okay:
  322. expected_result = a << bits
  323. return test("test_shl", res, [a, bits], expected_error, expected_result)
  324. def test_shr(a = 0, bits = 0, expected_error = Error.Okay):
  325. args = [arg_to_odin(a), bits]
  326. res = int_shr(*args)
  327. expected_result = None
  328. if expected_error == Error.Okay:
  329. if a < 0:
  330. # Don't pass negative numbers. We have a shr_signed.
  331. return False
  332. else:
  333. expected_result = a >> bits
  334. return test("test_shr", res, [a, bits], expected_error, expected_result)
  335. def test_shr_signed(a = 0, bits = 0, expected_error = Error.Okay):
  336. args = [arg_to_odin(a), bits]
  337. res = int_shr_signed(*args)
  338. expected_result = None
  339. if expected_error == Error.Okay:
  340. expected_result = a >> bits
  341. return test("test_shr_signed", res, [a, bits], expected_error, expected_result)
  342. def test_factorial(number = 0, expected_error = Error.Okay):
  343. print("Factorial:", number)
  344. args = [number]
  345. try:
  346. res = int_factorial(*args)
  347. except OSError as e:
  348. print("{} while trying to factorial {}.".format(e, number))
  349. if EXIT_ON_FAIL: exit(3)
  350. return False
  351. expected_result = None
  352. if expected_error == Error.Okay:
  353. expected_result = math.factorial(number)
  354. return test("test_factorial", res, [number], expected_error, expected_result)
  355. def test_gcd(a = 0, b = 0, expected_error = Error.Okay):
  356. args = [arg_to_odin(a), arg_to_odin(b)]
  357. res = int_gcd(*args)
  358. expected_result = None
  359. if expected_error == Error.Okay:
  360. expected_result = math.gcd(a, b)
  361. return test("test_gcd", res, [a, b], expected_error, expected_result)
  362. def test_lcm(a = 0, b = 0, expected_error = Error.Okay):
  363. args = [arg_to_odin(a), arg_to_odin(b)]
  364. res = int_lcm(*args)
  365. expected_result = None
  366. if expected_error == Error.Okay:
  367. expected_result = math.lcm(a, b)
  368. return test("test_lcm", res, [a, b], expected_error, expected_result)
  369. def test_is_square(a = 0, b = 0, expected_error = Error.Okay):
  370. args = [arg_to_odin(a)]
  371. res = is_square(*args)
  372. expected_result = None
  373. if expected_error == Error.Okay:
  374. expected_result = str(math.isqrt(a) ** 2 == a) if a > 0 else "False"
  375. return test("test_is_square", res, [a], expected_error, expected_result)
  376. # TODO(Jeroen): Make sure tests cover edge cases, fast paths, and so on.
  377. #
  378. # The last two arguments in tests are the expected error and expected result.
  379. #
  380. # The expected error defaults to None.
  381. # By default the Odin implementation will be tested against the Python one.
  382. # You can override that by supplying an expected result as the last argument instead.
  383. TESTS = {
  384. test_add: [
  385. [ 1234, 5432],
  386. ],
  387. test_sub: [
  388. [ 1234, 5432],
  389. ],
  390. test_mul: [
  391. [ 1234, 5432],
  392. [ 0xd3b4e926aaba3040e1c12b5ea553b5, 0x1a821e41257ed9281bee5bc7789ea7 ],
  393. [ 1 << 21_105, 1 << 21_501 ],
  394. ],
  395. test_sqr: [
  396. [ 5432],
  397. [ 0xd3b4e926aaba3040e1c12b5ea553b5 ],
  398. ],
  399. test_div: [
  400. [ 54321, 12345],
  401. [ 55431, 0, Error.Division_by_Zero],
  402. [ 12980742146337069150589594264770969721, 4611686018427387904 ],
  403. [ 831956404029821402159719858789932422, 243087903122332132 ],
  404. ],
  405. test_log: [
  406. [ 3192, 1, Error.Invalid_Argument],
  407. [ -1234, 2, Error.Math_Domain_Error],
  408. [ 0, 2, Error.Math_Domain_Error],
  409. [ 1024, 2],
  410. ],
  411. test_pow: [
  412. [ 0, -1, Error.Math_Domain_Error ], # Math
  413. [ 0, 0 ], # 1
  414. [ 0, 2 ], # 0
  415. [ 42, -1,], # 0
  416. [ 42, 1 ], # 1
  417. [ 42, 0 ], # 42
  418. [ 42, 2 ], # 42*42
  419. ],
  420. test_sqrt: [
  421. [ -1, Error.Invalid_Argument, ],
  422. [ 42, Error.Okay, ],
  423. [ 12345678901234567890, Error.Okay, ],
  424. [ 1298074214633706907132624082305024, Error.Okay, ],
  425. [ 686885735734829009541949746871140768343076607029752932751182108475420900392874228486622313727012705619148037570309621219533087263900443932890792804879473795673302686046941536636874184361869252299636701671980034458333859202703255467709267777184095435235980845369829397344182319113372092844648570818726316581751114346501124871729572474923695509057166373026411194094493240101036672016770945150422252961487398124677567028263059046193391737576836378376192651849283925197438927999526058932679219572030021792914065825542626400207956134072247020690107136531852625253942429167557531123651471221455967386267137846791963149859804549891438562641323068751514370656287452006867713758971418043865298618635213551059471668293725548570452377976322899027050925842868079489675596835389444833567439058609775325447891875359487104691935576723532407937236505941186660707032433807075470656782452889754501872408562496805517394619388777930253411467941214807849472083814447498068636264021405175653742244368865090604940094889189800007448083930490871954101880815781177612910234741529950538835837693870921008635195545246771593130784786737543736434086434015200264933536294884482218945403958647118802574342840790536176272341586020230110889699633073513016344826709214, Error.Okay, ],
  426. ],
  427. test_root_n: [
  428. [ 1298074214633706907132624082305024, 2, Error.Okay, ],
  429. ],
  430. test_shl_digit: [
  431. [ 3192, 1 ],
  432. [ 1298074214633706907132624082305024, 2 ],
  433. [ 1024, 3 ],
  434. ],
  435. test_shr_digit: [
  436. [ 3680125442705055547392, 1 ],
  437. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  438. [ 219504133884436710204395031992179571, 2 ],
  439. ],
  440. test_shl: [
  441. [ 3192, 1 ],
  442. [ 1298074214633706907132624082305024, 2 ],
  443. [ 1024, 3 ],
  444. ],
  445. test_shr: [
  446. [ 3680125442705055547392, 1 ],
  447. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  448. [ 219504133884436710204395031992179571, 2 ],
  449. ],
  450. test_shr_signed: [
  451. [ -611105530635358368578155082258244262, 12 ],
  452. [ -149195686190273039203651143129455, 12 ],
  453. [ 611105530635358368578155082258244262, 12 ],
  454. [ 149195686190273039203651143129455, 12 ],
  455. ],
  456. test_factorial: [
  457. [ 6_000 ], # Regular factorial, see cutoff in common.odin.
  458. [ 12_345 ], # Binary split factorial
  459. ],
  460. test_gcd: [
  461. [ 23, 25, ],
  462. [ 125, 25, ],
  463. [ 125, 0, ],
  464. [ 0, 0, ],
  465. [ 0, 125,],
  466. ],
  467. test_lcm: [
  468. [ 23, 25,],
  469. [ 125, 25, ],
  470. [ 125, 0, ],
  471. [ 0, 0, ],
  472. [ 0, 125,],
  473. ],
  474. test_is_square: [
  475. [ 12, ],
  476. [ 92232459121502451677697058974826760244863271517919321608054113675118660929276431348516553336313179167211015633639725554914519355444316239500734169769447134357534241879421978647995614218985202290368055757891124109355450669008628757662409138767505519391883751112010824030579849970582074544353971308266211776494228299586414907715854328360867232691292422194412634523666770452490676515117702116926803826546868467146319938818238521874072436856528051486567230096290549225463582766830777324099589751817442141036031904145041055454639783559905920619197290800070679733841430619962318433709503256637256772215111521321630777950145713049902839937043785039344243357384899099910837463164007565230287809026956254332260375327814271845678201, ]
  477. ],
  478. }
  479. if not args.fast_tests:
  480. TESTS[test_factorial].append(
  481. # This one on its own takes around 800ms, so we exclude it for FAST_TESTS
  482. [ 10_000 ],
  483. )
  484. total_passes = 0
  485. total_failures = 0
  486. #
  487. # test_shr_signed also tests shr, so we're not going to test shr randomly.
  488. #
  489. RANDOM_TESTS = [
  490. test_add, test_sub, test_mul, test_sqr, test_div,
  491. test_log, test_pow, test_sqrt, test_root_n,
  492. test_shl_digit, test_shr_digit, test_shl, test_shr_signed,
  493. test_gcd, test_lcm, test_is_square,
  494. ]
  495. SKIP_LARGE = [
  496. test_pow, test_root_n, # test_gcd,
  497. ]
  498. SKIP_LARGEST = []
  499. # Untimed warmup.
  500. for test_proc in TESTS:
  501. for t in TESTS[test_proc]:
  502. res = test_proc(*t)
  503. if __name__ == '__main__':
  504. print("\n---- math/big tests ----")
  505. print()
  506. max_name = 0
  507. for test_proc in TESTS:
  508. max_name = max(max_name, len(test_proc.__name__))
  509. fmt_string = "{name:>{max_name}}: {count_pass:7,} passes and {count_fail:7,} failures in {timing:9.3f} ms."
  510. fmt_string = fmt_string.replace("{max_name}", str(max_name))
  511. for test_proc in TESTS:
  512. count_pass = 0
  513. count_fail = 0
  514. TIMINGS = {}
  515. for t in TESTS[test_proc]:
  516. start = time.perf_counter()
  517. res = test_proc(*t)
  518. diff = time.perf_counter() - start
  519. TOTAL_TIME += diff
  520. if test_proc not in TIMINGS:
  521. TIMINGS[test_proc] = diff
  522. else:
  523. TIMINGS[test_proc] += diff
  524. if res:
  525. count_pass += 1
  526. total_passes += 1
  527. else:
  528. count_fail += 1
  529. total_failures += 1
  530. print(fmt_string.format(name=test_proc.__name__, count_pass=count_pass, count_fail=count_fail, timing=TIMINGS[test_proc] * 1_000))
  531. for BITS, ITERATIONS in BITS_AND_ITERATIONS:
  532. print()
  533. print("---- math/big with two random {bits:,} bit numbers ----".format(bits=BITS))
  534. print()
  535. #
  536. # We've already tested up to the 10th root.
  537. #
  538. TEST_ROOT_N_PARAMS = [2, 3, 4, 5, 6]
  539. for test_proc in RANDOM_TESTS:
  540. if BITS > 1_200 and test_proc in SKIP_LARGE: continue
  541. if BITS > 4_096 and test_proc in SKIP_LARGEST: continue
  542. count_pass = 0
  543. count_fail = 0
  544. TIMINGS = {}
  545. UNTIL_ITERS = ITERATIONS
  546. if test_proc == test_root_n and BITS == 1_200:
  547. UNTIL_ITERS /= 10
  548. UNTIL_TIME = TOTAL_TIME + BITS / args.timed_bits
  549. # We run each test for a second per 20k bits
  550. index = 0
  551. while we_iterate():
  552. a = randint(-(1 << BITS), 1 << BITS)
  553. b = randint(-(1 << BITS), 1 << BITS)
  554. if test_proc == test_div:
  555. # We've already tested division by zero above.
  556. bits = int(BITS * 0.6)
  557. b = randint(-(1 << bits), 1 << bits)
  558. if b == 0:
  559. b == 42
  560. elif test_proc == test_log:
  561. # We've already tested log's domain errors.
  562. a = randint(1, 1 << BITS)
  563. b = randint(2, 1 << 60)
  564. elif test_proc == test_pow:
  565. b = randint(1, 10)
  566. elif test_proc == test_sqrt:
  567. a = randint(1, 1 << BITS)
  568. b = Error.Okay
  569. elif test_proc == test_root_n:
  570. a = randint(1, 1 << BITS)
  571. b = TEST_ROOT_N_PARAMS[index]
  572. index = (index + 1) % len(TEST_ROOT_N_PARAMS)
  573. elif test_proc == test_shl_digit:
  574. b = randint(0, 10);
  575. elif test_proc == test_shr_digit:
  576. a = abs(a)
  577. b = randint(0, 10);
  578. elif test_proc == test_shl:
  579. b = randint(0, min(BITS, 120))
  580. elif test_proc == test_shr_signed:
  581. b = randint(0, min(BITS, 120))
  582. elif test_proc == test_is_square:
  583. a = randint(0, 1 << BITS)
  584. else:
  585. b = randint(0, 1 << BITS)
  586. res = None
  587. start = time.perf_counter()
  588. res = test_proc(a, b)
  589. diff = time.perf_counter() - start
  590. TOTAL_TIME += diff
  591. if test_proc not in TIMINGS:
  592. TIMINGS[test_proc] = diff
  593. else:
  594. TIMINGS[test_proc] += diff
  595. if res:
  596. count_pass += 1; total_passes += 1
  597. else:
  598. count_fail += 1; total_failures += 1
  599. print(fmt_string.format(name=test_proc.__name__, count_pass=count_pass, count_fail=count_fail, timing=TIMINGS[test_proc] * 1_000))
  600. print()
  601. print("---- THE END ----")
  602. print()
  603. print(fmt_string.format(name="total", count_pass=total_passes, count_fail=total_failures, timing=TOTAL_TIME * 1_000))
  604. if total_failures:
  605. exit(1)