]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/cintltst/usettest.c
ICU-66108.tar.gz
[apple/icu.git] / icuSources / test / cintltst / usettest.c
index d0d5656ebfa545e2513910a71a4810d267d1f455..d6569642c9edbd2ec2341095d5b3854a3cf3ed7d 100644 (file)
@@ -1,17 +1,18 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 **********************************************************************
-* Copyright (c) 2002-2005, International Business Machines
+* Copyright (c) 2002-2016, International Business Machines
 * Corporation and others.  All Rights Reserved.
 **********************************************************************
 */
 #include "unicode/uset.h"
 #include "unicode/ustring.h"
 #include "cintltst.h"
+#include "cmemory.h"
 #include <stdlib.h>
 #include <string.h>
 
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
 #define TEST(x) addTest(root, &x, "uset/" # x)
 
 static void TestAPI(void);
@@ -19,6 +20,8 @@ static void Testj2269(void);
 static void TestSerialized(void);
 static void TestNonInvariantPattern(void);
 static void TestBadPattern(void);
+static void TestFreezable(void);
+static void TestSpan(void);
 
 void addUSetTest(TestNode** root);
 
@@ -40,6 +43,8 @@ addUSetTest(TestNode** root) {
     TEST(TestSerialized);
     TEST(TestNonInvariantPattern);
     TEST(TestBadPattern);
+    TEST(TestFreezable);
+    TEST(TestSpan);
 }
 
 /*------------------------------------------------------------------
@@ -57,19 +62,19 @@ static void Testj2269() {
 }
 
 static const UChar PAT[] = {91,97,45,99,123,97,98,125,93,0}; /* "[a-c{ab}]" */
-static const int32_t PAT_LEN = (sizeof(PAT) / sizeof(PAT[0])) - 1;
+static const int32_t PAT_LEN = UPRV_LENGTHOF(PAT) - 1;
 
 static const UChar PAT_lb[] = {0x6C, 0x62, 0}; /* "lb" */
-static const int32_t PAT_lb_LEN = (sizeof(PAT_lb) / sizeof(PAT_lb[0])) - 1;
+static const int32_t PAT_lb_LEN = UPRV_LENGTHOF(PAT_lb) - 1;
 
 static const UChar VAL_SP[] = {0x53, 0x50, 0}; /* "SP" */
-static const int32_t VAL_SP_LEN = (sizeof(VAL_SP) / sizeof(VAL_SP[0])) - 1;
+static const int32_t VAL_SP_LEN = UPRV_LENGTHOF(VAL_SP) - 1;
 
 static const UChar STR_bc[] = {98,99,0}; /* "bc" */
-static const int32_t STR_bc_LEN = (sizeof(STR_bc) / sizeof(STR_bc[0])) - 1;
+static const int32_t STR_bc_LEN = UPRV_LENGTHOF(STR_bc) - 1;
 
 static const UChar STR_ab[] = {97,98,0}; /* "ab" */
-static const int32_t STR_ab_LEN = (sizeof(STR_ab) / sizeof(STR_ab[0])) - 1;
+static const int32_t STR_ab_LEN = UPRV_LENGTHOF(STR_ab) - 1;
 
 /**
  * Basic API test for uset.x
@@ -80,6 +85,14 @@ static void TestAPI() {
     UErrorCode ec;
     
     /* [] */
+    set = uset_openEmpty();
+    expect(set, "", "abc{ab}", NULL);
+    uset_close(set);
+
+    set = uset_open(1, 0);
+    expect(set, "", "abc{ab}", NULL);
+    uset_close(set);
+
     set = uset_open(1, 1);
     uset_clear(set);
     expect(set, "", "abc{ab}", NULL);
