File: 01_unicode_maputf8.t

package info (click to toggle)
libunicode-maputf8-perl 1.09-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 72 kB
  • ctags: 25
  • sloc: perl: 576; makefile: 33
file content (231 lines) | stat: -rwxr-xr-x 7,549 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl -w

use strict;
use lib ('./blib','../blib','../lib','./lib');
use Unicode::MapUTF8 qw(utf8_supported_charset to_utf8 from_utf8 utf8_charset_alias);

# General info for writing test modules: 
#
# When running as 'make test' the default
# working directory is the one _above_ the 
# 't/' directory. 

my @do_tests=(1..5);

my $test_subs = { 
       1 => { -code => \&test1, -desc => ' eight-bit                 ' },
       2 => { -code => \&test2, -desc => ' unicode                   ' },
       3 => { -code => \&test3, -desc => ' multi-byte                ' },
       4 => { -code => \&test4, -desc => ' jcode                     ' },
       5 => { -code => \&test5, -desc => ' charset aliases           ' },
};

my @charsets = utf8_supported_charset;

print $do_tests[0],'..',$do_tests[$#do_tests],"\n";
print STDERR "\n";
my $n_failures = 0;
foreach my $test (@do_tests) {
	my $sub  = $test_subs->{$test}->{-code};
	my $desc = $test_subs->{$test}->{-desc};
	my $failure = '';
	eval { $failure = &$sub; };
	if ($@) {
		$failure = $@;
	}
	if ($failure ne '') {
		chomp $failure;
		print "not ok $test\n";
		print STDERR "    $desc - $failure\n";
		$n_failures++;
	} else {
		print "ok $test\n";
		print STDERR "    $desc - ok\n";

	}
}
print "END\n";
exit;

########################################
# Eight bit conversions                #
########################################
sub test1 {
    my $charset       = 'ISO-8859-1';
    my $source_string = 'Hello World';
    my $utf8_string   = 'Hello World';
    my $result = test_general({ -charset => $charset,
                                 -source => $source_string,
                                   -utf8 => $utf8_string,
                             });
    return $result if ($result ne '');

    $source_string = '';
    $utf8_string    = '';
    $result = test_general({ -charset => $charset,
                              -source => $source_string,
                                -utf8 => $utf8_string,
                             });
    return $result if ($result ne '');

    return '';
}

########################################
# Unicode conversions                  #
########################################

sub test2 {
    my $charset       = 'UCS2';
    my $source_string = "\x00H\x00e\x00l\x00l\x00o\x00 \x00W\x00o\x00r\x00l\x00d";
    my $utf8_string   = 'Hello World';
    my $result = test_general({ -charset => $charset,
                                 -source => $source_string,
                                   -utf8 => $utf8_string,
                             });
    return $result if ($result ne '');

    $source_string = '';
    $utf8_string    = '';
    $result = test_general({ -charset => $charset,
                              -source => $source_string,
                                -utf8 => $utf8_string,
            });
    return $result if ($result ne '');

    return '';
}

########################################
# Multibyte conversions                #
########################################
sub test3 {
    return '';
}

########################################
# Japanese (Jcode) conversions         #
########################################
sub test4 {
    my $charset       = 'euc-jp';
    my $source_string = "Hello World";
    my $utf8_string   = 'Hello World';
    my $result = test_general({ -charset => $charset,
                                 -source => $source_string,
                                   -utf8 => $utf8_string,
                             });
    return $result if ($result ne '');

    $source_string = '';
    $utf8_string   = '';
    $result = test_general({ -charset => $charset,
                              -source => $source_string,
                                -utf8 => $utf8_string,
            });
    return $result if ($result ne '');
    
    return '';
}

########################################
# Charset aliases                      #
########################################
sub test5 {
    my $charset='ISO-8859-1';
    my $alias  ='latin-1_sort_of';
    eval {
        utf8_charset_alias({ $alias => $charset });
    };
	if ($@) { return "$@" }
    eval {
        my $aliased = utf8_charset_alias($alias);
        if ((! defined $aliased) || (lc($charset) ne lc($aliased))) {
            die("Alias crosscheck for '$alias' -> '$charset' returned a *different* charset of '$aliased'");
        }
    };
	if ($@) { return "Failed to alias character set '$charset' to '$alias': $@" }

    $charset       = $alias;
    my $source_string = 'Hello World';
    my $utf8_string   = 'Hello World';
    my $result = test_general({ -charset => $charset,
                                 -source => $source_string,
                                   -utf8 => $utf8_string,
                             });
    return $result if ($result ne '');

    $source_string = '';
    $utf8_string    = '';
    $result = test_general({ -charset => $charset,
                              -source => $source_string,
                                -utf8 => $utf8_string,
                             });
    return $result if ($result ne '');

    eval {
        utf8_charset_alias({ $alias => undef });
    };
	if ($@) { return "$@" }

    $source_string = 'Hello World';
    $utf8_string   = 'Hello World';
    eval { my $result = test_general({ -charset => $charset,
                                 -source => $source_string,
                                   -utf8 => $utf8_string,
                             });
    };
    if (! defined $@) {
        return "Failed to catch use of non-aliased charset";
    }

    return '';
}

########################################
# Generalized test framework           #
########################################

sub test_general {
    my ($parms) = shift;
    my $source_charset = $parms->{-charset};
    my $source_string  = $parms->{-source};
    my $utf8_string    = $parms->{-utf8};

	eval { 
        my $result_string = to_utf8({ -string => $source_string, 
                                     -charset => $source_charset });
        if ($utf8_string ne $result_string) {
           die ('(line ' . __LINE__ . ") conversion from '$source_charset' to UTF8 resulted in unexpected output. Expected '" . hexout($utf8_string) . "' but got '" . hexout($result_string) . "'\n");
        }
    };
	if ($@) { return "Failed to convert UTF8 text to $source_charset: $@" }
	eval { 
        my $result_string = from_utf8({ '-string' => $utf8_string, 
                                       '-charset' => $source_charset,
                                       }); 
        if ($source_string ne $result_string) {
           die ("conversion from UTF8 to '$source_charset' resulted in unexpected output. Expected '" . hexout($source_string) . "' but got '" . hexout($result_string) . "'\n");
        }
    };
	if ($@) { return "Failed to convert '$source_charset' text to UTF8: $@" }


	eval { 
           my $result_string = from_utf8({ -string => $source_string, 
                                          -charset => $source_charset,
                                          }); 
           if ($source_string ne to_utf8({ -string => $result_string, 
                                          -charset => $source_charset })) {
                die ("input and output strings differed");
           }     
    };
	if ($@) { return "Round trip conversion of '$source_charset' to UTF8 failed: $@" }

    return '';
}

sub hexout {
    my ($string) = @_;
    $string =~ s/([\x00-\xff])/unpack("H",$1).unpack("h",$1)/egos;
    return $string;
}