[ion/tests] More UTF8Helper tests

This commit is contained in:
Léa Saviot
2019-06-28 11:25:44 +02:00
committed by Émilie Feral
parent a2bda4b955
commit 1d5220ccb2

View File

@@ -145,6 +145,102 @@ QUIZ_CASE(ion_utf8_remove_code_point) {
assert_remove_code_point_gives(buffer, c, &indexToUpdate, stoppingPosition, indexToUpdateResult, result);
}
void assert_string_copy_until_code_point_gives(char * dst, size_t dstSize, const char * src, CodePoint c, const char * result, size_t returnedResult) {
quiz_assert(UTF8Helper::CopyUntilCodePoint(dst, dstSize, src, c) == returnedResult);
quiz_assert(strcmp(dst, result) == 0);
}
QUIZ_CASE(ion_utf8_helper_copy_until_code_point) {
constexpr int bufferSize = 100;
char buffer[bufferSize];
const char * s = "1234";
CodePoint c = '1';
const char * result = "";
size_t returnedResult = 0;
assert_string_copy_until_code_point_gives(buffer, bufferSize, s, c, result, returnedResult);
s = "1234";
c = '3';
result = "12";
returnedResult = 2;
assert_string_copy_until_code_point_gives(buffer, bufferSize, s, c, result, returnedResult);
s = "1234";
c = '6';
result = s;
returnedResult = 4;
assert_string_copy_until_code_point_gives(buffer, bufferSize, s, c, result, returnedResult);
}
QUIZ_CASE(ion_utf8_helper_previous_code_point_is) {
const char * s = "1234";
quiz_assert(UTF8Helper::PreviousCodePointIs(s, s+2, '2'));
quiz_assert(!UTF8Helper::PreviousCodePointIs(s, s+2, '4'));
s = "1234∑";
quiz_assert(UTF8Helper::PreviousCodePointIs(s, s+strlen(s), UCodePointNArySummation));
}
QUIZ_CASE(ion_utf8_helper_code_point_is) {
const char * s = "34";
quiz_assert(UTF8Helper::CodePointIs(s, '3'));
quiz_assert(!UTF8Helper::CodePointIs(s, '4'));
s = "∑1234";
quiz_assert(UTF8Helper::CodePointIs(s, UCodePointNArySummation));
}
void assert_string_remove_previous_glyph_gives(const char * text, char * location, const char * result, int returnedResult, CodePoint returnedCodePoint) {
CodePoint c = 0;
quiz_assert(UTF8Helper::RemovePreviousGlyph(text, location, &c) == returnedResult);
quiz_assert(c == returnedCodePoint);
quiz_assert(strcmp(text, result) == 0);
}
QUIZ_CASE(ion_utf8_helper_remove_previous_glyph) {
constexpr int bufferSize = 100;
char buffer[bufferSize];
// 3é4
buffer[0] = '3';
buffer[1] = 0x65;
buffer[2] = 0xCC;
buffer[3] = 0x81;
buffer[4] = '4';
buffer[5] = 0;
size_t sLen = 5;
char * location = const_cast<char *>(buffer+sLen - 1);
const char * result = "34";
int returnedResult = sLen - 2;
CodePoint returnedCodePoint = 'e';
assert_string_remove_previous_glyph_gives(buffer, location, result, returnedResult, returnedCodePoint);
const char * s = "345";
sLen = strlen(s);
strlcpy(buffer, s, sLen+1);
location = const_cast<char *>(buffer+sLen);
result = "34";
returnedResult = UTF8Decoder::CharSizeOfCodePoint('5');
returnedCodePoint = '5';
assert_string_remove_previous_glyph_gives(buffer, location, result, returnedResult, returnedCodePoint);
}
QUIZ_CASE(ion_utf8_code_point_at_glyph_offset) {
const char * s = "abc";
quiz_assert(UTF8Helper::CodePointAtGlyphOffset(s, 0) == s);
quiz_assert(UTF8Helper::CodePointAtGlyphOffset(s, 1) == s+1);
quiz_assert(UTF8Helper::CodePointAtGlyphOffset(s, 2) == s+2);
s = "a∑∫𝐢ty";
quiz_assert(UTF8Helper::CodePointAtGlyphOffset(s, 4) == s + strlen(s) - 2);
}
QUIZ_CASE(ion_utf8_glyph_offset_at_code_point) {
const char * s = "abc";
quiz_assert(UTF8Helper::GlyphOffsetAtCodePoint(s, s) == 0);
quiz_assert(UTF8Helper::GlyphOffsetAtCodePoint(s, s+1) == 1);
quiz_assert(UTF8Helper::GlyphOffsetAtCodePoint(s, s+2) == 2);
s = "a∑∫𝐢ty";
quiz_assert(UTF8Helper::GlyphOffsetAtCodePoint(s, s + strlen(s) - 2) == 4);
}
void assert_string_glyph_length_is(const char * string, int maxSize, size_t result) {
quiz_assert(UTF8Helper::StringGlyphLength(string, maxSize) == result);
}