File: 101_basic.t

package info (click to toggle)
libtie-hash-indexed-perl 0.08%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 10; perl: 10
file content (96 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download | duplicates (2)
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
################################################################################
#
# Copyright (c) Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################

use Test;

BEGIN { plan tests => 35 };

use Tie::Hash::Indexed;
ok(1);

$scalar = $] < 5.008003 || $] == 5.009
        ? 'skip: no scalar context for tied hashes' : '';

$broken_untie = $] == 5.009003 ? 'skip: broken untie' : '';

tie %h, 'Tie::Hash::Indexed';
ok(1);

sub scalar_h { $scalar ? 0 : scalar %h }

$s = &scalar_h;
skip($scalar, $s, 0);

%h = (foo => 1, bar => 2, zoo => 3, baz => 4);
ok(join(',', keys %h), 'foo,bar,zoo,baz');
ok(exists $h{foo});
ok(exists $h{bar});
ok(!exists $h{xxx});
ok(scalar keys %h, 4);
$s = &scalar_h;
skip($scalar, $s =~ /^(\d+)\/\d+$/ && $1 > 0 && $1 <= scalar keys %h);

$h{xxx} = 5;
ok(join(',', keys %h), 'foo,bar,zoo,baz,xxx');
ok(exists $h{xxx});
ok(scalar keys %h, 5);
$s = &scalar_h;
skip($scalar, $s =~ /^(\d+)\/\d+$/ && $1 > 0 && $1 <= scalar keys %h);

$h{foo} = 6;
ok(join(',', keys %h), 'foo,bar,zoo,baz,xxx');
ok(exists $h{foo});
ok(scalar keys %h, 5);
$s = &scalar_h;
skip($scalar, $s =~ /^(\d+)\/\d+$/ && $1 > 0 && $1 <= scalar keys %h);

while (my($k,$v) = each %h) {
  $key .= $k;
  push @val, $v;
}
ok($key, 'foobarzoobazxxx');
ok(join('|', @val), '6|2|3|4|5');

$val = delete $h{bar};
ok($val, 2);
ok(join(',', keys %h), 'foo,zoo,baz,xxx');
ok(join(',', values %h), '6,3,4,5');
ok(scalar keys %h, 4);
ok(!exists $h{bar});

$val = delete $h{bar};
ok(not defined $val);

$val = delete $h{nokey};
ok(not defined $val);

%h = ();
ok(scalar keys %h, 0);
ok(!exists $h{zoo});
$s = &scalar_h;
skip($scalar, $s, 0);

%h = (foo => 1, bar => 2, zoo => 3, baz => 4);
ok(join(',', %h), "foo,1,bar,2,zoo,3,baz,4");
ok(scalar keys %h, 4);

for ($h{foo}) { $_ = 42 }
ok($h{foo}, 42);

untie %h;

# TODO: these tests fail with recent versions of blead
skip($broken_untie, scalar keys %h, 0);
skip($broken_untie, join(',', %h), '');

# test Tie::InsertOrderHash-like initializer

tie my %hash => 'Tie::Hash::Indexed',
    foo => 1, bar => 2, zoo => 3, baz => 4;

ok(join(',', keys %hash), 'foo,bar,zoo,baz');