File: basic.t

package info (click to toggle)
libhash-ordered-perl 0.014-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: perl: 710; makefile: 2
file content (404 lines) | stat: -rw-r--r-- 12,215 bytes parent folder | download
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use 5.006;
use strict;
use warnings;
use Test::More 0.96;
use Test::FailWarnings;
use Test::Deep '!blessed';
use Test::Fatal;
binmode( Test::More->builder->$_, ":utf8" )
  for qw/output failure_output todo_output/;

use Hash::Ordered;

use constant HO => "Hash::Ordered";

subtest "constructors" => sub {

    my $hash;

    $hash = new_ok( HO, [], "new()" );
    cmp_deeply( [ $hash->keys ],   [], "empty keys" );
    cmp_deeply( [ $hash->values ], [], "empty values" );

    $hash = new_ok( HO, [ a => 1, b => 2 ], "new( \@pairs )" );
    cmp_deeply( [ $hash->keys ],   [qw/a b/], "keys ordered as expected" );
    cmp_deeply( [ $hash->values ], [qw/1 2/], "values ordered as expected" );

    like(
        exception { HO->new("a") },
        qr/requires key-value pairs/,
        "unbalanced args throws exception"
    );

    for my $size ( 10, 1000 ) {

        $hash = new_ok( HO, [ 1 .. $size * 2 ] );
        $hash->delete(3); # trigger tombstone on large hash

        my $clone = $hash->clone;
        cmp_deeply( [ $clone->as_list ], [ $hash->as_list ], "clone() returns copy" );

        my $same = $hash->clone( $hash->keys );
        cmp_deeply( [ $same->as_list ], [ $hash->as_list ], "clone( keys )" );

        my $rev = $hash->clone( reverse $hash->keys );
        my $expected = [ map { $_ => $hash->get($_) } reverse $hash->keys ];
        cmp_deeply( [ $rev->as_list ], $expected, "clone( reverse keys )" );

        my $filter = $hash->clone('5');
        cmp_deeply( [ $filter->as_list ], [ 5 => 6 ], "clone( '5' )" );

        my $extra = $hash->clone( 'c', '1' );
        cmp_deeply( [ $extra->as_list ], [ c => undef, 1 => 2 ], "clone( 'c', '1' )" );

    }

    $hash = new_ok( HO, [ a => 1, b => 2, a => 2 ] );
    cmp_deeply( [ $hash->keys ],   [qw/a b/], "keys ordered as expected" );
    cmp_deeply( [ $hash->values ], [qw/2 2/], "values ordered as expected" );

};

subtest "overloading" => sub {

    my $hash = new_ok( HO, [], "new()" );
    ok( !$hash, "empty hash is boolean false" );
    $hash->set( a => 1 );
    ok( !!$hash, "non-empty hash is boolean true" );

    $hash = new_ok( HO, [], "new()" );

    like(
        "$hash",
        qr/\AHash::Ordered=ARRAY\(0x[0-9a-f]+\)\z/,
        "stringified gives typical Perl object string form"
    );

    like( 0+ $hash, qr/\A\d+\z/, "numified gives typical Perl object decimal address" );

};

subtest "element methods" => sub {

    for my $size ( 10, 1000 ) {

        my $hash = new_ok( HO, [ 1 .. $size * 2 ] );
        $hash->delete(3); # trigger tombstone on large hash

        my @keys   = $hash->keys;
        my @values = $hash->values;

        ok( !$hash->exists("a"), "exists is false for non-existing element" );
        is( $hash->get("a"), undef, "get on non-existing element returns undef" );
        is( $hash->set( "a", 1 ), 1, "set on non-existing element returns new value" );
        is( $hash->get("a"), 1, "get on existing element returns value" );
        ok( $hash->exists("a"), "exists is true for existing element" );

        is( $hash->set( "b", 2 ), 2, "set another key" );
        cmp_deeply( [ $hash->keys ],   [ @keys,   qw/a b/ ], "keys ordered as expected" );
        cmp_deeply( [ $hash->values ], [ @values, qw/1 2/ ], "values ordered as expected" );

        is( $hash->delete("a"), 1,     "delete existing key returns old value" );
        is( $hash->delete("z"), undef, "delete non-existing key returns undef" );

        is( $hash->set( "b", 9 ), 9, "set existing key" );
        is( $hash->set( "c", 3 ), 3, "set another non-existent key" );
        cmp_deeply( [ $hash->keys ],   [ @keys,   qw/b c/ ], "keys ordered as expected" );
        cmp_deeply( [ $hash->values ], [ @values, qw/9 3/ ], "values ordered as expected" );

        {
            my @warnings;
            local $SIG{__WARN__} = sub { push @warnings, @_; return };
            $hash->set( undef, 42 );
            is( $hash->get(undef), 42, "undef is an acceptable key" );
            for (@warnings) {
                like( $_, qr/uninitialized value/, "undef warning" );
            }
        }
    }
};

