Quelle testDeflateStringToUTF8Buffer.cpp
Sprache: C
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// DeflateStringToUTF8Buffer does not write a null terminator, so the byte // following the last byte written to the |actual| buffer should retain // the value it held before the call to DeflateStringToUTF8Buffer, which is // initialized to 0x1.
char actual[100]; auto span = mozilla::Span(actual);
// Test with an ASCII string, which calls JSLinearString::latin1Chars // to retrieve the characters from the string and generates UTF-8 output // that is identical to the ASCII input.
// Test with a Latin-1 string, which calls JSLinearString::latin1Chars // like with the ASCII string but generates UTF-8 output that is different // from the ASCII input.
{ // Specify a destination buffer length of 3. That's exactly enough // space to encode the first two characters, which takes three bytes. constunsignedchar expected[] = {0xC3, 0x93, 0x68, 0x1};
memset(actual, 0x1, 100);
size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(3));
CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
CHECK_EQUAL(dstlen, 3u);
}
{ // Specify a destination buffer length of 4. That's only enough space // to encode the first two characters, which takes three bytes, because // the third character would take another two bytes. constunsignedchar expected[] = {0xC3, 0x93, 0x68, 0x1};
memset(actual, 0x1, 100);
size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(4));
CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
CHECK_EQUAL(dstlen, 3u);
}
{ // Specify a destination buffer length of 3. That's exactly enough // space to encode the first two characters, which takes three bytes. constunsignedchar expected[] = {0xCE, 0x8C, 0x68, 0x1};
memset(actual, 0x1, 100);
size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(3));
CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
CHECK_EQUAL(dstlen, 3u);
}
{ // Specify a destination buffer length of 4. That's only enough space // to encode the first two characters, which takes three bytes, because // the third character would take another two bytes. constunsignedchar expected[] = {0xCE, 0x8C, 0x68, 0x1};
memset(actual, 0x1, 100);
size_t dstlen = JS::DeflateStringToUTF8Buffer(linearStr, span.To(4));
CHECK_EQUAL(memcmp(actual, expected, sizeof(expected)), 0);
CHECK_EQUAL(dstlen, 3u);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.