Added compare_to as a standard macro.

This commit is contained in:
Christoffer Lerno
2023-08-17 10:13:00 +02:00
parent b05ba8d110
commit 9b0da89a03
4 changed files with 40 additions and 5 deletions

View File

@@ -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)
**/

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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()!;
}