PackBitsTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "src/effects/SkPackBits.h"
  8. #include "tests/Test.h"
  9. #include "include/utils/SkRandom.h"
  10. static SkRandom gRand;
  11. static const uint8_t gTest80[] = { 0, 0, 1, 1 };
  12. static const uint8_t gTest81[] = { 1, 2, 3, 4, 5, 6 };
  13. static const uint8_t gTest82[] = { 0, 0, 0, 1, 2, 3, 3, 3 };
  14. static const uint8_t gTest83[] = { 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 0, 0, 1 };
  15. static const uint8_t gTest84[] = { 1, 0, 3, 0, 0, 0, 2, 1, 1, 2 };
  16. static void rand_fill(uint8_t buffer[], int count) {
  17. for (int i = 0; i < count; i++)
  18. buffer[i] = (uint8_t)((gRand.nextU() >> 8) & 0x3);
  19. }
  20. static void test_pack8(skiatest::Reporter* reporter) {
  21. static const struct {
  22. const uint8_t* fSrc;
  23. int fCount;
  24. } gTests[] = {
  25. { gTest80, SK_ARRAY_COUNT(gTest80) },
  26. { gTest81, SK_ARRAY_COUNT(gTest81) },
  27. { gTest82, SK_ARRAY_COUNT(gTest82) },
  28. { gTest83, SK_ARRAY_COUNT(gTest83) },
  29. { gTest84, SK_ARRAY_COUNT(gTest84) }
  30. };
  31. for (size_t i = 4; i < SK_ARRAY_COUNT(gTests); i++) {
  32. uint8_t dst[100];
  33. size_t maxSize = SkPackBits::ComputeMaxSize8(gTests[i].fCount);
  34. size_t dstSize = SkPackBits::Pack8(gTests[i].fSrc,
  35. gTests[i].fCount, dst, maxSize - 1);
  36. REPORTER_ASSERT(reporter, dstSize == 0);
  37. dstSize = SkPackBits::Pack8(gTests[i].fSrc,
  38. gTests[i].fCount, dst, sizeof(dst));
  39. REPORTER_ASSERT(reporter, dstSize <= maxSize);
  40. uint8_t src[100];
  41. int srcCount = SkPackBits::Unpack8(dst, dstSize, src, gTests[i].fCount - 1);
  42. REPORTER_ASSERT(reporter, srcCount == 0);
  43. srcCount = SkPackBits::Unpack8(dst, dstSize, src, sizeof(src));
  44. bool match = gTests[i].fCount == srcCount &&
  45. memcmp(gTests[i].fSrc, src,
  46. gTests[i].fCount * sizeof(uint8_t)) == 0;
  47. REPORTER_ASSERT(reporter, match);
  48. }
  49. for (uint32_t size = 1; size <= 512; size += 1) {
  50. for (int n = 100; n; n--) {
  51. uint8_t src[600], src2[600];
  52. uint8_t dst[600];
  53. rand_fill(src, size);
  54. size_t dstSize = SkPackBits::Pack8(src, size, dst, sizeof(dst));
  55. size_t maxSize = SkPackBits::ComputeMaxSize8(size);
  56. REPORTER_ASSERT(reporter, maxSize >= dstSize);
  57. size_t srcCount = SkPackBits::Unpack8(dst, dstSize, src2, size);
  58. REPORTER_ASSERT(reporter, size == srcCount);
  59. bool match = memcmp(src, src2, size * sizeof(uint8_t)) == 0;
  60. REPORTER_ASSERT(reporter, match);
  61. }
  62. }
  63. }
  64. DEF_TEST(PackBits, reporter) {
  65. test_pack8(reporter);
  66. }