@@ -274,7 +287,7 @@ static void expectContainment(const USet* set,
                             (isIn ? "contains" : "does not contain"),
                             strCopy);
             } else {
-                log_err("FAIL: %s %s \"%s\"\n", pat,
+                log_data_err("FAIL: %s %s \"%s\" (Are you missing data?)\n", pat,
                         (isIn ? "does not contain" : "contains"),
                         strCopy);
             }
@@ -291,7 +304,7 @@ static void expectContainment(const USet* set,
                             (isIn ? "contains" : "does not contain"),
                             *p);
             } else {
-                log_err("FAIL: %s %s '%c'\n", pat,
+                log_data_err("FAIL: %s %s '%c' (Are you missing data?)\n", pat,
                         (isIn ? "does not contain" : "contains"),
                         *p);
             }
@@ -306,7 +319,7 @@ static void expectContainment(const USet* set,
                                     (isIn ? "contains" : "does not contain"),
                                     rangeStart, rangeEnd);
                     } else {
-                        log_err("FAIL: %s %s U+%04X-U+%04X\n", pat,
+                        log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat,
                                 (isIn ? "does not contain" : "contains"),
                                 rangeStart, rangeEnd);
                     }
@@ -324,7 +337,7 @@ static void expectContainment(const USet* set,
                         (isIn ? "contains" : "does not contain"),
                         rangeStart, rangeEnd);
         } else {
-            log_err("FAIL: %s %s U+%04X-U+%04X\n", pat,
+            log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat,
                     (isIn ? "does not contain" : "contains"),
                     rangeStart, rangeEnd);
         }
