|
| 1 | +/** |
| 2 | + * Copyright (c) 2018-present, Facebook, Inc. |
| 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 | +#include <sstream> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include <glog/logging.h> |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include "tc/core/check.h" |
| 24 | + |
| 25 | +// XXX:gtest doesn't define a macro that inspects the contents of the exception |
| 26 | +#define ASSERT_THROW_WHAT(x, y) \ |
| 27 | + try { \ |
| 28 | + (x); \ |
| 29 | + ASSERT_TRUE(false); \ |
| 30 | + } catch (std::runtime_error & e) { \ |
| 31 | + ASSERT_EQ(y, e.what()); \ |
| 32 | + } |
| 33 | + |
| 34 | +TEST(CHECK, Plain) { |
| 35 | + ASSERT_NO_THROW(TC_CHECK(true)); |
| 36 | + { |
| 37 | + std::stringstream expected; |
| 38 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 1 << ']'; |
| 39 | + ASSERT_THROW_WHAT(TC_CHECK(false), expected.str()); |
| 40 | + } |
| 41 | + |
| 42 | + { |
| 43 | + std::stringstream expected; |
| 44 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 << ']' |
| 45 | + << ": 1+1=3"; |
| 46 | + ASSERT_THROW_WHAT(TC_CHECK(false) << "1+1=3", expected.str()); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +TEST(CHECK, Vector) { |
| 51 | + std::stringstream expected; |
| 52 | + auto v = std::vector<int>{1, 2, 3, 4}; |
| 53 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 << ']' |
| 54 | + << ": 1,2,3,4"; |
| 55 | + ASSERT_THROW_WHAT((TC_CHECK(false) << v), expected.str()); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(CHECK, EQ) { |
| 59 | + ASSERT_NO_THROW(TC_CHECK_EQ(1, 1)); |
| 60 | + ASSERT_NO_THROW(TC_CHECK_EQ(std::string("aaa"), std::string("aaa"))); |
| 61 | + { |
| 62 | + std::stringstream expected; |
| 63 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 << "] " |
| 64 | + << "1 not equal to 2"; |
| 65 | + ASSERT_THROW_WHAT(TC_CHECK_EQ(1, 2), expected.str()); |
| 66 | + } |
| 67 | + |
| 68 | + { |
| 69 | + std::stringstream expected; |
| 70 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 71 | + << "] 1 not equal to 2: 2+2=5"; |
| 72 | + ASSERT_THROW_WHAT(TC_CHECK_EQ(1, 2) << "2+2=5", expected.str()); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +TEST(CHECK, NE) { |
| 77 | + ASSERT_NO_THROW(TC_CHECK_NE(1, 2)); |
| 78 | + ASSERT_NO_THROW(TC_CHECK_NE(std::string("aaa"), std::string("baa"))); |
| 79 | + { |
| 80 | + std::stringstream expected; |
| 81 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 82 | + << "] 1 equal to 1"; |
| 83 | + ASSERT_THROW_WHAT(TC_CHECK_NE(1, 1), expected.str()); |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + std::stringstream expected; |
| 88 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 89 | + << "] 1 equal to 1: 2+2=5"; |
| 90 | + ASSERT_THROW_WHAT(TC_CHECK_NE(1, 1) << "2+2=5", expected.str()); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +TEST(CHECK, LT) { |
| 95 | + ASSERT_NO_THROW(TC_CHECK_LT(1, 2)); |
| 96 | + ASSERT_NO_THROW(TC_CHECK_LT(std::string("aaa"), std::string("baa"))); |
| 97 | + { |
| 98 | + std::stringstream expected; |
| 99 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 100 | + << "] 1 not less than 1"; |
| 101 | + ASSERT_THROW_WHAT(TC_CHECK_LT(1, 1), expected.str()); |
| 102 | + } |
| 103 | + |
| 104 | + { |
| 105 | + std::stringstream expected; |
| 106 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 107 | + << "] 4 not less than " << 1 << ": 4+3=8"; |
| 108 | + ASSERT_THROW_WHAT(TC_CHECK_LT(4, 1) << "4+3=8", expected.str()); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +TEST(CHECK, GT) { |
| 113 | + ASSERT_NO_THROW(TC_CHECK_GT(2, 1)); |
| 114 | + ASSERT_NO_THROW(TC_CHECK_GT(std::string("ca"), std::string("baa"))); |
| 115 | + { |
| 116 | + std::stringstream expected; |
| 117 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 118 | + << "] 1 not greater than " << 1; |
| 119 | + ASSERT_THROW_WHAT(TC_CHECK_GT(1, 1), expected.str()); |
| 120 | + } |
| 121 | + |
| 122 | + { |
| 123 | + std::stringstream expected; |
| 124 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 125 | + << "] 2 not greater than 4: 3+3=7"; |
| 126 | + ASSERT_THROW_WHAT(TC_CHECK_GT(2, 4) << "3+3=7", expected.str()); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +TEST(CHECK, LE) { |
| 131 | + ASSERT_NO_THROW(TC_CHECK_LE(1, 2)); |
| 132 | + ASSERT_NO_THROW(TC_CHECK_LE(1, 1)); |
| 133 | + ASSERT_NO_THROW(TC_CHECK_LE(std::string("aaa"), std::string("baa"))); |
| 134 | + ASSERT_NO_THROW(TC_CHECK_LE(std::string("aa"), std::string("aa"))); |
| 135 | + { |
| 136 | + std::stringstream expected; |
| 137 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 138 | + << "] 2 not less than or equal to 1"; |
| 139 | + ASSERT_THROW_WHAT(TC_CHECK_LE(2, 1), expected.str()); |
| 140 | + } |
| 141 | + |
| 142 | + { |
| 143 | + std::stringstream expected; |
| 144 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 145 | + << "] 4 not less than or equal to 1: 4+5=10"; |
| 146 | + ASSERT_THROW_WHAT(TC_CHECK_LE(4, 1) << "4+5=10", expected.str()); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +TEST(CHECK, GE) { |
| 151 | + ASSERT_NO_THROW(TC_CHECK_GE(2, 1)); |
| 152 | + ASSERT_NO_THROW(TC_CHECK_GE(2, 2)); |
| 153 | + ASSERT_NO_THROW(TC_CHECK_GE(std::string("ca"), std::string("baa"))); |
| 154 | + ASSERT_NO_THROW(TC_CHECK_GE(std::string("ba"), std::string("ba"))); |
| 155 | + { |
| 156 | + std::stringstream expected; |
| 157 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 158 | + << "] 7 not greater than or equal to 9"; |
| 159 | + ASSERT_THROW_WHAT(TC_CHECK_GE(7, 9), expected.str()); |
| 160 | + } |
| 161 | + |
| 162 | + { |
| 163 | + std::stringstream expected; |
| 164 | + expected << "Check failed [" << __FILE__ << ':' << __LINE__ + 2 |
| 165 | + << "] 2 not greater than or equal to 6: 9+3=13"; |
| 166 | + ASSERT_THROW_WHAT(TC_CHECK_GE(2, 6) << "9+3=13", expected.str()); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +int main(int argc, char** argv) { |
| 171 | + ::testing::InitGoogleTest(&argc, argv); |
| 172 | + ::google::InitGoogleLogging(argv[0]); |
| 173 | + return RUN_ALL_TESTS(); |
| 174 | +} |
0 commit comments