Merge "Klockwork: remove unreachable code, check iterators" into tizen
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Text.cpp
index 595e5bf..e9ebb97 100644 (file)
@@ -41,11 +41,31 @@ int UtcDaliTextConstructor(void)
 
   DALI_TEST_CHECK( text.IsEmpty() );
 
-  Text someText( std::string( "Some text" ) );
+  Text someEmptyText1( "" );
 
-  DALI_TEST_CHECK( !someText.IsEmpty() );
+  DALI_TEST_CHECK( someEmptyText1.IsEmpty() );
 
-  Character c = someText[0];
+  Text someEmptyText2( std::string( "" ) );
+
+  DALI_TEST_CHECK( someEmptyText2.IsEmpty() );
+
+  Text someEmptyText3( text );
+
+  DALI_TEST_CHECK( someEmptyText3.IsEmpty() );
+
+  Text someText1( "Some text" );
+
+  DALI_TEST_CHECK( !someText1.IsEmpty() );
+
+  Text someText2( std::string( "Some text" ) );
+
+  DALI_TEST_CHECK( !someText2.IsEmpty() );
+
+  Text someText3( Text( std::string( "Some text" ) ) );
+
+  DALI_TEST_CHECK( !someText3.IsEmpty() );
+
+  Character c = someText1[0];
 
   Text cText( c );
 
@@ -86,7 +106,6 @@ int UtcDaliTextAssignmentOperator(void)
 
   Text someText4;
 
-  printf(" is text empty ? ...... %d \n", someText4.IsEmpty());
   // check for assignment of empty text
   someText2 = someText4;
   DALI_TEST_CHECK( someText2.IsEmpty() );
@@ -103,22 +122,28 @@ int UtcDaliTextSetGetText(void)
 {
   TestApplication application;
 
-  const std::string someText( "Some text");
+  const char* someText = "Some text";
+  const std::string someText2( "Some text2" );
 
   Text text;
   text.SetText( someText );
 
-  DALI_TEST_CHECK( someText == text.GetText() );
+  DALI_TEST_EQUALS( someText, text.GetText(), TEST_LOCATION );
+
+  text.SetText( someText2 );
+
+  DALI_TEST_EQUALS( someText2, text.GetText(), TEST_LOCATION );
 
   Character c = text[0];
   text.SetText( c );
 
-  DALI_TEST_CHECK( std::string("S") == text.GetText() );
+  DALI_TEST_EQUALS( std::string( "S" ), text.GetText(), TEST_LOCATION );
 
   Text text2;
   text2.SetText( text );
 
-  DALI_TEST_CHECK( text2.GetText() == text.GetText() );
+  DALI_TEST_EQUALS( text2.GetText(), text.GetText(), TEST_LOCATION );
+
   END_TEST;
 }
 
@@ -155,7 +180,7 @@ int UtcDaliTextAccessOperator01(void)
   catch( DaliException& e )
   {
     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
-    DALI_TEST_EQUALS( e.mCondition, "position < mString.size() && \"Text::operator[]: Character position is out of bounds\"", TEST_LOCATION );
+    DALI_TEST_EQUALS( e.mCondition, "position < mString.Count() && \"Text::operator[]: Character position is out of bounds\"", TEST_LOCATION );
 
     assert2 = true;
   }
@@ -221,19 +246,23 @@ int UtcDaliTextAppend(void)
 
   Text text( std::string( "Some text") );
 
-  text.Append( std::string( "A" ) );
+  text.Append( "A" );
 
   DALI_TEST_CHECK( std::string( "Some textA" ) == text.GetText() );
 
+  text.Append( std::string( "B" ) );
+
+  DALI_TEST_CHECK( std::string( "Some textAB" ) == text.GetText() );
+
   Character c = text[0];
   text.Append( c );
 
-  DALI_TEST_CHECK( std::string( "Some textAS" ) == text.GetText() );
+  DALI_TEST_CHECK( std::string( "Some textABS" ) == text.GetText() );
 
