[t/spec] Add tricky tests (which pass after latest Rakudo patch), unfudge old simple...
[pugs.git] / t / spec / S03-operators / comparison.t
blob77b631075de83eafd7c209e329222ef981851b18
1 use v6;
2 use Test;
4 plan 42;
6 # N.B.:  relational ops are in relational.t
8 # L<S03/Nonchaining binary precedence/Order::Increase, Order::Same, or Order::Decrease>
9 is(+Order::Increase, -1, 'Order::Increase numifies to -1');
10 is(+Order::Same,      0, 'Order::Same numifies to 0');
11 is(+Order::Decrease,  1, 'Order::Decrease numifies to 1');
13 #L<S03/Comparison semantics>
15 # spaceship comparisons (Num)
16 is(1 <=> 1, Order::Same,     '1 <=> 1 is same');
17 is(1 <=> 2, Order::Increase, '1 <=> 2 is increase');
18 is(2 <=> 1, Order::Decrease, '2 <=> 1 is decrease');
19 is('a' <=> '1', Order::Increase, '<=> is in numeric context');
21 is 0 <=> -1,   Order::Decrease, '0 <=> -1 is increase';
22 is -1 <=> 0,      Order::Increase, '-1 <=> 0 is decrease';
23 is 0 <=> -1/2,    Order::Decrease, '0 <=> -1/2 is decrease';
24 is 0 <=> 1/2,     Order::Increase, '0 <=> 1/2 is increase';
25 is -1/2 <=> 0,    Order::Increase, '-1/2 <=> 0 is decrease';
26 is 1/2 <=> 0,     Order::Decrease, '1/2 <=> 0 is decrease';
27 is 1/2 <=> 1/2,   Order::Same, '1/2 <=> 1/2 is same';
28 is -1/2 <=> -1/2, Order::Same, '-1/2 <=> -1/2 is same';
29 is 1/2 <=> -1/2,  Order::Decrease, '1/2 <=> -1/2 is decrease';
30 is -1/2 <=> 1/2,  Order::Increase, '-1/2 <=> 1/2 is increase';
32 # leg comparison (Str)
33 is('a' leg 'a', Order::Same,     'a leg a is same');
34 is('a' leg 'b', Order::Increase, 'a leg b is increase');
35 is('b' leg 'a', Order::Decrease, 'b leg a is decrease');
36 is('a' leg 1,   Order::Decrease, 'leg is in string context');
37 is("a" leg "a\0", Order::Increase, 'a leg a\0 is increase');
38 is("a\0" leg "a\0", Order::Same, 'a\0 leg a\0 is same');
39 is("a\0" leg "a", Order::Decrease, 'a\0 leg a is decrease');
41 # cmp comparison
42 is('a' cmp 'a', Order::Same,     'a cmp a is same');
43 is('a' cmp 'b', Order::Increase, 'a cmp b is increase');
44 is('b' cmp 'a', Order::Decrease, 'b cmp a is decrease');
45 is(1 cmp 1,     Order::Same,     '1 cmp 1 is same');
46 is(1 cmp 2,     Order::Increase, '1 cmp 2 is increase');
47 is(2 cmp 1,     Order::Decrease, '2 cmp 1 is decrease');
48 is('a' cmp 1,   Order::Decrease, '"a" cmp 1 is decrease'); # unspecced but P5 behavior
49 is("a" cmp "a\0", Order::Increase, 'a cmp a\0 is increase');
50 is("a\0" cmp "a\0", Order::Same, 'a\0 cmp a\0 is same');
51 is("a\0" cmp "a", Order::Decrease, 'a\0 cmp a is decrease');
53 # compare numerically with non-numeric
55     class Blue { 
56         method Numeric() { 3 + 5i; }
57     } 
58     my $a = Blue.new;
60     ok +$a == 3 + 5i, '+$a == 3 + 5i (just checking)';
61     ok $a == 3 + 5i, '$a == 3 + 5i';
62     ok $a != 3 + 4i, '$a != 3 + 4i';
63     nok $a != 3 + 5i, 'not true that $a != 3 + 5i';
64     
65     lives_ok { $a < 5 }, '$a < 5 lives okay';
66     lives_ok { $a <= 5 }, '$a <= 5 lives okay';
67     lives_ok { $a > 5 }, '$a > 5 lives okay';
68     lives_ok { $a >= 5 }, '$a => 5 lives okay';
71 # vim: ft=perl6