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