test_pten_exception.cc 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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>
#include "gtest/gtest.h"
15
#include "paddle/pten/api/ext/exception.h"
16

17 18 19
namespace paddle {
namespace tests {

20 21 22 23 24 25 26
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();
27
    EXPECT_TRUE(err_msg.find("An error occurred.") != std::string::npos);
28
#if _WIN32
29
    EXPECT_TRUE(err_msg.find("tests\\api\\test_pten_exception.cc") !=
30 31
                std::string::npos);
#else
32 33
    EXPECT_TRUE(err_msg.find("paddle/pten/tests/api/test_pten_exception.cc") !=
                std::string::npos);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#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
56
    EXPECT_TRUE(err_msg.find("tests\\api\\test_pten_exception.cc") !=
57 58
                std::string::npos);
#else
59 60
    EXPECT_TRUE(err_msg.find("paddle/pten/tests/api/test_pten_exception.cc") !=
                std::string::npos);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#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
86
    EXPECT_TRUE(err_msg.find("tests\\api\\test_pten_exception.cc") !=
87 88
                std::string::npos);
#else
89 90
    EXPECT_TRUE(err_msg.find("paddle/pten/tests/api/test_pten_exception.cc") !=
                std::string::npos);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#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
112
    EXPECT_TRUE(err_msg.find("tests\\api\\test_pten_exception.cc") !=
113 114
                std::string::npos);
#else
115 116
    EXPECT_TRUE(err_msg.find("paddle/pten/tests/api/test_pten_exception.cc") !=
                std::string::npos);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#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
132
    EXPECT_TRUE(err_msg.find("tests\\api\\test_pten_exception.cc") !=
133 134
                std::string::npos);
#else
135 136
    EXPECT_TRUE(err_msg.find("paddle/pten/tests/api/test_pten_exception.cc") !=
                std::string::npos);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#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
152
    EXPECT_TRUE(err_msg.find("tests\\api\\test_pten_exception.cc") !=
153 154
                std::string::npos);
#else
155 156
    EXPECT_TRUE(err_msg.find("paddle/pten/tests/api/test_pten_exception.cc") !=
                std::string::npos);
157 158 159 160
#endif
  }
  EXPECT_TRUE(caught_exception);
}
161 162 163

}  // namespace tests
}  // namespace paddle