reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkPackBits.h" |
| 3 | |
| 4 | static const uint16_t gTest0[] = { 0, 0, 1, 1 }; |
| 5 | static const uint16_t gTest1[] = { 1, 2, 3, 4, 5, 6 }; |
| 6 | static const uint16_t gTest2[] = { 0, 0, 0, 1, 2, 3, 3, 3 }; |
| 7 | static const uint16_t gTest3[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 }; |
| 8 | |
| 9 | #include "SkRandom.h" |
| 10 | static SkRandom gRand; |
| 11 | static void rand_fill(uint16_t buffer[], int count) { |
| 12 | for (int i = 0; i < count; i++) |
| 13 | buffer[i] = (uint16_t)gRand.nextU(); |
| 14 | } |
| 15 | |
| 16 | static void test_pack16(skiatest::Reporter* reporter) { |
| 17 | static const struct { |
| 18 | const uint16_t* fSrc; |
| 19 | int fCount; |
| 20 | } gTests[] = { |
| 21 | { gTest0, SK_ARRAY_COUNT(gTest0) }, |
| 22 | { gTest1, SK_ARRAY_COUNT(gTest1) }, |
| 23 | { gTest2, SK_ARRAY_COUNT(gTest2) }, |
| 24 | { gTest3, SK_ARRAY_COUNT(gTest3) } |
| 25 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 26 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 27 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); i++) { |
| 28 | uint8_t dst[100]; |
| 29 | size_t dstSize = SkPackBits::Pack16(gTests[i].fSrc, |
| 30 | gTests[i].fCount, dst); |
| 31 | uint16_t src[100]; |
| 32 | int srcCount = SkPackBits::Unpack16(dst, dstSize, src); |
| 33 | bool match = gTests[i].fCount == srcCount && memcmp(gTests[i].fSrc, src, |
| 34 | gTests[i].fCount * sizeof(uint16_t)) == 0; |
| 35 | REPORTER_ASSERT(reporter, match); |
| 36 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 37 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 38 | for (int n = 1000; n; n--) { |
| 39 | size_t size = 50; |
| 40 | uint16_t src[100], src2[100]; |
| 41 | uint8_t dst[200]; |
| 42 | rand_fill(src, size); |
| 43 | |
| 44 | size_t dstSize = SkPackBits::Pack16(src, size, dst); |
| 45 | size_t maxSize = SkPackBits::ComputeMaxSize16(size); |
| 46 | REPORTER_ASSERT(reporter, maxSize >= dstSize); |
| 47 | |
reed@android.com | 04225dc | 2009-03-20 04:59:37 +0000 | [diff] [blame] | 48 | size_t srcCount = SkPackBits::Unpack16(dst, dstSize, src2); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 49 | REPORTER_ASSERT(reporter, size == srcCount); |
| 50 | bool match = memcmp(src, src2, size * sizeof(uint16_t)) == 0; |
| 51 | REPORTER_ASSERT(reporter, match); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static const uint8_t gTest80[] = { 0, 0, 1, 1 }; |
| 56 | static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 }; |
| 57 | static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 }; |
| 58 | static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 }; |
| 59 | static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 }; |
| 60 | |
| 61 | static void rand_fill(uint8_t buffer[], int count) { |
| 62 | for (int i = 0; i < count; i++) |
| 63 | buffer[i] = (uint8_t)((gRand.nextU() >> 8) & 0x3); |
| 64 | } |
| 65 | |
| 66 | static void test_pack8(skiatest::Reporter* reporter) { |
| 67 | static const struct { |
| 68 | const uint8_t* fSrc; |
| 69 | int fCount; |
| 70 | } gTests[] = { |
| 71 | { gTest80, SK_ARRAY_COUNT(gTest80) }, |
| 72 | { gTest81, SK_ARRAY_COUNT(gTest81) }, |
| 73 | { gTest82, SK_ARRAY_COUNT(gTest82) }, |
| 74 | { gTest83, SK_ARRAY_COUNT(gTest83) }, |
| 75 | { gTest84, SK_ARRAY_COUNT(gTest84) } |
| 76 | }; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 77 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 78 | for (size_t i = 4; i < SK_ARRAY_COUNT(gTests); i++) { |
| 79 | uint8_t dst[100]; |
| 80 | size_t maxSize = SkPackBits::ComputeMaxSize8(gTests[i].fCount); |
| 81 | size_t dstSize = SkPackBits::Pack8(gTests[i].fSrc, |
| 82 | gTests[i].fCount, dst); |
| 83 | REPORTER_ASSERT(reporter, dstSize <= maxSize); |
| 84 | uint8_t src[100]; |
| 85 | int srcCount = SkPackBits::Unpack8(dst, dstSize, src); |
| 86 | bool match = gTests[i].fCount == srcCount && |
| 87 | memcmp(gTests[i].fSrc, src, |
| 88 | gTests[i].fCount * sizeof(uint8_t)) == 0; |
| 89 | REPORTER_ASSERT(reporter, match); |
| 90 | } |
| 91 | |
| 92 | for (size_t size = 1; size <= 512; size += 1) { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 93 | for (int n = 100; n; n--) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 94 | uint8_t src[600], src2[600]; |
| 95 | uint8_t dst[600]; |
| 96 | rand_fill(src, size); |
| 97 | |
| 98 | size_t dstSize = SkPackBits::Pack8(src, size, dst); |
| 99 | size_t maxSize = SkPackBits::ComputeMaxSize8(size); |
| 100 | REPORTER_ASSERT(reporter, maxSize >= dstSize); |
| 101 | |
reed@android.com | 04225dc | 2009-03-20 04:59:37 +0000 | [diff] [blame] | 102 | size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, size == srcCount); |
| 104 | bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0; |
| 105 | REPORTER_ASSERT(reporter, match); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 106 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 107 | for (int j = 0; j < 100; j++) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 108 | size_t skip = gRand.nextU() % size; |
| 109 | size_t write = gRand.nextU() % size; |
| 110 | if (skip + write > size) { |
| 111 | write = size - skip; |
| 112 | } |
| 113 | SkPackBits::Unpack8(src, skip, write, dst); |
| 114 | bool match = memcmp(src, src2 + skip, write) == 0; |
| 115 | REPORTER_ASSERT(reporter, match); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 121 | static void TestPackBits(skiatest::Reporter* reporter) { |
| 122 | test_pack8(reporter); |
| 123 | test_pack16(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 126 | #include "TestClassDef.h" |
| 127 | DEFINE_TESTCLASS("PackBits", PackBitsTestClass, TestPackBits) |