~ubuntu-branches/ubuntu/vivid/drizzle/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/test_tools/randgen/lib/GenTest/Comparator.pm

  • Committer: Package Import Robot
  • Author(s): Tobias Frost
  • Date: 2013-08-22 20:18:31 UTC
  • mto: (20.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20130822201831-gn3ozsh7o7wmc5tk
Tags: upstream-7.2.3
ImportĀ upstreamĀ versionĀ 7.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
 
2
# Use is subject to license terms.
 
3
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
# USA
 
17
 
 
18
package GenTest::Comparator;
 
19
 
 
20
use strict;
 
21
 
 
22
use GenTest;
 
23
use GenTest::Constants;
 
24
use GenTest::Result;
 
25
 
 
26
#
 
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.
 
30
#
 
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
 
34
#
 
35
 
 
36
1;
 
37
 
 
38
sub compare {
 
39
        my @resultsets = @_;
 
40
 
 
41
        return STATUS_OK if $#resultsets == 0;
 
42
 
 
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;
 
45
 
 
46
        foreach my $i (0..($#resultsets-1)) {
 
47
 
 
48
                my $resultset1 = $resultsets[$i];
 
49
                my $resultset2 = $resultsets[$i+1];
 
50
                if ($resultset1->status() != $resultset2->status()) {
 
51
                        return STATUS_ERROR_MISMATCH;
 
52
                } elsif (
 
53
                        (not defined $resultset1->data()) &&                                    # Only for DML statements
 
54
                        ($resultset1->affectedRows() != $resultset2->affectedRows())
 
55
                ) {
 
56
                        return STATUS_LENGTH_MISMATCH;
 
57
                } else {
 
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;
 
64
                }
 
65
        }
 
66
        return STATUS_OK;
 
67
}
 
68
 
 
69
sub dumpDiff {
 
70
        my @results = @_;
 
71
        my @files;
 
72
        my $diff;
 
73
 
 
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;
 
81
                close FILE;
 
82
        }
 
83
        
 
84
        my $diff_cmd = "diff -u $files[0] $files[1]";
 
85
 
 
86
        open (DIFF, "$diff_cmd|");
 
87
        while (<DIFF>) {
 
88
                $diff .= $_;
 
89
        }
 
90
        close DIFF;
 
91
 
 
92
        foreach my $file (@files) {
 
93
                unlink($file);
 
94
        }
 
95
        
 
96
        return $diff;
 
97
        
 
98
}
 
99
 
 
100
1;