blob: ee6aaea94e5bd10f785bfb3eb1874e635a021d99 [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
reed@google.comebd24962012-05-17 14:28:11 +00008#include "SkChunkAlloc.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +00009#include "SkUtils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000011
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());
reed@google.com6757a3c2013-06-19 19:25:36 +000017 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
reed@google.comebd24962012-05-17 14:28:11 +000018 REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
19 REPORTER_ASSERT(reporter, !alloc.contains(NULL));
20 REPORTER_ASSERT(reporter, !alloc.contains(reporter));
21
22 alloc.reset();
23 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
reed@google.com6757a3c2013-06-19 19:25:36 +000024 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
reed@google.comebd24962012-05-17 14:28:11 +000025 REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
26
27 size_t size = min >> 1;
28 void* ptr = alloc.allocThrow(size);
29 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= size);
reed@google.com6757a3c2013-06-19 19:25:36 +000030 REPORTER_ASSERT(reporter, alloc.totalUsed() == size);
reed@google.comebd24962012-05-17 14:28:11 +000031 REPORTER_ASSERT(reporter, alloc.blockCount() > 0);
32 REPORTER_ASSERT(reporter, alloc.contains(ptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.comebd24962012-05-17 14:28:11 +000034 alloc.reset();
35 REPORTER_ASSERT(reporter, !alloc.contains(ptr));
reed@google.com6757a3c2013-06-19 19:25:36 +000036 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
37 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
reed@google.comebd24962012-05-17 14:28:11 +000038}
39
40///////////////////////////////////////////////////////////////////////////////
41
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000042static void set_zero(void* dst, size_t bytes) {
43 char* ptr = (char*)dst;
44 for (size_t i = 0; i < bytes; ++i) {
45 ptr[i] = 0;
46 }
47}
48
49#define MAX_ALIGNMENT 64
50#define MAX_COUNT ((MAX_ALIGNMENT) * 32)
51#define PAD 32
52#define TOTAL (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
53
54#define VALUE16 0x1234
55#define VALUE32 0x12345678
56
57static 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]);
61 return false;
62 }
63 }
64 return true;
65}
66
67static 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]);
71 return false;
72 }
73 }
74 return true;
75}
76
77static void test_16(skiatest::Reporter* reporter) {
78 uint16_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000079
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000080 for (int count = 0; count < MAX_COUNT; ++count) {
81 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
82 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000084 uint16_t* base = &buffer[PAD + alignment];
85 sk_memset16(base, VALUE16, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000086
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000087 REPORTER_ASSERT(reporter,
88 compare16(buffer, 0, PAD + alignment) &&
89 compare16(base, VALUE16, count) &&
90 compare16(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000091 }
92 }
93}
94
95static void test_32(skiatest::Reporter* reporter) {
96 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000097
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000098 for (int count = 0; count < MAX_COUNT; ++count) {
99 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
100 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000102 uint32_t* base = &buffer[PAD + alignment];
103 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000105 REPORTER_ASSERT(reporter,
106 compare32(buffer, 0, PAD + alignment) &&
107 compare32(base, VALUE32, count) &&
108 compare32(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000109 }
110 }
111}
112
113/**
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.
117 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000118DEF_TEST(Memset, reporter) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000119 test_16(reporter);
120 test_32(reporter);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
reed@google.comebd24962012-05-17 14:28:11 +0000122 test_chunkalloc(reporter);
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000123}