src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_pred_test.elvts (about)

     1  ////////
     2  # bool #
     3  ////////
     4  
     5  ~> bool $true
     6  ▶ $true
     7  ~> bool a
     8  ▶ $true
     9  ~> bool [a]
    10  ▶ $true
    11  // "empty" values are also true in Elvish
    12  ~> bool []
    13  ▶ $true
    14  ~> bool [&]
    15  ▶ $true
    16  ~> bool (num 0)
    17  ▶ $true
    18  ~> bool ""
    19  ▶ $true
    20  // only errors, $nil and $false are false
    21  ~> bool ?(fail x)
    22  ▶ $false
    23  ~> bool $nil
    24  ▶ $false
    25  ~> bool $false
    26  ▶ $false
    27  
    28  ///////
    29  # not #
    30  ///////
    31  
    32  ~> not $false
    33  ▶ $true
    34  ~> not $nil
    35  ▶ $true
    36  ~> not ?(fail x)
    37  ▶ $true
    38  ~> not $true
    39  ▶ $false
    40  ~> not a
    41  ▶ $false
    42  
    43  //////
    44  # is #
    45  //////
    46  
    47  // The semantics of "is" is not well-defined, so these results might change in
    48  // future.
    49  ~> is 1 1
    50  ▶ $true
    51  ~> is a b
    52  ▶ $false
    53  ~> is [] []
    54  ▶ $true
    55  ~> is [1] [1]
    56  ▶ $false
    57  
    58  //////
    59  # eq #
    60  //////
    61  
    62  ~> eq 1 1
    63  ▶ $true
    64  ~> eq a b
    65  ▶ $false
    66  ~> eq [] []
    67  ▶ $true
    68  ~> eq [1] [1]
    69  ▶ $true
    70  ~> eq 1 1 2
    71  ▶ $false
    72  
    73  //////////
    74  # not-eq #
    75  //////////
    76  
    77  ~> not-eq a b
    78  ▶ $true
    79  ~> not-eq a a
    80  ▶ $false
    81  // not-eq only accepts two arguments
    82  ~> not-eq
    83  Exception: arity mismatch: arguments must be 2 values, but is 0 values
    84    [tty]:1:1-6: not-eq
    85  ~> not-eq 1
    86  Exception: arity mismatch: arguments must be 2 values, but is 1 value
    87    [tty]:1:1-8: not-eq 1
    88  ~> not-eq 1 2 1
    89  Exception: arity mismatch: arguments must be 2 values, but is 3 values
    90    [tty]:1:1-12: not-eq 1 2 1
    91  
    92  ///////////
    93  # compare #
    94  ///////////
    95  
    96  ## strings ##
    97  ~> compare a b
    98  ▶ (num -1)
    99  ~> compare b a
   100  ▶ (num 1)
   101  ~> compare x x
   102  ▶ (num 0)
   103  
   104  ## numbers ##
   105  ~> compare (num 1) (num 2)
   106  ▶ (num -1)
   107  ~> compare (num 2) (num 1)
   108  ▶ (num 1)
   109  ~> compare (num 3) (num 3)
   110  ▶ (num 0)
   111  ~> compare (num 1/4) (num 1/2)
   112  ▶ (num -1)
   113  ~> compare (num 1/3) (num 0.2)
   114  ▶ (num 1)
   115  ~> compare (num 3.0) (num 3)
   116  ▶ (num 0)
   117  ~> compare (num nan) (num 3)
   118  ▶ (num -1)
   119  ~> compare (num 3) (num nan)
   120  ▶ (num 1)
   121  ~> compare (num nan) (num nan)
   122  ▶ (num 0)
   123  
   124  ## booleans ##
   125  ~> compare $true $false
   126  ▶ (num 1)
   127  ~> compare $false $true
   128  ▶ (num -1)
   129  ~> compare $false $false
   130  ▶ (num 0)
   131  ~> compare $true $true
   132  ▶ (num 0)
   133  
   134  ## lists ##
   135  ~> compare [a, b] [a, a]
   136  ▶ (num 1)
   137  ~> compare [a, a] [a, b]
   138  ▶ (num -1)
   139  ~> compare [x, y] [x, y]
   140  ▶ (num 0)
   141  
   142  ## different types are uncomparable without &total. ##
   143  ~> compare 1 (num 1)
   144  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   145    [tty]:1:1-17: compare 1 (num 1)
   146  ~> compare x [x]
   147  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   148    [tty]:1:1-13: compare x [x]
   149  ~> compare a [&a=x]
   150  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   151    [tty]:1:1-16: compare a [&a=x]
   152  
   153  ## uncomparable types ##
   154  ~> compare { nop 1 } { nop 2}
   155  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   156    [tty]:1:1-26: compare { nop 1 } { nop 2}
   157  ~> compare [&foo=bar] [&a=b]
   158  Exception: bad value: inputs to "compare" or "order" must be comparable values, but is uncomparable values
   159    [tty]:1:1-25: compare [&foo=bar] [&a=b]
   160  
   161  ## total ordering for the same comparable type ##
   162  ~> compare &total (num 1) (num 3/2)
   163  ▶ (num -1)
   164  ~> compare &total (num 3/2) (num 2)
   165  ▶ (num -1)
   166  
   167  ## total ordering for the same uncomparable type ##
   168  ~> compare &total { nop 1 } { nop 2 }
   169  ▶ (num 0)
   170  ~> compare &total [&foo=bar] [&a=b]
   171  ▶ (num 0)
   172  
   173  ## total ordering for different types ##
   174  ~> == (compare &total foo (num 2)) (compare &total bar (num 10))
   175  ▶ $true
   176  ~> + (compare &total foo (num 2)) (compare &total (num 2) foo)
   177  ▶ (num 0)