Skip to content
Browse files

[gsoc_spectest] reorganization and a few more tests for ===, eqv, and…

… ranges. (7 tests added)

git-svn-id: http://svn.pugscode.org/pugs@21114 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information...
1 parent e90d4b6 commit 9a5861722c004ab526882767719348fdbf47e25c Auzon committed
Showing with 244 additions and 1 deletion.
  1. +92 −0 S03-operators/range.t
  2. +146 −0 S03-operators/value_equivalence.t
  3. +6 −1 S29-any/eqv.t
View
92 S03-operators/range.t
@@ -0,0 +1,92 @@
+use v6;
+
+use Test;
+
+plan 51;
+
+# 3..2 must *not* produce "3 2". Use reverse to get a reversed range. -lwall
+
+is ~(3..6), "3 4 5 6", "(..) works on numbers (1)";
+is ~(3..3), "3", "(..) works on numbers (2)";
+is ~(3..2), "", "(..) works on numbers (3)";
+is ~(8..11), "8 9 10 11", "(..) works on carried numbers (3)";
+
+is ~("a".."c"), "a b c", "(..) works on chars (1)";
+is ~("a".."a"), "a", "(..) works on chars (2)";
+is ~("b".."a"), "", "(..) works on chars (3)";
+is ~("Y".."AB"), "Y Z AA AB", "(..) works on carried chars (3)";
+
+is ~('Y'..'z', 'Y Z', '(..) works on uppercase letter .. lowercase letter');
+is ~('Y'..'_', 'Y Z', '(..) works on letter .. non-letter');
+
+isnt ~(0..^10), ~(0.. ^10), '(0..^10) is not the same as (0.. ^10)');
+
+is ~(3..9-3), "3 4 5 6", "(..) has correct precedence (1)";
+is ~(2+1..6), "3 4 5 6", "(..) has correct precedence (2)";
+
+# Test the three exclusive range operators:
+# L<S03/Range semantics/range operator has variants>
+is [1^..9], [2..9], "bottom-exclusive range (^..) works";
+is [1 ..^9], [1..8], "top-exclusive range (..^) works";
+is [1^..^9], [2..8], "double-exclusive range (^..^) works";
+is [1^..^2], [], "double-exclusive range (^..^) can produce null range";
+
+# tests of (x ^..^ x) here and below ensure that our implementation
+# of double-exclusive range does not blindly remove an element
+# from the head and tail of a list
+is [1^..^1], [], "double-exclusive range (x ^..^ x) where x is an int";
+
+is ["a"^.."z"], ["b".."z"], "bottom-exclusive string range (^..) works";
+is ["a"..^"z"], ["a".."y"], "top-exclusive string range (..^) works";
+is ["a"^..^"z"], ["b".."y"], "double-exclusive string range (^..^) works";
+is ['a'^..^'b'], [], "double-exclusive string range (^..^) can produce null range";
+is ['a' ^..^ 'a'], [], "double-exclusive range (x ^..^ x) where x is a char";
+
+is 1.5 ~~ 1^..^2, Bool::True, "lazy evaluation of the range operator", :todo<bug>;
+
+# Test the unary ^ operator
+is ~(^5), "0 1 2 3 4", "unary ^num produces the range 0..^num";
+is [^1], [0], "unary ^ on the boundary ^1 works";
+is [^0], [], "unary ^0 produces null range";
+
+# Test with floats
+# 2006-12-05:
+# 16:16 <TimToady> ~(1.9 ^..^ 4.9) should produce 2.9, 3.9
+# 16:17 <pmichaud> and ~(1.9 ^..^ 4.5) would produce the same?
+# 16:17 <TimToady> yes
+is ~(1.1 .. 4) , "1.1 2.1 3.1", "range with float .min";
+is ~(1.9 .. 4) , "1.9 2.9 3.9", "range with float .min";
+is ~(1.1 ^.. 4), "2.1 3.1" , "bottom exclusive range of float";
+is ~(1.9 ^.. 4), "2.9 3.9" , "bottom exclusive range of float";
+
+is ~(1 .. 4.1) , "1 2 3 4", "range with float .max";
+is ~(1 .. 4.9) , "1 2 3 4", "range with float .max";
+is ~(1 ..^ 4.1), "1 2 3 4", "top exclusive range of float";
+is ~(1 ..^ 4.9), "1 2 3 4", "top exclusive range of float";
+
+is ~(1.1 .. 4.1), "1.1 2.1 3.1 4.1", "range with float .min/.max";
+is ~(1.9 .. 4.1), "1.9 2.9 3.9" , "range with float .min/.max";
+is ~(1.1 .. 4.9), "1.1 2.1 3.1 4.1", "range with float .min/.max";
+is ~(1.9 .. 4.9), "1.9 2.9 3.9 4.9", "range with float .min/.max";
+
+is ~(1.1 ^..^ 4.1), "2.1 3.1" , "both exclusive float range";
+is ~(1.9 ^..^ 4.1), "2.9 3.9" , "both exclusive float range";
+is ~(1.1 ^..^ 4.9), "2.1 3.1 4.1", "both exclusive float range";
+is ~(1.9 ^..^ 4.9), "2.9 3.9" , "both exclusive float range";
+is [1.1 ^..^ 1.1], [], "double-exclusive range (x ^..^ x) where x is a float";
+
+# Test that the operands are forced to scalar context
+{
+ my @one = (1,);
+ my @three = (1, 1, 1);
+
+ is ~(@one .. 3) , "1 2 3", "lower inclusive limit is in scalar context";
+ is ~(@one ^.. 3) , "2 3" , "lower exclusive limit is in scalar context";
+ is ~(1 .. @three) , "1 2 3", "upper inclusive limit is in scalar context";
+ is ~(1 ..^ @three) , "1 2" , "upper exclusive limit is in scalar context";
+ is ~(@one .. @three), "1 2 3", "both inclusive limits are in scalar context";
+ is ~(@one ^..^ @three), "2" , "both exclusive limits are in scalar context";
+ is ~(@three .. @one), "" , "null range produced with lists forced to scalar context";
+}
+
+# For tests involving :by, see t/operators/adverbial_modifiers.t
View
146 S03-operators/value_equivalence.t
@@ -0,0 +1,146 @@
+use v6;
+
+use Test;
+
+=begin pod
+
+L<S03/"Chaining binary precedence" /Value identity>
+
+C<===> and C<eqv> are 2 distinct operators, where C<===> tests value
+equivalence for immutable types and reference equivalence for
+mutable types, and C<eqv> tests value equivalence for snapshots of mutable
+types. So C<(1,2) === (1,2)> returns true but C<[1,2] === [1,2]> returns
+false, and C<[1,2] eqv [1,2]> returns true.
+
+=end pod
+
+plan 51;
+
+# === on values
+{
+ ok (1 === 1), "=== on values (1)";
+ ok (0 === 0), "=== on values (2)";
+ ok !(0 === 1), "=== on values (3)";
+ ok ("abc" === "abc"), "=== on values(abc)";
+ ok !("abc" === "ABC"), "=== on values(abc === ABC)";
+ ok !(1 === 1.0), "=== on values (1 === 1.0)";
+ ok !(1 === "1"), '=== on values (1 === "1")';
+}
+
+# Value types
+{
+ my $a = 1;
+ my $b = 1;
+
+ ok $a === $a, "=== on value types (1-1)";
+ ok $b === $b, "=== on value types (1-2)";
+ ok $a === $b, "=== on value types (1-3)";
+}
+
+{
+ my $a = 1;
+ my $b = 2;
+
+ ok ($a === $a), "=== on value types (2-1)";
+ ok ($b === $b), "=== on value types (2-2)";
+ ok !($a === $b), "=== on value types (2-3)";
+}
+
+# Reference types
+{
+ my @a = (1,2,3);
+ my @b = (1,2,3);
+
+ ok (\@a === \@a), "=== on array references (1)";
+ ok (\@b === \@b), "=== on array references (2)";
+ ok !(\@a === \@b), "=== on array references (3)";
+}
+
+{
+ my $a = \3;
+ my $b = \3;
+
+ ok ($a === $a), "=== on scalar references (1-1)";
+ ok ($b === $b), "=== on scalar references (1-2)";
+ ok !($a === $b), "=== on scalar references (1-3)", :todo<bug>;
+}
+
+{
+ my $a = { 3 };
+ my $b = { 3 };
+
+ ok ($a === $a), "=== on sub references (1-1)";
+ ok ($b === $b), "=== on sub references (1-2)";
+ ok !($a === $b), "=== on sub references (1-3)";
+}
+
+{
+ ok (&say === &say), "=== on sub references (2-1)";
+ ok (&map === &map), "=== on sub references (2-2)";
+ ok !(&say === &map), "=== on sub references (2-3)";
+}
+
+{
+ my $num = 3;
+ my $a = \$num;
+ my $b = \$num;
+
+ ok ($a === $a), "=== on scalar references (2-1)";
+ ok ($b === $b), "=== on scalar references (2-2)";
+ ok ($a === $b), "=== on scalar references (2-3)";
+}
+
+{
+ ok !([1,2,3] === [4,5,6]), "=== on anonymous array references (1)";
+ ok !([1,2,3] === [1,2,3]), "=== on anonymous array references (2)";
+ ok !([] === []), "=== on anonymous array references (3)";
+}
+
+{
+ ok !({a => 1} === {a => 2}), "=== on anonymous hash references (1)";
+ ok !({a => 1} === {a => 1}), "=== on anonymous hash references (2)";
+}
+
+{
+ ok !(\3 === \4), "=== on anonymous scalar references (1)";
+ ok !(\3 === \3), "=== on anonymous scalar references (2)", :todo<bug>;
+ ok !(\undef === \undef), "=== on anonymous scalar references (3)", :todo<bug>;
+}
+
+# Chained === (not specced, but obvious)
+{
+ ok (3 === 3 === 3), "chained === (1)";
+ ok !(3 === 3 === 4), "chained === (2)";
+}
+
+# Subparam binding doesn't affect === test
+{
+ my $foo;
+ my $test = -> $arg { $foo === $arg };
+
+ $foo = 3;
+ ok $test($foo), "subparam binding doesn't affect === (1)";
+ ok $test(3), "subparam binding doesn't affect === (2)";
+
+ ok !$test(4), "subparam binding doesn't affect === (3)";
+ my $bar = 4;
+ ok !$test($bar), "subparam binding doesn't affect === (4)";
+}
+
+{
+ my $a = 1;
+ my $b = 2;
+ is($a === $a, Bool::True, '=== returns Bool::True when true');
+ is($a === $b, Bool::False, '=== returns Bool::False when false');
+}
+
+# L<S03/"Chaining binary precedence" /Negated relational operators>
+{
+ ok !(1 !=== 1), "!=== on values (1)";
+ ok !(0 !=== 0), "!=== on values (2)";
+ ok (1 !=== 0), "!=== on values (3)";
+ ok !("abc" !=== "abc"), "!=== on values(abc)";
+ ok ("abc" !=== "ABC"), "!=== on values(abc !=== ABC)";
+ ok (1 !=== 1.0), "!=== on values (1 !=== 1.0)";
+ ok (1 !=== "1"), '!=== on values (1 !=== "1")';
+}
View
7 S29-any/eqv.t
@@ -1,8 +1,9 @@
use v6;
use Test;
-plan 38;
+plan 40;
+# L<S03/Comparison semantics/Binary eqv tests equality much like === does>
# L<S29/Any/"=item eqv">
# eqv on values
@@ -117,3 +118,7 @@ plan 38;
ok !$test($bar), "subparam binding doesn't affect eqv (4)";
}
+{
+ is(1 eqv 1, Bool::True, 'eqv returns Bool::True when true');
+ is(0 eqv 1, Bool::False, 'eqv returns Bool::False when false');
+}

0 comments on commit 9a58617

Please sign in to comment.
Something went wrong with that request. Please try again.