test_uninitialized.cpp revision 2a4077a9186d6f6dff104bbd1a73484aae6d5c01
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
39namespace android {
40using std::uninitialized_copy;
41using std::uninitialized_fill;
42
43bool testCopyPod()
44{
45    int src[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
46    const int size = ARRAYSIZE(src);
47    int dest[size] = {0, };
48
49    EXPECT_TRUE(uninitialized_copy(src, src + size, dest) == dest);
50
51    EXPECT_TRUE(std::equal(src, src + size, dest));
52    return true;
53}
54
55
56bool testCopyPodOverflow()
57{
58    int src, dest;
59
60    // Should not crash
61    EXPECT_TRUE(uninitialized_copy(&src, &src + kMaxSizeT / sizeof(src) + 1, &dest) == &dest);
62    return true;
63}
64
65bool testCopyClass()
66{
67    const size_t kSize = 100;
68    CtorDtorCounter::reset();
69
70    CtorDtorCounter src[kSize];
71    CtorDtorCounter *dest = static_cast<CtorDtorCounter*>(malloc(kSize * sizeof(CtorDtorCounter)));
72
73    EXPECT_TRUE(CtorDtorCounter::mCtorCount == kSize);
74    EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0);
75    EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
76
77    CtorDtorCounter::reset();
78
79    EXPECT_TRUE(uninitialized_copy(src, src + kSize, dest) == dest);
80
81    EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
82    EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == kSize);
83    EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
84    free(dest);
85    return true;
86}
87
88struct A {};
89bool testCopyArray()
90{
91    {
92        const A src[] = {A()};
93        A one;
94        A *dest = &one;
95
96        EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest);
97    }
98    {
99        A src[] = {A()};
100        A one;
101        A *dest = &one;
102
103        EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest);
104    }
105    {
106        const A src[] = {A()};
107        A dest[1];
108
109        EXPECT_TRUE(uninitialized_copy(src, src + 1, dest) == dest);
110    }
111    return true;
112}
113
114bool testFillChar()
115{
116    const char src[] = {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'};
117    const int size = ARRAYSIZE(src);
118    char dest[size];
119
120    uninitialized_fill(dest, dest + size, 'a');
121
122    EXPECT_TRUE(std::equal(dest, dest + size, src));
123    return true;
124}
125
126bool testFillPod()
127{
128    const int src[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
129    const int size = ARRAYSIZE(src);
130    int dest[size];
131
132    uninitialized_fill(dest, dest + size, 10);
133
134    EXPECT_TRUE(std::equal(dest, dest + size, src));
135    return true;
136}
137
138bool testFillClass()
139{
140    const size_t kSize = 100;
141    CtorDtorCounter::reset();
142
143    CtorDtorCounter src;
144    CtorDtorCounter *dest = static_cast<CtorDtorCounter*>(malloc(kSize * sizeof(CtorDtorCounter)));
145
146    EXPECT_TRUE(CtorDtorCounter::mCtorCount == 1);
147    EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == 0);
148    EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
149
150    CtorDtorCounter::reset();
151
152    uninitialized_fill(dest, dest + kSize, src);
153
154    EXPECT_TRUE(CtorDtorCounter::mCtorCount == 0);
155    EXPECT_TRUE(CtorDtorCounter::mCopyCtorCount == kSize);
156    EXPECT_TRUE(CtorDtorCounter::mDtorCount == 0);
157    free(dest);
158    return true;
159}
160
161
162}  // namespace android
163
164int main(int argc, char **argv)
165{
166    // copy
167    FAIL_UNLESS(testCopyPod);
168    FAIL_UNLESS(testCopyPodOverflow);
169    FAIL_UNLESS(testCopyClass);
170    FAIL_UNLESS(testCopyArray);
171    // fill
172    FAIL_UNLESS(testFillChar);
173    FAIL_UNLESS(testFillPod);
174    FAIL_UNLESS(testFillClass);
175
176    return kPassed;
177}
178