|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "mozilla/ArrayUtils.h" |
|
6 |
|
7 #include <stdio.h> |
|
8 #include <stdlib.h> |
|
9 #include "nsString.h" |
|
10 #include "nsStringBuffer.h" |
|
11 #include "nsReadableUtils.h" |
|
12 #include "UTFStrings.h" |
|
13 #include "nsUnicharUtils.h" |
|
14 #include "mozilla/HashFunctions.h" |
|
15 |
|
16 using namespace mozilla; |
|
17 |
|
18 namespace TestUTF { |
|
19 |
|
20 bool |
|
21 test_valid() |
|
22 { |
|
23 for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { |
|
24 nsDependentCString str8(ValidStrings[i].m8); |
|
25 nsDependentString str16(ValidStrings[i].m16); |
|
26 |
|
27 if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) |
|
28 return false; |
|
29 |
|
30 if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) |
|
31 return false; |
|
32 |
|
33 nsCString tmp8("string "); |
|
34 AppendUTF16toUTF8(str16, tmp8); |
|
35 if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) |
|
36 return false; |
|
37 |
|
38 nsString tmp16(NS_LITERAL_STRING("string ")); |
|
39 AppendUTF8toUTF16(str8, tmp16); |
|
40 if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) |
|
41 return false; |
|
42 |
|
43 if (CompareUTF8toUTF16(str8, str16) != 0) |
|
44 return false; |
|
45 } |
|
46 |
|
47 return true; |
|
48 } |
|
49 |
|
50 bool |
|
51 test_invalid16() |
|
52 { |
|
53 for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) { |
|
54 nsDependentString str16(Invalid16Strings[i].m16); |
|
55 nsDependentCString str8(Invalid16Strings[i].m8); |
|
56 |
|
57 if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) |
|
58 return false; |
|
59 |
|
60 nsCString tmp8("string "); |
|
61 AppendUTF16toUTF8(str16, tmp8); |
|
62 if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) |
|
63 return false; |
|
64 |
|
65 if (CompareUTF8toUTF16(str8, str16) != 0) |
|
66 return false; |
|
67 } |
|
68 |
|
69 return true; |
|
70 } |
|
71 |
|
72 bool |
|
73 test_invalid8() |
|
74 { |
|
75 for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { |
|
76 nsDependentString str16(Invalid8Strings[i].m16); |
|
77 nsDependentCString str8(Invalid8Strings[i].m8); |
|
78 |
|
79 if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) |
|
80 return false; |
|
81 |
|
82 nsString tmp16(NS_LITERAL_STRING("string ")); |
|
83 AppendUTF8toUTF16(str8, tmp16); |
|
84 if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) |
|
85 return false; |
|
86 |
|
87 if (CompareUTF8toUTF16(str8, str16) != 0) |
|
88 return false; |
|
89 } |
|
90 |
|
91 return true; |
|
92 } |
|
93 |
|
94 bool |
|
95 test_malformed8() |
|
96 { |
|
97 // Don't run this test in debug builds as that intentionally asserts. |
|
98 #ifndef DEBUG |
|
99 for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { |
|
100 nsDependentCString str8(Malformed8Strings[i]); |
|
101 |
|
102 if (!NS_ConvertUTF8toUTF16(str8).IsEmpty()) |
|
103 return false; |
|
104 |
|
105 nsString tmp16(NS_LITERAL_STRING("string")); |
|
106 AppendUTF8toUTF16(str8, tmp16); |
|
107 if (!tmp16.Equals(NS_LITERAL_STRING("string"))) |
|
108 return false; |
|
109 |
|
110 if (CompareUTF8toUTF16(str8, EmptyString()) == 0) |
|
111 return false; |
|
112 } |
|
113 #endif |
|
114 |
|
115 return true; |
|
116 } |
|
117 |
|
118 bool |
|
119 test_hashas16() |
|
120 { |
|
121 for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { |
|
122 nsDependentCString str8(ValidStrings[i].m8); |
|
123 bool err; |
|
124 if (HashString(ValidStrings[i].m16) != |
|
125 HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || |
|
126 err) |
|
127 return false; |
|
128 } |
|
129 |
|
130 for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { |
|
131 nsDependentCString str8(Invalid8Strings[i].m8); |
|
132 bool err; |
|
133 if (HashString(Invalid8Strings[i].m16) != |
|
134 HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || |
|
135 err) |
|
136 return false; |
|
137 } |
|
138 |
|
139 // Don't run this test in debug builds as that intentionally asserts. |
|
140 #ifndef DEBUG |
|
141 for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { |
|
142 nsDependentCString str8(Malformed8Strings[i]); |
|
143 bool err; |
|
144 if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 || |
|
145 !err) |
|
146 return false; |
|
147 } |
|
148 #endif |
|
149 |
|
150 return true; |
|
151 } |
|
152 |
|
153 typedef bool (*TestFunc)(); |
|
154 |
|
155 static const struct Test |
|
156 { |
|
157 const char* name; |
|
158 TestFunc func; |
|
159 } |
|
160 tests[] = |
|
161 { |
|
162 { "test_valid", test_valid }, |
|
163 { "test_invalid16", test_invalid16 }, |
|
164 { "test_invalid8", test_invalid8 }, |
|
165 { "test_malformed8", test_malformed8 }, |
|
166 { "test_hashas16", test_hashas16 }, |
|
167 { nullptr, nullptr } |
|
168 }; |
|
169 |
|
170 } |
|
171 |
|
172 using namespace TestUTF; |
|
173 |
|
174 int main(int argc, char **argv) |
|
175 { |
|
176 int count = 1; |
|
177 if (argc > 1) |
|
178 count = atoi(argv[1]); |
|
179 |
|
180 while (count--) |
|
181 { |
|
182 for (const Test* t = tests; t->name != nullptr; ++t) |
|
183 { |
|
184 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--"); |
|
185 } |
|
186 } |
|
187 |
|
188 return 0; |
|
189 } |