blob: 4d73372d050af8623c5bc6ebc24318e188e5864c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/effects/SkPackBits.h"
9#include "tests/Test.h"
reed@android.comed673312009-02-27 16:24:51 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/utils/SkRandom.h"
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000012static SkRandom gRand;
reed@android.comed673312009-02-27 16:24:51 +000013static const uint8_t gTest80[] = { 0, 0, 1, 1 };
14static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 };
15static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 };
16static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 };
17static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 };
18
19static void rand_fill(uint8_t buffer[], int count) {
20 for (int i = 0; i < count; i++)
21 buffer[i] = (uint8_t)((gRand.nextU() >> 8) & 0x3);
22}
23
24static void test_pack8(skiatest::Reporter* reporter) {
25 static const struct {
26 const uint8_t* fSrc;
27 int fCount;
28 } gTests[] = {
29 { gTest80, SK_ARRAY_COUNT(gTest80) },
30 { gTest81, SK_ARRAY_COUNT(gTest81) },
31 { gTest82, SK_ARRAY_COUNT(gTest82) },
32 { gTest83, SK_ARRAY_COUNT(gTest83) },
33 { gTest84, SK_ARRAY_COUNT(gTest84) }
34 };
reed@android.com80e39a72009-04-02 16:59:40 +000035
reed@android.comed673312009-02-27 16:24:51 +000036 for (size_t i = 4; i < SK_ARRAY_COUNT(gTests); i++) {
37 uint8_t dst[100];
38 size_t maxSize = SkPackBits::ComputeMaxSize8(gTests[i].fCount);
39 size_t dstSize = SkPackBits::Pack8(gTests[i].fSrc,
jschuh699b8522015-06-04 15:10:37 -070040 gTests[i].fCount, dst, maxSize - 1);
41 REPORTER_ASSERT(reporter, dstSize == 0);
42 dstSize = SkPackBits::Pack8(gTests[i].fSrc,
43 gTests[i].fCount, dst, sizeof(dst));
reed@android.comed673312009-02-27 16:24:51 +000044 REPORTER_ASSERT(reporter, dstSize <= maxSize);
45 uint8_t src[100];
jschuh699b8522015-06-04 15:10:37 -070046 int srcCount = SkPackBits::Unpack8(dst, dstSize, src, gTests[i].fCount - 1);
47 REPORTER_ASSERT(reporter, srcCount == 0);
48 srcCount = SkPackBits::Unpack8(dst, dstSize, src, sizeof(src));
reed@android.comed673312009-02-27 16:24:51 +000049 bool match = gTests[i].fCount == srcCount &&
50 memcmp(gTests[i].fSrc, src,
51 gTests[i].fCount * sizeof(uint8_t)) == 0;
52 REPORTER_ASSERT(reporter, match);
53 }
54
bsalomon98806072014-12-12 15:11:17 -080055 for (uint32_t size = 1; size <= 512; size += 1) {
reed@android.come72fee52009-11-16 14:52:01 +000056 for (int n = 100; n; n--) {
reed@android.comed673312009-02-27 16:24:51 +000057 uint8_t src[600], src2[600];
58 uint8_t dst[600];
59 rand_fill(src, size);
60
jschuh699b8522015-06-04 15:10:37 -070061 size_t dstSize = SkPackBits::Pack8(src, size, dst, sizeof(dst));
reed@android.comed673312009-02-27 16:24:51 +000062 size_t maxSize = SkPackBits::ComputeMaxSize8(size);
63 REPORTER_ASSERT(reporter, maxSize >= dstSize);
64
jschuh699b8522015-06-04 15:10:37 -070065 size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2, size);
reed@android.comed673312009-02-27 16:24:51 +000066 REPORTER_ASSERT(reporter, size == srcCount);
67 bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0;
68 REPORTER_ASSERT(reporter, match);
reed@android.comed673312009-02-27 16:24:51 +000069 }
70 }
71}
72
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000073DEF_TEST(PackBits, reporter) {
reed@android.comd8730ea2009-02-27 22:06:06 +000074 test_pack8(reporter);
reed@android.comed673312009-02-27 16:24:51 +000075}