subtest "output and iteration" => sub {

    my $hash = new_ok( HO, [ 'a' .. 'z' ], "new('a'..'z')" );

    cmp_deeply( [ $hash->as_list ], [ 'a' .. 'z' ], "as_list" );

    cmp_deeply(
        [ $hash->as_list(qw/a c g zz/) ],
        [ a => 'b', c => 'd', g => 'h', zz => undef ],
        "as_list( keys )"
    );

    my @slice = $hash->values(qw/a c zz g/);
    cmp_deeply( \@slice, [ 'b', 'd', undef, 'h' ], "values( keys )" );

    my $iter = $hash->iterator;
    my @saw;
    while ( my ( $k, $v ) = $iter->() ) {
        push @saw, $k, $v;
    }
    cmp_deeply( [@saw], [ $hash->as_list ], "iterator walked hash in order" )
      or diag explain \@saw;

    $iter = $hash->iterator( reverse $hash->keys );
    @saw  = ();
    while ( my ( $k, $v ) = $iter->() ) {
        unshift @saw, $k, $v;
    }
    cmp_deeply(
        [@saw],
        [ $hash->as_list ],
        "iterator( reverse keys ) walked hash in expected order"
    ) or diag explain \@saw;
};

subtest "clear" => sub {

    my $hash = new_ok( HO, [ 'a' .. 'f' ], "new('a'..'f')" );

    cmp_deeply( [ $hash->as_list ], [ 'a' .. 'f' ], "as_list returns non-empty list" );
    is( $hash->clear, undef, "clearing hash returns undef" );
    cmp_deeply( [ $hash->as_list ], [], "as_list returns empty list" );
    cmp_deeply( $hash, HO->new, "cleared hash and new empty hash are equal" );

};

subtest "list methods" => sub {

    for my $size ( 10, 1000 ) {

        my @pairs = ( 1 .. $size * 2 );

        my $hash = new_ok( HO, \@pairs );
        $hash->delete(3); # trigger tombstone on large hash
        splice @pairs, 2, 2; # delete '3' and '4'
        my $hsize       = $hash->keys;
        my $vsize       = $hash->values;
        my $actual_size =()= keys %{ $hash->[0] };
        is( $hsize, $actual_size, "keys gives size in scalar context" );
        is( $vsize, $actual_size, "values gives size in scalar context" );

        is( $hash->push( b => 2, c => 3 ), $hsize + 2, "pushing 2 new pairs" );

        cmp_deeply(
            [ $hash->as_list ],
            [ @pairs, b => 2, c => 3 ],
            "hash keys/values correct after pushing new pairs"
        );

        cmp_deeply( [ $hash->pop ], [ c => 3 ], "pop returns last pair" );

        cmp_deeply(
            [ $hash->as_list ],
            [ @pairs, b => 2 ],
            "hash keys/values correct after pop"
        );

        is( $hash->unshift( y => 25, z => 26 ), $hsize + 3, "unshifting 2 pairs" );

        cmp_deeply(
            [ $hash->as_list ],
            [ y => 25, z => 26, @pairs, b => 2 ],
            "hash keys/values correct after unshifting new pairs"
        );

        cmp_deeply( [ $hash->shift ], [ y => 25 ], "shift returns first pair" );

        cmp_deeply(
            [ $hash->as_list ],
            [ z => 26, @pairs, b => 2 ],
            "hash keys/values correct after shifting"
        );

        ok( $hash->push( z => 42 ), "pushing existing key with new value" );

        cmp_deeply(
            [ $hash->as_list ],
            [ @pairs, b => 2, z => 42 ],
            "hash keys/values correct after pushing existing key"
        );

        ok( $hash->unshift( z => 26 ), "unshifting existing key with new value" );

        cmp_deeply(
            [ $hash->as_list ],
            [ z => 26, @pairs, b => 2 ],
            "hash keys/values correct after unshifting existing key"
        );

        ok( $hash->merge( z => 2, c => 3, d => 4 ), "merging key-value pairs" );

        cmp_deeply(
            [ $hash->as_list ],
            [ z => 2, @pairs, b => 2, c => 3, d => 4 ],
            "hash keys/values correct after merging pairs"
        );

        # scalar context pop/shift
        {
            $hash->push( zz => 'aa' );
            my $v = $hash->pop;
            is( $v, 'aa', "scalar pop returns value" );

            $hash->unshift( yy => 'bb' );
            $v = $hash->shift;
            is( $v, 'bb', "scalar shift returns value" );
        }

        # termination checks
        {
            my $clone = $hash->clone;
            1 while my ( $k, $v ) = $clone->pop;
            pass("pop terminates");
        }

        {
            my $clone = $hash->clone;
            1 while my ( $k, $v ) = $clone->shift;
            pass("shift terminates");
        }

    }

    # empty hash tests
    {
        my $hash = new_ok(HO);

        my ( $k, $v );

        ( $k, $v ) = $hash->pop;
        cmp_deeply( [ $k, $v ], [ undef, undef ], "pop empty in list context" );

        ( $k, $v ) = $hash->shift;
        cmp_deeply( [ $k, $v ], [ undef, undef ], "shift empty in list context" );

        $v = $hash->pop;
        is( $v, undef, "pop empty in scalar context" );

        $v = $hash->shift;
        is( $v, undef, "shift empty in scalar context" );
    }

};

