test_algorithm.cpp revision 42a884c6bc4ab780b0f69ced0d0beebaca3e0dfe
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#include "../include/algorithm"
31#ifndef ANDROID_ASTL_ALGORITHM__
32#error "Wrong header included!!"
33#endif
34#include "macros.h"
35
36namespace android {
37
38bool testSwapInt()
39{
40    int a = 5;
41    int b = 10;
42
43    std::swap(a, b);
44    EXPECT_TRUE(a == 10);
45    EXPECT_TRUE(b == 5);
46    return true;
47}
48
49bool testMin()
50{
51    int a = 5;
52    int b = 10;
53
54    int c = std::min(a, b);
55    EXPECT_TRUE(c == 5);
56    c = std::min(b, a);
57    EXPECT_TRUE(c == 5);
58    return true;
59}
60
61bool testMax()
62{
63    int a = 5;
64    int b = 10;
65
66    int c = std::max(a, b);
67    EXPECT_TRUE(c == 10);
68    c = std::max(b, a);
69    EXPECT_TRUE(c == 10);
70    return true;
71}
72
73bool testFill()
74{
75    int array[10];
76
77    std::fill(array, array + 10, 0xaa);
78    EXPECT_TRUE(array[0] == 0xaa);
79    EXPECT_TRUE(array[1] == 0xaa);
80    EXPECT_TRUE(array[2] == 0xaa);
81    EXPECT_TRUE(array[3] == 0xaa);
82    EXPECT_TRUE(array[4] == 0xaa);
83    EXPECT_TRUE(array[5] == 0xaa);
84    EXPECT_TRUE(array[6] == 0xaa);
85    EXPECT_TRUE(array[7] == 0xaa);
86    EXPECT_TRUE(array[8] == 0xaa);
87    EXPECT_TRUE(array[9] == 0xaa);
88    return true;
89}
90
91bool testFill_N()
92{
93    int array[10];
94
95    int *ret = std::fill_n(array, 10, 0xaa);
96    EXPECT_TRUE(array[0] == 0xaa);
97    EXPECT_TRUE(array[1] == 0xaa);
98    EXPECT_TRUE(array[2] == 0xaa);
99    EXPECT_TRUE(array[3] == 0xaa);
100    EXPECT_TRUE(array[4] == 0xaa);
101    EXPECT_TRUE(array[5] == 0xaa);
102    EXPECT_TRUE(array[6] == 0xaa);
103    EXPECT_TRUE(array[7] == 0xaa);
104    EXPECT_TRUE(array[8] == 0xaa);
105    EXPECT_TRUE(array[9] == 0xaa);
106    EXPECT_TRUE(ret == array + 10);
107
108    char array2[1] = { '\0' };
109    signed char sc = 1;
110
111    std::fill_n(array2, 1, sc);
112    EXPECT_TRUE(array2[0] == 1 );
113
114    return true;
115}
116
117struct Left1 { };
118struct Right1 { };
119bool operator==(const Left1&, const Right1&) {return true;}
120
121struct Left2 { };
122struct Right2 { };
123bool predicate(const Left2&, const Right2&) {return true;}
124
125bool testEqual()
126{
127    Left1 left1;
128    Right1 right1;
129    Left2 left2;
130    Right2 right2;
131
132    EXPECT_TRUE(std::equal(&left1, &left1, &right1));
133    EXPECT_TRUE(std::equal(&left2, &left2, &right2, predicate));
134    return true;
135}
136
137}  // namespace android
138
139int main(int argc, char **argv)
140{
141    FAIL_UNLESS(testSwapInt);
142    FAIL_UNLESS(testMin);
143    FAIL_UNLESS(testMax);
144    FAIL_UNLESS(testFill);
145    FAIL_UNLESS(testFill_N);
146    FAIL_UNLESS(testEqual);
147    return kPassed;
148}
149