/
eqv.t
208 lines (168 loc) Β· 6.12 KB
/
eqv.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use v6;
use Test;
plan 57;
# L<S03/Comparison semantics/Binary eqv tests equality much like === does>
# L<S32::Basics/Any/"=item eqv">
# eqv on values
{
ok (1 eqv 1), "eqv on values (1)";
ok (0 eqv 0), "eqv on values (2)";
ok !(0 eqv 1), "eqv on values (3)";
}
# Value types
{
my $a = 1;
my $b = 1;
ok $a eqv $a, "eqv on value types (1-1)";
ok $b eqv $b, "eqv on value types (1-2)";
ok $a eqv $b, "eqv on value types (1-3)";
}
{
my $a = 1;
my $b = 2;
ok ($a eqv $a), "eqv on value types (2-1)";
ok ($b eqv $b), "eqv on value types (2-2)";
ok !($a eqv $b), "eqv on value types (2-3)";
}
{
my @a = (1,2,3);
my @b = (1,2,3);
ok (\@a eqv \@a), "eqv on array references (1)";
ok (\@b eqv \@b), "eqv on array references (2)";
#?rakudo todo 'huh?'
ok !(\@a eqv \@b), "eqv on array references (3)";
@a := @b;
ok \@a eqv \@b, '\@array of two bound arrays are eqv';
}
{
my $a = \3;
my $b = \3;
ok ($a eqv $a), "eqv on scalar references (1-1)";
ok ($b eqv $b), "eqv on scalar references (1-2)";
ok ($a eqv $b), "eqv on scalar references (1-3)";
#?rakudo todo 'huh?'
ok (\$a !eqv \$b), "eqv on scalar references (1-4)";
}
{
my $a = { 3 };
my $b = { 3 };
ok ($a eqv $a), "eqv on sub references (1-1)";
ok ($b eqv $b), "eqv on sub references (1-2)";
# it's impossible to compare blocks for equivalence in general,
# and they have associations to different source locations
# (line number, column)
nok ($a eqv $b), "eqv on sub references (1-3)";
nok ($a eqv { 5 }), 'eqv on sub references (1-4)';
}
{
ok (&say eqv &say), "eqv on sub references (2-1)";
ok (&map eqv &map), "eqv on sub references (2-2)";
ok !(&say eqv &map), "eqv on sub references (2-3)";
}
{
my $num = 3; my $a = \$num;
my $b = \$num;
ok ($a eqv $a), "eqv on scalar references (2-1)";
ok ($b eqv $b), "eqv on scalar references (2-2)";
ok ($a eqv $b), "eqv on scalar references (2-3)";
}
{
nok ([1,2,3] eqv [4,5,6]), "eqv on anonymous array references (1)";
ok ([1,2,3] eqv [1,2,3]), "eqv on anonymous array references (2)";
ok ([] eqv []), "eqv on anonymous array references (3)";
}
{
ok !({a => 1} eqv {a => 2}), "eqv on anonymous hash references (-)";
ok ({a => 1} eqv {a => 1}), "eqv on anonymous hash references (+)";
ok ({a => 2, b => 1} eqv { b => 1, a => 2}), 'order really does not matter';
ok !({a => 1} eqv {a => 1, b => 2}), 'hashes: different number of pairs';
}
{
ok !(\3 eqv \4), "eqv on anonymous scalar references (1)";
# XXX the following seems bogus nowadays
#?rakudo todo 'huh?'
ok !(\3 eqv \3), "eqv on anonymous scalar references (2)";
#?rakudo skip 'huh?'
ok !(\Mu eqv \Mu), "eqv on anonymous scalar references (3)";
}
# Chained eqv (not specced, but obvious)
{
ok (3 eqv 3 eqv 3), "chained eqv (1)";
ok !(3 eqv 3 eqv 4), "chained eqv (2)";
}
# Subparam binding doesn't affect eqv test
{
my $foo;
my $test = -> $arg { $foo eqv $arg };
$foo = 3;
ok $test($foo), "subparam binding doesn't affect eqv (1)";
ok $test(3), "subparam binding doesn't affect eqv (2)";
ok !$test(4), "subparam binding doesn't affect eqv (3)";
my $bar = 4;
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');
}
{
is Any eqv Any, Bool::True, 'Any eqv Any';
}
{
ok 'a' eqv any(<a b c>), "eqv autothreads correctly";
}
# RT #75322 - Rakudo used to be confused when lists began with ()
{
nok ((), "x") eqv ((), 9), 'list starting with () - 1';
nok ((), (), 1) eqv ((), 9), 'list starting with () - 1';
nok ((), (), (), 1) eqv ((), (), ""), 'list starting with () - 1';
nok ((), (), (), 1) eqv ((), 4), 'list starting with () - 1';
ok ((), ()) eqv ((), ()), '((), ())';
}
# Nieczabug #142
{
nok 4 eqv 4.0, "Values should be eqv only if they are the same type";
nok 4 eqv '4', 'Str vs. Int';
}
subtest 'Setty eqv Setty' => {
plan 8;
my $a = ["arr"];
is-deeply Set.new(1, "a", Cool, $a) eqv Set.new(1, "a", Cool, $a), True,
'identical Sets eqv each other';
is-deeply SetHash.new(1, "a", Cool, $a) eqv SetHash.new(1, "a", Cool, $a),
True, 'identical SetHashes eqv each other';
is-deeply Set.new(42) eqv SetHash.new(42), False,
'Set does not eqv SetHash';
is-deeply set(<42>) eqv set( 42 ), False, 'IntStr does not eqv Int';
is-deeply set(<42>) eqv set('42'), False, 'IntStr does not eqv Str';
is-deeply set( 42 ) eqv set(<42>), False, 'Int does not eqv IntStr';
is-deeply set('42') eqv set(<42>), False, 'Str does not eqv IntStr';
is-deeply set(<42>) eqv set(<42>), True, 'IntStr does eqv IntStr';
}
subtest 'Seq eqv List' => {
my @tests = ().Seq => (), (1, 2).Seq => (1, 2), ().Seq => (1, 2),
(1, 2).Seq => (), (1β¦β) => (1β¦β).List, (1β¦β).List => (1β¦β);
plan +@tests;
is-deeply (.key eqv .value), False,
"{.key.^name}({.key}) not eqv {.value.^name}({.value})"
for @tests;
}
subtest 'Throws/lives in lazy cases' => {
plan 8;
# Note that `eqv` *can* compare lazy iterables when the answer
# doesn't require iterating over them. These cases do NOT throw:
# 1. Only one of the arguments is lazy
# 2. Both arguments are lazy, but are of different type
throws-like { (1β¦β) eqv (1β¦β) }, X::Cannot::Lazy, :action<eqv>,
'both lazy, same types (Seqs)';
throws-like { (1β¦β).List eqv (1β¦β).List }, X::Cannot::Lazy, :action<eqv>,
'both lazy, same types (Lists)';
throws-like { (1β¦β).Array eqv (1β¦β).Array }, X::Cannot::Lazy, :action<eqv>,
'both lazy, same types (Arrays)';
lives-ok { (1β¦β) eqv (1β¦β).List }, 'both lazy, different types';
lives-ok { (1β¦β) eqv (1,3) }, 'different types, only one lazy';
lives-ok { (1β¦β) eqv (1β¦3) }, 'Seqs, only one lazy';
lives-ok { (1β¦β).List eqv (1β¦3).List }, 'Lists, only one lazy';
lives-ok { (1β¦β).Array eqv (1β¦3).Array }, 'Arrays, only one lazy';
}
# vim: ft=perl6