Debian Codegraph @

testsuite/tests/iconv/iconv1/test.adb

raw
1 with Ada.Exceptions;
2 with Ada.Strings.Unbounded;
3 with Ada.Text_IO;
4 with GNAT.Source_Info;
5 with GNATCOLL.Iconv;
6 with Test_Assert;
7
8 function Test return Integer is
9
10 package A renames Test_Assert;
11 package Iconv renames GNATCOLL.Iconv;
12 package IO renames Ada.Text_IO;
13 package SI renames GNAT.Source_Info;
14
15
16 function Bytes_Image (Input : String) return String;
17 function Bytes_Image (Input : String) return String
18 is
19 Result : Ada.Strings.Unbounded.Unbounded_String;
20 begin
21 for C in Input'Range loop
22 Ada.Strings.Unbounded.Append (Result, Character'Pos (Input (C))'Img);
23 end loop;
24 return Ada.Strings.Unbounded.To_String (Result);
25 end Bytes_Image;
26
27 procedure Assert_Iconv
28 (Input : String;
29 Expected : String;
30 From_Code : String;
31 To_Code : String;
32 Transliteration : Boolean := False;
33 Ignore : Boolean := False;
34 Msg : String := "";
35 Location : String := SI.Source_Location)
36 is
37 -- Transform Input using Iconv and expect Expected as a result
38 -- The other parameters are for GNATCOLL.Iconv.Iconv
39 begin
40 declare
41 Result : constant String := Iconv.Iconv
42 (Input => Input,
43 To_Code => To_Code,
44 From_Code => From_Code,
45 Transliteration => Transliteration,
46 Ignore => Ignore);
47 Success : constant Boolean := Result = Expected;
48 begin
49 A.Assert (Success, Msg, Location);
50 if not Success then
51 IO.Put_Line ("iconv(" & Bytes_Image (Input) &
52 ", to_code => " & To_Code &
53 ", from_code => " & From_code &
54 ", transliteration => " & Transliteration'Img &
55 ", ignore => " & Ignore'Img);
56 IO.Put_Line ("- expect: " & Bytes_Image (Expected));
57
58 IO.Put_Line ("- got: " & Bytes_Image (Result));
59 end if;
60 end;
61 exception
62 when E : others =>
63 A.Assert (False, Msg, Location);
64 IO.Put_Line ("got exception: " & ASCII.LF &
65 Ada.Exceptions.Exception_Information (E));
66 end Assert_Iconv;
67
68
69 procedure Assert_Invalid_Sequence
70 (Input : String;
71 From_Code : String;
72 To_Code : String;
73 Transliteration : Boolean := False;
74 Ignore : Boolean := False;
75 Msg : String := "";
76 Location : String := SI.Source_Location)
77 is
78 -- Assert that input sequence will be considered by Iconv as an invalid
79 -- sequence.
80 begin
81 declare
82 Result : constant String := Iconv.Iconv
83 (Input => Input,
84 To_Code => To_Code,
85 From_Code => From_Code,
86 Transliteration => Transliteration,
87 Ignore => Ignore);
88 begin
89 A.Assert (False, Msg, Location);
90 IO.Put_Line ("iconv(" & Input &
91 ", to_code => " & To_Code &
92 ", from_code => " & From_code &
93 ", transliteration => " & Transliteration'Img &
94 ", ignore => " & Ignore'Img);
95 IO.Put_Line ("- expect: Invalid_Sequence_Error ");
96 IO.Put ("- got: ");
97 for C in Result'Range loop
98 IO.Put (Character'Pos (Result (C))'Img);
99 end loop;
100 IO.New_Line;
101 end;
102 exception
103 when Iconv.Invalid_Sequence_Error =>
104 A.Assert (True, Msg, Location);
105 when E : others =>
106 A.Assert (False, Msg, Location);
107 IO.Put_Line ("got exception: " & ASCII.LF &
108 Ada.Exceptions.Exception_Information (E));
109 end Assert_Invalid_Sequence;
110
111
112 Eacute : constant Character := Character'Val (16#E9#);
113 -- in Iso_8859-1
114
115 R_Koi8 : constant Character := Character'Val (16#D1#);
116 EAcute_Koi8_Trans : constant String := -- a 'e
117 Character'Val (39) & Character'Val (101);
118 -- In KOI8-R
119
120 Eacute_UTF8 : constant String :=
121 Character'Val (195) & Character'Val (169);
122 R_UTF8 : constant String :=
123 Character'Val (209) & Character'Val (143);
124
125 Ellipsis_UTF8 : constant String :=
126 Character'Val (16#E2#) & Character'Val (16#80#) & Character'Val (16#A6#);
127
128 Alveolar_Click : constant String :=
129 Character'Val (16#C7#) & Character'Val (16#82#);
130
131 begin
132 Assert_Iconv ("Simple test", "Simple test", "", "", Msg => "simple test");
133 Assert_Iconv ("Simple " & Eacute, "Simple " & Eacute_UTF8, Iconv.Iso_8859_1, Iconv.UTF8);
134 Assert_Iconv ("Simple " & R_Koi8, "Simple " & R_UTF8, Iconv.KOI8_R, Iconv.UTF8);
135
136 Assert_Invalid_Sequence
137 ("Simple " & Eacute, Iconv.Iso_8859_1, Iconv.KOI8_R);
138
139 -- Test with invalid character both in the middle and at the end
140 Assert_Iconv
141 ("Simple " & Eacute, "Simple ", Iconv.Iso_8859_1, Iconv.KOI8_R,
142 Ignore => True);
143 Assert_Iconv
144 ("T" & Eacute & "Simple", "TSimple", Iconv.Iso_8859_1, Iconv.KOI8_R,
145 Ignore => True);
146
147 Assert_Iconv
148 (Input => Ellipsis_UTF8,
149 Expected => "...",
150 From_Code => Iconv.UTF8,
151 To_Code => Iconv.ASCII,
152 Transliteration => True,
153 Msg => "Test transliteration on ellipsis");
154
155 Assert_Iconv
156 (Input => Ellipsis_UTF8,
157 Expected => "",
158 From_Code => Iconv.UTF8,
159 To_Code => Iconv.ASCII,
160 Transliteration => False,
161 Ignore => True,
162 Msg => "Test UTF-8 to ASCII of ellipsis without transliteration");
163
164 Assert_Iconv
165 (Input => Alveolar_Click & "A",
166 Expected => "?" & "A",
167 From_Code => Iconv.UTF8,
168 To_Code => Iconv.ASCII,
169 Transliteration => True,
170 Ignore => True,
171 Msg => "Test character for which there is no transliteration");
172
173 return A.Report;
174 end Test;