Fixed SIG_INTERNAL so that it works + more tests
[dbsrgits/DBM-Deep.git] / t / 16_circular.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
8db25060 5use Test::More tests => 31;
2a81bf9e 6use File::Temp qw( tempfile tempdir );
ffed8b01 7
8use_ok( 'DBM::Deep' );
9
2a81bf9e 10my $dir = tempdir( CLEANUP => 1 );
11my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
12my $db = DBM::Deep->new( $filename );
ffed8b01 13
14##
15# put/get simple keys
16##
17$db->{key1} = "value1";
18$db->{key2} = "value2";
19
1df992b3 20my @keys_1 = sort keys %$db;
21
22$db->{key3} = $db->{key1};
23
24my @keys_2 = sort keys %$db;
25is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" );
26is_deeply(
27 [ @keys_1, 'key3' ],
28 [ @keys_2 ],
29 "Keys still match after circular reference is added",
30);
31
8db25060 32$db->{key4} = { 'foo' => 'bar' };
75be6413 33$db->{key5} = $db->{key4};
8db25060 34$db->{key6} = $db->{key5};
1df992b3 35
36my @keys_3 = sort keys %$db;
1df992b3 37
8db25060 38is( @keys_3 + 0, @keys_2 + 3, "Correct number of keys" );
39is_deeply(
40 [ @keys_2, 'key4', 'key5', 'key6', ],
41 [ @keys_3 ],
42 "Keys still match after circular reference is added (@keys_3)",
43);
44
45##
46# Insert circular reference
47##
48$db->{circle} = $db;
49
50my @keys_4 = sort keys %$db;
51
52is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" );
53is_deeply(
54 [ 'circle', @keys_3 ],
55 [ @keys_4 ],
56 "Keys still match after circular reference is added",
57);
1df992b3 58
ffed8b01 59##
60# Make sure keys exist in both places
61##
62is( $db->{key1}, 'value1', "The value is there directly" );
63is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" );
64is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" );
65is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" );
66
67##
68# Make sure changes are reflected in both places
69##
70$db->{key1} = "another value";
71
72is( $db->{key1}, 'another value', "The value is there directly" );
73is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" );
74is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" );
75is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" );
76
77$db->{circle}{circle}{circle}{circle}{key1} = "circles";
78
79is( $db->{key1}, 'circles', "The value is there directly" );
80is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" );
81is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" );
82is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" );
8db25060 83
84is( $db->{key4}{foo}, 'bar' );
85is( $db->{key5}{foo}, 'bar' );
86is( $db->{key6}{foo}, 'bar' );
87
88$db->{key4}{foo2} = 'bar2';
89is( $db->{key4}{foo2}, 'bar2' );
90is( $db->{key5}{foo2}, 'bar2' );
91is( $db->{key6}{foo2}, 'bar2' );
92
93$db->{key4}{foo3} = 'bar3';
94is( $db->{key4}{foo3}, 'bar3' );
95is( $db->{key5}{foo3}, 'bar3' );
96is( $db->{key6}{foo3}, 'bar3' );
97
98$db->{key4}{foo4} = 'bar4';
99is( $db->{key4}{foo4}, 'bar4' );
100is( $db->{key5}{foo4}, 'bar4' );
101is( $db->{key6}{foo4}, 'bar4' );
102