UTC added for Text, Segmentation.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-CharacterSetConversion.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <iostream>
19
20 #include <stdlib.h>
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>
24
25
26 using namespace Dali;
27 using namespace Toolkit;
28 using namespace Text;
29
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.
32 //
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 );
38 //
39
40 //////////////////////////////////////////////////////////
41
42 namespace
43 {
44
45 struct GetNumberOfUtf8CharactersData
46 {
47   std::string  description;        ///< Description of the test.
48   std::string  text;               ///< input text.
49   unsigned int numberOfCharacters; ///< The expected number of characters.
50 };
51
52 bool GetNumberOfUtf8CharactersTest( const GetNumberOfUtf8CharactersData& data )
53 {
54   return GetNumberOfUtf8Characters( reinterpret_cast<const uint8_t*>( data.text.c_str() ), data.text.size() ) == data.numberOfCharacters;
55 }
56
57 //////////////////////////////////////////////////////////
58
59 struct GetNumberOfUtf8BytesData
60 {
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.
65 };
66
67 bool GetNumberOfUtf8BytesTest( const GetNumberOfUtf8BytesData& data )
68 {
69   return GetNumberOfUtf8Bytes( data.utf32, data.numberOfCharacters ) == data.numberOfBytes;
70 }
71
72 //////////////////////////////////////////////////////////
73
74 struct Utf8ToUtf32Data
75 {
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).
79 };
80
81
82 bool Utf8ToUtf32Test( const Utf8ToUtf32Data& data )
83 {
84   Vector<uint32_t> utf32;
85   utf32.Resize( data.text.size() );
86
87   const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( data.text.c_str() ),
88                                                    data.text.size(),
89                                                    utf32.Begin() );
90
91   for( unsigned int index = 0u; index < numberOfCharacters; ++index )
92   {
93     if( data.utf32[index] != utf32[index] )
94     {
95       return false;
96     }
97   }
98
99   return true;
100 }
101
102 } // namespace
103
104 //////////////////////////////////////////////////////////
105
106 struct Utf32ToUtf8Data
107 {
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.
112 };
113
114 bool Utf32ToUtf8Test( const Utf32ToUtf8Data& data )
115 {
116   std::string text;
117
118   Utf32ToUtf8( data.utf32, data.numberOfCharacters, text );
119
120   return text == data.text;
121 }
122
123 //////////////////////////////////////////////////////////
124
125 int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters(void)
126 {
127   ToolkitTestApplication application;
128   tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters");
129
130   const GetNumberOfUtf8CharactersData data[] =
131   {
132     {
133       "Latin script",
134       "Hello World",
135       11u,
136     },
137     {
138       "Arabic script",
139       "مرحبا بالعالم",
140       13u,
141     },
142     {
143       "Devanagari script",
144       "हैलो वर्ल्ड",
145       11u,
146     },
147     {
148       "Emojis",
149       "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
150       7u,
151     },
152   };
153   const unsigned int numberOfTests = 4u;
154
155   for( unsigned int index = 0u; index < numberOfTests; ++index )
156   {
157     if( !GetNumberOfUtf8CharactersTest( data[index] ) )
158     {
159       tet_result(TET_FAIL);
160     }
161   }
162
163   tet_result(TET_PASS);
164   END_TEST;
165 }
166
167 int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes(void)
168 {
169   ToolkitTestApplication application;
170   tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes");
171
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
176
177   const GetNumberOfUtf8BytesData data[] =
178   {
179     {
180       "Latin script",
181       utf32_01,
182       11u,
183       11u,
184     },
185     {
186       "Arabic script",
187       utf32_02,
188       13u,
189       25u,
190     },
191     {
192       "Devanagari script",
193       utf32_03,
194       11u,
195       31u,
196     },
197     {
198       "Emojis",
199       utf32_04,
200       7u,
201       19u,
202     },
203   };
204   const unsigned int numberOfTests = 4u;
205
206   for( unsigned int index = 0u; index < numberOfTests; ++index )
207   {
208     if( !GetNumberOfUtf8BytesTest( data[index] ) )
209     {
210       tet_result(TET_FAIL);
211     }
212   }
213
214   tet_result(TET_PASS);
215   END_TEST;
216 }
217
218 int UtcDaliTextCharacterSetConversionUtf8ToUtf32(void)
219 {
220   ToolkitTestApplication application;
221   tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes");
222
223   unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World
224   unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم
225   unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड
226   unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis
227
228   const Utf8ToUtf32Data data[] =
229   {
230     {
231       "Latin script",
232       "Hello World",
233       utf32_01,
234     },
235     {
236       "Arabic script",
237       "مرحبا بالعالم",
238       utf32_02,
239     },
240     {
241       "Devanagari script",
242       "हैलो वर्ल्ड",
243       utf32_03,
244     },
245     {
246       "Emojis",
247       "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
248       utf32_04,
249     },
250   };
251   const unsigned int numberOfTests = 4u;
252
253   for( unsigned int index = 0u; index < numberOfTests; ++index )
254   {
255     if( !Utf8ToUtf32Test( data[index] ) )
256     {
257       tet_result(TET_FAIL);
258     }
259   }
260
261   tet_result(TET_PASS);
262   END_TEST;
263 }
264
265 int UtcDaliTextCharacterSetConversionUtf32ToUtf8(void)
266 {
267   ToolkitTestApplication application;
268   tet_infoline(" UtcDaliTextCharacterSetConversionUtf32ToUtf8");
269
270   unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World
271   unsigned int utf32_02[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم
272   unsigned int utf32_03[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड
273   unsigned int utf32_04[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis
274
275   struct Utf32ToUtf8Data data[] =
276   {
277     {
278       "Latin script",
279       utf32_01,
280       11u,
281       "Hello World",
282     },
283     {
284       "Arabic script",
285       utf32_02,
286       13u,
287       "مرحبا بالعالم",
288     },
289     {
290       "Devanagari script",
291       utf32_03,
292       11u,
293       "हैलो वर्ल्ड",
294     },
295     {
296       "Emojis",
297       utf32_04,
298       7u,
299       "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
300     },
301   };
302
303   const unsigned int numberOfTests = 4u;
304
305   for( unsigned int index = 0u; index < numberOfTests; ++index )
306   {
307     if( !Utf32ToUtf8Test( data[index] ) )
308     {
309       tet_result(TET_FAIL);
310     }
311   }
312
313   tet_result(TET_PASS);
314   END_TEST;
315 }