blob: c79915989dccdd0b2378974c854c5f7d15d99c9e [file] [log] [blame]
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00001/*
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 */
reed@google.comebd24962012-05-17 14:28:11 +00007
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00008#include "Test.h"
reed@google.comebd24962012-05-17 14:28:11 +00009#include "SkChunkAlloc.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000010#include "SkUtils.h"
11
reed@google.comebd24962012-05-17 14:28:11 +000012static void test_chunkalloc(skiatest::Reporter* reporter) {
13 size_t min = 256;
14 SkChunkAlloc alloc(min);
rmistry@google.comd6176b02012-08-23 18:14:13 +000015
reed@google.comebd24962012-05-17 14:28:11 +000016 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));
rmistry@google.comd6176b02012-08-23 18:14:13 +000030
reed@google.comebd24962012-05-17 14:28:11 +000031 alloc.reset();
32 REPORTER_ASSERT(reporter, !alloc.contains(ptr));
33}
34
35///////////////////////////////////////////////////////////////////////////////
36
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000037static 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];
rmistry@google.comd6176b02012-08-23 18:14:13 +000074
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000075 for (int count = 0; count < MAX_COUNT; ++count) {
76 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
77 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000078
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000079 uint16_t* base = &buffer[PAD + alignment];
80 sk_memset16(base, VALUE16, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000081
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000082 REPORTER_ASSERT(reporter,
83 compare16(buffer, 0, PAD + alignment) &&
84 compare16(base, VALUE16, count) &&
85 compare16(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000086 }
87 }
88}
89
90static void test_32(skiatest::Reporter* reporter) {
91 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000093 for (int count = 0; count < MAX_COUNT; ++count) {
94 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
95 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000096
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000097 uint32_t* base = &buffer[PAD + alignment];
98 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000099
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000100 REPORTER_ASSERT(reporter,
101 compare32(buffer, 0, PAD + alignment) &&
102 compare32(base, VALUE32, count) &&
103 compare32(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000104 }
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);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
reed@google.comebd24962012-05-17 14:28:11 +0000117 test_chunkalloc(reporter);
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000118};
119
120#include "TestClassDef.h"
121DEFINE_TESTCLASS("Memset", TestMemsetClass, TestMemset)