6
6
use warnings FATAL => 'all';
13
use Apache::Const -compile => 'OK';
14
use APR::Const -compile => ':table';
16
use constant TABLE_SIZE => 20;
9
use Apache2::Const -compile => 'OK';
11
use TestAPRlib::table;
16
my $tests = TestAPRlib::table::num_of_tests();
24
17
plan $r, tests => $tests;
26
my $table = APR::Table::make($r->pool, TABLE_SIZE);
28
ok UNIVERSAL::isa($table, 'APR::Table');
30
# get on non-existing key
33
my $val = $table->get('foo');
34
ok t_cmp(undef, $val, '$val = $table->get("no_such_key")');
37
my @val = $table->get('foo');
38
ok t_cmp(0, +@val, '@val = $table->get("no_such_key")');
41
# set/add/get/copy normal values
43
$table->set(foo => 'bar');
46
my $val = $table->get('foo');
47
ok t_cmp('bar', $val, '$val = $table->get("foo")');
49
# add + get list context
50
$table->add(foo => 'tar');
51
$table->add(foo => 'kar');
52
my @val = $table->get('foo');
59
$table->set(too => 'boo');
60
my $table_copy = $table->copy($r->pool);
61
my $val_copy = $table->get('too');
62
ok t_cmp('boo', $val_copy, '$val = $table->get("too")');
63
my @val_copy = $table_copy->get('foo');
65
$val_copy[0] eq 'bar' &&
66
$val_copy[1] eq 'tar' &&
67
$val_copy[2] eq 'kar';
70
# make sure 0 comes through as 0 and not undef
72
$table->set(foo => 0);
73
my $zero = $table->get('foo');
74
ok t_cmp(0, $zero, 'table value 0 is not undef');
79
$table->set(foo => "bar");
81
ok t_cmp(undef, +$table->get('foo'), '$table->unset("foo")');
86
$table->set( merge => '1');
87
$table->merge(merge => 'a');
88
my $val = $table->get('merge');
89
ok t_cmp("1, a", $val, 'one val $table->merge(...)');
91
# if there is more than one value for the same key, merge does
92
# the job only for the first value
93
$table->add( merge => '2');
94
$table->merge(merge => 'b');
95
my @val = $table->get('merge');
96
ok t_cmp("1, a, b", $val[0], '$table->merge(...)');
97
ok t_cmp("2", $val[1], 'two values $table->merge(...)');
99
# if the key is not found, works like set/add
100
$table->merge(miss => 'a');
101
my $val_miss = $table->get('miss');
102
ok t_cmp("a", $val_miss, 'no value $table->merge(...)');
107
$table->set(foo => 0);
108
$table->set(bar => 1);
110
# t_cmp forces scalar context on get
111
ok t_cmp(undef, $table->get('foo'), '$table->clear');
112
ok t_cmp(undef, $table->get('bar'), '$table->clear');
117
for (1..TABLE_SIZE) {
118
$table->set(chr($_+97), $_);
123
$table->do("my_filter");
124
ok t_cmp(TABLE_SIZE, $filter_count);
126
# Filtering aborting in the middle
128
$table->do("my_filter_stop");
129
ok t_cmp(int(TABLE_SIZE)/2, $filter_count) ;
131
# Filtering with anon sub
134
my ($key,$value) = @_;
136
unless ($key eq chr($value+97)) {
137
die "arguments I recieved are bogus($key,$value)";
142
ok t_cmp(TABLE_SIZE, $filter_count, "table size");
145
$table->do("my_filter", "c", "b", "e");
146
ok t_cmp(3, $filter_count, "table size");
151
my $table = APR::Table::make($r->pool, TABLE_SIZE);
153
ok UNIVERSAL::isa($table, 'HASH');
155
ok UNIVERSAL::isa($table, 'HASH') && tied(%$table);
157
ok $table->{'foo'} = 'bar';
160
ok $table->{'foo'} eq 'bar';
162
ok delete $table->{'foo'} || 1;
164
ok not exists $table->{'foo'};
166
for (1..TABLE_SIZE) {
167
$table->{chr($_+97)} = $_;
171
foreach my $key (sort keys %$table) {
172
my_filter($key, $table->{$key});
174
ok $filter_count == TABLE_SIZE;
177
# overlap and compress routines
179
my $base = APR::Table::make($r->pool, TABLE_SIZE);
180
my $add = APR::Table::make($r->pool, TABLE_SIZE);
182
$base->set(foo => 'one');
183
$base->add(foo => 'two');
185
$add->set(foo => 'three');
186
$add->set(bar => 'beer');
188
my $overlay = $base->overlay($add, $r->pool);
190
my @foo = $overlay->get('foo');
191
my @bar = $overlay->get('bar');
194
ok t_cmp('beer', $bar[0]);
196
my $overlay2 = $overlay->copy($r->pool);
199
$overlay->compress(APR::OVERLAP_TABLES_MERGE);
200
# $add first, then $base
201
ok t_cmp($overlay->get('foo'),
203
"\$overlay->compress/merge");
204
ok t_cmp($overlay->get('bar'),
206
"\$overlay->compress/merge");
209
$overlay->compress(APR::OVERLAP_TABLES_SET);
210
# $add first, then $base
211
ok t_cmp($overlay2->get('foo'),
213
"\$overlay->compress/set");
214
ok t_cmp($overlay2->get('bar'),
216
"\$overlay->compress/set");
221
my $base = APR::Table::make($r->pool, TABLE_SIZE);
222
my $add = APR::Table::make($r->pool, TABLE_SIZE);
224
$base->set(bar => 'beer');
225
$base->set(foo => 'one');
226
$base->add(foo => 'two');
228
$add->set(foo => 'three');
230
$base->overlap($add, APR::OVERLAP_TABLES_SET);
232
my @foo = $base->get('foo');
233
my @bar = $base->get('bar');
235
ok t_cmp(1, +@foo, 'overlap/set');
236
ok t_cmp('three', $foo[0]);
237
ok t_cmp('beer', $bar[0]);
242
my $base = APR::Table::make($r->pool, TABLE_SIZE);
243
my $add = APR::Table::make($r->pool, TABLE_SIZE);
245
$base->set(foo => 'one');
246
$base->add(foo => 'two');
248
$add->set(foo => 'three');
249
$add->set(bar => 'beer');
251
$base->overlap($add, APR::OVERLAP_TABLES_MERGE);
253
my @foo = $base->get('foo');
254
my @bar = $base->get('bar');
256
ok t_cmp(1, +@foo, 'overlap/set');
257
ok t_cmp('one, two, three', $foo[0]);
258
ok t_cmp('beer', $bar[0]);
265
my($key, $value) = @_;
267
unless ($key eq chr($value+97)) {
268
die "arguments I received are bogus($key,$value)";
274
my($key, $value) = @_;
276
unless ($key eq chr($value+97)) {
277
die "arguments I received are bogus($key,$value)";
279
return $filter_count == int(TABLE_SIZE)/2 ? 0 : 1;
19
TestAPRlib::table::test();