test.py 34 KB

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