Debian Codegraph @

lang/perl/BerkeleyDB/t/examples3.t.T

raw
1 #!./perl -w
2
3 use strict ;
4
5 BEGIN {
6 unless(grep /blib/, @INC) {
7 chdir 't' if -d 't';
8 @INC = '../lib' if -d '../lib';
9 }
10 }
11
12 use lib 't';
13 use BerkeleyDB;
14 use Test::More;
15 use util ;
16
17 #BEGIN
18 #{
19 # if ($BerkeleyDB::db_version < 3) {
20 # print "1..0 # Skipping test, this needs Berkeley DB 3.x or better\n" ;
21 # exit 0 ;
22 # }
23 #}
24
25 plan(skip_all => "this needs Berkeley DB 3.x or better\n" )
26 if $BerkeleyDB::db_version < 3;
27
28
29
30 plan tests => 2;
31
32
33 my $Dfile = "dbhash.tmp";
34 my $Dfile2 = "dbhash2.tmp";
35 my $Dfile3 = "dbhash3.tmp";
36 unlink $Dfile;
37
38 umask(0) ;
39
40 my $redirect = "xyzt" ;
41
42
43 {
44 my $redirect = "xyzt" ;
45 {
46
47 my $redirectObj = new Redirect $redirect ;
48
49 ## BEGIN dupHash
50 use strict ;
51 use BerkeleyDB ;
52
53 my $filename = "fruit" ;
54 unlink $filename ;
55 my $db = new BerkeleyDB::Hash
56 -Filename => $filename,
57 -Flags => DB_CREATE,
58 -Property => DB_DUP
59 or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
60
61 # Add a few key/value pairs to the file
62 $db->db_put("red", "apple") ;
63 $db->db_put("orange", "orange") ;
64 $db->db_put("green", "banana") ;
65 $db->db_put("yellow", "banana") ;
66 $db->db_put("red", "tomato") ;
67 $db->db_put("green", "apple") ;
68
69 # print the contents of the file
70 my ($k, $v) = ("", "") ;
71 my $cursor = $db->db_cursor() ;
72 while ($cursor->c_get($k, $v, DB_NEXT) == 0)
73 { print "$k -> $v\n" }
74
75 undef $cursor ;
76 undef $db ;
77 ## END dupHash
78 unlink $filename ;
79 }
80
81 #print "[" . docat($redirect) . "]" ;
82 is(docat_del_sort($redirect), <<'EOM') ;
83 green -> apple
84 green -> banana
85 orange -> orange
86 red -> apple
87 red -> tomato
88 yellow -> banana
89 EOM
90
91 }
92
93 {
94 my $redirect = "xyzt" ;
95 {
96
97 my $redirectObj = new Redirect $redirect ;
98
99 ## BEGIN dupSortHash
100 use strict ;
101 use BerkeleyDB ;
102
103 my $filename = "fruit" ;
104 unlink $filename ;
105 my $db = new BerkeleyDB::Hash
106 -Filename => $filename,
107 -Flags => DB_CREATE,
108 -Property => DB_DUP | DB_DUPSORT
109 or die "Cannot open file $filename: $! $BerkeleyDB::Error\n" ;
110
111 # Add a few key/value pairs to the file
112 $db->db_put("red", "apple") ;
113 $db->db_put("orange", "orange") ;
114 $db->db_put("green", "banana") ;
115 $db->db_put("yellow", "banana") ;
116 $db->db_put("red", "tomato") ;
117 $db->db_put("green", "apple") ;
118
119 # print the contents of the file
120 my ($k, $v) = ("", "") ;
121 my $cursor = $db->db_cursor() ;
122 while ($cursor->c_get($k, $v, DB_NEXT) == 0)
123 { print "$k -> $v\n" }
124
125 undef $cursor ;
126 undef $db ;
127 ## END dupSortHash
128 unlink $filename ;
129 }
130
131 #print "[" . docat($redirect) . "]" ;
132 is(docat_del_sort($redirect), <<'EOM') ;
133 green -> apple
134 green -> banana
135 orange -> orange
136 red -> apple
137 red -> tomato
138 yellow -> banana
139 EOM
140
141 }
142
143