xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Tie/SubstrHash.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/usr/bin/perl -w
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate
4*0Sstevel@tonic-gateBEGIN {
5*0Sstevel@tonic-gate    chdir 't' if -d 't';
6*0Sstevel@tonic-gate    @INC = '.';
7*0Sstevel@tonic-gate    push @INC, '../lib';
8*0Sstevel@tonic-gate}
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gateprint "1..20\n";
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gateuse strict;
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gaterequire Tie::SubstrHash;
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gatemy %a;
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gatetie %a, 'Tie::SubstrHash', 3, 3, 3;
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate$a{abc} = 123;
21*0Sstevel@tonic-gate$a{bcd} = 234;
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gateprint "not " unless $a{abc} == 123;
24*0Sstevel@tonic-gateprint "ok 1\n";
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gateprint "not " unless keys %a == 2;
27*0Sstevel@tonic-gateprint "ok 2\n";
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gatedelete $a{abc};
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gateprint "not " unless $a{bcd} == 234;
32*0Sstevel@tonic-gateprint "ok 3\n";
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gateprint "not " unless (values %a)[0] == 234;
35*0Sstevel@tonic-gateprint "ok 4\n";
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gateeval { $a{abcd} = 123 };
38*0Sstevel@tonic-gateprint "not " unless $@ =~ /Key "abcd" is not 3 characters long/;
39*0Sstevel@tonic-gateprint "ok 5\n";
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gateeval { $a{abc} = 1234 };
42*0Sstevel@tonic-gateprint "not " unless $@ =~ /Value "1234" is not 3 characters long/;
43*0Sstevel@tonic-gateprint "ok 6\n";
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gateeval { $a = $a{abcd}; $a++  };
46*0Sstevel@tonic-gateprint "not " unless $@ =~ /Key "abcd" is not 3 characters long/;
47*0Sstevel@tonic-gateprint "ok 7\n";
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate@a{qw(abc cde)} = qw(123 345);
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gateprint "not " unless $a{cde} == 345;
52*0Sstevel@tonic-gateprint "ok 8\n";
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gateeval { $a{def} = 456 };
55*0Sstevel@tonic-gateprint "not " unless $@ =~ /Table is full \(3 elements\)/;
56*0Sstevel@tonic-gateprint "ok 9\n";
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate%a = ();
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gateprint "not " unless keys %a == 0;
61*0Sstevel@tonic-gateprint "ok 10\n";
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate# Tests 11..16 by Linc Madison.
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gatemy $hashsize = 119;                # arbitrary values from my data
66*0Sstevel@tonic-gatemy %test;
67*0Sstevel@tonic-gatetie %test, "Tie::SubstrHash", 13, 86, $hashsize;
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gatefor (my $i = 1; $i <= $hashsize; $i++) {
70*0Sstevel@tonic-gate        my $key1 = $i + 100_000;           # fix to uniform 6-digit numbers
71*0Sstevel@tonic-gate        my $key2 = "abcdefg$key1";
72*0Sstevel@tonic-gate        $test{$key2} = ("abcdefgh" x 10) . "$key1";
73*0Sstevel@tonic-gate}
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gatefor (my $i = 1; $i <= $hashsize; $i++) {
76*0Sstevel@tonic-gate        my $key1 = $i + 100_000;
77*0Sstevel@tonic-gate        my $key2 = "abcdefg$key1";
78*0Sstevel@tonic-gate	unless ($test{$key2}) {
79*0Sstevel@tonic-gate		print "not ";
80*0Sstevel@tonic-gate		last;
81*0Sstevel@tonic-gate	}
82*0Sstevel@tonic-gate}
83*0Sstevel@tonic-gateprint "ok 11\n";
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(1) == 2;
86*0Sstevel@tonic-gateprint "ok 12\n";
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(2) == 2;
89*0Sstevel@tonic-gateprint "ok 13\n";
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(5.5) == 7;
92*0Sstevel@tonic-gateprint "ok 14\n";
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(13) == 13;
95*0Sstevel@tonic-gateprint "ok 15\n";
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(13.000001) == 17;
98*0Sstevel@tonic-gateprint "ok 16\n";
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(114) == 127;
101*0Sstevel@tonic-gateprint "ok 17\n";
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(1000) == 1009;
104*0Sstevel@tonic-gateprint "ok 18\n";
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(1024) == 1031;
107*0Sstevel@tonic-gateprint "ok 19\n";
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gateprint "not " unless Tie::SubstrHash::findgteprime(10000) == 10007;
110*0Sstevel@tonic-gateprint "ok 20\n";
111*0Sstevel@tonic-gate
112