Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 259 lines (206 sloc) 8.771 kB
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
1 use v6;
2 use Test;
3
df27e0d @colomon Add tests for Set et al :exists and :delete.
colomon authored
4 plan 106;
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
5
6 sub showkv($x) {
7 $x.keys.sort.map({ $^k ~ ':' ~ $x{$^k} }).join(' ')
8 }
9
10 # L<S02/Immutable types/'the bag listop'>
11
12 {
13 my $b = bag <a foo a a a a b foo>;
14 isa_ok $b, Bag, '&bag produces a Bag';
15 is showkv($b), 'a:5 b:1 foo:2', '...with the right elements';
16
17 is $b<a>, 5, 'Single-key subscript (existing element)';
8d18a30 @colomon 70 more tests for Bag.
colomon authored
18 isa_ok $b<a>, Int, 'Single-key subscript yields an Int';
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
19 is $b<santa>, 0, 'Single-key subscript (nonexistent element)';
8d18a30 @colomon 70 more tests for Bag.
colomon authored
20 isa_ok $b<santa>, Int, 'Single-key subscript yields an Int (nonexistent element)';
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
21 ok $b.exists('a'), '.exists with existing element';
22 nok $b.exists('santa'), '.exists with nonexistent element';
23
8d18a30 @colomon 70 more tests for Bag.
colomon authored
24 is $b.values.elems, 3, "Values returns the correct number of values";
25 is ([+] $b.values), 8, "Values returns the correct sum";
26 ok ?$b, "Bool returns True if there is something in the Bag";
27 nok ?Bag.new(), "Bool returns False if there is nothing in the Bag";
28
29 my $hash;
30 lives_ok { $hash = $b.hash }, ".hash doesn't die";
31 isa_ok $hash, Hash, "...and it returned a Hash";
32 is showkv($hash), 'a:5 b:1 foo:2', '...with the right elements';
33
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
34 dies_ok { $b<a> = 5 }, "Can't assign to an element (Bags are immutable)";
8d18a30 @colomon 70 more tests for Bag.
colomon authored
35 dies_ok { $b<a>++ }, "Can't increment an element (Bags are immutable)";
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
36 dies_ok { $b.keys = <c d> }, "Can't assign to .keys";
37 dies_ok { $b.values = 3, 4 }, "Can't assign to .values";
38
8d18a30 @colomon 70 more tests for Bag.
colomon authored
39 is ~$b<a b>, "5 1", 'Multiple-element access';
40 is ~$b<a santa b easterbunny>, "5 0 1 0", 'Multiple-element access (with nonexistent elements)';
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
41
42 is $b.elems, 8, '.elems gives sum of values';
43 is +$b, 8, '+$bag gives sum of values';
44 }
45
71f1532 @tadzik Tune and fudge bag, keybag, keyset and set tests for Rakudo and for G…
tadzik authored
46 #?rakudo skip ':exists and :delete NYI'
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
47 {
df27e0d @colomon Add tests for Set et al :exists and :delete.
colomon authored
48 my $s = bag <a a b foo>;
49 is $s<a>:exists, True, ':exists with existing element';
50 is $s<santa>:exists, False, ':exists with nonexistent element';
51 dies_ok { $s<a>:delete }, ':delete does not work on bag';
52 }
53
54 {
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
55 my $b = bag 'a', False, 2, 'a', False, False;
56 my @ks = $b.keys;
f7052e6 @colomon Fudge for niecza.
colomon authored
57 #?niecza 2 todo
71f1532 @tadzik Tune and fudge bag, keybag, keyset and set tests for Rakudo and for G…
tadzik authored
58 #?rakudo 2 todo ''
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
59 is @ks.grep(Int)[0], 2, 'Int keys are left as Ints';
60 is @ks.grep(* eqv False).elems, 1, 'Bool keys are left as Bools';
61 is @ks.grep(Str)[0], 'a', 'And Str keys are permitted in the same set';
62 is $b{2, 'a', False}.sort.join(' '), '1 2 3', 'All keys have the right values';
63 }
64
f7052e6 @colomon Fudge for niecza.
colomon authored
65 #?niecza skip "Unmatched key in Hash.LISTSTORE"
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
66 {
67 my %h = bag <a b o p a p o o>;
68 ok %h ~~ Hash, 'A hash to which a Bag has been assigned remains a hash';
69 is showkv(%h), 'a:2 b:1 o:3 p:2', '...with the right elements';
70 }
71
72 {
8d18a30 @colomon 70 more tests for Bag.
colomon authored
73 my $b = bag <a b o p a p o o>;
74 isa_ok $b, Bag, '&Bag.new given an array of strings produces a Bag';
75 is showkv($b), 'a:2 b:1 o:3 p:2', '...with the right elements';
76 }
77
78 {
49f02a2 @colomon Make sure zero-valued pairs are properly ignored in the constructors.
colomon authored
79 my $b = bag [ foo => 10, bar => 17, baz => 42, santa => 0 ];
8d18a30 @colomon 70 more tests for Bag.
colomon authored
80 isa_ok $b, Bag, '&Bag.new given an array of pairs produces a Bag';
81 is showkv($b), 'bar:17 baz:42 foo:10', '... with the right elements';
82 }
83
84 {
49f02a2 @colomon Make sure zero-valued pairs are properly ignored in the constructors.
colomon authored
85 my $b = bag { foo => 10, bar => 17, baz => 42, santa => 0 }.hash;
8d18a30 @colomon 70 more tests for Bag.
colomon authored
86 isa_ok $b, Bag, '&Bag.new given a Hash produces a Bag';
87 is showkv($b), 'bar:17 baz:42 foo:10', '... with the right elements';
88 }
89
90 {
49f02a2 @colomon Make sure zero-valued pairs are properly ignored in the constructors.
colomon authored
91 my $b = bag { foo => 10, bar => 17, baz => 42, santa => 0 };
8d18a30 @colomon 70 more tests for Bag.
colomon authored
92 isa_ok $b, Bag, '&Bag.new given a Hash produces a Bag';
93 is showkv($b), 'bar:17 baz:42 foo:10', '... with the right elements';
94 }
95
96 {
97 my $b = bag set <foo bar foo bar baz foo>;
98 isa_ok $b, Bag, '&Bag.new given a Set produces a Bag';
99 is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
100 }
101
102 {
103 my $b = bag KeySet.new(set <foo bar foo bar baz foo>);
104 isa_ok $b, Bag, '&Bag.new given a KeySet produces a Bag';
105 is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
106 }
107
108 {
109 my $b = bag KeyBag.new(set <foo bar foo bar baz foo>);
110 isa_ok $b, Bag, '&Bag.new given a KeyBag produces a Bag';
111 is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
112 }
113
114 {
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
115 my $b = bag set <foo bar foo bar baz foo>;
116 isa_ok $b, Bag, '&bag given a Set produces a Bag';
117 is showkv($b), 'bar:1 baz:1 foo:1', '... with the right elements';
118 }
119
120 # L<S02/Names and Variables/'C<%x> may be bound to'>
121
122 {
123 my %b := bag <a b c b>;
124 isa_ok %b, Bag, 'A Bag bound to a %var is a Bag';
125 is showkv(%b), 'a:1 b:2 c:1', '...with the right elements';
126
127 is %b<b>, 2, 'Single-key subscript (existing element)';
128 is %b<santa>, 0, 'Single-key subscript (nonexistent element)';
129
130 dies_ok { %b<a> = 1 }, "Can't assign to an element (Bags are immutable)";
131 dies_ok { %b = bag <a b> }, "Can't assign to a %var implemented by Bag";
132 }
133
8d18a30 @colomon 70 more tests for Bag.
colomon authored
134 {
135 my $b = bag { foo => 10, bar => 1, baz => 2};
136
137 # .list is just the keys, as per TimToady:
138 # http://irclog.perlgeek.de/perl6/2012-02-07#i_5112706
139 isa_ok $b.list.elems, 3, ".list returns 3 things";
140 is $b.list.grep(Str).elems, 3, "... all of which are Str";
141
142 isa_ok $b.pairs.elems, 3, ".pairs returns 3 things";
143 is $b.pairs.grep(Pair).elems, 3, "... all of which are Pairs";
144 is $b.pairs.grep({ .key ~~ Str }).elems, 3, "... the keys of which are Strs";
145 is $b.pairs.grep({ .value ~~ Int }).elems, 3, "... and the values of which are Ints";
146
147 is $b.iterator.grep(Pair).elems, 3, ".iterator yields three Pairs";
148 is $b.iterator.grep({ .key ~~ Str }).elems, 3, "... the keys of which are Strs";
149 is $b.iterator.grep({True}).elems, 3, "... and nothing else";
150 }
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
151
152 {
8d18a30 @colomon 70 more tests for Bag.
colomon authored
153 my $b = bag { foo => 10000000000, bar => 17, baz => 42 };
154 my $s;
155 my $c;
156 lives_ok { $s = $b.perl }, ".perl lives";
157 isa_ok $s, Str, "... and produces a string";
158 ok $s.chars < 1000, "... of reasonable length";
159 lives_ok { $c = eval $s }, ".perl.eval lives";
160 isa_ok $c, Bag, "... and produces a Bag";
161 is showkv($c), showkv($b), "... and it has the correct values";
162 }
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
163
8d18a30 @colomon 70 more tests for Bag.
colomon authored
164 {
165 my $b = bag { foo => 10000000000, bar => 17, baz => 42 };
166 my $s;
167 lives_ok { $s = $b.Str }, ".Str lives";
168 isa_ok $s, Str, "... and produces a string";
169 ok $s.chars < 1000, "... of reasonable length";
170 ok $s ~~ /foo/, "... which mentions foo";
171 ok $s ~~ /bar/, "... which mentions bar";
172 ok $s ~~ /baz/, "... which mentions baz";
173 }
174
175 {
176 my $b = bag { foo => 10000000000, bar => 17, baz => 42 };
177 my $s;
178 lives_ok { $s = $b.gist }, ".gist lives";
179 isa_ok $s, Str, "... and produces a string";
180 ok $s.chars < 1000, "... of reasonable length";
181 ok $s ~~ /foo/, "... which mentions foo";
182 ok $s ~~ /bar/, "... which mentions bar";
183 ok $s ~~ /baz/, "... which mentions baz";
184 }
185
186 # L<S02/Names and Variables/'C<%x> may be bound to'>
187
188 {
189 my %b := bag "a", "b", "c", "b";
190 isa_ok %b, Bag, 'A Bag bound to a %var is a Bag';
191 is showkv(%b), 'a:1 b:2 c:1', '...with the right elements';
192
193 is %b<b>, 2, 'Single-key subscript (existing element)';
194 is %b<santa>, 0, 'Single-key subscript (nonexistent element)';
ea8f32d [bag.t] Use .roll instead of .pick with :replace.
Kodi Arfer authored
195 }
196
197 # L<S32::Containers/Bag/roll>
198
199 {
8d18a30 @colomon 70 more tests for Bag.
colomon authored
200 my $b = Bag.new("a", "b", "b");
201
202 my $a = $b.roll;
203 ok $a eq "a" || $a eq "b", "We got one of the two choices";
204
205 my @a = $b.roll(2);
206 is +@a, 2, '.roll(2) returns the right number of items';
207 is @a.grep(* eq 'a').elems + @a.grep(* eq 'b').elems, 2, '.roll(2) returned "a"s and "b"s';
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
208
71f1532 @tadzik Tune and fudge bag, keybag, keyset and set tests for Rakudo and for G…
tadzik authored
209 @a = $b.roll: 100;
ea0bb44 Revamped keyhash.t.
Kodi Arfer authored
210 is +@a, 100, '.roll(100) returns 100 items';
ea8f32d [bag.t] Use .roll instead of .pick with :replace.
Kodi Arfer authored
211 ok 2 < @a.grep(* eq 'a') < 75, '.roll(100) (1)';
212 ok @a.grep(* eq 'a') + 2 < @a.grep(* eq 'b'), '.roll(100) (2)';
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
213 }
214
8d18a30 @colomon 70 more tests for Bag.
colomon authored
215 {
216 my $b = Bag.new({"a" => 100000000000, "b" => 1});
217
218 my $a = $b.roll;
219 ok $a eq "a" || $a eq "b", "We got one of the two choices (and this was pretty quick, we hope!)";
220
221 my @a = $b.roll: 100;
222 is +@a, 100, '.roll(100) returns 100 items';
223 ok @a.grep(* eq 'a') > 97, '.roll(100) (1)';
224 ok @a.grep(* eq 'b') < 3, '.roll(100) (2)';
225 }
226
227 # L<S32::Containers/Bag/pick>
228
229 {
230 my $b = Bag.new("a", "b", "b");
231
232 my $a = $b.pick;
233 ok $a eq "a" || $a eq "b", "We got one of the two choices";
234
235 my @a = $b.pick(2);
236 is +@a, 2, '.pick(2) returns the right number of items';
237 ok @a.grep(* eq 'a').elems <= 1, '.pick(2) returned at most one "a"';
238 is @a.grep(* eq 'b').elems, 2 - @a.grep(* eq 'a').elems, '.pick(2) and the rest are "b"';
239
240 @a = $b.pick: *;
241 is +@a, 3, '.pick(*) returns the right number of items';
242 is @a.grep(* eq 'a').elems, 1, '.pick(*) (1)';
243 is @a.grep(* eq 'b').elems, 2, '.pick(*) (2)';
244 }
245
246 {
247 my $b = Bag.new({"a" => 100000000000, "b" => 1});
248
249 my $a = $b.pick;
250 ok $a eq "a" || $a eq "b", "We got one of the two choices (and this was pretty quick, we hope!)";
251
252 my @a = $b.pick: 100;
253 is +@a, 100, '.pick(100) returns 100 items';
254 ok @a.grep(* eq 'a') > 98, '.pick(100) (1)';
255 ok @a.grep(* eq 'b') < 2, '.pick(100) (2)';
256 }
257
8e4955e Added tests for Sets and Bags.
Kodi Arfer authored
258 # vim: ft=perl6
Something went wrong with that request. Please try again.