1 /* -*- c++ -*- */ 2 /* 3 * Copyright (C) 2009 The Android Open Source Project 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 31 #include "../include/memory" 32 #ifndef ANDROID_ASTL_MEMORY__ 33 #error "Wrong header included!!" 34 #endif 35 #include "common.h" 36 #include <algorithm> 37 #include <cstdlib> 38 39 namespace android { 40 using std::uninitialized_copy; 41 using std::uninitialized_fill; 42 43 bool testCopyPod() 44 { 45 { 46 int src[0]; 47 const int size = ARRAYSIZE(src); 48 int dest[10] = {0, }; 49 EXPECT_TRUE(uninitialized_copy(src, src + size, dest) == dest); 50 } 51 { 52 int src[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 53 const int size = ARRAYSIZE(src); 54 int dest[size] = {0, }; 55 56 EXPECT_TRUE(uninitialized_copy(src, src + size, dest) == dest + size); 57 58 EXPECT_TRUE(std::equal(src, src + size, dest)); 59 } 60 return true; 61 } 62 63 64 bool testCopyPodOverflow() 65 { 66 int src, dest; 67 68 // Should not crash 69 EXPECT_TRUE( 70 uninitialized_copy(&src, &src + kMaxSizeT / sizeof(src) + 1, &dest) == 71 &dest); 72 return true; 73 } 74 75 bool testCopyClass() 76 { 77 const size_t kSize = 100; 78 CtorDtorCounter::reset(); 79 80 CtorDtorCounter src[kSize]; 81 CtorDtorCounter *dest = static_cast<CtorDtorCounter*>( 82 malloc(kSize * sizeof(CtorDtorCounter))); 83 84 EXPECT_TRUE(CtorDtorCounter::mCtorCount == kSize); 85 EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0); 86 EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0); 87 88 CtorDtorCounter::reset(); 89 90 EXPECT_TRUE(uninitialized_copy(src, src + kSize, dest) == dest + kSize); 91 92 EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0); 93 EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == kSize); 94 EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0); 95 free(dest); 96 return true; 97 } 98 99 struct A {}; 100 bool testCopyArray() 101 { 102 { 103 A src[0]; 104 A one; 105 A *dest = &one; 106 // Empty, dest should not have moved. 107 EXPECT_TRUE(uninitialized_copy(src, src, dest) == dest); 108 } 109 { 110 const A src[] = {A()}; 111 A one; 112 A *dest = &one; 113 114 EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest + 1); 115 } 116 { 117 A src[] = {A()}; 118 A one; 119 A *dest = &one; 120 121 EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest + 1); 122 } 123 { 124 const A src[] = {A()}; 125 A dest[1]; 126 127 EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest + 1); 128 } 129 return true; 130 } 131 132 bool testFillChar() 133 { 134 const char src[] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'}; 135 const int size = ARRAYSIZE(src); 136 char dest[size]; 137 138 uninitialized_fill(dest, dest + size, 'a'); 139 140 EXPECT_TRUE(std::equal(dest, dest + size, src)); 141 return true; 142 } 143 144 bool testFillPod() 145 { 146 const int src[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}; 147 const int size = ARRAYSIZE(src); 148 int dest[size]; 149 150 uninitialized_fill(dest, dest + size, 10); 151 152 EXPECT_TRUE(std::equal(dest, dest + size, src)); 153 return true; 154 } 155 156 bool testFillClass() 157 { 158 const size_t kSize = 100; 159 CtorDtorCounter::reset(); 160 161 CtorDtorCounter src; 162 CtorDtorCounter *dest = static_cast<CtorDtorCounter*>( 163 malloc(kSize * sizeof(CtorDtorCounter))); 164 165 EXPECT_TRUE(CtorDtorCounter::mCtorCount == 1); 166 EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0); 167 EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0); 168 169 CtorDtorCounter::reset(); 170 171 uninitialized_fill(dest, dest + kSize, src); 172 173 EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0); 174 EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == kSize); 175 EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0); 176 free(dest); 177 return true; 178 } 179 180 181 } // namespace android 182 183 int main(int argc, char **argv) 184 { 185 // copy 186 FAIL_UNLESS(testCopyPod); 187 FAIL_UNLESS(testCopyPodOverflow); 188 FAIL_UNLESS(testCopyClass); 189 FAIL_UNLESS(testCopyArray); 190 // fill 191 FAIL_UNLESS(testFillChar); 192 FAIL_UNLESS(testFillPod); 193 FAIL_UNLESS(testFillClass); 194 195 return kPassed; 196 } 197