From 1d5220ccb2b6b15615226d34567f8f4d912ea1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 28 Jun 2019 11:25:44 +0200 Subject: [PATCH] [ion/tests] More UTF8Helper tests --- ion/test/utf8_helper.cpp | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/ion/test/utf8_helper.cpp b/ion/test/utf8_helper.cpp index 06a88d766..a0830d360 100644 --- a/ion/test/utf8_helper.cpp +++ b/ion/test/utf8_helper.cpp @@ -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(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(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); }