blob: ad1a21c77c66c84b5348a8e3d72dcc6381303041 [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
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000082 compare16(buffer, 0, PAD + alignment);
83 compare16(base, VALUE16, count);
84 compare16(base + count, 0, TOTAL - count - PAD - alignment);
85 }
86 }
87}
88
89static void test_32(skiatest::Reporter* reporter) {
90 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000091
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000092 for (int count = 0; count < MAX_COUNT; ++count) {
93 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
94 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000096 uint32_t* base = &buffer[PAD + alignment];
97 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000099 compare32(buffer, 0, PAD + alignment);
100 compare32(base, VALUE32, count);
101 compare32(base + count, 0, TOTAL - count - PAD - alignment);
102 }
103 }
104}
105
106/**
107 * Test sk_memset16 and sk_memset32.
108 * For performance considerations, implementations may take different paths
109 * depending on the alignment of the dst, and/or the size of the count.
110 */
111static void TestMemset(skiatest::Reporter* reporter) {
112 test_16(reporter);
113 test_32(reporter);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
reed@google.comebd24962012-05-17 14:28:11 +0000115 test_chunkalloc(reporter);
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000116};
117
118#include "TestClassDef.h"
119DEFINE_TESTCLASS("Memset", TestMemsetClass, TestMemset)