generate_tests.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 random import *
  10. import math
  11. import os
  12. import platform
  13. import time
  14. import gc
  15. from enum import Enum
  16. import argparse
  17. LEG_BITS = 60
  18. vectors = open('../test_vectors.odin', 'w')
  19. vectors.write("""package test_core_math_big
  20. import "core:math/big"
  21. // GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED
  22. //
  23. // This file is generated using `test_generator.py`
  24. //
  25. // GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED -=- GENERATED
  26. Big_Test_Operation :: enum {
  27. Add,
  28. Sub,
  29. Mul,
  30. Div,
  31. Sqr,
  32. Log,
  33. Sqrt,
  34. Pow,
  35. Root,
  36. Shl,
  37. Shr,
  38. Shr_Signed,
  39. Factorial,
  40. Gcd,
  41. Lcm,
  42. Is_Square,
  43. }
  44. Big_Test_Vector :: struct {
  45. op: Big_Test_Operation,
  46. a: string,
  47. b: string,
  48. exp: string,
  49. err: big.Error,
  50. }
  51. big_test_vectors := []Big_Test_Vector{
  52. """)
  53. parser = argparse.ArgumentParser(
  54. description = "Odin core:math/big test suite generator",
  55. epilog = "By default we generate regression and random tests with preset parameters.",
  56. formatter_class = argparse.ArgumentDefaultsHelpFormatter,
  57. )
  58. #
  59. # We skip randomized tests altogether if this is set.
  60. #
  61. no_random = parser.add_mutually_exclusive_group()
  62. no_random.add_argument(
  63. "-no-random",
  64. help = "Don't generate random tests",
  65. action = "store_true",
  66. )
  67. #
  68. # Normally we run a given number of cycles on each test.
  69. # Timed tests budget 1 second per 20_000 bits instead.
  70. #
  71. # For timed tests we budget a second per `n` bits and iterate until we hit that time.
  72. #
  73. timed_or_fast = no_random.add_mutually_exclusive_group()
  74. timed_or_fast.add_argument(
  75. "-timed",
  76. type = bool,
  77. default = False,
  78. help = "Timed tests instead of a preset number of iterations.",
  79. )
  80. parser.add_argument(
  81. "-timed-bits",
  82. type = int,
  83. metavar = "BITS",
  84. default = 20_000,
  85. help = "Timed tests. Every `BITS` worth of input is given a second of running time.",
  86. )
  87. #
  88. # For normal tests (non-timed), `-fast-tests` cuts down on the number of iterations.
  89. #
  90. timed_or_fast.add_argument(
  91. "-fast-tests",
  92. help = "Cut down on the number of iterations of each test",
  93. action = "store_true",
  94. )
  95. args = parser.parse_args()
  96. #
  97. # How many iterations of each random test do we want to run?
  98. #
  99. BITS_AND_ITERATIONS = [
  100. ( 120, 100),
  101. ( 1_200, 100),
  102. ( 4_096, 100),
  103. (12_000, 10),
  104. ]
  105. if args.fast_tests:
  106. for k in range(len(BITS_AND_ITERATIONS)):
  107. b, i = BITS_AND_ITERATIONS[k]
  108. BITS_AND_ITERATIONS[k] = (b, i // 10 if i >= 100 else 5)
  109. if args.no_random:
  110. BITS_AND_ITERATIONS = []
  111. TOTAL_TIME = 0
  112. UNTIL_TIME = 0
  113. UNTIL_ITERS = 0
  114. def we_iterate():
  115. if args.timed:
  116. return TOTAL_TIME < UNTIL_TIME
  117. else:
  118. global UNTIL_ITERS
  119. UNTIL_ITERS -= 1
  120. return UNTIL_ITERS != -1
  121. #
  122. # Error enum values
  123. #
  124. class Error(Enum):
  125. Okay = 0
  126. Out_Of_Memory = 1
  127. Invalid_Pointer = 2
  128. Invalid_Argument = 3
  129. Unknown_Error = 4
  130. Assignment_To_Immutable = 10
  131. Max_Iterations_Reached = 11
  132. Buffer_Overflow = 12
  133. Integer_Overflow = 13
  134. Integer_Underflow = 14
  135. Division_by_Zero = 30
  136. Math_Domain_Error = 31
  137. Cannot_Open_File = 50
  138. Cannot_Read_File = 51
  139. Cannot_Write_File = 52
  140. Unimplemented = 127
  141. #
  142. # Disable garbage collection
  143. #
  144. gc.disable()
  145. def arg_to_odin(a):
  146. if a >= 0:
  147. s = hex(a)[2:]
  148. else:
  149. s = '-' + hex(a)[3:]
  150. return s.encode('utf-8')
  151. def big_integer_sqrt(src):
  152. # The Python version on Github's CI doesn't offer math.isqrt.
  153. # We implement our own
  154. count = src.bit_length()
  155. a, b = count >> 1, count & 1
  156. x = 1 << (a + b)
  157. while True:
  158. # y = (x + n // x) // 2
  159. t1 = src // x
  160. t2 = t1 + x
  161. y = t2 >> 1
  162. if y >= x:
  163. return x
  164. x, y = y, x
  165. def big_integer_lcm(a, b):
  166. # Computes least common multiple as `|a*b|/gcd(a,b)`
  167. # Divide the smallest by the GCD.
  168. if a == 0 or b == 0:
  169. return 0
  170. if abs(a) < abs(b):
  171. # Store quotient in `t2` such that `t2 * b` is the LCM.
  172. lcm = a // math.gcd(a, b)
  173. return abs(b * lcm)
  174. else:
  175. # Store quotient in `t2` such that `t2 * a` is the LCM.
  176. lcm = b // math.gcd(a, b)
  177. return abs(a * lcm)
  178. def write_test_case(op = "", a = 0, b = 0, expected_result = 0, expected_error = Error.Okay):
  179. def test_arg_to_odin(a):
  180. if a >= 0:
  181. s = hex(a)[2:]
  182. else:
  183. s = '-' + hex(a)[3:]
  184. return s
  185. vectors.write("\t{{{}, ".format(op))
  186. vectors.write("\"{}\", ".format(test_arg_to_odin(a)))
  187. vectors.write("\"{}\", ".format(test_arg_to_odin(b)))
  188. if expected_result == None:
  189. vectors.write("\"0\", ")
  190. else:
  191. vectors.write("\"{}\", ".format(test_arg_to_odin(expected_result)))
  192. vectors.write("{}}},\n".format(expected_error)[5:])
  193. def test_add(a = 0, b = 0, expected_error = Error.Okay):
  194. args = [arg_to_odin(a), arg_to_odin(b)]
  195. expected_result = None
  196. if expected_error == Error.Okay:
  197. expected_result = a + b
  198. write_test_case(".Add", a, b, expected_result, expected_error)
  199. def test_sub(a = 0, b = 0, expected_error = Error.Okay):
  200. args = [arg_to_odin(a), arg_to_odin(b)]
  201. expected_result = None
  202. if expected_error == Error.Okay:
  203. expected_result = a - b
  204. write_test_case(".Sub", a, b, expected_result, expected_error)
  205. def test_mul(a = 0, b = 0, expected_error = Error.Okay):
  206. args = [arg_to_odin(a), arg_to_odin(b)]
  207. expected_result = None
  208. if expected_error == Error.Okay:
  209. expected_result = a * b
  210. write_test_case(".Mul", a, b, expected_result, expected_error)
  211. def test_sqr(a = 0, b = 0, expected_error = Error.Okay):
  212. args = [arg_to_odin(a)]
  213. expected_result = None
  214. if expected_error == Error.Okay:
  215. expected_result = a * a
  216. write_test_case(".Sqr", a, b, expected_result, expected_error)
  217. def test_div(a = 0, b = 0, expected_error = Error.Okay):
  218. args = [arg_to_odin(a), arg_to_odin(b)]
  219. expected_result = None
  220. if expected_error == Error.Okay:
  221. #
  222. # We don't round the division results, so if one component is negative, we're off by one.
  223. #
  224. if a < 0 and b > 0:
  225. expected_result = int(-(abs(a) // b))
  226. elif b < 0 and a > 0:
  227. expected_result = int(-(a // abs((b))))
  228. else:
  229. expected_result = a // b if b != 0 else None
  230. write_test_case(".Div", a, b, expected_result, expected_error)
  231. def test_log(a = 0, base = 0, expected_error = Error.Okay):
  232. args = [arg_to_odin(a), base]
  233. expected_result = None
  234. if expected_error == Error.Okay:
  235. expected_result = int(math.log(a, base))
  236. write_test_case(".Log", a, base, expected_result, expected_error)
  237. def test_pow(base = 0, power = 0, expected_error = Error.Okay):
  238. args = [arg_to_odin(base), power]
  239. expected_result = None
  240. if expected_error == Error.Okay:
  241. if power < 0:
  242. expected_result = 0
  243. else:
  244. # NOTE(Jeroen): Don't use `math.pow`, it's a floating point approximation.
  245. # Use built-in `pow` or `a**b` instead.
  246. expected_result = pow(base, power)
  247. write_test_case(".Pow", base, power, expected_result, expected_error)
  248. def test_sqrt(number = 0, expected_error = Error.Okay):
  249. args = [arg_to_odin(number)]
  250. expected_result = None
  251. if expected_error == Error.Okay:
  252. if number < 0:
  253. expected_result = 0
  254. else:
  255. expected_result = big_integer_sqrt(number)
  256. write_test_case(".Sqrt", number, 0, expected_result, expected_error)
  257. def root_n(number, root):
  258. u, s = number, number + 1
  259. while u < s:
  260. s = u
  261. t = (root-1) * s + number // pow(s, root - 1)
  262. u = t // root
  263. return s
  264. def test_root_n(number = 0, root = 0, expected_error = Error.Okay):
  265. args = [arg_to_odin(number), root]
  266. expected_result = None
  267. if expected_error == Error.Okay:
  268. if number < 0:
  269. expected_result = 0
  270. else:
  271. expected_result = root_n(number, root)
  272. write_test_case(".Root", number, root, expected_result, expected_error)
  273. def test_shl_leg(a = 0, digits = 0, expected_error = Error.Okay):
  274. args = [arg_to_odin(a), digits]
  275. expected_result = None
  276. if expected_error == Error.Okay:
  277. expected_result = a << (digits * LEG_BITS)
  278. write_test_case(".Shl", a, (digits * LEG_BITS), expected_result, expected_error)
  279. def test_shr_leg(a = 0, digits = 0, expected_error = Error.Okay):
  280. args = [arg_to_odin(a), digits]
  281. expected_result = None
  282. if expected_error == Error.Okay:
  283. if a < 0:
  284. # Don't pass negative numbers. We have a shr_signed.
  285. return False
  286. else:
  287. expected_result = a >> (digits * LEG_BITS)
  288. write_test_case(".Shr", a, (digits * LEG_BITS), expected_result, expected_error)
  289. def test_shl(a = 0, bits = 0, expected_error = Error.Okay):
  290. args = [arg_to_odin(a), bits]
  291. expected_result = None
  292. if expected_error == Error.Okay:
  293. expected_result = a << bits
  294. write_test_case(".Shl", a, bits, expected_result, expected_error)
  295. def test_shr(a = 0, bits = 0, expected_error = Error.Okay):
  296. args = [arg_to_odin(a), bits]
  297. expected_result = None
  298. if expected_error == Error.Okay:
  299. if a < 0:
  300. # Don't pass negative numbers. We have a shr_signed.
  301. return False
  302. else:
  303. expected_result = a >> bits
  304. write_test_case(".Shr", a, bits, expected_result, expected_error)
  305. def test_shr_signed(a = 0, bits = 0, expected_error = Error.Okay):
  306. args = [arg_to_odin(a), bits]
  307. expected_result = None
  308. if expected_error == Error.Okay:
  309. expected_result = a >> bits
  310. write_test_case(".Shr_Signed", a, bits, expected_result, expected_error)
  311. def test_factorial(number = 0, expected_error = Error.Okay):
  312. args = [number]
  313. expected_result = None
  314. if expected_error == Error.Okay:
  315. expected_result = math.factorial(number)
  316. write_test_case(".Factorial", number, 0, expected_result, expected_error)
  317. def test_gcd(a = 0, b = 0, expected_error = Error.Okay):
  318. args = [arg_to_odin(a), arg_to_odin(b)]
  319. expected_result = None
  320. if expected_error == Error.Okay:
  321. expected_result = math.gcd(a, b)
  322. write_test_case(".Gcd", a, b, expected_result, expected_error)
  323. def test_lcm(a = 0, b = 0, expected_error = Error.Okay):
  324. args = [arg_to_odin(a), arg_to_odin(b)]
  325. expected_result = None
  326. if expected_error == Error.Okay:
  327. expected_result = big_integer_lcm(a, b)
  328. write_test_case(".Lcm", a, b, expected_result, expected_error)
  329. def test_is_square(a = 0, b = 0, expected_error = Error.Okay):
  330. args = [arg_to_odin(a)]
  331. expected_result = False
  332. if expected_error == Error.Okay and a > 0:
  333. expected_result = big_integer_sqrt(a) ** 2 == a
  334. write_test_case(".Is_Square", a, 0, expected_result, expected_error)
  335. # TODO(Jeroen): Make sure tests cover edge cases, fast paths, and so on.
  336. #
  337. # The last two arguments in tests are the expected error and expected result.
  338. #
  339. # The expected error defaults to None.
  340. # By default the Odin implementation will be tested against the Python one.
  341. # You can override that by supplying an expected result as the last argument instead.
  342. TESTS = {
  343. test_add: [
  344. [ 1234, 5432],
  345. ],
  346. test_sub: [
  347. [ 1234, 5432],
  348. ],
  349. test_mul: [
  350. [ 1234, 5432],
  351. [ 0xd3b4e926aaba3040e1c12b5ea553b5, 0x1a821e41257ed9281bee5bc7789ea7 ],
  352. [ 1 << 21_105, 1 << 21_501 ],
  353. [
  354. 0x200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
  355. 0x200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
  356. ]
  357. ],
  358. test_sqr: [
  359. [ 5432],
  360. [ 0xd3b4e926aaba3040e1c12b5ea553b5 ],
  361. ],
  362. test_div: [
  363. [ 54321, 12345],
  364. [ 55431, 0, Error.Division_by_Zero],
  365. [ 12980742146337069150589594264770969721, 4611686018427387904 ],
  366. [ 831956404029821402159719858789932422, 243087903122332132 ],
  367. ],
  368. test_log: [
  369. [ 3192, 1, Error.Invalid_Argument],
  370. [ -1234, 2, Error.Math_Domain_Error],
  371. [ 0, 2, Error.Math_Domain_Error],
  372. [ 1024, 2],
  373. ],
  374. test_pow: [
  375. [ 0, -1, Error.Math_Domain_Error ], # Math
  376. [ 0, 0 ], # 1
  377. [ 0, 2 ], # 0
  378. [ 42, -1,], # 0
  379. [ 42, 1 ], # 1
  380. [ 42, 0 ], # 42
  381. [ 42, 2 ], # 42*42
  382. [ 1023423462055631945665902260039819522, 6],
  383. [ 2351415513563017480724958108064794964140712340951636081608226461329298597792428177392182921045756382154475969841516481766099091057155043079113409578271460350765774152509347176654430118446048617733844782454267084644777022821998489944144604889308377152515711394170267839394315842510152114743680838721625924309675796181595284284935359605488617487126635442626578631, 4],
  384. ],
  385. test_sqrt: [
  386. [ -1, Error.Invalid_Argument, ],
  387. [ 42, Error.Okay, ],
  388. [ 12345678901234567890, Error.Okay, ],
  389. [ 1298074214633706907132624082305024, Error.Okay, ],
  390. [ 0xa85e79177036820e9e63d14514884413c283db3dba2771f66ec888ae94fe253826ed3230efc1de0cbb4a2ba16fede5fe980d232472cca9e8f339714c56a9e64b5cff7538c33773f128898e8cad47234e8a086b4ce5b902231e2da75cc6cb510d892feb9c9c19ee5f5b7967cb7f081fb79099afe2d20203b0693ecc95c656e5515e0903a4ebc84d22fc2a176ba36dd795195535cfdf473e547930fbd6eae51ad11e974198b4733a10115f391c0fefd22654f5acd63c6415d4cbdaad6c1fc1812333d701b64bb230307fb37911561f5287efd67c2eec5a26a694931aec299c67874881bab0c42941cf0f4ef8ca3548e1adcc7f712eb714762184d656385ceacc7b9f75620dfa7ec62b70ee92a5998cee14ad2b9df3f0c861678bc3311c1fe78c5ce4ed30b90c56d18d50261a4f46fdbf6af94737920b50adf1229503edea8b32900000697f366eba632074a66dcd9999a1510ccefa6110bac2207602b16cd4ce42a36fbf276b5b14550faf75194256f175a867169ff30f8e4770d094b617e3df29612359e33d2a3e8f4e12acf243a22b2732e35a5039fea630886e80f49fb310cb34cd1ecb0dc3036761ac8eed5e2e3d6ea88c5b2f552405149fcb100f50368e969c7d1d45db10ea868838dddc3fbc54c9b658761522c31e46661f46205a6c8783d60638db10bc9515ece8509aa181332207c5a2753ee4a8297a65695fbd8184de, Error.Okay, ],
  391. ],
  392. test_root_n: [
  393. [ 1298074214633706907132624082305024, 2, Error.Okay, ],
  394. ],
  395. test_shl_leg: [
  396. [ 3192, 1 ],
  397. [ 1298074214633706907132624082305024, 2 ],
  398. [ 1024, 3 ],
  399. ],
  400. test_shr_leg: [
  401. [ 3680125442705055547392, 1 ],
  402. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  403. [ 219504133884436710204395031992179571, 2 ],
  404. ],
  405. test_shl: [
  406. [ 3192, 1 ],
  407. [ 1298074214633706907132624082305024, 2 ],
  408. [ 1024, 3 ],
  409. ],
  410. test_shr: [
  411. [ 3680125442705055547392, 1 ],
  412. [ 1725436586697640946858688965569256363112777243042596638790631055949824, 2 ],
  413. [ 219504133884436710204395031992179571, 2 ],
  414. ],
  415. test_shr_signed: [
  416. [ -611105530635358368578155082258244262, 12 ],
  417. [ -149195686190273039203651143129455, 12 ],
  418. [ 611105530635358368578155082258244262, 12 ],
  419. [ 149195686190273039203651143129455, 12 ],
  420. ],
  421. test_factorial: [
  422. [ 6_000 ], # Regular factorial, see cutoff in common.odin.
  423. [ 12_345 ], # Binary split factorial
  424. ],
  425. test_gcd: [
  426. [ 23, 25, ],
  427. [ 125, 25, ],
  428. [ 125, 0, ],
  429. [ 0, 0, ],
  430. [ 0, 125,],
  431. ],
  432. test_lcm: [
  433. [ 23, 25,],
  434. [ 125, 25, ],
  435. [ 125, 0, ],
  436. [ 0, 0, ],
  437. [ 0, 125,],
  438. ],
  439. test_is_square: [
  440. [ 12, ],
  441. [ 0x4fa3f9fe4edb58bfae7bab80b94ffce6e02cdd067c509f75a5918e510d002a8b41949dee96f482678b6e593ee2a984aa68809af5bdc3c0ee839c588b3b619e0f4a5267a7533765f8621dd20994a9a5bdd7faca4aab4f84a72f4f30d623a44cbc974d48e7ab63259d3141da5467e0a2225d90e6388f8d05e0bcdcb67f6d11c4e17d4c168b9fb23bf0932d6082ed82241b01d7d80bb43bf516fc650d86d62e13df218557df8b3f2e4eb295485e3f221c01130791c0b1b4c77fae4ae98e000e42d943a1dff9bfd960fdabe6a729913f99d74b1a7736c213b6c134bbc6914e0b5ae9d1909a32c2084af5a49a99a97a8c3856fdf1e4ff39306ede6234f85f0dca94382a118d97058d0be641c7b0cecead08450042a56dff16808115f78857d8844df61d8e930427d410ee33a63c79, ]
  442. ],
  443. }
  444. if not args.fast_tests:
  445. TESTS[test_factorial].append(
  446. # This one on its own takes around 800ms, so we exclude it for FAST_TESTS
  447. [ 10_000 ],
  448. )
  449. total_passes = 0
  450. total_failures = 0
  451. #
  452. # test_shr_signed also tests shr, so we're not going to test shr randomly.
  453. #
  454. RANDOM_TESTS = [
  455. test_add, test_sub, test_mul, test_sqr,
  456. test_log, test_pow, test_sqrt, test_root_n,
  457. test_shl_leg, test_shr_leg, test_shl, test_shr_signed,
  458. test_gcd, test_lcm, test_is_square, test_div,
  459. ]
  460. SKIP_LARGE = [
  461. test_pow, test_root_n, # test_gcd,
  462. ]
  463. SKIP_LARGEST = []
  464. # Untimed warmup.
  465. for test_proc in TESTS:
  466. for t in TESTS[test_proc]:
  467. res = test_proc(*t)
  468. if __name__ == '__main__':
  469. print("\n---- math/big tests ----")
  470. print()
  471. max_name = 0
  472. for test_proc in TESTS:
  473. max_name = max(max_name, len(test_proc.__name__))
  474. fmt_string = "{name:>{max_name}}, {test_count} tests"
  475. fmt_string = fmt_string.replace("{max_name}", str(max_name))
  476. for test_proc in TESTS:
  477. count = 0
  478. for t in TESTS[test_proc]:
  479. count += 1
  480. test_proc(*t)
  481. print(fmt_string.format(name=test_proc.__name__, test_count=count))
  482. for BITS, ITERATIONS in BITS_AND_ITERATIONS:
  483. print()
  484. print("---- math/big with two random {bits:,} bit numbers ----".format(bits=BITS))
  485. print()
  486. #
  487. # We've already tested up to the 10th root.
  488. #
  489. TEST_ROOT_N_PARAMS = [2, 3, 4, 5, 6]
  490. for test_proc in RANDOM_TESTS:
  491. if BITS > 1_200 and test_proc in SKIP_LARGE: continue
  492. if BITS > 4_096 and test_proc in SKIP_LARGEST: continue
  493. count = 0
  494. UNTIL_ITERS = ITERATIONS
  495. if test_proc == test_root_n and BITS == 1_200:
  496. UNTIL_ITERS /= 10
  497. UNTIL_TIME = TOTAL_TIME + BITS / args.timed_bits
  498. # We run each test for a second per 20k bits
  499. index = 0
  500. while we_iterate():
  501. a = randint(-(1 << BITS), 1 << BITS)
  502. b = randint(-(1 << BITS), 1 << BITS)
  503. if test_proc == test_div:
  504. # We've already tested division by zero above.
  505. bits = int(BITS * 0.6)
  506. b = randint(-(1 << bits), 1 << bits)
  507. if b == 0:
  508. b == 42
  509. elif test_proc == test_log:
  510. # We've already tested log's domain errors.
  511. a = randint(1, 1 << BITS)
  512. b = randint(2, 1 << 60)
  513. elif test_proc == test_pow:
  514. b = randint(1, 10)
  515. elif test_proc == test_sqrt:
  516. a = randint(1, 1 << BITS)
  517. b = Error.Okay
  518. elif test_proc == test_root_n:
  519. a = randint(1, 1 << BITS)
  520. b = TEST_ROOT_N_PARAMS[index]
  521. index = (index + 1) % len(TEST_ROOT_N_PARAMS)
  522. elif test_proc == test_shl_leg:
  523. b = randint(0, 10);
  524. elif test_proc == test_shr_leg:
  525. a = abs(a)
  526. b = randint(0, 10);
  527. elif test_proc == test_shl:
  528. b = randint(0, min(BITS, 120))
  529. elif test_proc == test_shr_signed:
  530. b = randint(0, min(BITS, 120))
  531. elif test_proc == test_is_square:
  532. a = randint(0, 1 << BITS)
  533. elif test_proc == test_lcm:
  534. smallest = min(a, b)
  535. biggest = max(a, b)
  536. # Randomly swap biggest and smallest
  537. if randint(1, 11) % 2 == 0:
  538. smallest, biggest = biggest, smallest
  539. a, b = smallest, biggest
  540. else:
  541. b = randint(0, 1 << BITS)
  542. count += 1
  543. test_proc(a, b)
  544. print(fmt_string.format(name=test_proc.__name__, test_count=count))
  545. print()
  546. print("---- THE END ----")
  547. vectors.write("}")
  548. if total_failures:
  549. exit(1)