3 #include <gtest/gtest.h> 4 #include <c10/util/Logging.h> 12 TEST(LoggingTest, TestEnforceTrue) {
14 CAFFE_ENFORCE(
true,
"Isn't it?");
17 TEST(LoggingTest, TestEnforceFalse) {
19 std::swap(FLAGS_caffe2_use_fatal_for_enforce, kFalse);
21 CAFFE_ENFORCE(
false,
"This throws.");
24 }
catch (const ::c10::Error&) {
26 std::swap(FLAGS_caffe2_use_fatal_for_enforce, kFalse);
29 TEST(LoggingTest, TestEnforceEquals) {
33 CAFFE_ENFORCE_THAT(Equals(++x, ++y));
36 }
catch (const ::c10::Error& err) {
37 EXPECT_NE(err.msg().find(
"5 vs 6"), string::npos);
41 CAFFE_ENFORCE_THAT(Equals(++x, y));
46 TEST(LoggingTest, EnforceShowcase) {
52 #define WRAP_AND_PRINT(exp) \ 55 } catch (const ::c10::Error&) { \ 58 WRAP_AND_PRINT(CAFFE_ENFORCE_EQ(one, two));
59 WRAP_AND_PRINT(CAFFE_ENFORCE_NE(one * 2, two));
60 WRAP_AND_PRINT(CAFFE_ENFORCE_GT(one, two));
61 WRAP_AND_PRINT(CAFFE_ENFORCE_GE(one, two));
62 WRAP_AND_PRINT(CAFFE_ENFORCE_LT(three, two));
63 WRAP_AND_PRINT(CAFFE_ENFORCE_LE(three, two));
65 WRAP_AND_PRINT(CAFFE_ENFORCE_EQ(
66 one * two + three, three * two,
"It's a pretty complicated expression"));
68 WRAP_AND_PRINT(CAFFE_ENFORCE_THAT(Equals(one * two + three, three * two)));
71 TEST(LoggingTest, Join) {
72 auto s = c10::Join(
", ", vector<int>({1, 2, 3}));
73 EXPECT_EQ(s,
"1, 2, 3");
74 s = c10::Join(
":", vector<string>());
76 s = c10::Join(
", ", set<int>({3, 1, 2}));
77 EXPECT_EQ(s,
"1, 2, 3");
80 #if GTEST_HAS_DEATH_TEST 81 TEST(LoggingDeathTest, TestEnforceUsingFatal) {
83 std::swap(FLAGS_caffe2_use_fatal_for_enforce, kTrue);
84 EXPECT_DEATH(CAFFE_ENFORCE(
false,
"This goes fatal."),
"");
85 std::swap(FLAGS_caffe2_use_fatal_for_enforce, kTrue);