-  Text text2( std::string("B") );
+  Text text2( std::string("C") );
   text.Append( text2 );
 
-  DALI_TEST_CHECK( std::string( "Some textASB" ) == text.GetText() );
+  DALI_TEST_CHECK( std::string( "Some textABSC" ) == text.GetText() );
 
   // append to a null text
 
@@ -241,6 +270,12 @@ int UtcDaliTextAppend(void)
   emptyText.Append( text2 );
   DALI_TEST_CHECK( text2.GetText() == emptyText.GetText() );
 
+  // append a null text
+
+  Text emptyText2;
+  emptyText.Append( emptyText2 );
+  DALI_TEST_CHECK( text2.GetText() == emptyText.GetText() );
+
   END_TEST;
 }
 
@@ -252,6 +287,8 @@ int UtcDaliTextRemove01(void)
 
   bool assert1 = false;
   bool assert2 = false;
+  bool assert3 = false;
+
   try
   {
     text.Remove( 100, 3 );
@@ -259,7 +296,7 @@ int UtcDaliTextRemove01(void)
   catch( DaliException& e )
   {
     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
-    DALI_TEST_EQUALS( e.mCondition, "position < mString.size() && \"Text::Remove: Character position is out of bounds\"", TEST_LOCATION );
+    DALI_TEST_EQUALS( e.mCondition, "position < mString.Count() && \"Text::Remove: Character position is out of bounds\"", TEST_LOCATION );
     assert1 = true;
   }
 
@@ -270,11 +307,23 @@ int UtcDaliTextRemove01(void)
   catch( DaliException& e )
   {
     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
-    DALI_TEST_EQUALS( e.mCondition, "position + numberOfCharacters <= mString.size() && \"Text::Remove: Character position + numberOfCharacters is out of bounds\"", TEST_LOCATION );
+    DALI_TEST_EQUALS( e.mCondition, "position + numberOfCharacters <= mString.Count() && \"Text::Remove: Character position + numberOfCharacters is out of bounds\"", TEST_LOCATION );
     assert2 = true;
   }
 
-  if( assert1 && assert2 )
+  try
+  {
+    text.SetText( std::string( "" ) );
+    text.Remove( 1, 300 );
+  }
+  catch( DaliException& e )
+  {
+    tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
+    DALI_TEST_EQUALS( e.mCondition, "NULL != mImpl && \"Text::Remove: Text is uninitialized\"", TEST_LOCATION );
+    assert3 = true;
+  }
+
+  if( assert1 && assert2 && assert3 )
   {
     tet_result( TET_PASS );
   }
@@ -291,16 +340,164 @@ int UtcDaliTextRemove02(void)
 
   Text text01( std::string( "Some text") );
   Text text02( std::string( "Somext") );
-  Text text03( std::string( "") );
+  Text text03( std::string( "" ) );
+  Text text04( std::string( "Hello world" ) );
+  Text text05( std::string( "world" ) );
 
-  text01.Remove( 3, 3 );
+  text01.Remove( 3u, 3u );
 
   DALI_TEST_EQUALS( text01.GetLength(), text02.GetLength(), TEST_LOCATION );
   DALI_TEST_EQUALS( text01.GetText(), text02.GetText(), TEST_LOCATION );
 
-  text01.Remove( 0, 6 );
+  text01.Remove( 0u, 0u );
+
+  DALI_TEST_EQUALS( text01.GetLength(), text02.GetLength(), TEST_LOCATION );
+  DALI_TEST_EQUALS( text01.GetText(), text02.GetText(), TEST_LOCATION );
+
+  text01.Remove( 0u, 6u );
 
   DALI_TEST_EQUALS( text01.GetLength(), text03.GetLength(), TEST_LOCATION );
   DALI_TEST_EQUALS( text01.GetText(), text03.GetText(), TEST_LOCATION );
+
+  text04.Remove( 0u, 6u );
+
+  DALI_TEST_EQUALS( text04.GetLength(), text05.GetLength(), TEST_LOCATION );
+  DALI_TEST_EQUALS( text04.GetText(), text05.GetText(), TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliTextFind(void)
+{
+  TestApplication application;
+
+  Text text;
+  Character c1 = Text( std::string( "c" ) )[0u];
+  Character c2 = Text( std::string( "o" ) )[0u];
+  Vector<std::size_t> positions;
+
+  // Find in void text.
+
+  positions.Clear();
+  text.Find( c1, 0u, 0u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
+
+  positions.Clear();
+  text.Find( Text::WHITE_SPACE, 0u, 0u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
+
+  positions.Clear();
+  text.Find( Text::NEW_LINE, 0u, 0u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
+
+  // Find in text.
+
+  // Find 'c' and 'o'
+  text.SetText( std::string( "Hello world" ) );
+
+  positions.Clear();
+  text.Find( c1, 0u, 11u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
+
+  positions.Clear();
+  text.Find( c2, 0u, 11u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 2u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[0u], 4u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[1u], 7u, TEST_LOCATION );
+
+  // Find white space
+  text.SetText( std::string( "   Hello  world  \n" ) );
+
+  positions.Clear();
+  text.Find( Text::WHITE_SPACE, 0u, 17u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 8u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[0u], 0u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[1u], 1u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[2u], 2u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[3u], 8u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[4u], 9u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[5u], 15u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[6u], 16u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[7u], 17u, TEST_LOCATION );
+
+  // Find new line character
+  text.SetText( std::string( "\n\nHello\nworld\n\n" ) );
+
+  positions.Clear();
+  text.Find( Text::NEW_LINE, 0u, 14u, positions );
+  DALI_TEST_EQUALS( positions.Count(), 5u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[0u], 0u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[1u], 1u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[2u], 7u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[3u], 13u, TEST_LOCATION );
+  DALI_TEST_EQUALS( positions[4u], 14u, TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliTextGetSubText(void)
+{
+  TestApplication application;
+
+  Text text;
+  Text subText;
+
+  // Get sub-text from a void text.
+
+  subText.SetText( "Hello" );
+  text.GetSubText( 0u, 1u, subText );
+  DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
+
+  // Get sub-text.
+
+  text.SetText( std::string( "Hello world" ) );
+
+  // Access out of bounds
+  subText.SetText( "Hello" );
+  text.GetSubText( 30u, 31u, subText );
+  DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
+  text.GetSubText( 0u, 31u, subText );
+  DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
+  text.GetSubText( 30u, 1u, subText );
+  DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
+
+  // Check it swaps the indices.
+  text.GetSubText( 8u, 2u, subText );
+  DALI_TEST_EQUALS( subText.GetText(), std::string( "row oll" ), TEST_LOCATION );
+
+  // Normal access.
+  subText.SetText( std::string( "" ) );
+  text.GetSubText( 4u, 6u, subText );
+  DALI_TEST_EQUALS( subText.GetText(), std::string( "o w" ), TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliTextIsWhiteSpaceNewLine(void)
+{
+  TestApplication application;
+
+  Text text;
+
+  // Query a void text.
+
+  DALI_TEST_CHECK( !text.IsWhiteSpace( 0u ) );
+  DALI_TEST_CHECK( !text.IsNewLine( 0u ) );
+
+  // Set a text
+  text.SetText( "Hello world\n" );
+
+  // Query out of bounds
+
+  DALI_TEST_CHECK( !text.IsWhiteSpace( 30u ) );
+  DALI_TEST_CHECK( !text.IsNewLine( 30u ) );
+
+  // Normal query.
+
+  DALI_TEST_CHECK( !text.IsWhiteSpace( 1u ) );
+  DALI_TEST_CHECK( !text.IsNewLine( 1u ) );
+  DALI_TEST_CHECK( text.IsWhiteSpace( 5u ) );
+  DALI_TEST_CHECK( text.IsNewLine( 11u ) );
+
   END_TEST;
 }