1
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
2
# Use is subject to license terms.
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18
package GenTest::Comparator;
23
use GenTest::Constants;
27
# In order to compare two data sets that may be sorted differently, we convert each row into a string,
28
# then sort the rows and convert them into one gigantic string. Such string representation is no longer
29
# dependent on sort order so can be compared safely.
31
# A O(N) algorithm that would avoid sorting is to use a hash representation of each data set. e.g. $hash{"A,B,C"} = 2
32
# if there were two rows containing "A,B,C". If two hashes contain the same keys with the same values, the two initial
33
# data sets were identical
41
return STATUS_OK if $#resultsets == 0;
43
return STATUS_WONT_HANDLE if $resultsets[0]->status() == STATUS_WONT_HANDLE || $resultsets[1]->status() == STATUS_WONT_HANDLE;
44
return STATUS_SKIP if $resultsets[0]->status() == STATUS_SKIP || $resultsets[1]->status() == STATUS_SKIP;
46
foreach my $i (0..($#resultsets-1)) {
48
my $resultset1 = $resultsets[$i];
49
my $resultset2 = $resultsets[$i+1];
50
if ($resultset1->status() != $resultset2->status()) {
51
return STATUS_ERROR_MISMATCH;
53
(not defined $resultset1->data()) && # Only for DML statements
54
($resultset1->affectedRows() != $resultset2->affectedRows())
56
return STATUS_LENGTH_MISMATCH;
58
my $data1 = $resultset1->data();
59
my $data2 = $resultset2->data();
60
return STATUS_LENGTH_MISMATCH if $#$data1 != $#$data2;
61
my $data1_sorted = join('<row>', sort map { join('<col>', map { defined $_ ? ($_ != 0 ? sprintf("%.4f", $_) : $_) : 'NULL' } @$_) } @$data1);
62
my $data2_sorted = join('<row>', sort map { join('<col>', map { defined $_ ? ($_ != 0 ? sprintf("%.4f", $_) : $_) : 'NULL' } @$_) } @$data2);
63
return STATUS_CONTENT_MISMATCH if $data1_sorted ne $data2_sorted;
74
foreach my $i (0..1) {
75
return undef if not defined $results[$i]->data();
76
my $data_sorted = join("\n", sort map { join("\t", map { defined $_ ? $_ : "NULL" } @$_) } @{$results[$i]->data()});
77
$data_sorted = $data_sorted."\n" if $data_sorted ne '';
78
$files[$i] = tmpdir()."/randgen".$$."-".time()."-server".$i.".dump";
79
open (FILE, ">".$files[$i]);
80
print FILE $data_sorted;
84
my $diff_cmd = "diff -u $files[0] $files[1]";
86
open (DIFF, "$diff_cmd|");
92
foreach my $file (@files) {