reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkRandom.h" |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 3 | #include "SkRefCnt.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 4 | #include "SkTSearch.h" |
| 5 | #include "SkTSort.h" |
| 6 | #include "SkUtils.h" |
| 7 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 8 | class RefClass : public SkRefCnt { |
| 9 | public: |
| 10 | RefClass(int n) : fN(n) {} |
| 11 | int get() const { return fN; } |
| 12 | |
| 13 | private: |
| 14 | int fN; |
| 15 | }; |
| 16 | |
| 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); |
reed@google.com | ddbf4c8 | 2011-03-08 16:06:06 +0000 | [diff] [blame] | 26 | REPORTER_ASSERT(reporter, rc0.get() != r0); |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 27 | |
| 28 | rc0 = r0; |
| 29 | REPORTER_ASSERT(reporter, rc0); |
| 30 | REPORTER_ASSERT(reporter, rc0 != rc1); |
reed@google.com | ddbf4c8 | 2011-03-08 16:06:06 +0000 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, rc0.get() == r0); |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 32 | |
| 33 | rc1 = rc0; |
| 34 | REPORTER_ASSERT(reporter, rc1); |
| 35 | REPORTER_ASSERT(reporter, rc0 == rc1); |
reed@google.com | ddbf4c8 | 2011-03-08 16:06:06 +0000 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, rc0.get() == r0); |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 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 | |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 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 | |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 67 | /////////////////////////////////////////////////////////////////////////////// |
| 68 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 69 | #define kSEARCH_COUNT 91 |
| 70 | |
| 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 | |
| 116 | static void test_utf16(skiatest::Reporter* reporter) { |
| 117 | static const SkUnichar gUni[] = { |
| 118 | 0x10000, 0x18080, 0x20202, 0xFFFFF, 0x101234 |
| 119 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 120 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 121 | uint16_t buf[2]; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 122 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 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 | |
| 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 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 165 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 166 | test_utf16(reporter); |
| 167 | test_search(reporter); |
reed@android.com | b00cd72 | 2010-04-16 20:35:47 +0000 | [diff] [blame] | 168 | test_refptr(reporter); |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 169 | test_autounref(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 170 | } |
| 171 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 172 | #include "TestClassDef.h" |
reed@google.com | a67573e | 2011-02-25 18:10:29 +0000 | [diff] [blame] | 173 | DEFINE_TESTCLASS("Utils", UtfTestClass, TestUTF) |