]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/ucdtest.cpp
ICU-511.34.tar.gz
[apple/icu.git] / icuSources / test / intltest / ucdtest.cpp
index 8393b9f447b7a099e27d8f0a7a9faeb3f9f87f80..4ae48b88d1416aa7fa832f4d66883feb1e1a5018 100644 (file)
@@ -1,6 +1,6 @@
 /********************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2010, International Business Machines Corporation and
+ * Copyright (c) 1997-2013, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 
@@ -8,8 +8,10 @@
 #include "unicode/uchar.h"
 #include "unicode/uniset.h"
 #include "unicode/putil.h"
+#include "unicode/uscript.h"
 #include "cstring.h"
 #include "hash.h"
+#include "patternprops.h"
 #include "normalizer2impl.h"
 #include "uparse.h"
 #include "ucdtest.h"
@@ -50,13 +52,16 @@ UnicodeTest::~UnicodeTest()
 
 void UnicodeTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
 {
-    if (exec) logln("TestSuite UnicodeTest: ");
-    switch (index) {
-        case 0: name = "TestAdditionalProperties"; if(exec) TestAdditionalProperties(); break;
-        case 1: name = "TestBinaryValues"; if(exec) TestBinaryValues(); break;
-        case 2: name = "TestConsistency"; if(exec) TestConsistency(); break;
-        default: name = ""; break; //needed to end loop
+    if(exec) {
+        logln("TestSuite UnicodeTest: ");
     }
+    TESTCASE_AUTO_BEGIN;
+    TESTCASE_AUTO(TestAdditionalProperties);
+    TESTCASE_AUTO(TestBinaryValues);
+    TESTCASE_AUTO(TestConsistency);
+    TESTCASE_AUTO(TestPatternProperties);
+    TESTCASE_AUTO(TestScriptMetadata);
+    TESTCASE_AUTO_END;
 }
 
 //====================================================
@@ -337,9 +342,9 @@ void UnicodeTest::TestConsistency() {
      * of the set for the first.
      */
     IcuTestErrorCode errorCode(*this, "TestConsistency");
-    const Normalizer2 *nfd=Normalizer2::getInstance(NULL, "nfc", UNORM2_DECOMPOSE, errorCode);
+    const Normalizer2 *nfd=Normalizer2::getNFDInstance(errorCode);
     const Normalizer2Impl *nfcImpl=Normalizer2Factory::getNFCImpl(errorCode);
