blob: f073b4c3e9474bf4f4205ba90fcdbd3c7e120a83 [file] [log] [blame]
joshualittf1f88952015-04-08 07:33:33 -07001/*
2 * Copyright 2015 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkTemplates.h"
9#include "tests/Test.h"
joshualittf1f88952015-04-08 07:33:33 -070010
11// Tests for some of the helpers in SkTemplates.h
12static void test_automalloc_realloc(skiatest::Reporter* reporter) {
13 SkAutoSTMalloc<1, int> array;
14
15 // test we have a valid pointer, should not crash
16 array[0] = 1;
17 REPORTER_ASSERT(reporter, array[0] == 1);
18
19 // using realloc for init
20 array.realloc(1);
21
22 array[0] = 1;
23 REPORTER_ASSERT(reporter, array[0] == 1);
24
25 // verify realloc can grow
26 array.realloc(2);
27 REPORTER_ASSERT(reporter, array[0] == 1);
28
29 // realloc can shrink
30 array.realloc(1);
31 REPORTER_ASSERT(reporter, array[0] == 1);
32
33 // should not crash
34 array.realloc(0);
35
36 // grow and shrink again
37 array.realloc(10);
38 for (int i = 0; i < 10; i++) {
39 array[i] = 10 - i;
40 }
41 array.realloc(20);
42 for (int i = 0; i < 10; i++) {
43 REPORTER_ASSERT(reporter, array[i] == 10 - i);
44 }
45 array.realloc(10);
46 for (int i = 0; i < 10; i++) {
47 REPORTER_ASSERT(reporter, array[i] == 10 - i);
48 }
49
50 array.realloc(1);
51 REPORTER_ASSERT(reporter, array[0] = 10);
52
53 // resets mixed with realloc, below stack alloc size
54 array.reset(0);
55 array.realloc(1);
56 array.reset(1);
57
58 array[0] = 1;
59 REPORTER_ASSERT(reporter, array[0] == 1);
60
61 // reset and realloc > stack size
62 array.reset(2);
63 array.realloc(3);
64 array[0] = 1;
65 REPORTER_ASSERT(reporter, array[0] == 1);
66 array.realloc(1);
67 REPORTER_ASSERT(reporter, array[0] == 1);
68}
69
70DEF_TEST(Templates, reporter) {
71 test_automalloc_realloc(reporter);
72}
csmartdaltond0e402f2016-06-23 12:55:14 -070073
74constexpr int static kStackPreallocCount = 10;
75
76// Ensures the containers in SkTemplates.h all have a consistent api.
77template<typename TContainer, typename TCount>
78static void test_container_apis(skiatest::Reporter* reporter) {
79 REPORTER_ASSERT(reporter, !TContainer((TCount)0).get());
John Stiles8354e9b2020-10-06 20:28:18 -040080 REPORTER_ASSERT(reporter, !TContainer((TCount)0).data());
csmartdaltond0e402f2016-06-23 12:55:14 -070081 REPORTER_ASSERT(reporter, TContainer((TCount)1).get());
John Stiles8354e9b2020-10-06 20:28:18 -040082 REPORTER_ASSERT(reporter, TContainer((TCount)1).data());
csmartdaltond0e402f2016-06-23 12:55:14 -070083 REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).get());
John Stiles8354e9b2020-10-06 20:28:18 -040084 REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).data());
csmartdaltond0e402f2016-06-23 12:55:14 -070085 REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).get());
John Stiles8354e9b2020-10-06 20:28:18 -040086 REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).data());
csmartdaltond0e402f2016-06-23 12:55:14 -070087
88 TContainer container;
89 // The default constructor may or may not init to empty, depending on the type of container.
90
91 container.reset((TCount)1);
92 REPORTER_ASSERT(reporter, container.get());
John Stiles8354e9b2020-10-06 20:28:18 -040093 REPORTER_ASSERT(reporter, container.get() == container.data());
csmartdaltond0e402f2016-06-23 12:55:14 -070094
95 container.reset((TCount)kStackPreallocCount);
96 REPORTER_ASSERT(reporter, container.get());
John Stiles8354e9b2020-10-06 20:28:18 -040097 REPORTER_ASSERT(reporter, container.get() == container.data());
csmartdaltond0e402f2016-06-23 12:55:14 -070098
99 container.reset((TCount)kStackPreallocCount + 1);
100 REPORTER_ASSERT(reporter, container.get());
John Stiles8354e9b2020-10-06 20:28:18 -0400101 REPORTER_ASSERT(reporter, container.get() == container.data());
csmartdaltond0e402f2016-06-23 12:55:14 -0700102
103 container.reset((TCount)0);
104 REPORTER_ASSERT(reporter, !container.get());
John Stiles8354e9b2020-10-06 20:28:18 -0400105 REPORTER_ASSERT(reporter, !container.data());
csmartdaltond0e402f2016-06-23 12:55:14 -0700106}
107
108DEF_TEST(TemplateContainerAPIs, reporter) {
109 test_container_apis<SkAutoTArray<int>, int>(reporter);
110 test_container_apis<SkAutoSTArray<kStackPreallocCount, int>, int>(reporter);
111 test_container_apis<SkAutoTMalloc<int>, size_t>(reporter);
112 test_container_apis<SkAutoSTMalloc<kStackPreallocCount, int>, size_t>(reporter);
113}
114
115// Ensures that realloc(0) results in a null pointer.
116template<typename TAutoMalloc> static void test_realloc_to_zero(skiatest::Reporter* reporter) {
117 TAutoMalloc autoMalloc(kStackPreallocCount);
118 REPORTER_ASSERT(reporter, autoMalloc.get());
119
120 autoMalloc.realloc(0);
121 REPORTER_ASSERT(reporter, !autoMalloc.get());
122
123 autoMalloc.realloc(kStackPreallocCount + 1);
124 REPORTER_ASSERT(reporter, autoMalloc.get());
125
126 autoMalloc.realloc(0);
127 REPORTER_ASSERT(reporter, !autoMalloc.get());
128
129 autoMalloc.realloc(kStackPreallocCount);
130 REPORTER_ASSERT(reporter, autoMalloc.get());
131}
132
133DEF_TEST(AutoReallocToZero, reporter) {
134 test_realloc_to_zero<SkAutoTMalloc<int> >(reporter);
135 test_realloc_to_zero<SkAutoSTMalloc<kStackPreallocCount, int> >(reporter);
136}
Mike Klein149e42e2017-04-07 14:18:29 -0400137
138DEF_TEST(SkAutoTMallocSelfMove, r) {
139#if defined(__clang__)
140 #pragma clang diagnostic push
141 #pragma clang diagnostic ignored "-Wself-move"
142#endif
143
144 SkAutoTMalloc<int> foo(20);
145 REPORTER_ASSERT(r, foo.get());
146
147 foo = std::move(foo);
Mike Klein16885072018-12-11 09:54:31 -0500148 REPORTER_ASSERT(r, foo.get()); // NOLINT(bugprone-use-after-move)
Mike Klein149e42e2017-04-07 14:18:29 -0400149
150#if defined(__clang__)
151 #pragma clang diagnostic pop
152#endif
153}