Dali 3D User Interface Engine
utc-Dali-Text-CharacterSetConversion.cpp
Go to the documentation of this file.
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>
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 
41 
42 namespace
43 {
44 
46 {
47  std::string description;
48  std::string text;
49  unsigned int numberOfCharacters;
50 };
51 
53 {
54  return GetNumberOfUtf8Characters( reinterpret_cast<const uint8_t*>( data.text.c_str() ), data.text.size() ) == data.numberOfCharacters;
55 }
56 
58 
60 {
61  std::string description;
62  unsigned int* utf32;
63  unsigned int numberOfCharacters;
64  unsigned int numberOfBytes;
65 };
66 
68 {
69  return GetNumberOfUtf8Bytes( data.utf32, data.numberOfCharacters ) == data.numberOfBytes;
70 }
71 
73 
75 {
76  std::string description;
77  std::string text;
78  unsigned int* 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 
105 
107 {
108  std::string description;
109  unsigned int* utf32;
110  unsigned int numberOfCharacters;
111  std::string 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 
124 
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  {
160  }
161  }
162 
164  END_TEST;
165 }
166 
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  {
211  }
212  }
213 
215  END_TEST;
216 }
217 
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[] = { 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
228 
229  const Utf8ToUtf32Data data[] =
230  {
231  {
232  "Latin script",
233  "Hello World",
234  utf32_01,
235  },
236  {
237  "Latin script with 'CR' and 'CR'+'LF'",
238  "\xd Hello\xd\xa World",
239  utf32_02,
240  },
241  {
242  "Arabic script",
243  "مرحبا بالعالم",
244  utf32_03,
245  },
246  {
247  "Devanagari script",
248  "हैलो वर्ल्ड",
249  utf32_04,
250  },
251  {
252  "Emojis",
253  "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
254  utf32_05,
255  },
256  };
257  const unsigned int numberOfTests = 5u;
258 
259  for( unsigned int index = 0u; index < numberOfTests; ++index )
260  {
261  if( !Utf8ToUtf32Test( data[index] ) )
262  {
264  }
265  }
266 
268  END_TEST;
269 }
270 
272 {
273  ToolkitTestApplication application;
274  tet_infoline(" UtcDaliTextCharacterSetConversionUtf32ToUtf8");
275 
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
280 
281  struct Utf32ToUtf8Data data[] =
282  {
283  {
284  "Latin script",
285  utf32_01,
286  11u,
287  "Hello World",
288  },
289  {
290  "Arabic script",
291  utf32_02,
292  13u,
293  "مرحبا بالعالم",
294  },
295  {
296  "Devanagari script",
297  utf32_03,
298  11u,
299  "हैलो वर्ल्ड",
300  },
301  {
302  "Emojis",
303  utf32_04,
304  7u,
305  "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
306  },
307  };
308 
309  const unsigned int numberOfTests = 4u;
310 
311  for( unsigned int index = 0u; index < numberOfTests; ++index )
312  {
313  if( !Utf32ToUtf8Test( data[index] ) )
314  {
316  }
317  }
318 
320  END_TEST;
321 }
Dali Docs Home
Read more about Dali