-    if(errorCode.isFailure()) {
+    if(!nfcImpl->ensureCanonIterData(errorCode) || errorCode.isFailure()) {
         dataerrln("Normalizer2::getInstance(NFD) or Normalizer2Factory::getNFCImpl() failed - %s\n",
                   errorCode.errorName());
         errorCode.reset();
@@ -369,3 +374,127 @@ void UnicodeTest::TestConsistency() {
     }
 #endif
 }
+
+/**
+ * Test various implementations of Pattern_Syntax & Pattern_White_Space.
+ */
+void UnicodeTest::TestPatternProperties() {
+    IcuTestErrorCode errorCode(*this, "TestPatternProperties()");
+    UnicodeSet syn_pp;
+    UnicodeSet syn_prop(UNICODE_STRING_SIMPLE("[:Pattern_Syntax:]"), errorCode);
+    UnicodeSet syn_list(
+        "[!-/\\:-@\\[-\\^`\\{-~"
+        "\\u00A1-\\u00A7\\u00A9\\u00AB\\u00AC\\u00AE\\u00B0\\u00B1\\u00B6\\u00BB\\u00BF\\u00D7\\u00F7"
+        "\\u2010-\\u2027\\u2030-\\u203E\\u2041-\\u2053\\u2055-\\u205E\\u2190-\\u245F\\u2500-\\u2775"
+        "\\u2794-\\u2BFF\\u2E00-\\u2E7F\\u3001-\\u3003\\u3008-\\u3020\\u3030\\uFD3E\\uFD3F\\uFE45\\uFE46]", errorCode);
+    UnicodeSet ws_pp;
+    UnicodeSet ws_prop(UNICODE_STRING_SIMPLE("[:Pattern_White_Space:]"), errorCode);
+    UnicodeSet ws_list(UNICODE_STRING_SIMPLE("[\\u0009-\\u000D\\ \\u0085\\u200E\\u200F\\u2028\\u2029]"), errorCode);
+    UnicodeSet syn_ws_pp;
+    UnicodeSet syn_ws_prop(syn_prop);
+    syn_ws_prop.addAll(ws_prop);
+    for(UChar32 c=0; c<=0xffff; ++c) {
+        if(PatternProps::isSyntax(c)) {
+            syn_pp.add(c);
+        }
+        if(PatternProps::isWhiteSpace(c)) {
+            ws_pp.add(c);
+        }
+        if(PatternProps::isSyntaxOrWhiteSpace(c)) {
+            syn_ws_pp.add(c);
+        }
+    }
+    compareUSets(syn_pp, syn_prop,
+                 "PatternProps.isSyntax()", "[:Pattern_Syntax:]", TRUE);
+    compareUSets(syn_pp, syn_list,
+                 "PatternProps.isSyntax()", "[Pattern_Syntax ranges]", TRUE);
+    compareUSets(ws_pp, ws_prop,
+                 "PatternProps.isWhiteSpace()", "[:Pattern_White_Space:]", TRUE);
+    compareUSets(ws_pp, ws_list,
+                 "PatternProps.isWhiteSpace()", "[Pattern_White_Space ranges]", TRUE);
+    compareUSets(syn_ws_pp, syn_ws_prop,
+                 "PatternProps.isSyntaxOrWhiteSpace()",
+                 "[[:Pattern_Syntax:][:Pattern_White_Space:]]", TRUE);
+}
+
+// So far only minimal port of Java & cucdtst.c compareUSets().
+UBool
+UnicodeTest::compareUSets(const UnicodeSet &a, const UnicodeSet &b,
+                          const char *a_name, const char *b_name,
+                          UBool diffIsError) {
+    UBool same= a==b;
+    if(!same && diffIsError) {
+        errln("Sets are different: %s vs. %s\n", a_name, b_name);
+    }
+    return same;
+}
+
+namespace {
+
+/**
+ * Maps a special script code to the most common script of its encoded characters.
+ */
+UScriptCode getCharScript(UScriptCode script) {
+    switch(script) {
+    case USCRIPT_SIMPLIFIED_HAN:
+    case USCRIPT_TRADITIONAL_HAN:
+        return USCRIPT_HAN;
+    case USCRIPT_JAPANESE:
+        return USCRIPT_HIRAGANA;
+    case USCRIPT_KOREAN:
+        return USCRIPT_HANGUL;
+    default:
+        return script;
+    }
+}
+
+}  // namespace
+
+void UnicodeTest::TestScriptMetadata() {
+    IcuTestErrorCode errorCode(*this, "TestScriptMetadata()");
+    UnicodeSet rtl("[[:bc=R:][:bc=AL:]-[:Cn:]-[:sc=Common:]]", errorCode);
+    // So far, sample characters are uppercase.
+    // Georgian is special.
+    UnicodeSet cased("[[:Lu:]-[:sc=Common:]-[:sc=Geor:]]", errorCode);
+    for(int32_t sci = 0; sci < USCRIPT_CODE_LIMIT; ++sci) {
+        UScriptCode sc = (UScriptCode)sci;
+        // Run the test with -v to see which script has failures:
+        // .../intltest$ make && ./intltest utility/UnicodeTest/TestScriptMetadata -v | grep -C 3 FAIL
+        logln(uscript_getShortName(sc));
+        UScriptUsage usage = uscript_getUsage(sc);
+        UnicodeString sample = uscript_getSampleUnicodeString(sc);
+        UnicodeSet scriptSet;
+        scriptSet.applyIntPropertyValue(UCHAR_SCRIPT, sc, errorCode);
+        if(usage == USCRIPT_USAGE_NOT_ENCODED) {
+            assertTrue("not encoded, no sample", sample.isEmpty());
+            assertFalse("not encoded, not RTL", uscript_isRightToLeft(sc));
+            assertFalse("not encoded, not LB letters", uscript_breaksBetweenLetters(sc));
+            assertFalse("not encoded, not cased", uscript_isCased(sc));
+            assertTrue("not encoded, no characters", scriptSet.isEmpty());
+        } else {
+            assertFalse("encoded, has a sample character", sample.isEmpty());
+            UChar32 firstChar = sample.char32At(0);
+            UScriptCode charScript = getCharScript(sc);
+            assertEquals("script(sample(script))",
+                         charScript, uscript_getScript(firstChar, errorCode));
+            assertEquals("RTL vs. set", rtl.contains(firstChar), uscript_isRightToLeft(sc));
+            assertEquals("cased vs. set", cased.contains(firstChar), uscript_isCased(sc));
+            assertEquals("encoded, has characters", sc == charScript, !scriptSet.isEmpty());
+            if(uscript_isRightToLeft(sc)) {
+                rtl.removeAll(scriptSet);
+            }
+            if(uscript_isCased(sc)) {
+                cased.removeAll(scriptSet);
+            }
+        }
+    }
+    UnicodeString pattern;
+    assertEquals("no remaining RTL characters",
+                 UnicodeString("[]"), rtl.toPattern(pattern));
+    assertEquals("no remaining cased characters",
+                 UnicodeString("[]"), cased.toPattern(pattern));
+
+    assertTrue("Hani breaks between letters", uscript_breaksBetweenLetters(USCRIPT_HAN));
+    assertTrue("Thai breaks between letters", uscript_breaksBetweenLetters(USCRIPT_THAI));
+    assertFalse("Latn does not break between letters", uscript_breaksBetweenLetters(USCRIPT_LATIN));
+}