From 50feeaa285b651c8661e25984af4ce58f88c9340 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sat, 31 Jul 2021 23:47:52 +0200 Subject: [PATCH] big: Add test for `factorial`. --- core/math/big/build.bat | 4 ++-- core/math/big/test.odin | 18 ++++++++++++++++++ core/math/big/test.py | 17 ++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/core/math/big/build.bat b/core/math/big/build.bat index 2a21e4f98..69dfe7995 100644 --- a/core/math/big/build.bat +++ b/core/math/big/build.bat @@ -1,7 +1,7 @@ @echo off -odin run . -vet +:odin run . -vet :odin build . -build-mode:shared -show-timings -o:minimal -use-separate-modules -:odin build . -build-mode:shared -show-timings -o:size -use-separate-modules +odin build . -build-mode:shared -show-timings -o:size -use-separate-modules :odin build . -build-mode:shared -show-timings -o:speed -use-separate-modules python test.py diff --git a/core/math/big/test.odin b/core/math/big/test.odin index b8643813c..a03a7e879 100644 --- a/core/math/big/test.odin +++ b/core/math/big/test.odin @@ -276,3 +276,21 @@ PyRes :: struct { return PyRes{res = r, err = .None}; } +/* + dest = factorial(n) +*/ +@export test_factorial :: proc "c" (n: DIGIT) -> (res: PyRes) { + context = runtime.default_context(); + err: Error; + + dest := &Int{}; + defer destroy(dest); + + if err = factorial(dest, n); err != .None { return PyRes{res=":factorial:factorial(n):", err=err}; } + + r: cstring; + r, err = int_itoa_cstring(dest, 16, context.temp_allocator); + if err != .None { return PyRes{res=":factorial:itoa(res):", err=err}; } + return PyRes{res = r, err = .None}; +} + diff --git a/core/math/big/test.py b/core/math/big/test.py index abfa42336..75152821f 100644 --- a/core/math/big/test.py +++ b/core/math/big/test.py @@ -134,6 +134,8 @@ int_shl = load(l.test_shl, [c_char_p, c_longlong], Res) int_shr = load(l.test_shr, [c_char_p, c_longlong], Res) int_shr_signed = load(l.test_shr_signed, [c_char_p, c_longlong], Res) +int_factorial = load(l.test_factorial, [c_uint64], Res) + def test(test_name: "", res: Res, param=[], expected_error = Error.Okay, expected_result = "", radix=16): passed = True r = None @@ -321,6 +323,16 @@ def test_shr_signed(a = 0, bits = 0, expected_error = Error.Okay): return test("test_shr_signed", res, [a, bits], expected_error, expected_result) +def test_factorial(n = 0, expected_error = Error.Okay): + args = [n] + res = int_factorial(*args) + expected_result = None + if expected_error == Error.Okay: + expected_result = math.factorial(n) + + return test("test_factorial", res, [n], expected_error, expected_result) + + # TODO(Jeroen): Make sure tests cover edge cases, fast paths, and so on. # # The last two arguments in tests are the expected error and expected result. @@ -394,7 +406,10 @@ TESTS = { [ -149195686190273039203651143129455, 12 ], [ 611105530635358368578155082258244262, 12 ], [ 149195686190273039203651143129455, 12 ], - ] + ], + test_factorial: [ + [ 12_345 ], + ], } total_passes = 0