blob: a22590c3973c804cd3e16747b85c4173c1fac75a [file] [log] [blame]
reed@android.comed673312009-02-27 16:24:51 +00001#include "Test.h"
2#include "SkPackBits.h"
3
4static const uint16_t gTest0[] = { 0, 0, 1, 1 };
5static const uint16_t gTest1[] = { 1, 2, 3, 4, 5, 6 };
6static const uint16_t gTest2[] = { 0, 0, 0, 1, 2, 3, 3, 3 };
7static const uint16_t gTest3[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 };
8
9#include "SkRandom.h"
10static SkRandom gRand;
11static 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
16static 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.com80e39a72009-04-02 16:59:40 +000026
reed@android.comed673312009-02-27 16:24:51 +000027 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.com80e39a72009-04-02 16:59:40 +000037
reed@android.comed673312009-02-27 16:24:51 +000038 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.com04225dc2009-03-20 04:59:37 +000048 size_t srcCount = SkPackBits::Unpack16(dst, dstSize, src2);
reed@android.comed673312009-02-27 16:24:51 +000049 REPORTER_ASSERT(reporter, size == srcCount);
50 bool match = memcmp(src, src2, size * sizeof(uint16_t)) == 0;
51 REPORTER_ASSERT(reporter, match);
52 }
53}
54
55static const uint8_t gTest80[] = { 0, 0, 1, 1 };
56static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 };
57static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 };
58static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 };
59static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 };
60
61static 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
66static 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.com80e39a72009-04-02 16:59:40 +000077
reed@android.comed673312009-02-27 16:24:51 +000078 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.come72fee52009-11-16 14:52:01 +000093 for (int n = 100; n; n--) {
reed@android.comed673312009-02-27 16:24:51 +000094 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.com04225dc2009-03-20 04:59:37 +0000102 size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2);
reed@android.comed673312009-02-27 16:24:51 +0000103 REPORTER_ASSERT(reporter, size == srcCount);
104 bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0;
105 REPORTER_ASSERT(reporter, match);
reed@android.com80e39a72009-04-02 16:59:40 +0000106
reed@android.come72fee52009-11-16 14:52:01 +0000107 for (int j = 0; j < 100; j++) {
reed@android.comed673312009-02-27 16:24:51 +0000108 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.comd8730ea2009-02-27 22:06:06 +0000121static void TestPackBits(skiatest::Reporter* reporter) {
122 test_pack8(reporter);
123 test_pack16(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000124}
125
reed@android.comd8730ea2009-02-27 22:06:06 +0000126#include "TestClassDef.h"
127DEFINE_TESTCLASS("PackBits", PackBitsTestClass, TestPackBits)