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