Debian Codegraph @

test/utf16.t

raw
1 #!/usr/bin/perl
2
3 # Requires perl-Test-Simple installation.
4 use Test::Simple tests => 36;
5
6 $suffix = "";
7 if (-e "../dos2unix.exe") {
8 $suffix = ".exe";
9 }
10
11 $system = `uname -s`;
12 if ($system =~ m/MINGW/)
13 {
14 $unix=0;
15 } else {
16 $unix=1;
17 }
18
19 $DOS2UNIX = "../dos2unix" . $suffix;
20 $MAC2UNIX = "../mac2unix" . $suffix;
21 $UNIX2DOS = "../unix2dos" . $suffix;
22 $UNIX2MAC = "../unix2mac" . $suffix;
23
24 if (defined $ENV{'D2U_UTF8_LOCALE'}) {
25 $ENV{'LC_ALL'} = $ENV{'D2U_UTF8_LOCALE'};
26 } else {
27 print "error: Environment variable D2U_UTF8_LOCALE is not set.";
28 exit 1;
29 }
30
31 system("$DOS2UNIX -v -n utf16le.txt out_unix.txt; cmp out_unix.txt utf8unix.txt");
32 ok( $? == 0, 'DOS UTF-16LE to Unix UTF-8' );
33 system("$DOS2UNIX -v -n utf16be.txt out_unix.txt; cmp out_unix.txt utf8unix.txt");
34 ok( $? == 0, 'DOS UTF-16BE to Unix UTF-8' );
35 system("$UNIX2DOS -v -n utf16le.txt out_dos.txt; cmp out_dos.txt utf8dos.txt");
36 ok( $? == 0, 'DOS UTF-16LE to DOS UTF-8' );
37 system("$UNIX2DOS -v -n utf16be.txt out_dos.txt; cmp out_dos.txt utf8dos.txt");
38 ok( $? == 0, 'DOS UTF-16BE to DOS UTF-8' );
39
40 system("$DOS2UNIX -v -ul -n utf16len.txt out_unix.txt; cmp out_unix.txt utf8unix.txt");
41 ok( $? == 0, 'UTF-16LE without BOM to UTF-8' );
42 system("$DOS2UNIX -v -ub -n utf16ben.txt out_unix.txt; cmp out_unix.txt utf8unix.txt");
43 ok( $? == 0, 'UTF-16BE without BOM to UTF-8' );
44 system("$DOS2UNIX -v -ul -n utf16be.txt out_unix.txt; cmp out_unix.txt utf8unix.txt");
45 ok( $? == 0, 'BOM overrides -ul' );
46 system("$DOS2UNIX -v -ub -n utf16le.txt out_unix.txt; cmp out_unix.txt utf8unix.txt");
47 ok( $? == 0, 'BOM overrides -ub' );
48
49 system("$DOS2UNIX -v -b -n utf16le.txt out_unix.txt; cmp out_unix.txt utf8unxb.txt");
50 ok( $? == 0, 'DOS UTF-16LE to Unix UTF-8, keep BOM' );
51 system("$UNIX2DOS -v -r -n utf16le.txt out_dos.txt; cmp out_dos.txt utf8dosn.txt");
52 ok( $? == 0, 'DOS UTF-16LE to DOS UTF-8, remove BOM' );
53
54 system("$MAC2UNIX -v -n utf16le.txt out_unix.txt; cmp out_unix.txt utf8dosn.txt");
55 ok( $? == 0, 'mac2unix does not change utf16 DOS line breaks.' );
56 system("$UNIX2MAC -v -n utf16le.txt out_mac.txt; cmp out_mac.txt utf8dos.txt");
57 ok( $? == 0, 'unix2mac does not change utf16 DOS line breaks.' );
58
59 system("$UNIX2DOS -v -u -n utf16le.txt out_dos.txt; cmp out_dos.txt utf16le.txt");
60 ok( $? == 0, 'DOS UTF-16LE to DOS UTF-16' );
61 system("$UNIX2DOS -v -u -n utf16be.txt out_dos.txt; cmp out_dos.txt utf16be.txt");
62 ok( $? == 0, 'DOS UTF-16BE to DOS UTF-16' );
63 system("$DOS2UNIX -v -b -u -n utf16.txt out_unix.txt; cmp out_unix.txt utf16u.txt");
64 ok( $? == 0, 'DOS UTF-16LE to Unix UTF-16' );
65 system("$MAC2UNIX -v -b -u -n utf16m.txt out_unix.txt; cmp out_unix.txt utf16u.txt");
66 ok( $? == 0, 'Mac UTF-16LE to Unix UTF-16' );
67 system("$UNIX2DOS -v -b -u -n utf16u.txt out_dos.txt; cmp out_dos.txt utf16.txt");
68 ok( $? == 0, 'Unix UTF-16 to DOS UTF-16LE' );
69 system("$UNIX2MAC -v -b -u -n utf16u.txt out_mac.txt; cmp out_mac.txt utf16m.txt");
70 ok( $? == 0, 'Unix UTF-16 to Mac UTF-16LE' );
71
72 system("$DOS2UNIX -v -f -n utf16bin.txt out_unix.txt; cmp out_unix.txt unix_bin.txt");
73 ok( $? == 0, 'Dos2unix, force UTF-16 file with binary symbols' );
74 system("$UNIX2DOS -v -f -r -n utf16bin.txt out_dos.txt; cmp out_dos.txt dos_bin.txt");
75 ok( $? == 0, 'Unix2dos, force UTF-16 file with binary symbols' );
76
77 system("$DOS2UNIX -v -n invalhig.txt out_unix.txt");
78 $result = ($? >> 8);
79 ok( $result == 1, 'Dos2unix, invalid surrogate pair, missing low surrogate' );
80 system("$DOS2UNIX -v -n invallow.txt out_unix.txt");
81 $result = ($? >> 8);
82 ok( $result == 1, 'Dos2unix, invalid surrogate pair, missing high surrogate' );
83
84 system("$DOS2UNIX -q -n invalhig.txt out_unix.txt");
85 $result = ($? >> 8);
86 ok( $result == 0, 'Dos2unix, invalid surrogate pair, missing low surrogate, quiet' );
87 system("$DOS2UNIX -q -n invallow.txt out_unix.txt");
88 $result = ($? >> 8);
89 ok( $result == 0, 'Dos2unix, invalid surrogate pair, missing high surrogate, quiet' );
90
91 system("cat utf16le.txt | $DOS2UNIX -v > out_unix.txt; cmp out_unix.txt utf8unix.txt");
92 ok( $? == 0, 'UTF-16LE with BOM to UTF-8, stdin/out' );
93
94 system("cat utf16u.txt | $UNIX2DOS -v -u > out_dos.txt; cmp out_dos.txt utf16.txt");
95 ok( $? == 0, 'UTF-16LE with BOM to UTF-16LE, stdin/out' );
96
97 system("$DOS2UNIX -v -O utf16le.txt > out_unix.txt; cmp out_unix.txt utf8unix.txt");
98 ok( $? == 0, 'UTF-16LE with BOM to UTF-8, to stdout' );
99
100 system("$UNIX2DOS -v -O -u utf16u.txt > out_dos.txt; cmp out_dos.txt utf16.txt");
101 ok( $? == 0, 'UTF-16LE with BOM to UTF-16LE, to stdout' );
102
103 system("$UNIX2DOS -v -u -m -n unix.txt out_dos.txt; cmp out_dos.txt dos_bom.txt");
104 ok( $? == 0, 'Option -u must not disable -m on ASCII input');
105
106 system("$DOS2UNIX -ul -i utf16le.txt utf16len.txt utf8unxb.txt gb18030.txt > outinfo.txt");
107 system("$DOS2UNIX outinfo.txt; diff info_ul.txt outinfo.txt");
108 ok( $? == 0, 'Option -i, --info combined with -ul');
109
110 system("$DOS2UNIX -ub -i utf16be.txt utf16ben.txt utf8unxb.txt gb18030.txt > outinfo.txt");
111 system("$DOS2UNIX outinfo.txt; diff info_ub.txt outinfo.txt");
112 ok( $? == 0, 'Option -i, --info combined with -ub');
113
114 system("$DOS2UNIX -v -7 -n utf16le.txt out_unix.txt chardos.txt out_u7.txt; cmp out_unix.txt utf8unix.txt");
115 ok( $? == 0, '7bit disabled for utf16');
116
117 system("cmp out_u7.txt charu7.txt");
118 ok( $? == 0, '7bit enabled again, dos2unix');
119
120 system("$DOS2UNIX -i dos.txt unix.txt mac.txt mixed.txt utf16le.txt utf16be.txt utf16len.txt utf8unix.txt utf8dos.txt gb18030.txt > outinfo.txt");
121 system("$DOS2UNIX outinfo.txt; diff info_ucs.txt outinfo.txt");
122 ok( $? == 0, 'Option -i, --info');
123
124 $ENV{'LC_ALL'} = 'C';
125
126 system("$DOS2UNIX -v -n utf16le.txt out_unix.txt");
127 $result = ($? >> 8);
128 if ( $unix ) { $expected = 1; } else { $expected = 0 };
129 print "UNIX" . $unix . "\n";
130 print "EXP" . $expected . "\n";
131 ok( $result == $expected, 'DOS UTF-16LE to Unix in C locale, conversion error.' );
132 system("$DOS2UNIX -v -n utf16.txt out_unix.txt");
133 ok( $? == 0, 'DOS UTF-16LE to Unix in C locale, conversion OK' );