test.py 36 KB

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