1# $Id: 02hash.t,v 0.19 2006/10/08 03:37:29 ray Exp $
2# Before `make install' is performed this script should be runnable with
3# `make test'. After `make install' it should work as `perl test.pl'
4
5######################### We start with some black magic to print on failure.
6
7# Change 1..1 below to 1..last_test_to_print .
8# (It may become useful if the test is moved to ./t subdirectory.)
9
10my $has_data_dumper;
11BEGIN {
12  $| = 1;
13  my $tests = 11;
14  eval q[use Data::Dumper];
15  if (!$@) {
16    $has_data_dumper = 1;
17    $tests++;
18  }
19  print "1..$tests\n";
20}
21END {print "not ok 1\n" unless $loaded;}
22use Clone qw( clone );
23$loaded = 1;
24print "ok 1\n";
25
26######################### End of black magic.
27
28# Insert your test code below (better if it prints "ok 13"
29# (correspondingly "not ok 13") depending on the success of chunk 13
30# of the test code):
31
32package Test::Hash;
33
34use vars @ISA;
35
36@ISA = qw(Clone);
37
38sub new
39  {
40    my $class = shift;
41    my %self = @_;
42    bless \%self, $class;
43  }
44
45sub DESTROY
46  {
47    my $self = shift;
48    # warn "DESTROYING $self";
49  }
50
51package main;
52
53sub ok     { print "ok $test\n"; $test++ }
54sub not_ok { print "not ok $test\n"; $test++ }
55
56$^W = 0;
57$test = 2;
58
59my $a = Test::Hash->new(
60    level => 1,
61    href  => {
62      level => 2,
63      href  => {
64        level => 3,
65        href  => {
66          level => 4,
67        },
68      },
69    },
70  );
71
72$a->{a} = $a;
73
74my $b = $a->clone(0);
75my $c = $a->clone(3);
76
77$a->{level} == $b->{level} ? ok : not_ok;
78
79$b->{href} == $a->{href} ? ok : not_ok;
80$c->{href} != $a->{href} ? ok : not_ok;
81
82$b->{href}{href} == $a->{href}{href} ? ok : not_ok;
83$c->{href}{href} != $a->{href}{href} ? ok : not_ok;
84
85$c->{href}{href}{level} == 3 ? ok : not_ok;
86$c->{href}{href}{href}{level} == 4 ? ok : not_ok;
87
88$b->{href}{href}{href} == $a->{href}{href}{href} ? ok : not_ok;
89$c->{href}{href}{href} == $a->{href}{href}{href} ? ok : not_ok;
90
91my %circ = ();
92$circ{c} = \%circ;
93my $cref = clone(\%circ);
94if ($has_data_dumper) {
95  Dumper(\%circ) eq Dumper($cref) ? ok : not_ok;
96}
97
98# test for unicode support
99{
100  my $a = { chr(256) => 1 };
101  my $b = clone( $a );
102  ord( (keys(%$a))[0] ) == ord( (keys(%$b))[0] ) ? ok : not_ok;
103}
104