blob: 36d0f0a46793029afd6753760f171281285b0141 [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"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.comebd24962012-05-17 14:28:11 +000010#include "SkChunkAlloc.h"
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000011#include "SkUtils.h"
12
reed@google.comebd24962012-05-17 14:28:11 +000013static void test_chunkalloc(skiatest::Reporter* reporter) {
14 size_t min = 256;
15 SkChunkAlloc alloc(min);
rmistry@google.comd6176b02012-08-23 18:14:13 +000016
reed@google.comebd24962012-05-17 14:28:11 +000017 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
reed@google.com6757a3c2013-06-19 19:25:36 +000018 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
reed@google.comebd24962012-05-17 14:28:11 +000019 REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
20 REPORTER_ASSERT(reporter, !alloc.contains(NULL));
21 REPORTER_ASSERT(reporter, !alloc.contains(reporter));
22
23 alloc.reset();
24 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
reed@google.com6757a3c2013-06-19 19:25:36 +000025 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
reed@google.comebd24962012-05-17 14:28:11 +000026 REPORTER_ASSERT(reporter, 0 == alloc.blockCount());
27
28 size_t size = min >> 1;
29 void* ptr = alloc.allocThrow(size);
30 REPORTER_ASSERT(reporter, alloc.totalCapacity() >= size);
reed@google.com6757a3c2013-06-19 19:25:36 +000031 REPORTER_ASSERT(reporter, alloc.totalUsed() == size);
reed@google.comebd24962012-05-17 14:28:11 +000032 REPORTER_ASSERT(reporter, alloc.blockCount() > 0);
33 REPORTER_ASSERT(reporter, alloc.contains(ptr));
rmistry@google.comd6176b02012-08-23 18:14:13 +000034
reed@google.comebd24962012-05-17 14:28:11 +000035 alloc.reset();
36 REPORTER_ASSERT(reporter, !alloc.contains(ptr));
reed@google.com6757a3c2013-06-19 19:25:36 +000037 REPORTER_ASSERT(reporter, 0 == alloc.totalCapacity());
38 REPORTER_ASSERT(reporter, 0 == alloc.totalUsed());
reed@google.comebd24962012-05-17 14:28:11 +000039}
40
41///////////////////////////////////////////////////////////////////////////////
42
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000043static void set_zero(void* dst, size_t bytes) {
44 char* ptr = (char*)dst;
45 for (size_t i = 0; i < bytes; ++i) {
46 ptr[i] = 0;
47 }
48}
49
50#define MAX_ALIGNMENT 64
51#define MAX_COUNT ((MAX_ALIGNMENT) * 32)
52#define PAD 32
53#define TOTAL (PAD + MAX_ALIGNMENT + MAX_COUNT + PAD)
54
55#define VALUE16 0x1234
56#define VALUE32 0x12345678
57
58static bool compare16(const uint16_t base[], uint16_t value, int count) {
59 for (int i = 0; i < count; ++i) {
60 if (base[i] != value) {
61 SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
62 return false;
63 }
64 }
65 return true;
66}
67
68static bool compare32(const uint32_t base[], uint32_t value, int count) {
69 for (int i = 0; i < count; ++i) {
70 if (base[i] != value) {
71 SkDebugf("[%d] expected %x found %x\n", i, value, base[i]);
72 return false;
73 }
74 }
75 return true;
76}
77
78static void test_16(skiatest::Reporter* reporter) {
79 uint16_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000081 for (int count = 0; count < MAX_COUNT; ++count) {
82 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
83 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000085 uint16_t* base = &buffer[PAD + alignment];
86 sk_memset16(base, VALUE16, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +000087
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000088 REPORTER_ASSERT(reporter,
89 compare16(buffer, 0, PAD + alignment) &&
90 compare16(base, VALUE16, count) &&
91 compare16(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000092 }
93 }
94}
95
96static void test_32(skiatest::Reporter* reporter) {
97 uint32_t buffer[TOTAL];
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +000099 for (int count = 0; count < MAX_COUNT; ++count) {
100 for (int alignment = 0; alignment < MAX_ALIGNMENT; ++alignment) {
101 set_zero(buffer, sizeof(buffer));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000103 uint32_t* base = &buffer[PAD + alignment];
104 sk_memset32(base, VALUE32, count);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000105
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000106 REPORTER_ASSERT(reporter,
107 compare32(buffer, 0, PAD + alignment) &&
108 compare32(base, VALUE32, count) &&
109 compare32(base + count, 0, TOTAL - count - PAD - alignment));
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000110 }
111 }
112}
113
114/**
115 * Test sk_memset16 and sk_memset32.
116 * For performance considerations, implementations may take different paths
117 * depending on the alignment of the dst, and/or the size of the count.
118 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000119DEF_TEST(Memset, reporter) {
mike@reedtribe.orgc52b1922012-01-07 03:49:13 +0000120 test_16(reporter);
121 test_32(reporter);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
reed@google.comebd24962012-05-17 14:28:11 +0000123 test_chunkalloc(reporter);
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000124}