9c07e332bd01de6c2f82946778f6e8b874d3e19b
[cql-java-moved-to-github.git] / test / regression / runcanon
1 #!/usr/bin/perl -w
2
3 # $Id: runcanon,v 1.1 2007-06-29 13:05:12 mike Exp $
4 #
5 # Tests that all sample queries can be rendered into idempotent
6 # canoncial form.
7
8 use IO::File;
9 use strict;
10
11 $ENV{CLASSPATH} .= ":../../lib/cql-java.jar";
12
13 my($ntests, $ncorrect) = (0, 0);
14
15 while (<sections/*>) {
16     my $sdir = $_;
17     s@sections/@@;
18     next if /^CVS$/ || /^10$/;
19     print "testing section $_ - ", read_file("$sdir/name"), "\n";
20
21     while (<$sdir/*.cql>) {
22         my $qfile = $_;
23         s@sections/([0-9]+/.*)\.cql@$1@;
24         my $query = read_file($qfile);
25         my $canonical = `CQLParser -c '$query'`;
26         chomp($canonical);
27         my $maybe = `CQLParser -c '$canonical'`;
28         chomp($maybe);
29         print "$query // $canonical ";
30         $ntests++;
31         if ($maybe eq $canonical) {
32             $ncorrect++;
33             print " OK\n";
34         } else {
35             print "### $maybe\n";
36         }
37     }
38 }
39
40 print sprintf("%d of %d passed: %d%%\n",
41               $ncorrect, $ntests, (100 * $ncorrect) / $ntests);
42
43 sub read_file {
44     my($name) = @_;
45
46     $name = "<$name" if $name !~ /\|$/;
47     my $fh = new IO::File("$name")
48         or die "can't read '$name': $!";
49     my $contents = join('', <$fh>);
50     $fh->close();
51     return $contents;
52 }