From f7e73f010f873b37b8f617c62873992e6e41dc5d Mon Sep 17 00:00:00 2001 From: Victor Cebollada Date: Wed, 6 May 2015 07:31:20 +0100 Subject: [PATCH] UTC added for Text, CharacterSetConversion. Change-Id: Ia32ce8ab447035517f93156bdea5c3a3a6f556e7 Signed-off-by: Victor Cebollada --- .../src/dali-toolkit-internal/CMakeLists.txt | 1 + .../utc-Dali-Text-CharacterSetConversion.cpp | 315 +++++++++++++++++++++ .../internal/text/character-set-conversion.cpp | 5 + .../internal/text/character-set-conversion.h | 5 + 4 files changed, 326 insertions(+) create mode 100644 automated-tests/src/dali-toolkit-internal/utc-Dali-Text-CharacterSetConversion.cpp diff --git a/automated-tests/src/dali-toolkit-internal/CMakeLists.txt b/automated-tests/src/dali-toolkit-internal/CMakeLists.txt index 8011f6a..f7500ac 100644 --- a/automated-tests/src/dali-toolkit-internal/CMakeLists.txt +++ b/automated-tests/src/dali-toolkit-internal/CMakeLists.txt @@ -8,6 +8,7 @@ SET(CAPI_LIB "dali-toolkit-internal") # List of test case sources (Only these get parsed for test cases) SET(TC_SOURCES utc-Dali-PushButton.cpp + utc-Dali-Text-CharacterSetConversion.cpp ) # Append list of test harness files (Won't get parsed for test cases) diff --git a/automated-tests/src/dali-toolkit-internal/utc-Dali-Text-CharacterSetConversion.cpp b/automated-tests/src/dali-toolkit-internal/utc-Dali-Text-CharacterSetConversion.cpp new file mode 100644 index 0000000..9f816ef --- /dev/null +++ b/automated-tests/src/dali-toolkit-internal/utc-Dali-Text-CharacterSetConversion.cpp @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include + +#include +#include +#include +#include + + +using namespace Dali; +using namespace Toolkit; +using namespace Text; + +// Tests the following functions for scripts with different number of bytes per character. +// Latin 1 byte per character, Arabic 2 bytes per character, Devanagari 3 bytes per character and emojis 4 bytes per character. +// +// uint32_t GetNumberOfUtf8Characters( const uint8_t* const utf8, uint32_t length ); +// uint32_t GetNumberOfUtf8Bytes( const uint32_t* const utf32, uint32_t numberOfCharacters ); +// uint32_t Utf8ToUtf32( const uint8_t* const utf8, uint32_t length, uint32_t* utf32 ); +// uint32_t Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, uint8_t* utf8 ); +// void Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, std::string& utf8 ); +// + +////////////////////////////////////////////////////////// + +namespace +{ + +struct GetNumberOfUtf8CharactersData +{ + std::string description; ///< Description of the test. + std::string text; ///< input text. + unsigned int numberOfCharacters; ///< The expected number of characters. +}; + +bool GetNumberOfUtf8CharactersTest( const GetNumberOfUtf8CharactersData& data ) +{ + return GetNumberOfUtf8Characters( reinterpret_cast( data.text.c_str() ), data.text.size() ) == data.numberOfCharacters; +} + +////////////////////////////////////////////////////////// + +struct GetNumberOfUtf8BytesData +{ + std::string description; ///< Description of the test. + unsigned int* utf32; ///< input text in utf32. + unsigned int numberOfCharacters; ///< The number of characters. + unsigned int numberOfBytes; ///< The expected number of bytes in utf8. +}; + +bool GetNumberOfUtf8BytesTest( const GetNumberOfUtf8BytesData& data ) +{ + return GetNumberOfUtf8Bytes( data.utf32, data.numberOfCharacters ) == data.numberOfBytes; +} + +////////////////////////////////////////////////////////// + +struct Utf8ToUtf32Data +{ + std::string description; ///< Description of the test. + std::string text; ///< input text. + unsigned int* utf32; ///< The expected text (array of bytes with text encoded in utf32). +}; + + +bool Utf8ToUtf32Test( const Utf8ToUtf32Data& data ) +{ + Vector utf32; + utf32.Resize( data.text.size() ); + + const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast( data.text.c_str() ), + data.text.size(), + utf32.Begin() ); + + for( unsigned int index = 0u; index < numberOfCharacters; ++index ) + { + if( data.utf32[index] != utf32[index] ) + { + return false; + } + } + + return true; +} + +} // namespace + +////////////////////////////////////////////////////////// + +struct Utf32ToUtf8Data +{ + std::string description; ///< Description of the test. + unsigned int* utf32; ///< The input text (array of bytes with text encoded in utf32). + unsigned int numberOfCharacters; ///< The number of characters. + std::string text; ///< The expected text. +}; + +bool Utf32ToUtf8Test( const Utf32ToUtf8Data& data ) +{ + std::string text; + + Utf32ToUtf8( data.utf32, data.numberOfCharacters, text ); + + return text == data.text; +} + +////////////////////////////////////////////////////////// + +int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters(void) +{ + ToolkitTestApplication application; + tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters"); + + const GetNumberOfUtf8CharactersData data[] = + { + { + "Latin script", + "Hello World", + 11u, + }, + { + "Arabic script", + "مرحبا بالعالم", + 13u, + }, + { + "Devanagari script", + "हैलो वर्ल्ड", + 11u, + }, + { + "Emojis", + "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84", + 7u, + }, + }; + const unsigned int numberOfTests = 4u; + + for( unsigned int index = 0u; index < numberOfTests; ++index ) + { + if( !GetNumberOfUtf8CharactersTest( data[index] ) ) + { + tet_result(TET_FAIL); + } + } + + tet_result(TET_PASS); + END_TEST; +} + +int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes(void) +{ + ToolkitTestApplication application; + tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes"); + + unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World + unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم + unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड + unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis + + const GetNumberOfUtf8BytesData data[] = + { + { + "Latin script", + utf32_01, + 11u, + 11u, + }, + { + "Arabic script", + utf32_02, + 13u, + 25u, + }, + { + "Devanagari script", + utf32_03, + 11u, + 31u, + }, + { + "Emojis", + utf32_04, + 7u, + 19u, + }, + }; + const unsigned int numberOfTests = 4u; + + for( unsigned int index = 0u; index < numberOfTests; ++index ) + { + if( !GetNumberOfUtf8BytesTest( data[index] ) ) + { + tet_result(TET_FAIL); + } + } + + tet_result(TET_PASS); + END_TEST; +} + +int UtcDaliTextCharacterSetConversionUtf8ToUtf32(void) +{ + ToolkitTestApplication application; + tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes"); + + unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World + unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم + unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड + unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis + + const Utf8ToUtf32Data data[] = + { + { + "Latin script", + "Hello World", + utf32_01, + }, + { + "Arabic script", + "مرحبا بالعالم", + utf32_02, + }, + { + "Devanagari script", + "हैलो वर्ल्ड", + utf32_03, + }, + { + "Emojis", + "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84", + utf32_04, + }, + }; + const unsigned int numberOfTests = 4u; + + for( unsigned int index = 0u; index < numberOfTests; ++index ) + { + if( !Utf8ToUtf32Test( data[index] ) ) + { + tet_result(TET_FAIL); + } + } + + tet_result(TET_PASS); + END_TEST; +} + +int UtcDaliTextCharacterSetConversionUtf32ToUtf8(void) +{ + ToolkitTestApplication application; + tet_infoline(" UtcDaliTextCharacterSetConversionUtf32ToUtf8"); + + unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World + unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم + unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड + unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis + + struct Utf32ToUtf8Data data[] = + { + { + "Latin script", + utf32_01, + 11u, + "Hello World", + }, + { + "Arabic script", + utf32_02, + 13u, + "مرحبا بالعالم", + }, + { + "Devanagari script", + utf32_03, + 11u, + "हैलो वर्ल्ड", + }, + { + "Emojis", + utf32_04, + 7u, + "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84", + }, + }; + + const unsigned int numberOfTests = 4u; + + for( unsigned int index = 0u; index < numberOfTests; ++index ) + { + if( !Utf32ToUtf8Test( data[index] ) ) + { + tet_result(TET_FAIL); + } + } + + tet_result(TET_PASS); + END_TEST; +} diff --git a/dali-toolkit/internal/text/character-set-conversion.cpp b/dali-toolkit/internal/text/character-set-conversion.cpp index b1b9993..67c294b 100644 --- a/dali-toolkit/internal/text/character-set-conversion.cpp +++ b/dali-toolkit/internal/text/character-set-conversion.cpp @@ -24,6 +24,9 @@ namespace Dali namespace Toolkit { +namespace Text +{ + namespace { const static uint8_t U1 = 1u; @@ -222,6 +225,8 @@ void Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, std: Utf32ToUtf8( utf32, numberOfCharacters, reinterpret_cast(&utf8[0]) ); } +} // namespace Text + } // namespace Toolkit } // namespace Dali diff --git a/dali-toolkit/internal/text/character-set-conversion.h b/dali-toolkit/internal/text/character-set-conversion.h index 19784fb..720114b 100644 --- a/dali-toolkit/internal/text/character-set-conversion.h +++ b/dali-toolkit/internal/text/character-set-conversion.h @@ -31,6 +31,9 @@ namespace Dali namespace Toolkit { +namespace Text +{ + /** * @brief Retrieves the number of characters of the text array encoded in UTF8 * @@ -86,6 +89,8 @@ uint32_t Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, */ void Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, std::string& utf8 ); +} // namespace Text + } // namespace Toolkit } // namespace Dali -- 2.7.4