test_phi_exception.cc 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <iostream>
#include <string>
14

15
#include "gtest/gtest.h"
16
#include "paddle/phi/api/ext/exception.h"
17

18 19 20
namespace paddle {
namespace tests {

21 22 23 24 25 26 27
TEST(PD_THROW, empty) {
  bool caught_exception = false;
  try {
    PD_THROW();
  } catch (const std::exception& e) {
    caught_exception = true;
    std::string err_msg = e.what();
28
    EXPECT_TRUE(err_msg.find("An error occurred.") != std::string::npos);
29
#if _WIN32
30
    EXPECT_TRUE(err_msg.find("tests\\api\\test_phi_exception.cc") !=
31 32
                std::string::npos);
#else
33
    EXPECT_TRUE(err_msg.find("paddle/phi/tests/api/test_phi_exception.cc") !=
34
                std::string::npos);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#endif
  }
  EXPECT_TRUE(caught_exception);
}

TEST(PD_THROW, non_empty) {
  bool caught_exception = false;
  try {
    PD_THROW("PD_THROW returns ",
             false,
             ". DataType of ",
             1,
             " is INT. ",
             "DataType of ",
             0.23,
             " is FLOAT. ");
  } catch (const std::exception& e) {
    caught_exception = true;
    std::string err_msg = e.what();
    EXPECT_TRUE(err_msg.find("PD_THROW returns 0. DataType of 1 is INT. ") !=
                std::string::npos);
#if _WIN32
57
    EXPECT_TRUE(err_msg.find("tests\\api\\test_phi_exception.cc") !=
58 59
                std::string::npos);
#else
60
    EXPECT_TRUE(err_msg.find("paddle/phi/tests/api/test_phi_exception.cc") !=
61
                std::string::npos);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
#endif
  }
  EXPECT_TRUE(caught_exception);
}

TEST(PD_CHECK, OK) {
  PD_CHECK(true);
  PD_CHECK(true, "PD_CHECK returns ", true, "now");

  const size_t a = 1;
  const size_t b = 10;
  PD_CHECK(a < b);
  PD_CHECK(a < b, "PD_CHECK returns ", true, a, "should < ", b);
}

TEST(PD_CHECK, FAILED) {
  bool caught_exception = false;
  try {
    PD_CHECK(false);
  } catch (const std::exception& e) {
    caught_exception = true;
    std::string err_msg = e.what();
    EXPECT_TRUE(err_msg.find("Expected false, but it's not satisfied.") !=
                std::string::npos);
#if _WIN32
87
    EXPECT_TRUE(err_msg.find("tests\\api\\test_phi_exception.cc") !=
88 89
                std::string::npos);
#else
90
    EXPECT_TRUE(err_msg.find("paddle/phi/tests/api/test_phi_exception.cc") !=
91
                std::string::npos);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#endif
  }
  EXPECT_TRUE(caught_exception);

  caught_exception = false;
  try {
    PD_CHECK(false,
             "PD_CHECK returns ",
             false,
             ". DataType of ",
             1,
             " is INT. ",
             "DataType of ",
             0.23,
             " is FLOAT. ");
  } catch (const std::exception& e) {
    caught_exception = true;
    std::string err_msg = e.what();
    EXPECT_TRUE(err_msg.find("PD_CHECK returns 0. DataType of 1 is INT. ") !=
                std::string::npos);
#if _WIN32
113
    EXPECT_TRUE(err_msg.find("tests\\api\\test_phi_exception.cc") !=
114 115
                std::string::npos);
#else
116
    EXPECT_TRUE(err_msg.find("paddle/phi/tests/api/test_phi_exception.cc") !=
117
                std::string::npos);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#endif
  }
  EXPECT_TRUE(caught_exception);

  const size_t a = 1;
  const size_t b = 10;
  caught_exception = false;
  try {
    PD_CHECK(a > b);
  } catch (const std::exception& e) {
    caught_exception = true;
    std::string err_msg = e.what();
    EXPECT_TRUE(err_msg.find("Expected a > b, but it's not satisfied.") !=
                std::string::npos);
#if _WIN32
133
    EXPECT_TRUE(err_msg.find("tests\\api\\test_phi_exception.cc") !=
134 135
                std::string::npos);
#else
136
    EXPECT_TRUE(err_msg.find("paddle/phi/tests/api/test_phi_exception.cc") !=
137
                std::string::npos);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#endif
  }
  EXPECT_TRUE(caught_exception);

  const size_t c = 123;
  const float d = 0.345;
  caught_exception = false;
  try {
    PD_CHECK(c < d, "PD_CHECK returns ", false, ", because ", c, " > ", d);
  } catch (const std::exception& e) {
    caught_exception = true;
    std::string err_msg = e.what();
    EXPECT_TRUE(err_msg.find("PD_CHECK returns 0, because 123 > 0.345") !=
                std::string::npos);
#if _WIN32
153
    EXPECT_TRUE(err_msg.find("tests\\api\\test_phi_exception.cc") !=
154 155
                std::string::npos);
#else
156
    EXPECT_TRUE(err_msg.find("paddle/phi/tests/api/test_phi_exception.cc") !=
157
                std::string::npos);
158 159 160 161
#endif
  }
  EXPECT_TRUE(caught_exception);
}
162 163 164

}  // namespace tests
}  // namespace paddle