xref: /aosp_15_r20/external/skia/tests/TemplatesTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 
8 #include "include/private/base/SkTemplates.h"
9 #include "tests/Test.h"
10 
11 #include <cstddef>
12 #include <utility>
13 
14 using namespace skia_private;
15 
16 // Tests for some of the helpers in SkTemplates.h
test_automalloc_realloc(skiatest::Reporter * reporter)17 static void test_automalloc_realloc(skiatest::Reporter* reporter) {
18     AutoSTMalloc<1, int> array;
19 
20     // test we have a valid pointer, should not crash
21     array[0] = 1;
22     REPORTER_ASSERT(reporter, array[0] == 1);
23 
24     // using realloc for init
25     array.realloc(1);
26 
27     array[0] = 1;
28     REPORTER_ASSERT(reporter, array[0] == 1);
29 
30     // verify realloc can grow
31     array.realloc(2);
32     REPORTER_ASSERT(reporter, array[0] == 1);
33 
34     // realloc can shrink
35     array.realloc(1);
36     REPORTER_ASSERT(reporter, array[0] == 1);
37 
38     // should not crash
39     array.realloc(0);
40 
41     // grow and shrink again
42     array.realloc(10);
43     for (int i = 0; i < 10; i++) {
44         array[i] = 10 - i;
45     }
46     array.realloc(20);
47     for (int i = 0; i < 10; i++) {
48         REPORTER_ASSERT(reporter, array[i] == 10 - i);
49     }
50     array.realloc(10);
51     for (int i = 0; i < 10; i++) {
52         REPORTER_ASSERT(reporter, array[i] == 10 - i);
53     }
54 
55     array.realloc(1);
56     REPORTER_ASSERT(reporter, array[0] = 10);
57 
58     // resets mixed with realloc, below stack alloc size
59     array.reset(0);
60     array.realloc(1);
61     array.reset(1);
62 
63     array[0] = 1;
64     REPORTER_ASSERT(reporter, array[0] == 1);
65 
66     // reset and realloc > stack size
67     array.reset(2);
68     array.realloc(3);
69     array[0] = 1;
70     REPORTER_ASSERT(reporter, array[0] == 1);
71     array.realloc(1);
72     REPORTER_ASSERT(reporter, array[0] == 1);
73 }
74 
DEF_TEST(Templates,reporter)75 DEF_TEST(Templates, reporter) {
76     test_automalloc_realloc(reporter);
77 }
78 
79 constexpr int static kStackPreallocCount = 10;
80 
81 // Ensures the containers in SkTemplates.h all have a consistent api.
82 template<typename TContainer, typename TCount>
test_container_apis(skiatest::Reporter * reporter)83 static void test_container_apis(skiatest::Reporter* reporter) {
84     REPORTER_ASSERT(reporter, !TContainer((TCount)0).get());
85     REPORTER_ASSERT(reporter, !TContainer((TCount)0).data());
86     REPORTER_ASSERT(reporter, TContainer((TCount)1).get());
87     REPORTER_ASSERT(reporter, TContainer((TCount)1).data());
88     REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).get());
89     REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).data());
90     REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).get());
91     REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).data());
92 
93     TContainer container;
94     // The default constructor may or may not init to empty, depending on the type of container.
95 
96     container.reset((TCount)1);
97     REPORTER_ASSERT(reporter, container.get());
98     REPORTER_ASSERT(reporter, container.get() == container.data());
99 
100     container.reset((TCount)kStackPreallocCount);
101     REPORTER_ASSERT(reporter, container.get());
102     REPORTER_ASSERT(reporter, container.get() == container.data());
103 
104     container.reset((TCount)kStackPreallocCount + 1);
105     REPORTER_ASSERT(reporter, container.get());
106     REPORTER_ASSERT(reporter, container.get() == container.data());
107 
108     container.reset((TCount)0);
109     REPORTER_ASSERT(reporter, !container.get());
110     REPORTER_ASSERT(reporter, !container.data());
111 }
112 
DEF_TEST(TemplateContainerAPIs,reporter)113 DEF_TEST(TemplateContainerAPIs, reporter) {
114     test_container_apis<AutoTArray<int>, int>(reporter);
115     test_container_apis<AutoSTArray<kStackPreallocCount, int>, int>(reporter);
116     test_container_apis<AutoTMalloc<int>, size_t>(reporter);
117     test_container_apis<AutoSTMalloc<kStackPreallocCount, int>, size_t>(reporter);
118 }
119 
120 // Ensures that realloc(0) results in a null pointer.
test_realloc_to_zero(skiatest::Reporter * reporter)121 template<typename TAutoMalloc> static void test_realloc_to_zero(skiatest::Reporter* reporter) {
122     TAutoMalloc autoMalloc(kStackPreallocCount);
123     REPORTER_ASSERT(reporter, autoMalloc.get());
124 
125     autoMalloc.realloc(0);
126     REPORTER_ASSERT(reporter, !autoMalloc.get());
127 
128     autoMalloc.realloc(kStackPreallocCount + 1);
129     REPORTER_ASSERT(reporter, autoMalloc.get());
130 
131     autoMalloc.realloc(0);
132     REPORTER_ASSERT(reporter, !autoMalloc.get());
133 
134     autoMalloc.realloc(kStackPreallocCount);
135     REPORTER_ASSERT(reporter, autoMalloc.get());
136 }
137 
DEF_TEST(AutoReallocToZero,reporter)138 DEF_TEST(AutoReallocToZero, reporter) {
139     test_realloc_to_zero<AutoTMalloc<int> >(reporter);
140     test_realloc_to_zero<AutoSTMalloc<kStackPreallocCount, int> >(reporter);
141 }
142 
DEF_TEST(AutoTMallocSelfMove,r)143 DEF_TEST(AutoTMallocSelfMove, r) {
144 #if defined(__clang__)
145     #pragma clang diagnostic push
146     #pragma clang diagnostic ignored "-Wself-move"
147 #elif defined(__GNUC__) && __GNUC__ >= 13
148     #pragma GCC diagnostic push
149     #pragma GCC diagnostic ignored "-Wself-move"
150 #endif
151 
152     AutoTMalloc<int> foo(20);
153     REPORTER_ASSERT(r, foo.get());
154 
155     foo = std::move(foo);
156     REPORTER_ASSERT(r, foo.get());  // NOLINT(bugprone-use-after-move)
157 
158 #if defined(__clang__)
159     #pragma clang diagnostic pop
160 #elif defined(__GNUC__) && __GNUC__ >= 13
161     #pragma GCC diagnostic pop
162 #endif
163 }
164