@@ -212,4 +212,72 @@ namespace CodexTest
212
212
CHECK (sourceBuffer[i] == (const char16)encodedBuffer[i]);
213
213
}
214
214
}
215
+
216
+ template <typename TTestCase, typename TDecodeFunc>
217
+ void RunUtf8DecodeTestCase (const TTestCase &testCases, const TDecodeFunc func)
218
+ {
219
+ const int numTestCases = _countof (testCases);
220
+ const charcount_t charCount = _countof (testCases[0 ].result );
221
+ char16 decodedBuffer[charCount + 1 ]; // +1 in case a null-terminating func is passed in
222
+
223
+ for (int i = 0 ; i < numTestCases; i++)
224
+ {
225
+ bool chunkEndsInTruncatedSequence = false ;
226
+ size_t decodedCount = func (decodedBuffer, testCases[i].utf8Encoding , testCases[i].utf8Encoding + testCases[i].bytesToDecode , utf8::DecodeOptions::doChunkedEncoding, &chunkEndsInTruncatedSequence);
227
+ CHECK (decodedCount == testCases[i].expectedDecodedChars );
228
+
229
+ for (size_t j = 0 ; j < decodedCount; j++)
230
+ {
231
+ CHECK (decodedBuffer[j] == testCases[i].result [j]);
232
+ }
233
+
234
+ CHECK (testCases[i].shouldEndInTruncation == chunkEndsInTruncatedSequence);
235
+ }
236
+ }
237
+
238
+ TEST_CASE (" CodexTest_DecodeUnitsInto_ChunkEndsInTruncatedSequence" , " [CodexTest]" )
239
+ {
240
+ struct TestCase
241
+ {
242
+ int bytesToDecode;
243
+ size_t expectedDecodedChars;
244
+ bool shouldEndInTruncation;
245
+ char16 result[8 ];
246
+ utf8char_t utf8Encoding[8 ];
247
+ };
248
+
249
+ TestCase testCases[] = {
250
+ { 2 , 1 , false , { 0xc1 }, { 0xc3 , 0x81 } }, // Valid 2-byte sequence
251
+ { 1 , 0 , true , { 0x0 }, { 0xc3 , 0x81 } }, // Valid 2-byte sequence truncated at the end of the chunk
252
+ { 2 , 2 , false , { 0xfffd , 0x79 },{ 0xc3 , 0x79 } }, // Invalid 2-byte sequence
253
+ { 1 , 0 , true , { 0x0 }, { 0xc3 , 0x79 } }, // Invalid 2-byte sequence truncated at the end of the chunk
254
+ { 3 , 1 , false , { 0x3042 },{ 0xe3 , 0x81 , 0x82 } }, // Valid 3-byte sequence
255
+ { 1 , 0 , true , { 0x0 }, { 0xe3 , 0x81 , 0x82 } }, // Valid 3-byte sequence truncated at the end of the chunk
256
+ { 2 , 0 , true , { 0x0 }, { 0xe3 , 0x81 , 0x82 } }, // Valid 3-byte sequence truncated at the end of the chunk
257
+ { 3 , 3 , false , { 0xfffd , 0x79 , 0xfffd }, { 0xe3 , 0x79 , 0x82 } }, // Invalid 3-byte sequence
258
+ { 1 , 0 , true , { 0x0 }, { 0xe3 , 0x79 , 0x82 } }, // Invalid 3-byte sequence truncated at the end of the chunk
259
+ { 2 , 0 , true , { 0x0 }, { 0xe3 , 0x79 , 0x82 } }, // Invalid 3-byte sequence truncated at the end of the chunk
260
+ { 3 , 3 , false , { 0xfffd , 0xfffd , 0x79 }, { 0xe3 , 0x81 , 0x79 } }, // Invalid 3-byte sequence
261
+ { 1 , 0 , true , { 0x0 }, { 0xe3 , 0x81 , 0x79 } }, // Invalid 3-byte sequence truncated at the end of the chunk
262
+ { 2 , 0 , true , { 0x0 }, { 0xe3 , 0x81 , 0x79 } }, // Invalid 3-byte sequence truncated at the end of the chunk
263
+ { 4 , 2 , false , { 0xd9c4 , 0xdc83 }, { 0xf2 , 0x81 , 0x82 , 0x83 } }, // Valid 4-byte sequence
264
+ { 1 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x82 , 0x83 } }, // Valid 4-byte sequence truncated at the end of the chunk
265
+ { 2 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x82 , 0x83 } }, // Valid 4-byte sequence truncated at the end of the chunk
266
+ { 3 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x82 , 0x83 } }, // Valid 4-byte sequence truncated at the end of the chunk
267
+ { 4 , 4 , false , { 0xfffd , 0x79 , 0xfffd , 0xfffd }, { 0xf2 , 0x79 , 0x82 , 0x83 } }, // Invalid 4-byte sequence
268
+ { 1 , 0 , true , { 0x0 }, { 0xf2 , 0x79 , 0x82 , 0x83 } }, // Invalid 4-byte sequence truncated at the end of the chunk
269
+ { 2 , 0 , true , { 0x0 }, { 0xf2 , 0x79 , 0x82 , 0x83 } }, // Invalid 4-byte sequence truncated at the end of the chunk
270
+ { 3 , 0 , true , { 0x0 }, { 0xf2 , 0x79 , 0x82 , 0x83 } }, // Invalid 4-byte sequence truncated at the end of the chunk
271
+ { 4 , 4 , false , { 0xfffd , 0xfffd , 0x79 , 0xfffd }, { 0xf2 , 0x81 , 0x79 , 0x83 } }, // Invalid 4-byte sequence
272
+ { 1 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x79 , 0x83 } }, // Invalid 4-byte sequence truncated at the end of the chunk
273
+ { 2 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x79 , 0x83 } }, // Invalid 4-byte sequence truncated at the end of the chunk
274
+ { 3 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x79 , 0x83 } }, // Invalid 4-byte sequence truncated at the end of the chunk
275
+ { 4 , 4 , false , { 0xfffd , 0xfffd , 0xfffd , 0x79 }, { 0xf2 , 0x81 , 0x82 , 0x79 } }, // Invalid 4-byte sequence
276
+ { 1 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x82 , 0x79 } }, // Invalid 4-byte sequence truncated at the end of the chunk
277
+ { 2 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x82 , 0x79 } }, // Invalid 4-byte sequence truncated at the end of the chunk
278
+ { 3 , 0 , true , { 0x0 }, { 0xf2 , 0x81 , 0x82 , 0x79 } }, // Invalid 4-byte sequence truncated at the end of the chunk
279
+ };
280
+
281
+ RunUtf8DecodeTestCase (testCases, utf8::DecodeUnitsIntoAndNullTerminateNoAdvance);
282
+ }
215
283
};
0 commit comments