Add U0 case to Utf8ToUtf32
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-CharacterSetConversion.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 9f816ef..3175baf
@@ -30,6 +30,7 @@ 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.
 //
 // 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.
 //
+// uint8_t GetUtf8Length( uint8_t utf8LeadByte );
 // 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 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 );
@@ -122,6 +123,67 @@ bool Utf32ToUtf8Test( const Utf32ToUtf8Data& data )
 
 //////////////////////////////////////////////////////////
 
 
 //////////////////////////////////////////////////////////
 
+int UtcDaliTextCharacterSetConversionGetUtf8Length(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliTextCharacterSetConversionGetUtf8Length");
+
+  // Copy of the table used to get the size in bytes of a character encoded with utf8.
+  // If the table used by the GetUtf8Length() function is updated, this one needs to be updated as well.
+  const static uint8_t U1 = 1u;
+  const static uint8_t U2 = 2u;
+  const static uint8_t U3 = 3u;
+  const static uint8_t U4 = 4u;
+  const static uint8_t U0 = 0u;
+  const static uint8_t UTF8_LENGTH[256] = {
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, // lead byte = 0xxx xxxx (U+0000 - U+007F + some extended ascii characters)
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1, U1, U1, U1, U1, U1, U1, U1, U1, //
+    U1, U1,                                 //
+
+    U2, U2, U2, U2, U2, U2, U2, U2, U2, U2, //
+    U2, U2, U2, U2, U2, U2, U2, U2, U2, U2, // lead byte = 110x xxxx (U+0080 - U+07FF)
+    U2, U2, U2, U2, U2, U2, U2, U2, U2, U2, //
+    U2, U2,                                 //
+
+    U3, U3, U3, U3, U3, U3, U3, U3, U3, U3, // lead byte = 1110 xxxx (U+0800 - U+FFFF)
+    U3, U3, U3, U3, U3, U3,                 //
+
+    U4, U4, U4, U4, U4, U4, U4, U4,         // lead byte = 1111 0xxx (U+10000 - U+1FFFFF)
+
+    U0, U0, U0, U0,                         // Non valid.
+    U0, U0, U0, U0,                         // Non valid.
+  };
+
+  for( unsigned int index = 0; index < 256u; ++index )
+  {
+    if( GetUtf8Length( index ) != UTF8_LENGTH[static_cast<uint8_t>(index)] )
+    {
+      tet_result(TET_FAIL);
+    }
+  }
+
+  tet_result(TET_PASS);
+  END_TEST;
+}
+
+
 int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters(void)
 {
   ToolkitTestApplication application;
 int UtcDaliTextCharacterSetConversionGetNumberOfUtf8Characters(void)
 {
   ToolkitTestApplication application;
@@ -220,10 +282,14 @@ int UtcDaliTextCharacterSetConversionUtf8ToUtf32(void)
   ToolkitTestApplication application;
   tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes");
 
   ToolkitTestApplication application;
   tet_infoline(" UtcDaliTextCharacterSetConversionGetNumberOfUtf8Bytes");
 
+  char utf8_06[] = { -8, -7, -6, -5, -4, -3, -2, -1 }; // Invalid string
+
   unsigned int utf32_01[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World
   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
+  unsigned int utf32_02[] = { 0xA, 0x20, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0xA, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64 }; // Hello World + CR and CR+LF
+  unsigned int utf32_03[] = { 0x645, 0x631, 0x62D, 0x628, 0x627, 0x20, 0x628, 0x627, 0x644, 0x639, 0x627, 0x644, 0x645 }; // مرحبا بالعالم
+  unsigned int utf32_04[] = { 0x939, 0x948, 0x932, 0x94B, 0x20, 0x935, 0x930, 0x94D, 0x932, 0x94D, 0x921 }; // हैलो वर्ल्ड
+  unsigned int utf32_05[] = { 0x1F601, 0x20, 0x1F602, 0x20, 0x1F603, 0x20, 0x1F604 }; // Emojis
+  unsigned int utf32_06[] = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; // Invalid string
 
   const Utf8ToUtf32Data data[] =
   {
 
   const Utf8ToUtf32Data data[] =
   {
@@ -233,22 +299,32 @@ int UtcDaliTextCharacterSetConversionUtf8ToUtf32(void)
       utf32_01,
     },
     {
       utf32_01,
     },
     {
+      "Latin script with 'CR' and 'CR'+'LF'",
+      "\xd Hello\xd\xa World",
+      utf32_02,
+    },
+    {
       "Arabic script",
       "مرحبا بالعالم",
       "Arabic script",
       "مرحبا بالعالم",
-      utf32_02,
+      utf32_03,
     },
     {
       "Devanagari script",
       "हैलो वर्ल्ड",
     },
     {
       "Devanagari script",
       "हैलो वर्ल्ड",
-      utf32_03,
+      utf32_04,
     },
     {
       "Emojis",
       "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
     },
     {
       "Emojis",
       "\xF0\x9F\x98\x81 \xF0\x9F\x98\x82 \xF0\x9F\x98\x83 \xF0\x9F\x98\x84",
-      utf32_04,
+      utf32_05,
+    },
+    {
+      "Invalid text",
+      utf8_06,
+      utf32_06,
     },
   };
     },
   };
-  const unsigned int numberOfTests = 4u;
+  const unsigned int numberOfTests = 6u;
 
   for( unsigned int index = 0u; index < numberOfTests; ++index )
   {
 
   for( unsigned int index = 0u; index < numberOfTests; ++index )
   {