summaryrefslogtreecommitdiff
path: root/t/compare.t
blob: 94a6732d39fa7db943b5796f73ac4a9cad0c336f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- Mode: Perl -*-

BEGIN { unshift @INC, "lib", "../lib" }
use strict;
use warnings;
# use diagnostics;

use Data::Compare;

local $^W = 1;
print "1..45\n";

my $t = 1;

my $s0 = undef;
my $s1 = 0;
my $s2 = 10;

# 1 .. 4
&comp($s0, $s0, 1);
&comp($s1, $s1, 1);
&comp($s2, $s2, 1);
&comp($s0, $s1, 0);

my $s3 = \$s2;
my $s4 = \$s1;
my $s5 = "$s4";
my $s6 = 0;
my $s7 = \$s6;

# 5 .. 8
&comp($s3, $s3, 1);
&comp($s3, $s4, 0);
&comp($s4, $s5, 0);
&comp($s4, $s7, 1);

my $a1 = [];
my $a2 = [ 0 ];
my $a3 = [ '' ];
my $a4 = [ 1, 2, 3 ];
my $a5 = [ 1, 2, 4 ];
my $a6 = [ 1, 2, 3, 5 ];

# 9 .. 13
&comp($a1, $a1, 1);
&comp($a1, $a2, 0);
&comp($a2, $a3, 0);
&comp($a4, $a5, 0);
&comp($a4, $a6, 0);

my $h1 = {};
my $h2 = { 'foo' => 'bar' };
my $h3 = { 'foo' => 'bar' };
my $h4 = { 'foo' => 'bar', 'bar' => 'foo' };

# 14 .. 19
&comp($h1, $s0, 0);
&comp($h1, $h1, 1);
&comp($h2, $h2, 1);
&comp($h2, $h3, 1);
&comp($h1, $h2, 0);
&comp($h3, $h4, 0);

my $o1 = bless [ 'FOO', 'BAR' ], 'foo';
my $o2 = bless [ 'FOO', 'BAR' ], 'foo';
my $o3 = bless [ 'FOO', 'BAR' ], 'fool';
my $o4 = bless [ 'FOO', 'BAR', 'BAZ' ], 'foo';

# 20 .. 22
&comp($o1, $o2, 1);
&comp($o1, $o3, 0);
&comp($o1, $o4, 0);

my $o5 = bless { 'FOO' => 'BAR' }, 'foo';
my $o6 = bless { 'FOO' => 'BAR' }, 'foo';
my $o7 = bless { 'FOO' => 'BAR' }, 'fool';
my $o8 = bless { 'FOO' => 'BAR', 'foo' => 'BAZ' }, 'foo';

# 23 .. 25
&comp($o5, $o6, 1);
&comp($o5, $o7, 0);
&comp($o5, $o8, 0);

my $s8  = 0;
my $o9  = bless \$s0, 'foo';
my $o10 = bless \$s8, 'foo';
my $o11 = bless \$s1, 'foo';

# 26 .. 27
&comp($o9,  $o10, 0);
&comp($o10, $o11, 1);

my $g1 = \*STDIN;
my $g2 = \*STDOUT;

# 28 .. 29
&comp($g1, $g1, 1);
&comp($g1, $g2, 0);

my $o12 = bless $g1, 'foo';
my $o13 = bless $g2, 'foo';

# 30 .. 31
&comp($o12, $o12, 1);
&comp($o12, $o13, 0);

my $o16 = bless sub { print "foo\n" }, 'foo';
my $o17 = bless sub { print "foo\n" }, 'foo';

# 32
&comp($o16, $o17, 0); # :(

my $v1 = { 'foo' => [ 1, { 'bar' => 'baz' }, 3 ] };
my $v2 = { 'bar' => 'baz' };
my $v3 = [ 1, $v2, 3 ];
my $v4 = { 'foo' => $v3 };

# 33
&comp($v1, $v4, 1);

# 34 .. 37
&comp(\\1, \\1, 1);
&comp(\\1, \\2, 0);
&comp(\\1, 1, 0);
&comp(\\1, \1, 0);

# 38 .. 40
&comp(qr/abc/i, qr/abc/i, 1, "Identical regexen");
&comp(qr/abc/i, qr/[aA][bB][cC]/, 0, "Non-identical regexen");
&comp(qr/abc/i, '(?i-xsm:abc)', 0, "Regex and scalar which stringify the same");

# 41 .. 43
# scalar cross
$a = [];
my($x, $y);
$x=\$y; 
$y=\$x; 
$a->[0]=\$a->[1]; 
$a->[1]=\$a->[0]; 
&comp([$x, $y], $a, 1, "two parallel circular structures compare the same");

# these two are probably superfluous, as they test referential equality
# rather than any of the stuff we added to do with circles and recursion
&comp([$x, $y], [$y, $x], 1, "looking at a circle from two different starting points compares the same");
&comp([$x, $y], [$x, $y], 1, "a circular structure compares to itself");

$a = [];
$b = [];
$a->[0] = { foo => { bar => $a } };
$b->[0] = { foo => { bar => $b } };
$a->[1] = $b->[1] = 5;
comp($a, $b, 1, "structure of a circle plus same data compares the same");

$a->[1] = 6;
comp($a, $b, 0, "structure of a circle plus different data compares different");
sub comp {
  my $a = shift;
  my $b = shift;
  my $expect = shift;
  my $comment = shift;

  print Compare ($a, $b) == $expect ? "" : "not ", "ok ", $t++,
    ($comment) ? " $comment\n" : "\n";
}