joshualitt | f1f8895 | 2015-04-08 07:33:33 -0700 | [diff] [blame] | 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 "SkTemplates.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | // Tests for some of the helpers in SkTemplates.h |
| 12 | static 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 | |
| 70 | DEF_TEST(Templates, reporter) { |
| 71 | test_automalloc_realloc(reporter); |
| 72 | } |
csmartdalton | d0e402f | 2016-06-23 12:55:14 -0700 | [diff] [blame] | 73 | |
| 74 | constexpr int static kStackPreallocCount = 10; |
| 75 | |
| 76 | // Ensures the containers in SkTemplates.h all have a consistent api. |
| 77 | template<typename TContainer, typename TCount> |
| 78 | static void test_container_apis(skiatest::Reporter* reporter) { |
| 79 | REPORTER_ASSERT(reporter, !TContainer((TCount)0).get()); |
| 80 | REPORTER_ASSERT(reporter, TContainer((TCount)1).get()); |
| 81 | REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).get()); |
| 82 | REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).get()); |
| 83 | |
| 84 | TContainer container; |
| 85 | // The default constructor may or may not init to empty, depending on the type of container. |
| 86 | |
| 87 | container.reset((TCount)1); |
| 88 | REPORTER_ASSERT(reporter, container.get()); |
| 89 | |
| 90 | container.reset((TCount)kStackPreallocCount); |
| 91 | REPORTER_ASSERT(reporter, container.get()); |
| 92 | |
| 93 | container.reset((TCount)kStackPreallocCount + 1); |
| 94 | REPORTER_ASSERT(reporter, container.get()); |
| 95 | |
| 96 | container.reset((TCount)0); |
| 97 | REPORTER_ASSERT(reporter, !container.get()); |
| 98 | } |
| 99 | |
| 100 | DEF_TEST(TemplateContainerAPIs, reporter) { |
| 101 | test_container_apis<SkAutoTArray<int>, int>(reporter); |
| 102 | test_container_apis<SkAutoSTArray<kStackPreallocCount, int>, int>(reporter); |
| 103 | test_container_apis<SkAutoTMalloc<int>, size_t>(reporter); |
| 104 | test_container_apis<SkAutoSTMalloc<kStackPreallocCount, int>, size_t>(reporter); |
| 105 | } |
| 106 | |
| 107 | // Ensures that realloc(0) results in a null pointer. |
| 108 | template<typename TAutoMalloc> static void test_realloc_to_zero(skiatest::Reporter* reporter) { |
| 109 | TAutoMalloc autoMalloc(kStackPreallocCount); |
| 110 | REPORTER_ASSERT(reporter, autoMalloc.get()); |
| 111 | |
| 112 | autoMalloc.realloc(0); |
| 113 | REPORTER_ASSERT(reporter, !autoMalloc.get()); |
| 114 | |
| 115 | autoMalloc.realloc(kStackPreallocCount + 1); |
| 116 | REPORTER_ASSERT(reporter, autoMalloc.get()); |
| 117 | |
| 118 | autoMalloc.realloc(0); |
| 119 | REPORTER_ASSERT(reporter, !autoMalloc.get()); |
| 120 | |
| 121 | autoMalloc.realloc(kStackPreallocCount); |
| 122 | REPORTER_ASSERT(reporter, autoMalloc.get()); |
| 123 | } |
| 124 | |
| 125 | DEF_TEST(AutoReallocToZero, reporter) { |
| 126 | test_realloc_to_zero<SkAutoTMalloc<int> >(reporter); |
| 127 | test_realloc_to_zero<SkAutoSTMalloc<kStackPreallocCount, int> >(reporter); |
| 128 | } |
Mike Klein | 149e42e | 2017-04-07 14:18:29 -0400 | [diff] [blame] | 129 | |
| 130 | DEF_TEST(SkAutoTMallocSelfMove, r) { |
| 131 | #if defined(__clang__) |
| 132 | #pragma clang diagnostic push |
| 133 | #pragma clang diagnostic ignored "-Wself-move" |
| 134 | #endif |
| 135 | |
| 136 | SkAutoTMalloc<int> foo(20); |
| 137 | REPORTER_ASSERT(r, foo.get()); |
| 138 | |
| 139 | foo = std::move(foo); |
| 140 | REPORTER_ASSERT(r, foo.get()); |
| 141 | |
| 142 | #if defined(__clang__) |
| 143 | #pragma clang diagnostic pop |
| 144 | #endif |
| 145 | } |