Explorar o código

ncd: Implement num_min and num_max functions.

Ambroz Bizjak %!s(int64=11) %!d(string=hai) anos
pai
achega
86f709fa7b
Modificáronse 2 ficheiros con 18 adicións e 0 borrados
  1. 8 0
      ncd/modules/basic_functions.c
  2. 10 0
      ncd/tests/basic_functions.ncd

+ 8 - 0
ncd/modules/basic_functions.c

@@ -339,6 +339,8 @@ DEFINE_INT_OPERATOR(subtract, (n1 - n2), (n1 < n2), "subtraction underflow")
 DEFINE_INT_OPERATOR(multiply, (n1 * n2), (n2 != 0 && n1 > UINTMAX_MAX / n2), "multiplication overflow")
 DEFINE_INT_OPERATOR(divide, (n1 / n2), (n2 == 0), "division quotient is zero")
 DEFINE_INT_OPERATOR(modulo, (n1 % n2), (n2 == 0), "modulo modulus is zero")
+DEFINE_INT_OPERATOR(min, (n1 < n2 ? n1 : n2), (0), "")
+DEFINE_INT_OPERATOR(max, (n1 > n2 ? n1 : n2), (0), "")
 
 
 // Encode and decode value.
@@ -507,6 +509,12 @@ static struct NCDModuleFunction const functions[] = {
     }, {
         .func_name = "num_modulo",
         .func_eval = integer_operator_modulo_eval
+    }, {
+        .func_name = "num_min",
+        .func_eval = integer_operator_min_eval
+    }, {
+        .func_name = "num_max",
+        .func_eval = integer_operator_max_eval
     }, {
         .func_name = "encode_value",
         .func_eval = encode_value_eval

+ 10 - 0
ncd/tests/basic_functions.ncd

@@ -234,6 +234,16 @@ process main {
     assert(a);
     
     
+    var(@num_min("3", "5")) x;
+    val_equal(x, "3") a;
+    assert(a);
+    
+    
+    var(@num_max("3", "5")) x;
+    val_equal(x, "5") a;
+    assert(a);
+    
+    
     var(@encode_value("foo")) x;
     val_equal(x, "\"foo\"") a;
     assert(a);