ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/TDB_FileX/t/02_tie.t
Revision: 1.2
Committed: Fri May 2 19:41:38 2025 UTC (17 months, 4 weeks ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: rel-0_97, HEAD
Changes since 1.1: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #! /usr/bin/perl -w
2
3 #
4 # test callbacks
5 #
6
7 use Test;
8 use Fcntl;
9
10 BEGIN { plan tests => 10 };
11
12 use TDB_FileX ":all";
13
14 my $tdb = TDB_FileX->open("test.tdb", tdb_flags => CLEAR_IF_FIRST, nocow => 1, mutex => 1)
15 or die "Couldn't open test.tdb: $!";
16
17 $tdb->store(arg1 => 'val1');
18 $tdb->store(arg2 => 'val2');
19 $tdb->store(arg3 => 'val3');
20
21 my %found;
22 ok($tdb->traverse(sub { $found{$_[0]} = $_[1] }), 3);
23 ok($found{arg1}, 'val1');
24 ok($found{arg2}, 'val2');
25 ok($found{arg3}, 'val3');
26
27 # test coderef return value (false means stop)
28 ok($tdb->traverse(sub { 0 }), 1);
29
30 # test undef coderef (should simply return keys count)
31 ok($tdb->traverse, 3);
32
33
34 # trigger constructor log callback
35 # (O_WRONLY is an error)
36 my ($level, $msg);
37 ok(!TDB_FileX->open("test.tdb", tdb_flags => DEFAULT, open_flags => O_WRONLY, mode => 0666, log_cb => sub { ($level, $msg) = @_ }));
38 ok($level, 1);
39 ok($msg);
40
41
42 # test separate logging functions
43 if (0) { # modern tdb does not fail this reopen call
44
45 my ($called1, $called2);
46 $called1 = $called2 = 0;
47
48 my $tdb1 = TDB_FileX->open("test1.tdb");
49 ok($tdb1);
50 $tdb1->set_logging_function(sub { $called1++ });
51
52 my $tdb2 = TDB_FileX->open("test2.tdb");
53 ok($tdb2);
54 $tdb2->set_logging_function(sub { $called2++ });
55
56 # reopen after unlink should trigger a logged error
57 ok(unlink(qw(test1.tdb test2.tdb)), 2);
58
59 ok(!$tdb1->reopen);
60 ok($called1, 1);
61 ok($called2, 0);
62
63 ok(!$tdb2->reopen);
64 ok($called2, 1);
65 }
66
67
68 # hash callback
69
70 $tdb = TDB_FileX->open("dummy.tdb", tdb_flags => INTERNAL, open_flags => O_RDWR, mode => 0666, log_cb => sub { ord substr $_[0], 0, 1 })
71 or die "Couldn't open test3.tdb: $!";
72
73 ok($tdb);
74 $tdb->store(ant => 'val1');
75 $tdb->store(apple => 'val2');
76 $tdb->store(banana => 'val3');
77
78 # FIXME: this shows the right thing (two records with the one hash
79 # value (97) and one record with another (98)), just have to find a
80 # way to machine test that..
81 #$tdb->dump_all;