MemsetTest.cpp revision 54f0d1b7113cb0dc184e522539aab1030a28a421
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 "SkChunkAlloc.h"
10#include "SkUtils.h"
11
12static void test_chunkalloc(skiatest::Reporter* reporter) {
13    size_t min = 256;
14    SkChunkAlloc alloc(min);
15
16    REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
17    REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
18    REPORTER_ASSERT(reporter, !alloc.contains(NULL));
19    REPORTER_ASSERT(reporter, !alloc.contains(reporter));
20
21    alloc.reset();
22    REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
23    REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
24
25    size_t size = min >> 1;
26    void* ptr = alloc.allocThrow(size);
27    REPORTER_ASSERT(reporter, alloc.totalCapacity() >= size);
28    REPORTER_ASSERT(reporter, alloc.blockCount() > 0);
29    REPORTER_ASSERT(reporter, alloc.contains(ptr));
30
31    alloc.reset();
32    REPORTER_ASSERT(reporter, !alloc.contains(ptr));
33}
34
35///////////////////////////////////////////////////////////////////////////////
36
37static void set_zero(void* dst, size_t bytes) {
38    char* ptr = (char*)dst;
39    for (size_t i = 0; i < bytes; ++i) {
40        ptr[i] = 0;
41    }
42}
43
44#define MAX_ALIGNMENT   64
45#define MAX_COUNT       ((MAX_ALIGNMENT) * 32)
46#define PAD             32
47#define TOTAL           (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
48
49#define VALUE16         0x1234
50#define VALUE32         0x12345678
51
52static bool compare16(const uint16_t base[], uint16_t value, int count) {
53    for (int i = 0; i < count; ++i) {
54        if (base[i] != value) {
55            SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
56            return false;
57        }
58    }
59    return true;
60}
61
62static bool compare32(const uint32_t base[], uint32_t value, int count) {
63    for (int i = 0; i < count; ++i) {
64        if (base[i] != value) {
65            SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
66            return false;
67        }
68    }
69    return true;
70}
71
72static void test_16(skiatest::Reporter* reporter) {
73    uint16_t buffer[TOTAL];
74
75    for (int count = 0; count < MAX_COUNT; ++count) {
76        for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
77            set_zero(buffer, sizeof(buffer));
78
79            uint16_t* base = &buffer[PAD + alignment];
80            sk_memset16(base, VALUE16, count);
81
82            REPORTER_ASSERT(reporter,
83                compare16(buffer,       0,       PAD + alignment) &&
84                compare16(base,         VALUE16, count) &&
85                compare16(base + count, 0,       TOTAL - count - PAD - alignment));
86        }
87    }
88}
89
90static void test_32(skiatest::Reporter* reporter) {
91    uint32_t buffer[TOTAL];
92
93    for (int count = 0; count < MAX_COUNT; ++count) {
94        for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
95            set_zero(buffer, sizeof(buffer));
96
97            uint32_t* base = &buffer[PAD + alignment];
98            sk_memset32(base, VALUE32, count);
99
100            REPORTER_ASSERT(reporter,
101                compare32(buffer,       0,       PAD + alignment) &&
102                compare32(base,         VALUE32, count) &&
103                compare32(base + count, 0,       TOTAL - count - PAD - alignment));
104        }
105    }
106}
107
108/**
109 *  Test sk_memset16 and sk_memset32.
110 *  For performance considerations, implementations may take different paths
111 *  depending on the alignment of the dst, and/or the size of the count.
112 */
113static void TestMemset(skiatest::Reporter* reporter) {
114    test_16(reporter);
115    test_32(reporter);
116
117    test_chunkalloc(reporter);
118};
119
120#include "TestClassDef.h"
121DEFINE_TESTCLASS("Memset", TestMemsetClass, TestMemset)
122