1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "Test.h"
9#include "SkUtils.h"
10
11static void set_zero(void* dst, size_t bytes) {
12    char* ptr = (char*)dst;
13    for (size_t i = 0; i < bytes; ++i) {
14        ptr[i] = 0;
15    }
16}
17
18#define MAX_ALIGNMENT   64
19#define MAX_COUNT       ((MAX_ALIGNMENT) * 32)
20#define PAD             32
21#define TOTAL           (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
22
23#define VALUE16         0x1234
24#define VALUE32         0x12345678
25
26static bool compare16(const uint16_t base[], uint16_t value, int count) {
27    for (int i = 0; i < count; ++i) {
28        if (base[i] != value) {
29            SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
30            return false;
31        }
32    }
33    return true;
34}
35
36static bool compare32(const uint32_t base[], uint32_t value, int count) {
37    for (int i = 0; i < count; ++i) {
38        if (base[i] != value) {
39            SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
40            return false;
41        }
42    }
43    return true;
44}
45
46static void test_16(skiatest::Reporter* reporter) {
47    uint16_t buffer[TOTAL];
48
49    for (int count = 0; count < MAX_COUNT; ++count) {
50        for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
51            set_zero(buffer, sizeof(buffer));
52
53            uint16_t* base = &buffer[PAD + alignment];
54            sk_memset16(base, VALUE16, count);
55
56            compare16(buffer,       0,       PAD + alignment);
57            compare16(base,         VALUE16, count);
58            compare16(base + count, 0,       TOTAL - count - PAD - alignment);
59        }
60    }
61}
62
63static void test_32(skiatest::Reporter* reporter) {
64    uint32_t buffer[TOTAL];
65
66    for (int count = 0; count < MAX_COUNT; ++count) {
67        for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
68            set_zero(buffer, sizeof(buffer));
69
70            uint32_t* base = &buffer[PAD + alignment];
71            sk_memset32(base, VALUE32, count);
72
73            compare32(buffer,       0,       PAD + alignment);
74            compare32(base,         VALUE32, count);
75            compare32(base + count, 0,       TOTAL - count - PAD - alignment);
76        }
77    }
78}
79
80/**
81 *  Test sk_memset16 and sk_memset32.
82 *  For performance considerations, implementations may take different paths
83 *  depending on the alignment of the dst, and/or the size of the count.
84 */
85static void TestMemset(skiatest::Reporter* reporter) {
86    test_16(reporter);
87    test_32(reporter);
88};
89
90#include "TestClassDef.h"
91DEFINE_TESTCLASS("Memset", TestMemsetClass, TestMemset)
92