2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkChunkAlloc.h"
12 static void test_chunkalloc(skiatest::Reporter* reporter) {
14 SkChunkAlloc alloc(min);
16 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
17 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
18 REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
19 REPORTER_ASSERT(reporter, !alloc.contains(NULL));
20 REPORTER_ASSERT(reporter, !alloc.contains(reporter));
23 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
24 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
25 REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
27 size_t size = min >> 1;
28 void* ptr = alloc.allocThrow(size);
29 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= size);
30 REPORTER_ASSERT(reporter, alloc.totalUsed() == size);
31 REPORTER_ASSERT(reporter, alloc.blockCount() > 0);
32 REPORTER_ASSERT(reporter, alloc.contains(ptr));
35 REPORTER_ASSERT(reporter, !alloc.contains(ptr));
36 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
37 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
40 ///////////////////////////////////////////////////////////////////////////////
42 static void set_zero(void* dst, size_t bytes) {
43 char* ptr = (char*)dst;
44 for (size_t i = 0; i < bytes; ++i) {
49 #define MAX_ALIGNMENT 64
50 #define MAX_COUNT ((MAX_ALIGNMENT) * 32)
52 #define TOTAL (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
54 #define VALUE16 0x1234
55 #define VALUE32 0x12345678
57 static bool compare16(const uint16_t base[], uint16_t value, int count) {
58 for (int i = 0; i < count; ++i) {
59 if (base[i] != value) {
60 SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
67 static bool compare32(const uint32_t base[], uint32_t value, int count) {
68 for (int i = 0; i < count; ++i) {
69 if (base[i] != value) {
70 SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
77 static void test_16(skiatest::Reporter* reporter) {
78 uint16_t buffer[TOTAL];
80 for (int count = 0; count < MAX_COUNT; ++count) {
81 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
82 set_zero(buffer, sizeof(buffer));
84 uint16_t* base = &buffer[PAD + alignment];
85 sk_memset16(base, VALUE16, count);
87 REPORTER_ASSERT(reporter,
88 compare16(buffer, 0, PAD + alignment) &&
89 compare16(base, VALUE16, count) &&
90 compare16(base + count, 0, TOTAL - count - PAD - alignment));
95 static void test_32(skiatest::Reporter* reporter) {
96 uint32_t buffer[TOTAL];
98 for (int count = 0; count < MAX_COUNT; ++count) {
99 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
100 set_zero(buffer, sizeof(buffer));
102 uint32_t* base = &buffer[PAD + alignment];
103 sk_memset32(base, VALUE32, count);
105 REPORTER_ASSERT(reporter,
106 compare32(buffer, 0, PAD + alignment) &&
107 compare32(base, VALUE32, count) &&
108 compare32(base + count, 0, TOTAL - count - PAD - alignment));
114 * Test sk_memset16 and sk_memset32.
115 * For performance considerations, implementations may take different paths
116 * depending on the alignment of the dst, and/or the size of the count.
118 DEF_TEST(Memset, reporter) {
122 test_chunkalloc(reporter);