2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <dali-toolkit/internal/text/character-set-conversion.h>
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
27 using namespace Toolkit;
30 // Tests the following functions for scripts with different number of bytes per character.
31 // Latin 1 byte per character, Arabic 2 bytes per character, Devanagari 3 bytes per character and emojis 4 bytes per character.
33 // uint32_t GetNumberOfUtf8Characters( const uint8_t* const utf8, uint32_t length );
34 // uint32_t GetNumberOfUtf8Bytes( const uint32_t* const utf32, uint32_t numberOfCharacters );
35 // uint32_t Utf8ToUtf32( const uint8_t* const utf8, uint32_t length, uint32_t* utf32 );
36 // uint32_t Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, uint8_t* utf8 );
37 // void Utf32ToUtf8( const uint32_t* const utf32, uint32_t numberOfCharacters, std::string& utf8 );
40 //////////////////////////////////////////////////////////
45 struct GetNumberOfUtf8CharactersData
47 std::string description; ///< Description of the test.
48 std::string text; ///< input text.
49 unsigned int numberOfCharacters; ///< The expected number of characters.
52 bool GetNumberOfUtf8CharactersTest( const GetNumberOfUtf8CharactersData& data )
54 return GetNumberOfUtf8Characters( reinterpret_cast<const uint8_t*>( data.text.c_str() ), data.text.size() ) == data.numberOfCharacters;
57 //////////////////////////////////////////////////////////
59 struct GetNumberOfUtf8BytesData
61 std::string description; ///< Description of the test.
62 unsigned int* utf32; ///< input text in utf32.
63 unsigned int numberOfCharacters; ///< The number of characters.
64 unsigned int numberOfBytes; ///< The expected number of bytes in utf8.
67 bool GetNumberOfUtf8BytesTest( const GetNumberOfUtf8BytesData& data )
69 return GetNumberOfUtf8Bytes( data.utf32, data.numberOfCharacters ) == data.numberOfBytes;
72 //////////////////////////////////////////////////////////
74 struct Utf8ToUtf32Data
76 std::string description; ///< Description of the test.
77 std::string text; ///< input text.
78 unsigned int* utf32; ///< The expected text (array of bytes with text encoded in utf32).
82 bool Utf8ToUtf32Test( const Utf8ToUtf32Data& data )
84 Vector<uint32_t> utf32;
85 utf32.Resize( data.text.size() );
87 const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( data.text.c_str() ),
91 for( unsigned int index = 0u; index < numberOfCharacters; ++index )
93 if( data.utf32[index] != utf32[index] )
104 //////////////////////////////////////////////////////////
106 struct Utf32ToUtf8Data
108 std::string description; ///< Description of the test.
109 unsigned int* utf32; ///< The input text (array of bytes with text encoded in utf32).
110 unsigned int numberOfCharacters; ///< The number of characters.
111 std::string text; ///< The expected text.
114 bool Utf32ToUtf8Test( const Utf32ToUtf8Data& data )
118 Utf32ToUtf8( data.utf32, data.numberOfCharacters, text );
120 return text == data.text;
123 //////////////////////////////////////////////////////////
125 int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters(void)
127 ToolkitTestApplication application;
128 tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters");
130 const GetNumberOfUtf8CharactersData data[] =
149 "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
153 const unsigned int numberOfTests = 4u;
155 for( unsigned int index = 0u; index < numberOfTests; ++index )
157 if( !GetNumberOfUtf8CharactersTest( data[index] ) )
159 tet_result(TET_FAIL);
163 tet_result(TET_PASS);
167 int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes(void)
169 ToolkitTestApplication application;
170 tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes");
172 unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World
173 unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم
174 unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड
175 unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis
177 const GetNumberOfUtf8BytesData data[] =
204 const unsigned int numberOfTests = 4u;
206 for( unsigned int index = 0u; index < numberOfTests; ++index )
208 if( !GetNumberOfUtf8BytesTest( data[index] ) )
210 tet_result(TET_FAIL);
214 tet_result(TET_PASS);
218 int UtcDaliTextCharacterSetConversionUtf8ToUtf32(void)
220 ToolkitTestApplication application;
221 tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes");
223 unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World
224 unsigned int utf32_02[] = { 0xA, 0x20, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0xA, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World + CR and CR+LF
225 unsigned int utf32_03[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم
226 unsigned int utf32_04[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड
227 unsigned int utf32_05[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis
229 const Utf8ToUtf32Data data[] =
237 "Latin script with 'CR' and 'CR'+'LF'",
238 "\xd Hello\xd\xa World",
253 "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
257 const unsigned int numberOfTests = 5u;
259 for( unsigned int index = 0u; index < numberOfTests; ++index )
261 if( !Utf8ToUtf32Test( data[index] ) )
263 tet_result(TET_FAIL);
267 tet_result(TET_PASS);
271 int UtcDaliTextCharacterSetConversionUtf32ToUtf8(void)
273 ToolkitTestApplication application;
274 tet_infoline(" UtcDaliTextCharacterSetConversionUtf32ToUtf8");
276 unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World
277 unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم
278 unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड
279 unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis
281 struct Utf32ToUtf8Data data[] =
305 "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
309 const unsigned int numberOfTests = 4u;
311 for( unsigned int index = 0u; index < numberOfTests; ++index )
313 if( !Utf32ToUtf8Test( data[index] ) )
315 tet_result(TET_FAIL);
319 tet_result(TET_PASS);