MemsetTest.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "include/utils/SkRandom.h"
  8. #include "src/core/SkUtils.h"
  9. #include "tests/Test.h"
  10. static void set_zero(void* dst, size_t bytes) {
  11. char* ptr = (char*)dst;
  12. for (size_t i = 0; i < bytes; ++i) {
  13. ptr[i] = 0;
  14. }
  15. }
  16. #define MAX_ALIGNMENT 64
  17. #define MAX_COUNT ((MAX_ALIGNMENT) * 32)
  18. #define PAD 32
  19. #define TOTAL (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
  20. #define VALUE16 0x1234
  21. #define VALUE32 0x12345678
  22. static void compare16(skiatest::Reporter* r, const uint16_t base[],
  23. uint16_t value, int count) {
  24. for (int i = 0; i < count; ++i) {
  25. if (base[i] != value) {
  26. ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
  27. return;
  28. }
  29. }
  30. }
  31. static void compare32(skiatest::Reporter* r, const uint32_t base[],
  32. uint32_t value, int count) {
  33. for (int i = 0; i < count; ++i) {
  34. if (base[i] != value) {
  35. ERRORF(r, "[%d] expected %x found %x\n", i, value, base[i]);
  36. return;
  37. }
  38. }
  39. }
  40. static void test_16(skiatest::Reporter* reporter) {
  41. uint16_t buffer[TOTAL];
  42. for (int count = 0; count < MAX_COUNT; ++count) {
  43. for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
  44. set_zero(buffer, sizeof(buffer));
  45. uint16_t* base = &buffer[PAD + alignment];
  46. sk_memset16(base, VALUE16, count);
  47. compare16(reporter, buffer, 0, PAD + alignment);
  48. compare16(reporter, base, VALUE16, count);
  49. compare16(reporter, base + count, 0, TOTAL - count - PAD - alignment);
  50. }
  51. }
  52. }
  53. static void test_32(skiatest::Reporter* reporter) {
  54. uint32_t buffer[TOTAL];
  55. for (int count = 0; count < MAX_COUNT; ++count) {
  56. for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
  57. set_zero(buffer, sizeof(buffer));
  58. uint32_t* base = &buffer[PAD + alignment];
  59. sk_memset32(base, VALUE32, count);
  60. compare32(reporter, buffer, 0, PAD + alignment);
  61. compare32(reporter, base, VALUE32, count);
  62. compare32(reporter, base + count, 0, TOTAL - count - PAD - alignment);
  63. }
  64. }
  65. }
  66. /**
  67. * Test sk_memset16 and sk_memset32.
  68. * For performance considerations, implementations may take different paths
  69. * depending on the alignment of the dst, and/or the size of the count.
  70. */
  71. DEF_TEST(Memset, reporter) {
  72. test_16(reporter);
  73. test_32(reporter);
  74. }