epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 8 | #include "SkPackBits.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "Test.h" |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 10 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 11 | #include "SkRandom.h" |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 12 | static SkRandom gRand; |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 13 | static const uint8_t gTest80[] = { 0, 0, 1, 1 }; |
| 14 | static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 }; |
| 15 | static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 }; |
| 16 | static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 }; |
| 17 | static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 }; |
| 18 | |
| 19 | static 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 | |
| 24 | static 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.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 35 | |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 36 | 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, |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 40 | 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.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, dstSize <= maxSize); |
| 45 | uint8_t src[100]; |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 46 | 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.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 49 | 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 | |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 55 | for (uint32_t size = 1; size <= 512; size += 1) { |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 56 | for (int n = 100; n; n--) { |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 57 | uint8_t src[600], src2[600]; |
| 58 | uint8_t dst[600]; |
| 59 | rand_fill(src, size); |
| 60 | |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 61 | size_t dstSize = SkPackBits::Pack8(src, size, dst, sizeof(dst)); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 62 | size_t maxSize = SkPackBits::ComputeMaxSize8(size); |
| 63 | REPORTER_ASSERT(reporter, maxSize >= dstSize); |
| 64 | |
jschuh | 699b852 | 2015-06-04 15:10:37 -0700 | [diff] [blame] | 65 | size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2, size); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, size == srcCount); |
| 67 | bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0; |
| 68 | REPORTER_ASSERT(reporter, match); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 73 | DEF_TEST(PackBits, reporter) { |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 74 | test_pack8(reporter); |
reed@android.com | ed67331 | 2009-02-27 16:24:51 +0000 | [diff] [blame] | 75 | } |