@@ -362,7 +375,7 @@ static void expectItems(const USet* set,
     pat=aescstrdup(ustr, length);
 
     if (uset_isEmpty(set) != (strlen(items)==0)) {
-        log_err("FAIL: %s should return %s from isEmpty\n",
+        log_data_err("FAIL: %s should return %s from isEmpty (Are you missing data?)\n",
                 pat,
                 strlen(items)==0 ? "TRUE" : "FALSE");
     }
@@ -379,7 +392,7 @@ static void expectItems(const USet* set,
         if (start > end || start == -1) {
             /* Fetch our next item */
             if (itemIndex >= itemCount) {
-                log_err("FAIL: ran out of items iterating %s\n", pat);
+                log_data_err("FAIL: ran out of items iterating %s (Are you missing data?)\n", pat);
                 return;
             }
 
@@ -476,11 +489,11 @@ TestSerialized() {
     errorCode=U_ZERO_ERROR;
     set=uset_openPattern(pattern, -1, &errorCode);
     if(U_FAILURE(errorCode)) {
-        log_err("uset_openPattern([:Cf:]) failed - %s\n", u_errorName(errorCode));
+        log_data_err("uset_openPattern([:Cf:]) failed - %s (Are you missing data?)\n", u_errorName(errorCode));
         return;
     }
 
-    length=uset_serialize(set, buffer, LENGTHOF(buffer), &errorCode);
+    length=uset_serialize(set, buffer, UPRV_LENGTHOF(buffer), &errorCode);
     if(U_FAILURE(errorCode)) {
         log_err("unable to uset_serialize([:Cf:]) - %s\n", u_errorName(errorCode));
         uset_close(set);
@@ -529,4 +542,114 @@ static void TestBadPattern(void) {
     }
 }
 
+static USet *openIDSet() {
+    UErrorCode errorCode = U_ZERO_ERROR;
+    U_STRING_DECL(pattern, "[:ID_Continue:]", 15);
+    U_STRING_INIT(pattern, "[:ID_Continue:]", 15);
+    return uset_openPattern(pattern, 15, &errorCode);
+}
+
+static void TestFreezable() {
+    USet *idSet;
+    USet *frozen;
+    USet *thawed;
+
+    idSet=openIDSet();
+
+    if (idSet == NULL) {
+        log_data_err("openIDSet() returned NULL. (Are you missing data?)\n");
+        uset_close(idSet);
+        return;
+    }
+
+    frozen=uset_clone(idSet);
+
+    if (frozen == NULL) {
+        log_err("uset_Clone() returned NULL\n");
+        return;
+    }
+
+    if(!uset_equals(frozen, idSet)) {
+        log_err("uset_clone() did not make an equal copy\n");
+    }
+
+    uset_freeze(frozen);
+    uset_addRange(frozen, 0xd802, 0xd805);
+
+    if(uset_isFrozen(idSet) || !uset_isFrozen(frozen) || !uset_equals(frozen, idSet)) {
+        log_err("uset_freeze() or uset_isFrozen() does not work\n");
+    }
+
+    thawed=uset_cloneAsThawed(frozen);
+
+    if (thawed == NULL) {
+        log_err("uset_cloneAsThawed(frozen) returned NULL");
+        uset_close(frozen);
+        uset_close(idSet);
+        return;
+    }
+
+    uset_addRange(thawed, 0xd802, 0xd805);
+
+    if(uset_isFrozen(thawed) || uset_equals(thawed, idSet) || !uset_containsRange(thawed, 0xd802, 0xd805)) {
+        log_err("uset_cloneAsThawed() does not work\n");
+    }
+
+    uset_close(idSet);
+    uset_close(frozen);
+    uset_close(thawed);
+}
+
+static void TestSpan() {
+    static const UChar s16[2]={ 0xe01, 0x3000 };
+    static const char* s8="\xE0\xB8\x81\xE3\x80\x80";
+
+    USet *idSet=openIDSet();
+
+    if (idSet == NULL) {
+        log_data_err("openIDSet() returned NULL (Are you missing data?)\n");
+        return;
+    }
+
+    if(
+        1!=uset_span(idSet, s16, 2, USET_SPAN_CONTAINED) ||
+        0!=uset_span(idSet, s16, 2, USET_SPAN_NOT_CONTAINED) ||
+        2!=uset_spanBack(idSet, s16, 2, USET_SPAN_CONTAINED) ||
+        1!=uset_spanBack(idSet, s16, 2, USET_SPAN_NOT_CONTAINED)
+    ) {
+        log_err("uset_span() or uset_spanBack() does not work\n");
+    }
+
+    if(
+        3!=uset_spanUTF8(idSet, s8, 6, USET_SPAN_CONTAINED) ||
+        0!=uset_spanUTF8(idSet, s8, 6, USET_SPAN_NOT_CONTAINED) ||
+        6!=uset_spanBackUTF8(idSet, s8, 6, USET_SPAN_CONTAINED) ||
+        3!=uset_spanBackUTF8(idSet, s8, 6, USET_SPAN_NOT_CONTAINED)
+    ) {
+        log_err("uset_spanUTF8() or uset_spanBackUTF8() does not work\n");
+    }
+
+    uset_freeze(idSet);
+
+    if(
+        1!=uset_span(idSet, s16, 2, USET_SPAN_CONTAINED) ||
+        0!=uset_span(idSet, s16, 2, USET_SPAN_NOT_CONTAINED) ||
+        2!=uset_spanBack(idSet, s16, 2, USET_SPAN_CONTAINED) ||
+        1!=uset_spanBack(idSet, s16, 2, USET_SPAN_NOT_CONTAINED)
+    ) {
+        log_err("uset_span(frozen) or uset_spanBack(frozen) does not work\n");
+    }
+
+    if(
+        3!=uset_spanUTF8(idSet, s8, 6, USET_SPAN_CONTAINED) ||
+        0!=uset_spanUTF8(idSet, s8, 6, USET_SPAN_NOT_CONTAINED) ||
+        6!=uset_spanBackUTF8(idSet, s8, 6, USET_SPAN_CONTAINED) ||
+        3!=uset_spanBackUTF8(idSet, s8, 6, USET_SPAN_NOT_CONTAINED)
+    ) {
+        log_err("uset_spanUTF8(frozen) or uset_spanBackUTF8(frozen) does not work\n");
+    }
+
+    uset_close(idSet);
+}
+
 /*eof*/