OSDN Git Service

Support JIS X 0213.
[nkf/nkf.git] / test.pl
1 #!/usr/bin/perl
2 #
3 # nkf test program for nkf 2.0
4 #    Shinji KONO <kono@ie.u-ryukyu.ac.jp>
5 # Sun Aug 18 12:25:40 JST 1996
6 # Sun Nov  8 00:16:06 JST 1998
7 # Sun Sep  8 14:04:02 JST 2002
8 #
9 # This is useful when you add new patch on nkf.
10 # Since this test is too strict, faileurs may not mean
11 # wrong conversion. 
12 #
13 # nkf 2.0 utf8
14 # nkf 1.5 differs on MIME decoding
15 # nkf 1.4 passes Basic Conversion tests
16 # nkf PDS version passes Basic Conversion tests  using "nkf -iB -oB "
17 #
18
19 if ($ARGV[0]) {
20     $nkf = $ARGV[0];
21 } else {
22     $nkf = 'MSWin32' eq $^O ? ".\\nkf" : "./nkf";
23 }
24 # $nkf = "doscmd nkf.exe";
25
26 # If you want to see the testing process, set next flag.
27
28 $error_count = 0;
29 $diff=1;
30
31 # &library_test0();
32 sub test {
33     &command_test(@_);
34 }
35
36 sub command_test {
37     local ($nkf,$in,@ans) = @_;
38     local ($result);
39
40     $result = '';
41     open(OUT,"> nkf.in");
42     binmode OUT;
43     print OUT $in;
44     close(OUT);
45     system("$nkf <nkf.in >nkf.out");   # easy
46     open(IN,"< nkf.out");
47     binmode IN;
48     while(<IN>) {
49         $result .= $_;
50     }
51
52     $result =~ s/ //g if $nkf =~ /-\w+m[NS]/o;
53     foreach $ans (@ans) {
54         $ans =~ s/ //g if $nkf =~ /-\w+m[NS]/o;
55         if ($result eq $ans) {
56             print "Ok\n";
57             return $result;
58         }
59     }
60     $ans = $ans[0];
61     print "Fail\n";
62     system "mv nkf.in nkf.in.$error_count.bad";
63     system "mv nkf.out nkf.out.$error_count.bad";
64     open(OUT,"> nkf.expect.$error_count.bad");
65     binmode OUT;
66     print OUT $ans;
67     close(OUT);
68     $error_count++;
69     if ($diff) {
70         open(R,"|od -c >tmp.result.bad"); binmode R; print R $result; close(R);
71         open(R,"|od -c >tmp.expect.bad"); binmode R; print R $ans; close(R);
72         system "diff -c tmp.result.bad tmp.expect.bad";
73     }
74     return $result;
75 }
76
77 sub command_tests {
78     my @tests = @_;
79     my ($in, $out, $ans);
80
81     for (my $i = 0; $i <= $#tests; $i += 3){
82         my $nkf = $tests[$i];
83         $in = $tests[$i+1];
84         $ans = $tests[$i+2];
85         $out = '';
86         open(OUT, "> nkf.in");
87         binmode OUT;
88         print OUT $in;
89         close(OUT);
90         system("$nkf <nkf.in >nkf.out");   # easy
91         open(IN,"< nkf.out");
92         binmode IN;
93         while (<IN>) {
94             $out .= $_;
95         }
96         close(IN);
97         $out =~ s/ //g if $nkf =~ /-\w+m[NS]/o;
98         $ans =~ s/ //g if $nkf =~ /-\w+m[NS]/o;
99         if ($out ne $ans) {
100             last;
101         }
102     }
103     if ($out eq $ans) {
104         print "Ok\n";
105         return;
106     }
107     print "Fail\n";
108     system "mv nkf.in nkf.in.$error_count.bad";
109     system "mv nkf.out nkf.out.$error_count.bad";
110     open(OUT,"> nkf.expect.$error_count.bad");
111     binmode OUT;
112     print OUT $ans;
113     close(OUT);
114     $error_count++;
115     if ($diff) {
116         open(R,"|od -c >tmp.result.bad"); binmode R; print R $out; close(R);
117         open(R,"|od -c >tmp.expect.bad"); binmode R; print R $ans; close(R);
118         system "diff -c tmp.result.bad tmp.expect.bad";
119     }
120     return;
121 }
122
123 do "nkf_test.pl";
124 unlink "nkf.in";
125 unlink "nkf.out";
126
127 if ($error_count > 1) {
128     printf("%d errors were found.\n", $error_count);
129 } elsif ($error_count == 1) {
130     printf("1 error was found.\n");
131 } else {
132     printf("All tests are succeeded.\n");
133 }