blib/lib/FP/Trie/t.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 219 CC 3 SCAR 14.6
CC
3
Cov
0%
t
12.0
random_key
2.0
BEGIN
2.0
CRAP 4.3
1 #
2 # Copyright (c) 2015-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Trie::t -- tests for FP::Trie
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 run by test suite
19  
20 =head1 NOTE
21  
22 This is alpha software! Read the status section in the package README
23 or on the L<website|http://functional-perl.org/>.
24  
25 =cut
26  
27 package FP::Trie::t;
28  
29 use strict;
30 use warnings;
31 use warnings FATAL => 'uninitialized';
32  
33 use FP::Trie;
34 use FP::List qw(string_to_list);
35 use FP::Ops "the_method";
36 use Chj::TEST;
37 use FP::Carp;
38  
39 my $t = empty_trie->set(string_to_list("Hello"), "World");
40  
41 TEST { [$t->perhaps_ref(string_to_list("Hell"))] }
42 [];
43  
44 TEST { [$t->perhaps_ref(string_to_list("Hello"))] }
45 ["World"];
46  
47 TEST_STDOUT {
48 if (my ($subt) = $t->perhaps_skip(string_to_list("Hell"))) {
49 print $subt->sublevels_length;
50 if (my ($v) = $subt->perhaps_ref(string_to_list("o"))) {
51 print $v;
52 }
53 }
54 }
55 "1World";
56  
57 TEST_STDOUT {
58 if (my ($subt) = $t->perhaps_skip(string_to_list("Hello"))) {
59 print $subt->sublevels_length;
60 if (my ($v) = $subt->perhaps_ref(string_to_list(""))) {
61 print $v;
62 }
63 }
64 }
65 "0World";
66  
67 TEST_STDOUT {
68 if (my ($subt) = $t->perhaps_skip(string_to_list(""))) {
69 print $subt->sublevels_length;
70 if (my ($v) = $subt->perhaps_ref(string_to_list("Hello"))) {
71 print $v;
72 }
73 }
74 }
75 "1World";
76  
77 my $t2 = $t->update(string_to_list("Hell"), sub { [@_, "yes"] });
78 my $t3 = $t2->update(string_to_list("Hello"), sub { [@_, "2"] });
79 my $t4 = $t3->set(string_to_list("Hell"), "no");
80  
81 TEST {
82 $t3->perhaps_ref(string_to_list "Hell")
83 }
84 ["yes"];
85  
86 TEST {
87 $t4->perhaps_ref(string_to_list "Hell")
88 }
89 "no";
90  
91 TEST {
92 $t4->perhaps_ref(string_to_list "Hello")
93 }
94 ["World", "2"];
95  
96 # keys, values
97  
98 TEST {
99 empty_trie->keys->array
100 }
101 [];
102  
103 TEST {
104 $t4->keys->map (the_method "string")->array
105 }
106 ["Hell", "Hello"];
107  
108 TEST {
109 $t4->values->array
110 }
111 ["no", ["World", "2"]];
112  
113 # delete
114  
115 my $t5 = $t4->delete(string_to_list("Hell"));
116  
117 TEST { [$t5->perhaps_ref(string_to_list("Hell"))] }
118 [];
119  
120 TEST { [$t5->perhaps_ref(string_to_list("Hello"))] }
121 [["World", "2"]];
122  
123 my $t6 = $t4->delete(string_to_list("Hello"));
124  
125 TEST { [$t6->perhaps_ref(string_to_list("Hell"))] }
126 ["no"];
127  
128 TEST { [$t6->perhaps_ref(string_to_list("Hello"))] }
129 [];
130  
131 TEST {
132 $t5->delete(string_to_list("Hell")) eq $t5
133 }
134 1;
135  
136 TEST_EXCEPTION {
137 $t5->xdelete(string_to_list("Hell"))
138 }
139 "key not found";
140  
141 # [do we want a delete_prefix method that deletes everything below
142 # that prefix?]
143  
144 # Randomized tests for delete (too many edge cases):
145 sub random_key {
146 my $len = int rand 3;
147 join("", map { chr(32 + int rand 64) } 1 .. $len)
148 }
149  
150 use FP::Repl::WithRepl 'withrepl'; ##
151  
152 TEST {
153 my $trie = empty_trie;
154 my $hash = {};
155  
156 my $set_both = sub {
157 @_ == 2 or fp_croak_arity 2;
158 my ($k, $i) = @_;
159 $trie = $trie->set(string_to_list($k), $i);
160 $$hash{$k} = $i;
161 };
162  
163 my $delete_both = sub {
164 @_ == 1 or fp_croak_arity 1;
165 my ($k) = @_;
166 $trie = $trie->delete(string_to_list $k);
167 delete $$hash{$k};
168 };
169  
170 withrepl {
171 my $oldk = random_key;
172 &$set_both($oldk, -1);
173  
174 for my $i (1 .. 1000) {
175 my $k = random_key;
176 &$set_both($k, $i);
177 $trie->ref(string_to_list $oldk) == $$hash{$oldk} or die "bug";
178 $oldk = $k;
179 DEL: {
180 my $k2 = random_key;
181 redo DEL if $k2 eq $oldk;
182 &$delete_both($k2);
183 }
184 }
185  
186 for my $k (keys %$hash) {
187 $trie->exists(string_to_list $k) or die "missing '$k' in trie";
188 }
189  
190 $trie->keys->for_each(
191 sub {
192 my ($kl) = @_;
193 my $k = $kl->string;
194 exists $$hash{$k} or die "key in trie that shouldn't be: '$k'";
195 delete $$hash{$k};
196 }
197 );
198  
199 keys %$hash and die "keys didn't give all keys it should have";
200 };
201 ''
202 }
203 '';
204  
205 # verify that forked structures work:
206  
207 my $t7 = $t4->set(string_to_list("Hare"), "Krishna")
208 ->set(string_to_list("He"), "Helium")->set(string_to_list("H"), "Heroic");
209  
210 sub t {
211 my ($key) = @_;
212 my ($t, $r, $maybe_last, $maybe_r) = $t7->skip(string_to_list($key));
213 [
214 $t->sublevels_length, $r, [$t->perhaps_value],
215 $maybe_last ? $maybe_last->perhaps_value : undef,
216 $maybe_r ? $maybe_r->string : undef
217 ]
218 }
219  
220 TEST { t "" }
221 [1, undef, [], undef, undef];
222  
223 TEST { t "H" }
224 [2, undef, ['Heroic'], 'Heroic', ""];
225  
226 TEST { t "Ha" }
227 [1, undef, [], 'Heroic', "a"];
228  
229 TEST { t "Hell" }
230 [1, undef, ["no"], 'no', ""];
231  
232 TEST { t "Hello" }
233 do {
234 my $v = ["World", 2];
235 [0, undef, [$v], $v, ""]
236 };
237  
238 1