Home | History | Annotate | Download | only in tests
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ppapi/tests/test_test_internals.h"
      6 
      7 #include <vector>
      8 
      9 namespace {
     10 
     11 std::string CheckEqual(const std::string& expected, const std::string& actual) {
     12   if (expected != actual) {
     13     return std::string("Expected : \"") + expected + "\", got : \"" + actual +
     14        "\"";
     15   }
     16   PASS();
     17 }
     18 
     19 std::string Negate(const std::string& result) {
     20   if (result.empty())
     21     return std::string("FAIL: String was empty.");
     22   return std::string();
     23 }
     24 
     25 class CallCounter {
     26  public:
     27   CallCounter() : num_calls_(0) {}
     28 
     29   int return_zero() {
     30     ++num_calls_;
     31     return 0;
     32   }
     33   double return_zero_as_double() {
     34     ++num_calls_;
     35     return 0.0;
     36   }
     37 
     38   int num_calls() const { return num_calls_; }
     39 
     40  private:
     41   int num_calls_;
     42 };
     43 
     44 }
     45 
     46 REGISTER_TEST_CASE(TestInternals);
     47 
     48 bool TestTestInternals::Init() {
     49   return true;
     50 }
     51 
     52 void TestTestInternals::RunTests(const std::string& filter) {
     53   RUN_TEST(ToString, filter);
     54   RUN_TEST(PassingComparisons, filter);
     55   RUN_TEST(FailingComparisons, filter);
     56   RUN_TEST(EvaluateOnce, filter);
     57 }
     58 
     59 #define WRAP_LEFT_PARAM(a) \
     60     internal::ParameterWrapper<IS_NULL_LITERAL(a)>::WrapValue(a)
     61 std::string TestTestInternals::TestToString() {
     62   // We don't use most ASSERT macros here, because they rely on ToString.
     63   // ASSERT_SUBTEST_SUCCESS does not use ToString.
     64   ASSERT_SUBTEST_SUCCESS(CheckEqual(WRAP_LEFT_PARAM(NULL).ToString(), "0"));
     65   ASSERT_SUBTEST_SUCCESS(CheckEqual(WRAP_LEFT_PARAM(0).ToString(), "0"));
     66   ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(5), "5"));
     67   int32_t x = 5;
     68   ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(x + 1), "6"));
     69   std::string str = "blah";
     70   ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(str + "blah"),
     71                                     "blahblah"));
     72   std::vector<int> vec;
     73   ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(vec), std::string()));
     74 
     75   PASS();
     76 }
     77 
     78 #define COMPARE_DOUBLE_EQ(a, b) \
     79     internal::CompareDoubleEq( \
     80         internal::ParameterWrapper<IS_NULL_LITERAL(a)>::WrapValue(a), \
     81         (b), #a, #b, __FILE__, __LINE__)
     82 std::string TestTestInternals::TestPassingComparisons() {
     83   // These comparisons should all "pass", meaning they should return the empty
     84   // string.
     85   {
     86     const std::string* const kNull = NULL;
     87     const std::string* const kDeadBeef =
     88         reinterpret_cast<const std::string*>(0xdeadbeef);
     89     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, NULL, kNull));
     90     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, kDeadBeef, kDeadBeef));
     91     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, NULL, kDeadBeef));
     92     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, kDeadBeef, kNull));
     93   } {
     94     const int64_t zero_int32 = 0;
     95     const int64_t zero_int64 = 0;
     96     const int32_t zero_uint32 = 0;
     97     const int64_t zero_uint64 = 0;
     98     const int32_t one_int32 = 1;
     99     const int64_t one_int64 = 1;
    100     const int32_t one_uint32 = 1;
    101     const int64_t one_uint64 = 1;
    102     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_int32));
    103     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_int64));
    104     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_uint32));
    105     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_uint64));
    106     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_int32));
    107     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_int64));
    108     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_uint32));
    109     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_uint64));
    110 
    111     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_int32));
    112     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_int64));
    113     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_uint32));
    114     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_uint64));
    115     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_int32));
    116     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_int64));
    117     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_uint32));
    118     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_uint64));
    119 
    120     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_int32));
    121     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_uint32));
    122     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_int64));
    123     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_uint64));
    124 
    125     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_int32));
    126     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_uint32));
    127     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_int64));
    128     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_uint64));
    129 
    130     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_int32));
    131     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_uint32));
    132     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_int64));
    133     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_uint64));
    134 
    135     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_int32));
    136     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_uint32));
    137     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_int64));
    138     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_uint64));
    139 
    140     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_int32));
    141     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_uint32));
    142     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_int64));
    143     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_uint64));
    144 
    145     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_int32));
    146     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_uint32));
    147     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_int64));
    148     ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_uint64));
    149   } {
    150     ASSERT_SUBTEST_SUCCESS(
    151         COMPARE_BINARY_INTERNAL(EQ, "hello", std::string("hello")));
    152     std::vector<int> int_vector1(10, 10);
    153     std::vector<int> int_vector2(int_vector1);
    154     ASSERT_SUBTEST_SUCCESS(
    155         COMPARE_BINARY_INTERNAL(EQ, int_vector1, int_vector2));
    156   } {
    157     const double kZeroDouble = 0.0;
    158     const double kPositiveDouble = 1.1;
    159     ASSERT_SUBTEST_SUCCESS(
    160         COMPARE_BINARY_INTERNAL(LT, kZeroDouble, kPositiveDouble));
    161     ASSERT_SUBTEST_SUCCESS(
    162         COMPARE_BINARY_INTERNAL(GT, kPositiveDouble, kZeroDouble));
    163     ASSERT_SUBTEST_SUCCESS(COMPARE_DOUBLE_EQ(0.0, kZeroDouble));
    164     ASSERT_SUBTEST_SUCCESS(COMPARE_DOUBLE_EQ(1.0 + 0.1, kPositiveDouble));
    165   }
    166 
    167   // TODO: Things that return non-empty string.
    168   // TODO: Test that the parameter is evaluated exactly once.
    169   PASS();
    170 }
    171 
    172 #define ASSERT_SUBTEST_FAILURE(param) ASSERT_SUBTEST_SUCCESS(Negate(param))
    173 std::string TestTestInternals::TestFailingComparisons() {
    174   // Note, we don't really worry about the content of failure strings here.
    175   // That's mostly covered by the ToString test above. This test just makes
    176   // sure that comparisons which should return a non-empty string do so.
    177   {
    178     const std::string* const kNull = NULL;
    179     const std::string* const kDeadBeef =
    180         reinterpret_cast<const std::string*>(0xdeadbeef);
    181     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, NULL, kNull));
    182     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, kDeadBeef, kDeadBeef));
    183     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, NULL, kDeadBeef));
    184     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, kDeadBeef, kNull));
    185   }
    186 
    187   // Now, just make sure we get any non-empty string at all, which will indicate
    188   // test failure. We mostly rely on the ToString test to get the formats right.
    189   {
    190     const int64_t zero_int32 = 0;
    191     const int64_t zero_int64 = 0;
    192     const int32_t zero_uint32 = 0;
    193     const int64_t zero_uint64 = 0;
    194     const int32_t one_int32 = 1;
    195     const int64_t one_int64 = 1;
    196     const int32_t one_uint32 = 1;
    197     const int64_t one_uint64 = 1;
    198     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_int32));
    199     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_int64));
    200     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_uint32));
    201     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_uint64));
    202     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_int32));
    203     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_int64));
    204     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_uint32));
    205     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_uint64));
    206 
    207     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_int32));
    208     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_int64));
    209     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_uint32));
    210     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_uint64));
    211     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_int32));
    212     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_int64));
    213     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_uint32));
    214     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_uint64));
    215 
    216     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_int32));
    217     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_uint32));
    218     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_int64));
    219     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_uint64));
    220 
    221     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_int32));
    222     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_uint32));
    223     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_int64));
    224     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_uint64));
    225 
    226     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_int32));
    227     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_uint32));
    228     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_int64));
    229     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_uint64));
    230 
    231     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_int32));
    232     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_uint32));
    233     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_int64));
    234     ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_uint64));
    235   } {
    236     ASSERT_SUBTEST_FAILURE(
    237         COMPARE_BINARY_INTERNAL(EQ, "goodbye", std::string("hello")));
    238     std::vector<int> int_vector1(10, 10);
    239     std::vector<int> int_vector2;
    240     ASSERT_SUBTEST_FAILURE(
    241         COMPARE_BINARY_INTERNAL(EQ, int_vector1, int_vector2));
    242   } {
    243     const double kZeroDouble = 0.0;
    244     const double kPositiveDouble = 1.1;
    245     ASSERT_SUBTEST_FAILURE(
    246         COMPARE_BINARY_INTERNAL(GT, kZeroDouble, kPositiveDouble));
    247     ASSERT_SUBTEST_FAILURE(
    248         COMPARE_BINARY_INTERNAL(LT, kPositiveDouble, kZeroDouble));
    249     ASSERT_SUBTEST_FAILURE(COMPARE_DOUBLE_EQ(1.1, kZeroDouble));
    250     ASSERT_SUBTEST_FAILURE(COMPARE_DOUBLE_EQ(0.0, kPositiveDouble));
    251   }
    252 
    253   // TODO: Test that the parameter is evaluated exactly once.
    254   PASS();
    255 }
    256 #undef COMPARE
    257 #undef COMPARE_DOUBLE_EQ
    258 
    259 std::string TestTestInternals::TestEvaluateOnce() {
    260   // Make sure that the ASSERT macros only evaluate each parameter once.
    261   {
    262     CallCounter call_counter1;
    263     CallCounter call_counter2;
    264     ASSERT_EQ(call_counter1.return_zero(), call_counter2.return_zero());
    265     assert(call_counter1.num_calls() == 1);
    266     assert(call_counter2.num_calls() == 1);
    267   } {
    268     CallCounter call_counter1;
    269     CallCounter call_counter2;
    270     ASSERT_DOUBLE_EQ(call_counter1.return_zero_as_double(),
    271                      call_counter2.return_zero_as_double());
    272     assert(call_counter1.num_calls() == 1);
    273     assert(call_counter2.num_calls() == 1);
    274   }
    275   PASS();
    276 }
    277 
    278