blob: f6b02112f09fac9800173640396ba15e85070e72 [file] [log] [blame]
reed@android.comed673312009-02-27 16:24:51 +00001#include "Test.h"
2#include "SkRandom.h"
3#include "SkTSearch.h"
4#include "SkTSort.h"
5#include "SkUtils.h"
6
7#define kSEARCH_COUNT 91
8
9static void test_search(skiatest::Reporter* reporter) {
10 int i, array[kSEARCH_COUNT];
11 SkRandom rand;
12
13 for (i = 0; i < kSEARCH_COUNT; i++) {
14 array[i] = rand.nextS();
15 }
16
17 SkTHeapSort<int>(array, kSEARCH_COUNT);
18 // make sure we got sorted properly
19 for (i = 1; i < kSEARCH_COUNT; i++) {
20 REPORTER_ASSERT(reporter, array[i-1] <= array[i]);
21 }
22
23 // make sure we can find all of our values
24 for (i = 0; i < kSEARCH_COUNT; i++) {
25 int index = SkTSearch<int>(array, kSEARCH_COUNT, array[i], sizeof(int));
26 REPORTER_ASSERT(reporter, index == i);
27 }
28
29 // make sure that random values are either found, or the correct
30 // insertion index is returned
31 for (i = 0; i < 10000; i++) {
32 int value = rand.nextS();
33 int index = SkTSearch<int>(array, kSEARCH_COUNT, value, sizeof(int));
34
35 if (index >= 0) {
36 REPORTER_ASSERT(reporter,
37 index < kSEARCH_COUNT && array[index] == value);
38 } else {
39 index = ~index;
40 REPORTER_ASSERT(reporter, index <= kSEARCH_COUNT);
41 if (index < kSEARCH_COUNT) {
42 REPORTER_ASSERT(reporter, value < array[index]);
43 if (index > 0) {
44 REPORTER_ASSERT(reporter, value > array[index - 1]);
45 }
46 } else {
47 // we should append the new value
48 REPORTER_ASSERT(reporter, value > array[kSEARCH_COUNT - 1]);
49 }
50 }
51 }
52}
53
54static void test_utf16(skiatest::Reporter* reporter) {
55 static const SkUnichar gUni[] = {
56 0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234
57 };
58
59 uint16_t buf[2];
60
61 for (size_t i = 0; i < SK_ARRAY_COUNT(gUni); i++) {
62 size_t count = SkUTF16_FromUnichar(gUni[i], buf);
63 REPORTER_ASSERT(reporter, count == 2);
64 size_t count2 = SkUTF16_CountUnichars(buf, 2);
65 REPORTER_ASSERT(reporter, count2 == 1);
66 const uint16_t* ptr = buf;
67 SkUnichar c = SkUTF16_NextUnichar(&ptr);
68 REPORTER_ASSERT(reporter, c == gUni[i]);
69 REPORTER_ASSERT(reporter, ptr - buf == 2);
70 }
71}
72
73static void TestUTF(skiatest::Reporter* reporter) {
74 static const struct {
75 const char* fUtf8;
76 SkUnichar fUni;
77 } gTest[] = {
78 { "a", 'a' },
79 { "\x7f", 0x7f },
80 { "\xC2\x80", 0x80 },
81 { "\xC3\x83", (3 << 6) | 3 },
82 { "\xDF\xBF", 0x7ff },
83 { "\xE0\xA0\x80", 0x800 },
84 { "\xE0\xB0\xB8", 0xC38 },
85 { "\xE3\x83\x83", (3 << 12) | (3 << 6) | 3 },
86 { "\xEF\xBF\xBF", 0xFFFF },
87 { "\xF0\x90\x80\x80", 0x10000 },
88 { "\xF3\x83\x83\x83", (3 << 18) | (3 << 12) | (3 << 6) | 3 }
89 };
90
91 for (size_t i = 0; i < SK_ARRAY_COUNT(gTest); i++) {
92 const char* p = gTest[i].fUtf8;
93 int n = SkUTF8_CountUnichars(p);
94 SkUnichar u0 = SkUTF8_ToUnichar(gTest[i].fUtf8);
95 SkUnichar u1 = SkUTF8_NextUnichar(&p);
96
97 REPORTER_ASSERT(reporter, n == 1);
98 REPORTER_ASSERT(reporter, u0 == u1);
99 REPORTER_ASSERT(reporter, u0 == gTest[i].fUni);
100 REPORTER_ASSERT(reporter,
101 p - gTest[i].fUtf8 == (int)strlen(gTest[i].fUtf8));
102 }
103
104 test_utf16(reporter);
105 test_search(reporter);
106}
107
reed@android.comd8730ea2009-02-27 22:06:06 +0000108#include "TestClassDef.h"
109DEFINE_TESTCLASS("UTF", UtfTestClass, TestUTF)