blob: 2af4f1867daf49d40c54ea7a0e8a72354dab0f77 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@android.comed673312009-02-27 16:24:51 +00008#include "SkPackBits.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "Test.h"
reed@android.comed673312009-02-27 16:24:51 +000010
11static const uint16_t gTest0[] = { 0, 0, 1, 1 };
12static const uint16_t gTest1[] = { 1, 2, 3, 4, 5, 6 };
13static const uint16_t gTest2[] = { 0, 0, 0, 1, 2, 3, 3, 3 };
14static const uint16_t gTest3[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 };
15
16#include "SkRandom.h"
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000017static SkRandom gRand;
reed@android.comed673312009-02-27 16:24:51 +000018static void rand_fill(uint16_t buffer[], int count) {
19 for (int i = 0; i < count; i++)
20 buffer[i] = (uint16_t)gRand.nextU();
21}
22
23static void test_pack16(skiatest::Reporter* reporter) {
24 static const struct {
25 const uint16_t* fSrc;
26 int fCount;
27 } gTests[] = {
28 { gTest0, SK_ARRAY_COUNT(gTest0) },
29 { gTest1, SK_ARRAY_COUNT(gTest1) },
30 { gTest2, SK_ARRAY_COUNT(gTest2) },
31 { gTest3, SK_ARRAY_COUNT(gTest3) }
32 };
reed@android.com80e39a72009-04-02 16:59:40 +000033
reed@android.comed673312009-02-27 16:24:51 +000034 for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); i++) {
35 uint8_t dst[100];
36 size_t dstSize = SkPackBits::Pack16(gTests[i].fSrc,
37 gTests[i].fCount, dst);
38 uint16_t src[100];
39 int srcCount = SkPackBits::Unpack16(dst, dstSize, src);
40 bool match = gTests[i].fCount == srcCount && memcmp(gTests[i].fSrc, src,
41 gTests[i].fCount * sizeof(uint16_t)) == 0;
42 REPORTER_ASSERT(reporter, match);
43 }
reed@android.com80e39a72009-04-02 16:59:40 +000044
reed@android.comed673312009-02-27 16:24:51 +000045 for (int n = 1000; n; n--) {
46 size_t size = 50;
47 uint16_t src[100], src2[100];
48 uint8_t dst[200];
49 rand_fill(src, size);
50
51 size_t dstSize = SkPackBits::Pack16(src, size, dst);
52 size_t maxSize = SkPackBits::ComputeMaxSize16(size);
53 REPORTER_ASSERT(reporter, maxSize >= dstSize);
54
reed@android.com04225dc2009-03-20 04:59:37 +000055 size_t srcCount = SkPackBits::Unpack16(dst, dstSize, src2);
reed@android.comed673312009-02-27 16:24:51 +000056 REPORTER_ASSERT(reporter, size == srcCount);
57 bool match = memcmp(src, src2, size * sizeof(uint16_t)) == 0;
58 REPORTER_ASSERT(reporter, match);
59 }
60}
61
62static const uint8_t gTest80[] = { 0, 0, 1, 1 };
63static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 };
64static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 };
65static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 };
66static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 };
67
68static void rand_fill(uint8_t buffer[], int count) {
69 for (int i = 0; i < count; i++)
70 buffer[i] = (uint8_t)((gRand.nextU() >> 8) & 0x3);
71}
72
73static void test_pack8(skiatest::Reporter* reporter) {
74 static const struct {
75 const uint8_t* fSrc;
76 int fCount;
77 } gTests[] = {
78 { gTest80, SK_ARRAY_COUNT(gTest80) },
79 { gTest81, SK_ARRAY_COUNT(gTest81) },
80 { gTest82, SK_ARRAY_COUNT(gTest82) },
81 { gTest83, SK_ARRAY_COUNT(gTest83) },
82 { gTest84, SK_ARRAY_COUNT(gTest84) }
83 };
reed@android.com80e39a72009-04-02 16:59:40 +000084
reed@android.comed673312009-02-27 16:24:51 +000085 for (size_t i = 4; i < SK_ARRAY_COUNT(gTests); i++) {
86 uint8_t dst[100];
87 size_t maxSize = SkPackBits::ComputeMaxSize8(gTests[i].fCount);
88 size_t dstSize = SkPackBits::Pack8(gTests[i].fSrc,
89 gTests[i].fCount, dst);
90 REPORTER_ASSERT(reporter, dstSize <= maxSize);
91 uint8_t src[100];
92 int srcCount = SkPackBits::Unpack8(dst, dstSize, src);
93 bool match = gTests[i].fCount == srcCount &&
94 memcmp(gTests[i].fSrc, src,
95 gTests[i].fCount * sizeof(uint8_t)) == 0;
96 REPORTER_ASSERT(reporter, match);
97 }
98
99 for (size_t size = 1; size <= 512; size += 1) {
reed@android.come72fee52009-11-16 14:52:01 +0000100 for (int n = 100; n; n--) {
reed@android.comed673312009-02-27 16:24:51 +0000101 uint8_t src[600], src2[600];
102 uint8_t dst[600];
103 rand_fill(src, size);
104
105 size_t dstSize = SkPackBits::Pack8(src, size, dst);
106 size_t maxSize = SkPackBits::ComputeMaxSize8(size);
107 REPORTER_ASSERT(reporter, maxSize >= dstSize);
108
reed@android.com04225dc2009-03-20 04:59:37 +0000109 size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2);
reed@android.comed673312009-02-27 16:24:51 +0000110 REPORTER_ASSERT(reporter, size == srcCount);
111 bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0;
112 REPORTER_ASSERT(reporter, match);
reed@android.com80e39a72009-04-02 16:59:40 +0000113
reed@android.come72fee52009-11-16 14:52:01 +0000114 for (int j = 0; j < 100; j++) {
reed@android.comed673312009-02-27 16:24:51 +0000115 size_t skip = gRand.nextU() % size;
116 size_t write = gRand.nextU() % size;
117 if (skip + write > size) {
118 write = size - skip;
119 }
120 SkPackBits::Unpack8(src, skip, write, dst);
121 bool match = memcmp(src, src2 + skip, write) == 0;
122 REPORTER_ASSERT(reporter, match);
123 }
124 }
125 }
126}
127
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000128DEF_TEST(PackBits, reporter) {
reed@android.comd8730ea2009-02-27 22:06:06 +0000129 test_pack8(reporter);
130 test_pack16(reporter);
reed@android.comed673312009-02-27 16:24:51 +0000131}