Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 //#define LOG_NDEBUG 0
     18 #define LOG_TAG "C2_test"
     19 
     20 #include <gtest/gtest.h>
     21 
     22 #include <C2.h>
     23 
     24 /* ======================================= STATIC TESTS ======================================= */
     25 
     26 template<int N>
     27 struct c2_const_checker
     28 {
     29     inline constexpr static int num() { return N; }
     30 };
     31 
     32 constexpr auto min_i32_i32 = c2_min(int32_t(1), int32_t(2));
     33 static_assert(std::is_same<decltype(min_i32_i32), const int32_t>::value, "should be int32_t");
     34 constexpr auto min_i32_i64 = c2_min(int32_t(3), int64_t(2));
     35 static_assert(std::is_same<decltype(min_i32_i64), const int64_t>::value, "should be int64_t");
     36 constexpr auto min_i8_i32 = c2_min(int8_t(0xff), int32_t(0xffffffff));
     37 static_assert(std::is_same<decltype(min_i8_i32), const int32_t>::value, "should be int32_t");
     38 
     39 static_assert(c2_const_checker<min_i32_i32>::num() == 1, "should be 1");
     40 static_assert(c2_const_checker<min_i32_i64>::num() == 2, "should be 2");
     41 static_assert(c2_const_checker<min_i8_i32>::num() == 0xffffffff, "should be 0xffffffff");
     42 
     43 constexpr auto min_u32_u32 = c2_min(uint32_t(1), uint32_t(2));
     44 static_assert(std::is_same<decltype(min_u32_u32), const uint32_t>::value, "should be uint32_t");
     45 constexpr auto min_u32_u64 = c2_min(uint32_t(3), uint64_t(2));
     46 static_assert(std::is_same<decltype(min_u32_u64), const uint32_t>::value, "should be uint32_t");
     47 constexpr auto min_u32_u8 = c2_min(uint32_t(0xffffffff), uint8_t(0xff));
     48 static_assert(std::is_same<decltype(min_u32_u8), const uint8_t>::value, "should be uint8_t");
     49 
     50 static_assert(c2_const_checker<min_u32_u32>::num() == 1, "should be 1");
     51 static_assert(c2_const_checker<min_u32_u64>::num() == 2, "should be 2");
     52 static_assert(c2_const_checker<min_u32_u8>::num() == 0xff, "should be 0xff");
     53 
     54 constexpr auto max_i32_i32 = c2_max(int32_t(1), int32_t(2));
     55 static_assert(std::is_same<decltype(max_i32_i32), const int32_t>::value, "should be int32_t");
     56 constexpr auto max_i32_i64 = c2_max(int32_t(3), int64_t(2));
     57 static_assert(std::is_same<decltype(max_i32_i64), const int64_t>::value, "should be int64_t");
     58 constexpr auto max_i8_i32 = c2_max(int8_t(0xff), int32_t(0xffffffff));
     59 static_assert(std::is_same<decltype(max_i8_i32), const int32_t>::value, "should be int32_t");
     60 
     61 static_assert(c2_const_checker<max_i32_i32>::num() == 2, "should be 2");
     62 static_assert(c2_const_checker<max_i32_i64>::num() == 3, "should be 3");
     63 static_assert(c2_const_checker<max_i8_i32>::num() == 0xffffffff, "should be 0xffffffff");
     64 
     65 constexpr auto max_u32_u32 = c2_max(uint32_t(1), uint32_t(2));
     66 static_assert(std::is_same<decltype(max_u32_u32), const uint32_t>::value, "should be uint32_t");
     67 constexpr auto max_u32_u64 = c2_max(uint32_t(3), uint64_t(2));
     68 static_assert(std::is_same<decltype(max_u32_u64), const uint64_t>::value, "should be uint64_t");
     69 constexpr auto max_u32_u8 = c2_max(uint32_t(0x7fffffff), uint8_t(0xff));
     70 static_assert(std::is_same<decltype(max_u32_u8), const uint32_t>::value, "should be uint32_t");
     71 
     72 static_assert(c2_const_checker<max_u32_u32>::num() == 2, "should be 2");
     73 static_assert(c2_const_checker<max_u32_u64>::num() == 3, "should be 3");
     74 static_assert(c2_const_checker<max_u32_u8>::num() == 0x7fffffff, "should be 0x7fffffff");
     75 
     76 /* ======================================= COUNTER TESTS ======================================= */
     77 
     78 void c2_cntr_static_test() {
     79     // sanity checks for construction/assignment
     80     constexpr c2_cntr32_t c32_a(123);
     81     constexpr c2_cntr64_t c64_a(-456);
     82     c2_cntr32_t c32_b __unused = c64_a;
     83     // c32_b = 64.; // DISALLOWED
     84     // c2_cntr64_t c64_b = c32_a; // DISALLOWED
     85 
     86     // sanity checks for some constexpr operators
     87     static_assert(std::is_same<decltype(c32_a + c64_a), decltype(c64_a + c32_a)>::value, "+ should result same type");
     88     static_assert(c32_a + c64_a == c2_cntr32_t(-333), "123 + -456 = -333");
     89     static_assert(c32_a + c32_a == c2_cntr32_t(246), "123 + 123 = 246");
     90     static_assert(c64_a + c32_a == c2_cntr32_t(-333), "-456 + 123 = 579");
     91     static_assert(std::is_same<decltype(c32_a + 1), decltype(1 + c32_a)>::value, "+ should result same type");
     92     static_assert(c32_a + 456 == c2_cntr32_t(579), "123 + 456 = 579");
     93     static_assert(456 + c64_a == c2_cntr64_t(0), "456 + -456 = 0");
     94     static_assert(std::is_same<decltype(c32_a - c64_a), decltype(c64_a - c32_a)>::value, "- should result same type");
     95     static_assert(c32_a - c64_a == c2_cntr32_t(579), "123 - -456 = 579");
     96     static_assert(c32_a - c32_a == c2_cntr32_t(0), "123 - 123 = 0");
     97     static_assert(c64_a - c32_a == c2_cntr32_t(-579), "-456 - 123 = -579");
     98     static_assert(std::is_same<decltype(c32_a - 1), decltype(1 - c32_a)>::value, "- should result same type");
     99     static_assert(c32_a - 456 == c2_cntr32_t(-333), "123 - 456 = -333");
    100     static_assert(456 - c64_a == c2_cntr64_t(912), "456 - -456 = 912");
    101     static_assert(std::is_same<decltype(c32_a * c64_a), decltype(c64_a * c32_a)>::value, "* should result same type");
    102     static_assert(c32_a * c64_a == c2_cntr32_t(-56088), "123 * -456 = -56088");
    103     static_assert(c32_a * c32_a == c2_cntr32_t(15129), "123 * 123 = 15129");
    104     static_assert(c64_a * c32_a == c2_cntr32_t(-56088), "-456 * 123 = -56088");
    105     static_assert(std::is_same<decltype(c32_a * 1), decltype(1 * c32_a)>::value, "* should result same type");
    106     static_assert(c32_a * 456 == c2_cntr32_t(56088), "123 * 456 = 56088");
    107     static_assert(456 * c64_a == c2_cntr64_t(-207936), "456 * -456 = -207936");
    108 
    109     static_assert((c32_a << 26u) == c2_cntr32_t(0xEC000000), "123 << 26 = 0xEC000000");
    110 
    111     // sanity checks for unary operators
    112     static_assert(c2_cntr32_t(1) == +c2_cntr32_t(1), "1 == +1");
    113     static_assert(c2_cntr32_t(1) == -c2_cntr32_t(-1), "1 == --1");
    114 
    115     // sanity checks for comparison
    116     using c8_t = c2_cntr_t<uint8_t>;
    117     static_assert(c8_t(-0x80) > c8_t(0x7f), "80 > 7F");
    118     static_assert(c8_t(-0x80) >= c8_t(0x7f), "80 >= 7F");
    119     static_assert(c8_t(0x7f) > c8_t(0x7e), "7F > 7E");
    120     static_assert(c8_t(0x7f) >= c8_t(0x7e), "7F >= 7E");
    121     static_assert(!(c8_t(-0x80) > c8_t(0)), "80 !> 00");
    122     static_assert(!(c8_t(-0x80) >= c8_t(0)), "80 !>= 00");
    123     static_assert(!(c8_t(-0x80) > c8_t(-0x80)), "80 !> 80");
    124     static_assert(c8_t(-0x80) >= c8_t(-0x80), "80 >= 80");
    125 
    126     static_assert(c8_t(-0x80) == c8_t(0x80), "80 == 80");
    127     static_assert(!(c8_t(-0x80) == c8_t(0)), "80 != 0");
    128     static_assert(c8_t(-0x80) != c8_t(0x7f), "80 != 7F");
    129     static_assert(!(c8_t(0x7f) != c8_t(0x7f)), "80 != 7F");
    130 
    131     static_assert(c8_t(0x7f) < c8_t(-0x80), "7F < 80");
    132     static_assert(c8_t(0x7f) <= c8_t(-0x80), "7F < 80");
    133     static_assert(c8_t(0x7e) < c8_t(0x7f), "7E < 7F");
    134     static_assert(c8_t(0x7e) <= c8_t(0x7f), "7E < 7F");
    135     static_assert(!(c8_t(-0x40) < c8_t(0x40)), "-40 !< 40");
    136     static_assert(!(c8_t(-0x40) <= c8_t(0x40)), "-40 !<= 40");
    137     static_assert(!(c8_t(-0x40) < c8_t(-0x40)), "-40 !< -40");
    138     static_assert(c8_t(-0x40) <= c8_t(-0x40), "-40 <= -40");
    139 
    140     static_assert(c2_cntr32_t(-0x7fffffff - 1) > c2_cntr32_t(0x7fffffff), "80 > 7F");
    141     static_assert(!(c2_cntr32_t(-0x7fffffff - 1) > c2_cntr32_t(0)), "80 !> 00");
    142     static_assert(c2_cntr32_t(1) == c2_cntr32_t(c2_cntr64_t(0x100000001ul)), "1 == 1");
    143 }
    144 
    145 class C2Test : public ::testing::Test {
    146 };
    147 
    148 TEST_F(C2Test, CounterTest) {
    149     c2_cntr32_t c32_a(123);
    150     c2_cntr64_t c64_a(-456);
    151     EXPECT_EQ(c32_a += 3, c2_cntr32_t(126));
    152     EXPECT_EQ(c32_a += c64_a, c2_cntr32_t(-330));
    153     EXPECT_EQ(c32_a <<= 2u, c2_cntr32_t(-1320));
    154     EXPECT_EQ(c64_a *= 3, c2_cntr64_t(-1368));
    155     EXPECT_EQ(c32_a -= c64_a, c2_cntr32_t(48));
    156     EXPECT_EQ(c32_a -= 40, c2_cntr32_t(8));
    157     EXPECT_EQ(c32_a *= c32_a, c2_cntr32_t(64));
    158 }
    159 
    160