mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Added compare_to as a standard macro.
This commit is contained in:
@@ -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)
|
||||
**/
|
||||
|
||||
@@ -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
|
||||
|
||||
22
test/unit/stdlib/core/comparison.c3
Normal file
22
test/unit/stdlib/core/comparison.c3
Normal 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);
|
||||
}
|
||||
@@ -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()!;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user