Home | History | Annotate | Download | only in tests
      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 "common.h"
     35 
     36 namespace android {
     37 
     38 bool 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 
     49 bool 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 
     61 bool 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 
     73 bool 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 
     91 bool 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 
    117 struct Left1 { };
    118 struct Right1 { };
    119 bool operator==(const Left1&, const Right1&) {return true;}
    120 
    121 struct Left2 { };
    122 struct Right2 { };
    123 bool predicate(const Left2&, const Right2&) {return true;}
    124 
    125 bool 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 bool testCopy()
    138 {
    139     {
    140         int data[] = {1,2,3,4,5,6};
    141         std::copy(data + 2, data + 5, data);
    142         EXPECT_TRUE(data[0] == 3);
    143         EXPECT_TRUE(data[1] == 4);
    144         EXPECT_TRUE(data[2] == 5);
    145         EXPECT_TRUE(data[3] == 4);
    146         EXPECT_TRUE(data[4] == 5);
    147         EXPECT_TRUE(data[5] == 6);
    148     }
    149     return true;
    150 }
    151 
    152 }  // namespace android
    153 
    154 int main(int argc, char **argv)
    155 {
    156     FAIL_UNLESS(testSwapInt);
    157     FAIL_UNLESS(testMin);
    158     FAIL_UNLESS(testMax);
    159     FAIL_UNLESS(testFill);
    160     FAIL_UNLESS(testFill_N);
    161     FAIL_UNLESS(testEqual);
    162     FAIL_UNLESS(testCopy);
    163     return kPassed;
    164 }
    165