1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Jeff Walden <jwalden+code@mit.edu>.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
40 nsresult
TestGoodSurrogatePair()
42 // When this string is decoded, the surrogate pair is U+10302 and the rest of
43 // the string is specified by indexes 2 onward.
44 const PRUnichar goodPairData
[] = { 0xD800, 0xDF02, 0x65, 0x78, 0x0 };
45 nsDependentString
goodPair16(goodPairData
);
47 PRUint32 byteCount
= 0;
48 char* goodPair8
= ToNewUTF8String(goodPair16
, &byteCount
);
51 fail("out of memory creating goodPair8");
52 return NS_ERROR_OUT_OF_MEMORY
;
57 fail("wrong number of bytes; expected 6, got %lu", byteCount
);
58 return NS_ERROR_FAILURE
;
61 const char expected8
[] = { 0xF0, 0x90, 0x8C, 0x82, 0x65, 0x78, 0x0 };
62 if (0 != memcmp(expected8
, goodPair8
, sizeof(expected8
)))
64 fail("wrong translation to UTF8");
65 return NS_ERROR_FAILURE
;
68 // This takes a different code path from the above, so test it to make sure
69 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration.
70 nsDependentCString
expected(expected8
);
71 if (0 != CompareUTF8toUTF16(expected
, goodPair16
))
73 fail("bad comparison between UTF-8 and equivalent UTF-16");
74 return NS_ERROR_FAILURE
;
79 passed("TestGoodSurrogatePair");
83 nsresult
TestBackwardsSurrogatePair()
85 // When this string is decoded, the two surrogates are wrongly ordered and
86 // must each be interpreted as U+FFFD.
87 const PRUnichar backwardsPairData
[] = { 0xDDDD, 0xD863, 0x65, 0x78, 0x0 };
88 nsDependentString
backwardsPair16(backwardsPairData
);
90 PRUint32 byteCount
= 0;
91 char* backwardsPair8
= ToNewUTF8String(backwardsPair16
, &byteCount
);
94 fail("out of memory creating backwardsPair8");
95 return NS_ERROR_OUT_OF_MEMORY
;
100 fail("wrong number of bytes; expected 8, got %lu", byteCount
);
101 return NS_ERROR_FAILURE
;
104 const char expected8
[] =
105 { 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x65, 0x78, 0x0 };
106 if (0 != memcmp(expected8
, backwardsPair8
, sizeof(expected8
)))
108 fail("wrong translation to UTF8");
109 return NS_ERROR_FAILURE
;
112 // This takes a different code path from the above, so test it to make sure
113 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration.
114 nsDependentCString
expected(expected8
);
115 if (0 != CompareUTF8toUTF16(expected
, backwardsPair16
))
117 fail("bad comparison between UTF-8 and malformed but equivalent UTF-16");
118 return NS_ERROR_FAILURE
;
121 NS_Free(backwardsPair8
);
123 passed("TestBackwardsSurrogatePair");
127 nsresult
TestMalformedUTF16OrphanHighSurrogate()
129 // When this string is decoded, the high surrogate should be replaced and the
130 // rest of the string is specified by indexes 1 onward.
131 const PRUnichar highSurrogateData
[] = { 0xD863, 0x74, 0x65, 0x78, 0x74, 0x0 };
132 nsDependentString
highSurrogate16(highSurrogateData
);
134 PRUint32 byteCount
= 0;
135 char* highSurrogate8
= ToNewUTF8String(highSurrogate16
, &byteCount
);
138 fail("out of memory creating highSurrogate8");
139 return NS_ERROR_OUT_OF_MEMORY
;
144 fail("wrong number of bytes; expected 7, got %lu", byteCount
);
145 return NS_ERROR_FAILURE
;
148 const char expected8
[] = { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 };
149 if (0 != memcmp(expected8
, highSurrogate8
, sizeof(expected8
)))
151 fail("wrong translation to UTF8");
152 return NS_ERROR_FAILURE
;
155 // This takes a different code path from the above, so test it to make sure
156 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration.
157 nsDependentCString
expected(expected8
);
158 if (0 != CompareUTF8toUTF16(expected
, highSurrogate16
))
160 fail("bad comparison between UTF-8 and malformed but equivalent UTF-16");
161 return NS_ERROR_FAILURE
;
164 NS_Free(highSurrogate8
);
166 passed("TestMalformedUTF16OrphanHighSurrogate");
170 nsresult
TestMalformedUTF16OrphanLowSurrogate()
172 // When this string is decoded, the low surrogate should be replaced and the
173 // rest of the string is specified by indexes 1 onward.
174 const PRUnichar lowSurrogateData
[] = { 0xDDDD, 0x74, 0x65, 0x78, 0x74, 0x0 };
175 nsDependentString
lowSurrogate16(lowSurrogateData
);
177 PRUint32 byteCount
= 0;
178 char* lowSurrogate8
= ToNewUTF8String(lowSurrogate16
, &byteCount
);
181 fail("out of memory creating lowSurrogate8");
182 return NS_ERROR_OUT_OF_MEMORY
;
187 fail("wrong number of bytes; expected 7, got %lu", byteCount
);
188 return NS_ERROR_FAILURE
;
191 const char expected8
[] = { 0xEF, 0xBF, 0xBD, 0x74, 0x65, 0x78, 0x74, 0x0 };
192 if (0 != memcmp(expected8
, lowSurrogate8
, sizeof(expected8
)))
194 fail("wrong translation to UTF8");
195 return NS_ERROR_FAILURE
;
198 // This takes a different code path from the above, so test it to make sure
199 // the UTF-16 enumeration remains in sync with the UTF-8 enumeration.
200 nsDependentCString
expected(expected8
);
201 if (0 != CompareUTF8toUTF16(expected
, lowSurrogate16
))
203 fail("bad comparison between UTF-8 and malformed but equivalent UTF-16");
204 return NS_ERROR_FAILURE
;
207 NS_Free(lowSurrogate8
);
209 passed("TestMalformedUTF16OrphanLowSurrogate");
214 int main(int argc
, char** argv
)
216 ScopedXPCOM
xpcom("TestEncoding");
222 if (NS_FAILED(TestGoodSurrogatePair()))
224 if (NS_FAILED(TestBackwardsSurrogatePair()))
226 if (NS_FAILED(TestMalformedUTF16OrphanHighSurrogate()))
228 if (NS_FAILED(TestMalformedUTF16OrphanLowSurrogate()))