Home | History | Annotate | Download | only in tests
      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 "SkPackBits.h"
      9 #include "Test.h"
     10 
     11 #include "SkRandom.h"
     12 static SkRandom gRand;
     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     };
     35 
     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,
     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));
     44         REPORTER_ASSERT(reporter, dstSize <= maxSize);
     45         uint8_t src[100];
     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));
     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 
     55     for (uint32_t size = 1; size <= 512; size += 1) {
     56         for (int n = 100; n; n--) {
     57             uint8_t src[600], src2[600];
     58             uint8_t dst[600];
     59             rand_fill(src, size);
     60 
     61             size_t dstSize = SkPackBits::Pack8(src, size, dst, sizeof(dst));
     62             size_t maxSize = SkPackBits::ComputeMaxSize8(size);
     63             REPORTER_ASSERT(reporter, maxSize >= dstSize);
     64 
     65             size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2, size);
     66             REPORTER_ASSERT(reporter, size == srcCount);
     67             bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0;
     68             REPORTER_ASSERT(reporter, match);
     69         }
     70     }
     71 }
     72 
     73 DEF_TEST(PackBits, reporter) {
     74     test_pack8(reporter);
     75 }
     76