blob: 647ea25283eb65fbac27d8d4c627415d62b9ff94 [file] [log] [blame]
Herb Derbyac04fef2017-01-13 17:34:33 -05001/*
2 * Copyright 2016 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 */
7
8#include "Test.h"
9#include "SkArenaAlloc.h"
10
11namespace {
12
13 static int created, destroyed;
14
15 struct Foo {
16 Foo() : x(-2), y(-3.0f) { created++; }
17 Foo(int X, float Y) : x(X), y(Y) { created++; }
18 ~Foo() { destroyed++; }
19
20 int x;
21 float y;
22 };
23
24 struct Big {
25 Big() {}
26 uint32_t array[128];
27 };
28
29}
30
31struct WithDtor {
32 ~WithDtor() { }
33};
34
35DEF_TEST(ArenaAlloc, r) {
36
37 {
38 created = 0;
39 destroyed = 0;
40
41 SkArenaAlloc arena{nullptr, 0};
42 REPORTER_ASSERT(r, *arena.make<int>(3) == 3);
43 Foo* foo = arena.make<Foo>(3, 4.0f);
44 REPORTER_ASSERT(r, foo->x == 3);
45 REPORTER_ASSERT(r, foo->y == 4.0f);
46 REPORTER_ASSERT(r, created == 1);
47 REPORTER_ASSERT(r, destroyed == 0);
48 arena.makeArrayDefault<int>(10);
49 int* zeroed = arena.makeArray<int>(10);
50 for (int i = 0; i < 10; i++) {
51 REPORTER_ASSERT(r, zeroed[i] == 0);
52 }
53 Foo* fooArray = arena.makeArrayDefault<Foo>(10);
54 REPORTER_ASSERT(r, fooArray[3].x == -2);
55 REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
56 REPORTER_ASSERT(r, created == 11);
57 REPORTER_ASSERT(r, destroyed == 0);
58 arena.make<typename std::aligned_storage<10,8>::type>();
59 }
60 REPORTER_ASSERT(r, created == 11);
61 REPORTER_ASSERT(r, destroyed == 11);
62
63 {
64 created = 0;
65 destroyed = 0;
Herb Derby15172242017-01-19 03:32:23 +000066 char block[1024];
Herb Derbyac04fef2017-01-13 17:34:33 -050067 SkArenaAlloc arena{block};
68
69 REPORTER_ASSERT(r, *arena.make<int>(3) == 3);
70 Foo* foo = arena.make<Foo>(3, 4.0f);
71 REPORTER_ASSERT(r, foo->x == 3);
72 REPORTER_ASSERT(r, foo->y == 4.0f);
73 REPORTER_ASSERT(r, created == 1);
74 REPORTER_ASSERT(r, destroyed == 0);
75 arena.makeArrayDefault<int>(10);
76 int* zeroed = arena.makeArray<int>(10);
77 for (int i = 0; i < 10; i++) {
78 REPORTER_ASSERT(r, zeroed[i] == 0);
79 }
80 Foo* fooArray = arena.makeArrayDefault<Foo>(10);
81 REPORTER_ASSERT(r, fooArray[3].x == -2);
82 REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
83 REPORTER_ASSERT(r, created == 11);
84 REPORTER_ASSERT(r, destroyed == 0);
85 arena.make<typename std::aligned_storage<10,8>::type>();
86 }
87 REPORTER_ASSERT(r, created == 11);
88 REPORTER_ASSERT(r, destroyed == 11);
89
90 {
91 created = 0;
92 destroyed = 0;
93 std::unique_ptr<char[]> block{new char[1024]};
94 SkArenaAlloc arena{block.get(), 1024};
95
96 REPORTER_ASSERT(r, *arena.make<int>(3) == 3);
97 Foo* foo = arena.make<Foo>(3, 4.0f);
98 REPORTER_ASSERT(r, foo->x == 3);
99 REPORTER_ASSERT(r, foo->y == 4.0f);
100 REPORTER_ASSERT(r, created == 1);
101 REPORTER_ASSERT(r, destroyed == 0);
102 arena.makeArrayDefault<int>(10);
103 int* zeroed = arena.makeArray<int>(10);
104 for (int i = 0; i < 10; i++) {
105 REPORTER_ASSERT(r, zeroed[i] == 0);
106 }
107 Foo* fooArray = arena.makeArrayDefault<Foo>(10);
108 REPORTER_ASSERT(r, fooArray[3].x == -2);
109 REPORTER_ASSERT(r, fooArray[4].y == -3.0f);
110 REPORTER_ASSERT(r, created == 11);
111 REPORTER_ASSERT(r, destroyed == 0);
112 arena.make<typename std::aligned_storage<10,8>::type>();
113 }
114 REPORTER_ASSERT(r, created == 11);
115 REPORTER_ASSERT(r, destroyed == 11);
Herb Derbyac04fef2017-01-13 17:34:33 -0500116}