test_pten_exception.cc 4.7 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 20 21 22 23

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