1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/private/SkTemplates.h"
9cb93a386Sopenharmony_ci#include "tests/Test.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci// Tests for some of the helpers in SkTemplates.h
12cb93a386Sopenharmony_cistatic void test_automalloc_realloc(skiatest::Reporter* reporter) {
13cb93a386Sopenharmony_ci    SkAutoSTMalloc<1, int> array;
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci    // test we have a valid pointer, should not crash
16cb93a386Sopenharmony_ci    array[0] = 1;
17cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ci    // using realloc for init
20cb93a386Sopenharmony_ci    array.realloc(1);
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci    array[0] = 1;
23cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    // verify realloc can grow
26cb93a386Sopenharmony_ci    array.realloc(2);
27cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci    // realloc can shrink
30cb93a386Sopenharmony_ci    array.realloc(1);
31cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    // should not crash
34cb93a386Sopenharmony_ci    array.realloc(0);
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    // grow and shrink again
37cb93a386Sopenharmony_ci    array.realloc(10);
38cb93a386Sopenharmony_ci    for (int i = 0; i < 10; i++) {
39cb93a386Sopenharmony_ci        array[i] = 10 - i;
40cb93a386Sopenharmony_ci    }
41cb93a386Sopenharmony_ci    array.realloc(20);
42cb93a386Sopenharmony_ci    for (int i = 0; i < 10; i++) {
43cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, array[i] == 10 - i);
44cb93a386Sopenharmony_ci    }
45cb93a386Sopenharmony_ci    array.realloc(10);
46cb93a386Sopenharmony_ci    for (int i = 0; i < 10; i++) {
47cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, array[i] == 10 - i);
48cb93a386Sopenharmony_ci    }
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci    array.realloc(1);
51cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] = 10);
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    // resets mixed with realloc, below stack alloc size
54cb93a386Sopenharmony_ci    array.reset(0);
55cb93a386Sopenharmony_ci    array.realloc(1);
56cb93a386Sopenharmony_ci    array.reset(1);
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    array[0] = 1;
59cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci    // reset and realloc > stack size
62cb93a386Sopenharmony_ci    array.reset(2);
63cb93a386Sopenharmony_ci    array.realloc(3);
64cb93a386Sopenharmony_ci    array[0] = 1;
65cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
66cb93a386Sopenharmony_ci    array.realloc(1);
67cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, array[0] == 1);
68cb93a386Sopenharmony_ci}
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ciDEF_TEST(Templates, reporter) {
71cb93a386Sopenharmony_ci    test_automalloc_realloc(reporter);
72cb93a386Sopenharmony_ci}
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ciconstexpr int static kStackPreallocCount = 10;
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_ci// Ensures the containers in SkTemplates.h all have a consistent api.
77cb93a386Sopenharmony_citemplate<typename TContainer, typename TCount>
78cb93a386Sopenharmony_cistatic void test_container_apis(skiatest::Reporter* reporter) {
79cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !TContainer((TCount)0).get());
80cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !TContainer((TCount)0).data());
81cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, TContainer((TCount)1).get());
82cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, TContainer((TCount)1).data());
83cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).get());
84cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).data());
85cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).get());
86cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).data());
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    TContainer container;
89cb93a386Sopenharmony_ci    // The default constructor may or may not init to empty, depending on the type of container.
90cb93a386Sopenharmony_ci
91cb93a386Sopenharmony_ci    container.reset((TCount)1);
92cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, container.get());
93cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, container.get() == container.data());
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci    container.reset((TCount)kStackPreallocCount);
96cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, container.get());
97cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, container.get() == container.data());
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci    container.reset((TCount)kStackPreallocCount + 1);
100cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, container.get());
101cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, container.get() == container.data());
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    container.reset((TCount)0);
104cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !container.get());
105cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !container.data());
106cb93a386Sopenharmony_ci}
107cb93a386Sopenharmony_ci
108cb93a386Sopenharmony_ciDEF_TEST(TemplateContainerAPIs, reporter) {
109cb93a386Sopenharmony_ci    test_container_apis<SkAutoTArray<int>, int>(reporter);
110cb93a386Sopenharmony_ci    test_container_apis<SkAutoSTArray<kStackPreallocCount, int>, int>(reporter);
111cb93a386Sopenharmony_ci    test_container_apis<SkAutoTMalloc<int>, size_t>(reporter);
112cb93a386Sopenharmony_ci    test_container_apis<SkAutoSTMalloc<kStackPreallocCount, int>, size_t>(reporter);
113cb93a386Sopenharmony_ci}
114cb93a386Sopenharmony_ci
115cb93a386Sopenharmony_ci// Ensures that realloc(0) results in a null pointer.
116cb93a386Sopenharmony_citemplate<typename TAutoMalloc> static void test_realloc_to_zero(skiatest::Reporter* reporter) {
117cb93a386Sopenharmony_ci    TAutoMalloc autoMalloc(kStackPreallocCount);
118cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, autoMalloc.get());
119cb93a386Sopenharmony_ci
120cb93a386Sopenharmony_ci    autoMalloc.realloc(0);
121cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !autoMalloc.get());
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci    autoMalloc.realloc(kStackPreallocCount + 1);
124cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, autoMalloc.get());
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci    autoMalloc.realloc(0);
127cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !autoMalloc.get());
128cb93a386Sopenharmony_ci
129cb93a386Sopenharmony_ci    autoMalloc.realloc(kStackPreallocCount);
130cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, autoMalloc.get());
131cb93a386Sopenharmony_ci}
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_ciDEF_TEST(AutoReallocToZero, reporter) {
134cb93a386Sopenharmony_ci    test_realloc_to_zero<SkAutoTMalloc<int> >(reporter);
135cb93a386Sopenharmony_ci    test_realloc_to_zero<SkAutoSTMalloc<kStackPreallocCount, int> >(reporter);
136cb93a386Sopenharmony_ci}
137cb93a386Sopenharmony_ci
138cb93a386Sopenharmony_ciDEF_TEST(SkAutoTMallocSelfMove, r) {
139cb93a386Sopenharmony_ci#if defined(__clang__)
140cb93a386Sopenharmony_ci    #pragma clang diagnostic push
141cb93a386Sopenharmony_ci    #pragma clang diagnostic ignored "-Wself-move"
142cb93a386Sopenharmony_ci#endif
143cb93a386Sopenharmony_ci
144cb93a386Sopenharmony_ci    SkAutoTMalloc<int> foo(20);
145cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, foo.get());
146cb93a386Sopenharmony_ci
147cb93a386Sopenharmony_ci    foo = std::move(foo);
148cb93a386Sopenharmony_ci    REPORTER_ASSERT(r, foo.get());  // NOLINT(bugprone-use-after-move)
149cb93a386Sopenharmony_ci
150cb93a386Sopenharmony_ci#if defined(__clang__)
151cb93a386Sopenharmony_ci    #pragma clang diagnostic pop
152cb93a386Sopenharmony_ci#endif
153cb93a386Sopenharmony_ci}
154