blob: 8ec063e77dfa90f147cfd28fd89bf4e118d7bc8c [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);
26 REPORTER_ASSERT(reporter, rc0 != r0);
27
28 rc0 = r0;
29 REPORTER_ASSERT(reporter, rc0);
30 REPORTER_ASSERT(reporter, rc0 != rc1);
31 REPORTER_ASSERT(reporter, rc0 == r0);
32
33 rc1 = rc0;
34 REPORTER_ASSERT(reporter, rc1);
35 REPORTER_ASSERT(reporter, rc0 == rc1);
36 REPORTER_ASSERT(reporter, rc0 == 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
46///////////////////////////////////////////////////////////////////////////////
47
reed@android.comed673312009-02-27 16:24:51 +000048#define kSEARCH_COUNT 91
49
50static void test_search(skiatest::Reporter* reporter) {
51 int i, array[kSEARCH_COUNT];
52 SkRandom rand;
53
54 for (i = 0; i < kSEARCH_COUNT; i++) {
55 array[i] = rand.nextS();
56 }
57
58 SkTHeapSort<int>(array, kSEARCH_COUNT);
59 // make sure we got sorted properly
60 for (i = 1; i < kSEARCH_COUNT; i++) {
61 REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
62 }
63
64 // make sure we can find all of our values
65 for (i = 0; i < kSEARCH_COUNT; i++) {
66 int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
67 REPORTER_ASSERT(reporter, index == i);
68 }
69
70 // make sure that random values are either found, or the correct
71 // insertion index is returned
72 for (i = 0; i < 10000; i++) {
73 int value = rand.nextS();
74 int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
75
76 if (index >= 0) {
77 REPORTER_ASSERT(reporter,
78 index < kSEARCH_COUNT && array[index] == value);
79 } else {
80 index = ~index;
81 REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
82 if (index < kSEARCH_COUNT) {
83 REPORTER_ASSERT(reporter, value < array[index]);
84 if (index > 0) {
85 REPORTER_ASSERT(reporter, value > array[index - 1]);
86 }
87 } else {
88 // we should append the new value
89 REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
90 }
91 }
92 }
93}
94
95static void test_utf16(skiatest::Reporter* reporter) {
96 static const SkUnichar gUni[] = {
97 0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234
98 };
reed@android.com80e39a72009-04-02 16:59:40 +000099
reed@android.comed673312009-02-27 16:24:51 +0000100 uint16_t buf[2];
reed@android.com80e39a72009-04-02 16:59:40 +0000101
reed@android.comed673312009-02-27 16:24:51 +0000102 for (size_t i = 0; i < SK_ARRAY_COUNT(gUni); i++) {
103 size_t count = SkUTF16_FromUnichar(gUni[i], buf);
104 REPORTER_ASSERT(reporter, count == 2);
105 size_t count2 = SkUTF16_CountUnichars(buf, 2);
106 REPORTER_ASSERT(reporter, count2 == 1);
107 const uint16_t* ptr = buf;
108 SkUnichar c = SkUTF16_NextUnichar(&ptr);
109 REPORTER_ASSERT(reporter, c == gUni[i]);
110 REPORTER_ASSERT(reporter, ptr - buf == 2);
111 }
112}
113
114static void TestUTF(skiatest::Reporter* reporter) {
115 static const struct {
116 const char* fUtf8;
117 SkUnichar fUni;
118 } gTest[] = {
119 { "a", 'a' },
120 { "\x7f", 0x7f },
121 { "\xC2\x80", 0x80 },
122 { "\xC3\x83", (3 << 6) | 3 },
123 { "\xDF\xBF", 0x7ff },
124 { "\xE0\xA0\x80", 0x800 },
125 { "\xE0\xB0\xB8", 0xC38 },
126 { "\xE3\x83\x83", (3 << 12) | (3 << 6) | 3 },
127 { "\xEF\xBF\xBF", 0xFFFF },
128 { "\xF0\x90\x80\x80", 0x10000 },
129 { "\xF3\x83\x83\x83", (3 << 18) | (3 << 12) | (3 << 6) | 3 }
130 };
131
132 for (size_t i = 0; i < SK_ARRAY_COUNT(gTest); i++) {
133 const char* p = gTest[i].fUtf8;
134 int n = SkUTF8_CountUnichars(p);
135 SkUnichar u0 = SkUTF8_ToUnichar(gTest[i].fUtf8);
136 SkUnichar u1 = SkUTF8_NextUnichar(&p);
137
138 REPORTER_ASSERT(reporter, n == 1);
139 REPORTER_ASSERT(reporter, u0 == u1);
140 REPORTER_ASSERT(reporter, u0 == gTest[i].fUni);
141 REPORTER_ASSERT(reporter,
142 p - gTest[i].fUtf8 == (int)strlen(gTest[i].fUtf8));
143 }
reed@android.com80e39a72009-04-02 16:59:40 +0000144
reed@android.comed673312009-02-27 16:24:51 +0000145 test_utf16(reporter);
146 test_search(reporter);
reed@android.comb00cd722010-04-16 20:35:47 +0000147 test_refptr(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000148}
149
reed@android.comd8730ea2009-02-27 22:06:06 +0000150#include "TestClassDef.h"
151DEFINE_TESTCLASS("UTF", UtfTestClass, TestUTF)