OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkTemplates.h" | 8 #include "SkTemplates.h" |
9 #include "Test.h" | 9 #include "Test.h" |
10 | 10 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 array.realloc(3); | 63 array.realloc(3); |
64 array[0] = 1; | 64 array[0] = 1; |
65 REPORTER_ASSERT(reporter, array[0] == 1); | 65 REPORTER_ASSERT(reporter, array[0] == 1); |
66 array.realloc(1); | 66 array.realloc(1); |
67 REPORTER_ASSERT(reporter, array[0] == 1); | 67 REPORTER_ASSERT(reporter, array[0] == 1); |
68 } | 68 } |
69 | 69 |
70 DEF_TEST(Templates, reporter) { | 70 DEF_TEST(Templates, reporter) { |
71 test_automalloc_realloc(reporter); | 71 test_automalloc_realloc(reporter); |
72 } | 72 } |
| 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 ty
pe 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>(report
er); |
| 105 } |
| 106 |
| 107 // Ensures that realloc(0) results in a null pointer. |
| 108 template<typename TAutoMalloc> static void test_realloc_to_zero(skiatest::Report
er* 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 } |
OLD | NEW |