ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / strtest.cpp
index 0ec61eba8fdf1a32276973c0f27f761f761ec1aa..ccc618a55444ad6fd72b046153b46b42053c864b 100644 (file)
@@ -1,10 +1,12 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /********************************************************************
  * COPYRIGHT: 
  * Copyright (c) 1997-2015, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 /*   file name:  strtest.cpp
-*   encoding:   US-ASCII
+*   encoding:   UTF-8
 *   tab size:   8 (not used)
 *   indentation:4
 *
@@ -20,6 +22,8 @@
 #include "unicode/stringpiece.h"
 #include "unicode/unistr.h"
 #include "unicode/ustring.h"
+#include "unicode/utf_old.h"    // for UTF8_COUNT_TRAIL_BYTES
+#include "unicode/utf8.h"
 #include "charstr.h"
 #include "cstr.h"
 #include "intltest.h"
@@ -146,8 +150,18 @@ StringTest::Test_UTF8_COUNT_TRAIL_BYTES() {
         || UTF8_COUNT_TRAIL_BYTES(0xF0) != 3)
     {
         errln("Test_UTF8_COUNT_TRAIL_BYTES: UTF8_COUNT_TRAIL_BYTES does not work right! "
-              "See utf8.h.");
-    }
+              "See utf_old.h.");
+    }
+       // Note: U8_COUNT_TRAIL_BYTES (current) and UTF8_COUNT_TRAIL_BYTES (deprecated)
+       //       have completely different implementations.
+       if (U8_COUNT_TRAIL_BYTES(0x7F) != 0
+               || U8_COUNT_TRAIL_BYTES(0xC0) != 1
+               || U8_COUNT_TRAIL_BYTES(0xE0) != 2
+               || U8_COUNT_TRAIL_BYTES(0xF0) != 3)
+       {
+               errln("Test_UTF8_COUNT_TRAIL_BYTES: U8_COUNT_TRAIL_BYTES does not work right! "
+                       "See utf8.h.");
+       }
 }
 
 void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
@@ -170,6 +184,7 @@ void StringTest::runIndexedTest(int32_t index, UBool exec, const char *&name, ch
     TESTCASE_AUTO(TestStringByteSink);
     TESTCASE_AUTO(TestCharString);
     TESTCASE_AUTO(TestCStr);
+    TESTCASE_AUTO(Testctou);
     TESTCASE_AUTO_END;
 }
 
@@ -197,14 +212,12 @@ StringTest::TestStringPiece() {
     if(abcd.empty() || abcd.data()!=abcdefg_chars || abcd.length()!=4 || abcd.size()!=4) {
         errln("StringPiece(abcdefg_chars, 4) failed");
     }
-#if U_HAVE_STD_STRING
     // Construct from std::string.
     std::string uvwxyz_string("uvwxyz");
     StringPiece uvwxyz(uvwxyz_string);
     if(uvwxyz.empty() || uvwxyz.data()!=uvwxyz_string.data() || uvwxyz.length()!=6 || uvwxyz.size()!=6) {
         errln("StringPiece(uvwxyz_string) failed");
     }
-#endif
     // Substring constructor with pos.
     StringPiece sp(abcd, -1);
     if(sp.empty() || sp.data()!=abcdefg_chars || sp.length()!=4 || sp.size()!=4) {
@@ -448,7 +461,6 @@ StringTest::TestCheckedArrayByteSink() {
 
 void
 StringTest::TestStringByteSink() {
-#if U_HAVE_STD_STRING
     // Not much to test because only the constructor and Append()
     // are implemented, and trivially so.
     std::string result("abc");  // std::string
@@ -457,7 +469,6 @@ StringTest::TestStringByteSink() {
     if(result != "abcdef") {
         errln("StringByteSink did not Append() as expected");
     }
-#endif
 }
 
 #if defined(_MSC_VER)
@@ -543,3 +554,11 @@ StringTest::TestCStr() {
         errln("%s:%d CStr(s)() failed. Expected \"%s\", got \"%s\"", __FILE__, __LINE__, cs, CStr(us)());
     }
 }
+
+void
+StringTest::Testctou() {
+  const char *cs = "Fa\\u0127mu";
+  UnicodeString u = ctou(cs);
+  assertEquals("Testing unescape@0", (int32_t)0x0046, u.charAt(0));
+  assertEquals("Testing unescape@2", (int32_t)295, u.charAt(2));
+}