Imported Upstream version 3.2.0
[debian/amanda] / installcheck / Amanda_Config_FoldingHash.pl
1 # Copyright (c) 2009 Zmanda, Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 22;
20 use strict;
21 use warnings;
22
23 use lib "@amperldir@";
24 use Amanda::Config::FoldingHash;
25 use Data::Dumper;
26
27 my $h = Amanda::Config::FoldingHash->new();
28 ok(!(exists $h->{'key'}), "key doesn't exist in new hash");
29 is_deeply([sort(keys(%$h))], [], "hash starts out with no keys");
30
31 $h->{'key'} = 3;
32 ok((exists $h->{'key'}), "key exists after assignment");
33 is($h->{'key'}, 3, "can fetch value stored in simple key");
34 is($h->{'kEy'}, 3, "can fetch value stored in key via fold");
35
36 $h->{'key'} = 4;
37 is($h->{'key'}, 4, "updating the one key works");
38 is_deeply([sort(keys(%$h))], [sort(qw(key))], "hash now has the one key");
39
40 delete $h->{'key'};
41 ok(!(exists $h->{'key'}), "key doesn't exist after deletion");
42 is($h->{'key'}, undef, "got undef fetching deleted key");
43
44 $h->{'kEY'} = 20;
45 ok((exists $h->{'kEY'}), "unfolded key exists after assignment");
46 ok((exists $h->{'key'}), "folded key exists after assignment");
47 is($h->{'key'}, 20, "can fetch value stored in folded key");
48 is_deeply([sort(keys(%$h))], [sort(qw(key))], "key is folded in list");
49
50 $h->{'key'} = 92;
51 is($h->{'key'}, 92, "updated folded key");
52
53 $h->{'Key'} = 37;
54 is($h->{'key'}, 37, "updated key via fold");
55
56 $h->{'_some-OTHER_kEy_'} = undef;
57 ok((exists $h->{'_some-OTHER_kEy_'}), "longer key exists after assigning undef");
58 ok((exists $h->{'-some-other-key-'}), "longer folded key exists too");
59 ok((exists $h->{'-SOME_other_key_'}), "longer key exists via fold");
60 is_deeply([sort(keys(%$h))], [sort(qw(key -some-other-key-))], "keys list as folded");
61
62 delete $h->{'KeY'};
63 ok(!(exists $h->{'key'}), "key doesn't exist after deletion via fold");
64 is($h->{'key'}, undef, "got undef fetching folded deleted key");
65
66 for my $k (qw(_ __ a-B Cf_ __g-h i__j-k L)) {
67     $h->{$k} = $k;
68 }
69
70 is_deeply([sort(keys(%$h))], [sort(qw(-some-other-key- - -- a-b cf- --g-h i--j-k l))],
71    "various keys are listed as folded");
72