From 9b0da89a03f30f6d6c361dca4ae00a9c3be4d221 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 17 Aug 2023 10:13:00 +0200 Subject: [PATCH] Added `compare_to` as a standard macro. --- lib/std/core/builtin_comparison.c3 | 14 ++++++++++++++ lib/std/math/math.c3 | 1 - test/unit/stdlib/core/comparison.c3 | 22 ++++++++++++++++++++++ test/unit/stdlib/threads/simple_thread.c3 | 8 ++++---- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/unit/stdlib/core/comparison.c3 diff --git a/lib/std/core/builtin_comparison.c3 b/lib/std/core/builtin_comparison.c3 index 7dc2270ec..e60a151c0 100644 --- a/lib/std/core/builtin_comparison.c3 +++ b/lib/std/core/builtin_comparison.c3 @@ -48,6 +48,20 @@ macro greater(a, b) @builtin $endswitch } +/** + * @require types::is_comparable_value(a) && types::is_comparable_value(b) + **/ +macro int compare_to(a, b) @builtin +{ + $switch + $case $defined(a.compare_to): + return a.compare_to(b); + $case $defined(a.less): + return (int)b.less(a) - (int)a.less(b); + $default: + return (int)(a > b) - (int)(a < b); + $endswitch +} /** * @require types::is_comparable_value(a) && types::is_comparable_value(b) **/ diff --git a/lib/std/math/math.c3 b/lib/std/math/math.c3 index e65f7d3b5..c4b827a26 100644 --- a/lib/std/math/math.c3 +++ b/lib/std/math/math.c3 @@ -131,7 +131,6 @@ macro sign(x) $endif } - /** * @require values::@is_int(x) || values::@is_float(x) "Expected an integer or floating point value" * @checked x + y diff --git a/test/unit/stdlib/core/comparison.c3 b/test/unit/stdlib/core/comparison.c3 new file mode 100644 index 000000000..af0fdf9b8 --- /dev/null +++ b/test/unit/stdlib/core/comparison.c3 @@ -0,0 +1,22 @@ +module comparison @test; + +fn void compare_long() +{ + assert(compare_to(long.max, long.min) == 1); + assert(compare_to(long.min, long.max) == -1); + assert(compare_to(long.min, long.min) == 0); +} + +fn void compare_ulong() +{ + assert(compare_to(ulong.max, ulong.min) == 1); + assert(compare_to(ulong.min, ulong.max) == -1); + assert(compare_to(ulong.min, ulong.min) == 0); +} + +fn void compare_int128() +{ + assert(compare_to(int128.max, int128.min) == 1); + assert(compare_to(int128.min, int128.max) == -1); + assert(compare_to(int128.min, int128.min) == 0); +} diff --git a/test/unit/stdlib/threads/simple_thread.c3 b/test/unit/stdlib/threads/simple_thread.c3 index 5e8b60229..33d6b0770 100644 --- a/test/unit/stdlib/threads/simple_thread.c3 +++ b/test/unit/stdlib/threads/simple_thread.c3 @@ -19,7 +19,7 @@ Mutex m; fn void! testrun_mutex() @test { - Thread[100] ts; + Thread[20] ts; a = 0; m.init()!; foreach (&t : ts) @@ -43,7 +43,7 @@ fn void! testrun_mutex() @test { assert(t.join()! == 0); } - assert(a == 100); + assert(a == ts.len); m.destroy()!; } @@ -63,12 +63,12 @@ fn void! testrun_mutex_timeout() @test TimedMutex m; m.init()!; m.lock()!; - if (try m.lock_timeout(100)) + if (try m.lock_timeout(20)) { assert(false, "lock_timeout should fail"); } m.unlock()!; - m.lock_timeout(100)!; + m.lock_timeout(20)!; m.unlock()!; }