blob: 4f36afe4a5e7ab79d64fc57c343144d5fbbf2343 [file] [log] [blame]
reed@android.comed673312009-02-27 16:24:51 +00001#include "Test.h"
2#include "SkRandom.h"
reed@android.comb00cd722010-04-16 20:35:47 +00003#include "SkRefCnt.h"
reed@android.comed673312009-02-27 16:24:51 +00004#include "SkTSearch.h"
5#include "SkTSort.h"
6#include "SkUtils.h"
7
reed@android.comb00cd722010-04-16 20:35:47 +00008class RefClass : public SkRefCnt {
9public:
10 RefClass(int n) : fN(n) {}
11 int get() const { return fN; }
12
13private:
14 int fN;
15};
16
17static 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);
reed@google.comddbf4c82011-03-08 16:06:06 +000026 REPORTER_ASSERT(reporter, rc0.get() != r0);
reed@android.comb00cd722010-04-16 20:35:47 +000027
28 rc0 = r0;
29 REPORTER_ASSERT(reporter, rc0);
30 REPORTER_ASSERT(reporter, rc0 != rc1);
reed@google.comddbf4c82011-03-08 16:06:06 +000031 REPORTER_ASSERT(reporter, rc0.get() == r0);
reed@android.comb00cd722010-04-16 20:35:47 +000032
33 rc1 = rc0;
34 REPORTER_ASSERT(reporter, rc1);
35 REPORTER_ASSERT(reporter, rc0 == rc1);
reed@google.comddbf4c82011-03-08 16:06:06 +000036 REPORTER_ASSERT(reporter, rc0.get() == r0);
reed@android.comb00cd722010-04-16 20:35:47 +000037
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
reed@google.coma67573e2011-02-25 18:10:29 +000046static 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
epoger@google.combcc56832011-05-20 17:35:46 +000067/////////////////////////////////////////////////////////////////////////////
reed@android.comb00cd722010-04-16 20:35:47 +000068
reed@android.comed673312009-02-27 16:24:51 +000069#define kSEARCH_COUNT 91
70
71static 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
116static void test_utf16(skiatest::Reporter* reporter) {
117 static const SkUnichar gUni[] = {
118 0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234
119 };
reed@android.com80e39a72009-04-02 16:59:40 +0000120
reed@android.comed673312009-02-27 16:24:51 +0000121 uint16_t buf[2];
reed@android.com80e39a72009-04-02 16:59:40 +0000122
reed@android.comed673312009-02-27 16:24:51 +0000123 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
135static 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 }
reed@android.com80e39a72009-04-02 16:59:40 +0000165
reed@android.comed673312009-02-27 16:24:51 +0000166 test_utf16(reporter);
167 test_search(reporter);
reed@android.comb00cd722010-04-16 20:35:47 +0000168 test_refptr(reporter);
reed@google.coma67573e2011-02-25 18:10:29 +0000169 test_autounref(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000170}
171
reed@android.comd8730ea2009-02-27 22:06:06 +0000172#include "TestClassDef.h"
reed@google.coma67573e2011-02-25 18:10:29 +0000173DEFINE_TESTCLASS("Utils", UtfTestClass, TestUTF)