Refactored to _descend to fix the recursion bug
[dbsrgits/DBM-Deep.git] / t / 08_deephash.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5
6 plan skip_all => "You must set \$ENV{LONG_TESTS} to run the long tests"
7     unless $ENV{LONG_TESTS};
8
9 use t::common qw( new_dbm );
10
11 diag "This test can take up to several minutes to run. Please be patient.";
12
13 use_ok( 'DBM::Deep' );
14
15 my $dbm_factory = new_dbm( type => DBM::Deep->TYPE_HASH );
16 while ( my $dbm_maker = $dbm_factory->() ) {
17     my $max_levels = 1000;
18
19     {
20         my $db = $dbm_maker->();
21
22         ##
23         # basic deep hash
24         ##
25         $db->{company} = {};
26         $db->{company}->{name} = "My Co.";
27         $db->{company}->{employees} = {};
28         $db->{company}->{employees}->{"Henry Higgins"} = {};
29         $db->{company}->{employees}->{"Henry Higgins"}->{salary} = 90000;
30
31         is( $db->{company}->{name}, "My Co.", "Set and retrieved a second-level value" );
32         is( $db->{company}->{employees}->{"Henry Higgins"}->{salary}, 90000, "Set and retrieved a fourth-level value" );
33
34         ##
35         # super deep hash
36         ##
37         $db->{base_level} = {};
38         my $temp_db = $db->{base_level};
39
40         for my $k ( 0 .. $max_levels ) {
41             $temp_db->{"level$k"} = {};
42             $temp_db = $temp_db->{"level$k"};
43         }
44         $temp_db->{deepkey} = "deepvalue";
45     }
46
47     {
48         my $db = $dbm_maker->();
49
50         my $cur_level = -1;
51         my $temp_db = $db->{base_level};
52         for my $k ( 0 .. $max_levels ) {
53             $cur_level = $k;
54             $temp_db = $temp_db->{"level$k"};
55             eval { $temp_db->isa( 'DBM::Deep' ) } or last;
56         }
57         is( $cur_level, $max_levels, "We read all the way down to level $cur_level" );
58         is( $temp_db->{deepkey}, "deepvalue", "And we retrieved the value at the bottom of the ocean" );
59     }
60 }
61
62 done_testing;