subtest "modifiers" => sub {

    my $hash = new_ok( HO, [ 'a' => 0 ] );

    # preinc
    is( $hash->preinc('a'), 1, "preinc return" );

    # add

    is( $hash->add( 'a', 2 ),  3, "add +2 return" );
    is( $hash->add( 'a', -1 ), 2, "add -1 return" );

    # postinc

    is( $hash->postinc('a'), '2', "postinc return" );
    is( $hash->get('a'),     3,   "value was incremented" );

    # predec

    is( $hash->predec('a'), '2', "predec return" );

    # postdec

    is( $hash->postdec('a'), '2', "postdec return" );
    is( $hash->get('a'),     1,   "value was decremented" );

    # concat

    is( $hash->concat('a'), '1', "concat undef return" );
    is( $hash->concat( 'a', 'a' ), '1a', "concat 'a' return" );

    # or_equals
    is( $hash->or_equals( 'a', 42 ), '1a', "or_equals on existing key" );
    is( $hash->or_equals( 'b', 0 ),  '0',  "or_equals on new key" );
    is( $hash->or_equals( 'b', 42 ), '42', "or_equals on existing, false key" );

    # dor_equals
    is( $hash->dor_equals( 'a', 23 ), '1a', "dor_equals on existing key" );
    is( $hash->dor_equals( 'c', 0 ),  '0',  "dor_equals on new key" );
    is( $hash->dor_equals( 'c', 42 ), '0',  "dor_equals on existing, false key" );
};

subtest 'key ordering' => sub {

    my $hash = new_ok( HO, [] );

    cmp_deeply(
        [ $hash->keys ],
        [],
        'New hash has no keys'
    );

    my $rc = $hash->set( 'Rogers' => 'Captain America' );
    is( $rc, 'Captain America', 'Proper return value' );
    cmp_deeply(
        [ $hash->keys ],
        [ 'Rogers' ],
        'Added Rogers'
    );
    cmp_deeply(
        [ $hash->values ],
        [ 'Captain America' ],
        'Added Rogers'
    );

    # Try to replace an existing entry with or_equals.
    $rc = $hash->or_equals( 'Rogers' => 'Human Torch' );
    is( $rc, 'Captain America', 'Rogers is still Captain America' );

    # Try to replace an existing entry with dor_equals.
    $rc = $hash->dor_equals( 'Rogers' => 'The Impossible Man' );
    is( $rc, 'Captain America', 'Rogers is *still* Captain America' );

    $rc = $hash->or_equals( 'Banner' => 'Hulk' );
    is( $rc, 'Hulk', 'Proper return value' );
    cmp_deeply(
        [ $hash->keys ],
        [ 'Rogers', 'Banner' ],
        'Added Banner through or_equals'
    );
    cmp_deeply(
        [ $hash->values ],
        [ 'Captain America', 'Hulk' ],
        'Added Banner through or_equals'
    );

    $rc = $hash->dor_equals( 'Romanoff' => 'Black Widow' );
    is( $rc, 'Black Widow', 'Proper return value' );
    cmp_deeply(
        [ $hash->keys ],
        [ 'Rogers', 'Banner', 'Romanoff' ],
        'Added Romanoff through or_equals'
    );
    cmp_deeply(
        [ $hash->values ],
        [ 'Captain America', 'Hulk', 'Black Widow' ],
        'Added Romanoff through or_equals'
    );

    # In Captain America #180, Rogers becomes Nomad.
    # Replace him with a call to push().
    $rc = $hash->push( 'Rogers' => 'Nomad' );
    is( $rc, 3, 'There are still three keys' );
    cmp_deeply(
        [ $hash->keys ],
        [ 'Banner', 'Romanoff', 'Rogers' ],
        'Changing Rogers puts him at the end of the list'
    );
    cmp_deeply(
        [ $hash->values ],
        [ 'Hulk', 'Black Widow', 'Nomad' ],
        'Changing Rogers puts him at the end of the list'
    );


};


done_testing;
exit 0;