• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "Test.h"
2 #include "SkRandom.h"
3 #include "SkRefCnt.h"
4 #include "SkTSearch.h"
5 #include "SkTSort.h"
6 #include "SkUtils.h"
7 
8 class RefClass : public SkRefCnt {
9 public:
RefClass(int n)10     RefClass(int n) : fN(n) {}
get() const11     int get() const { return fN; }
12 
13 private:
14     int fN;
15 };
16 
test_refptr(skiatest::Reporter * reporter)17 static void test_refptr(skiatest::Reporter* reporter) {
18     RefClass* r0 = new RefClass(0);
19 
20     SkRefPtr<RefClass> rc0;
21     REPORTER_ASSERT(reporter, rc0.get() == NULL);
22     REPORTER_ASSERT(reporter, !rc0);
23 
24     SkRefPtr<RefClass> rc1;
25     REPORTER_ASSERT(reporter, rc0 == rc1);
26     REPORTER_ASSERT(reporter, rc0.get() != r0);
27 
28     rc0 = r0;
29     REPORTER_ASSERT(reporter, rc0);
30     REPORTER_ASSERT(reporter, rc0 != rc1);
31     REPORTER_ASSERT(reporter, rc0.get() == r0);
32 
33     rc1 = rc0;
34     REPORTER_ASSERT(reporter, rc1);
35     REPORTER_ASSERT(reporter, rc0 == rc1);
36     REPORTER_ASSERT(reporter, rc0.get() == r0);
37 
38     rc0 = NULL;
39     REPORTER_ASSERT(reporter, rc0.get() == NULL);
40     REPORTER_ASSERT(reporter, !rc0);
41     REPORTER_ASSERT(reporter, rc0 != rc1);
42 
43     r0->unref();
44 }
45 
test_autounref(skiatest::Reporter * reporter)46 static void test_autounref(skiatest::Reporter* reporter) {
47     RefClass obj(0);
48     REPORTER_ASSERT(reporter, 1 == obj.getRefCnt());
49 
50     SkAutoTUnref<RefClass> tmp(&obj);
51     REPORTER_ASSERT(reporter, &obj == tmp.get());
52     REPORTER_ASSERT(reporter, 1 == obj.getRefCnt());
53 
54     REPORTER_ASSERT(reporter, &obj == tmp.detach());
55     REPORTER_ASSERT(reporter, 1 == obj.getRefCnt());
56     REPORTER_ASSERT(reporter, NULL == tmp.detach());
57     REPORTER_ASSERT(reporter, NULL == tmp.get());
58 
59     obj.ref();
60     REPORTER_ASSERT(reporter, 2 == obj.getRefCnt());
61     {
62         SkAutoTUnref<RefClass> tmp2(&obj);
63     }
64     REPORTER_ASSERT(reporter, 1 == obj.getRefCnt());
65 }
66 
67 /////////////////////////////////////////////////////////////////////////////
68 
69 #define kSEARCH_COUNT   91
70 
test_search(skiatest::Reporter * reporter)71 static void test_search(skiatest::Reporter* reporter) {
72     int         i, array[kSEARCH_COUNT];
73     SkRandom    rand;
74 
75     for (i = 0; i < kSEARCH_COUNT; i++) {
76         array[i] = rand.nextS();
77     }
78 
79     SkTHeapSort<int>(array, kSEARCH_COUNT);
80     // make sure we got sorted properly
81     for (i = 1; i < kSEARCH_COUNT; i++) {
82         REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
83     }
84 
85     // make sure we can find all of our values
86     for (i = 0; i < kSEARCH_COUNT; i++) {
87         int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
88         REPORTER_ASSERT(reporter, index == i);
89     }
90 
91     // make sure that random values are either found, or the correct
92     // insertion index is returned
93     for (i = 0; i < 10000; i++) {
94         int value = rand.nextS();
95         int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
96 
97         if (index >= 0) {
98             REPORTER_ASSERT(reporter,
99                             index < kSEARCH_COUNT && array[index] == value);
100         } else {
101             index = ~index;
102             REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
103             if (index < kSEARCH_COUNT) {
104                 REPORTER_ASSERT(reporter, value < array[index]);
105                 if (index > 0) {
106                     REPORTER_ASSERT(reporter, value > array[index - 1]);
107                 }
108             } else {
109                 // we should append the new value
110                 REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
111             }
112         }
113     }
114 }
115 
test_utf16(skiatest::Reporter * reporter)116 static void test_utf16(skiatest::Reporter* reporter) {
117     static const SkUnichar gUni[] = {
118         0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234
119     };
120 
121     uint16_t buf[2];
122 
123     for (size_t i = 0; i < SK_ARRAY_COUNT(gUni); i++) {
124         size_t count = SkUTF16_FromUnichar(gUni[i], buf);
125         REPORTER_ASSERT(reporter, count == 2);
126         size_t count2 = SkUTF16_CountUnichars(buf, 2);
127         REPORTER_ASSERT(reporter, count2 == 1);
128         const uint16_t* ptr = buf;
129         SkUnichar c = SkUTF16_NextUnichar(&ptr);
130         REPORTER_ASSERT(reporter, c == gUni[i]);
131         REPORTER_ASSERT(reporter, ptr - buf == 2);
132     }
133 }
134 
TestUTF(skiatest::Reporter * reporter)135 static void TestUTF(skiatest::Reporter* reporter) {
136     static const struct {
137         const char* fUtf8;
138         SkUnichar   fUni;
139     } gTest[] = {
140         { "a",                  'a' },
141         { "\x7f",               0x7f },
142         { "\xC2\x80",           0x80 },
143         { "\xC3\x83",           (3 << 6) | 3    },
144         { "\xDF\xBF",           0x7ff },
145         { "\xE0\xA0\x80",       0x800 },
146         { "\xE0\xB0\xB8",       0xC38 },
147         { "\xE3\x83\x83",       (3 << 12) | (3 << 6) | 3    },
148         { "\xEF\xBF\xBF",       0xFFFF },
149         { "\xF0\x90\x80\x80",   0x10000 },
150         { "\xF3\x83\x83\x83",   (3 << 18) | (3 << 12) | (3 << 6) | 3    }
151     };
152 
153     for (size_t i = 0; i < SK_ARRAY_COUNT(gTest); i++) {
154         const char* p = gTest[i].fUtf8;
155         int         n = SkUTF8_CountUnichars(p);
156         SkUnichar   u0 = SkUTF8_ToUnichar(gTest[i].fUtf8);
157         SkUnichar   u1 = SkUTF8_NextUnichar(&p);
158 
159         REPORTER_ASSERT(reporter, n == 1);
160         REPORTER_ASSERT(reporter, u0 == u1);
161         REPORTER_ASSERT(reporter, u0 == gTest[i].fUni);
162         REPORTER_ASSERT(reporter,
163                         p - gTest[i].fUtf8 == (int)strlen(gTest[i].fUtf8));
164     }
165 
166     test_utf16(reporter);
167     test_search(reporter);
168     test_refptr(reporter);
169     test_autounref(reporter);
170 }
171 
172 #include "TestClassDef.h"
173 DEFINE_TESTCLASS("Utils", UtfTestClass, TestUTF)
174