|
|
|
|
@ -17,10 +17,6 @@ limitations under the License. */
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "paddle/fluid/platform/enforce.h"
|
|
|
|
|
#include "paddle/fluid/string/piece.h"
|
|
|
|
|
|
|
|
|
|
using StringPiece = paddle::string::Piece;
|
|
|
|
|
using paddle::string::HasPrefix;
|
|
|
|
|
|
|
|
|
|
TEST(ENFORCE, OK) {
|
|
|
|
|
PADDLE_ENFORCE(true, "Enforce is ok %d now %f", 123, 0.345);
|
|
|
|
|
@ -35,8 +31,9 @@ TEST(ENFORCE, FAILED) {
|
|
|
|
|
PADDLE_ENFORCE(false, "Enforce is not ok %d at all", 123);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
HasPrefix(StringPiece(error.what()), "Enforce is not ok 123 at all"));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Enforce is not ok 123 at all") !=
|
|
|
|
|
std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
|
|
|
|
|
@ -45,8 +42,8 @@ TEST(ENFORCE, FAILED) {
|
|
|
|
|
PADDLE_ENFORCE(false, "Enforce is not ok at all");
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
HasPrefix(StringPiece(error.what()), "Enforce is not ok at all"));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Enforce is not ok at all") != std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
|
|
|
|
|
@ -75,9 +72,9 @@ TEST(ENFORCE_EQ, NO_EXTRA_MSG_FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(a, 1 + 3);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
HasPrefix(
|
|
|
|
|
StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected a == 1 + 3, but received a:2 != 1 + 3:4.");
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected a == 1 + 3, but received a:2 != 1 "
|
|
|
|
|
"+ 3:4.") != std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -89,9 +86,9 @@ TEST(ENFORCE_EQ, EXTRA_MSG_FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_EQ(a, 1 + 3, "%s size not match", "their");
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
HasPrefix(StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected a == 1 + 3, but received a:2 != 1 + "
|
|
|
|
|
"3:4.\ntheir size not match");
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected a == 1 + 3, but received a:2 != 1 + "
|
|
|
|
|
"3:4.\ntheir size not match") != std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -108,10 +105,9 @@ TEST(ENFORCE_NE, FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_NE(1.0, 1UL);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(HasPrefix(
|
|
|
|
|
StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected 1.0 != 1UL, but received 1.0:1 == 1UL:1."))
|
|
|
|
|
<< error.what() << " does not have expected prefix";
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected 1.0 != 1UL, but "
|
|
|
|
|
"received 1.0:1 == 1UL:1.") != std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -123,9 +119,9 @@ TEST(ENFORCE_GT, FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_GT(1, 2);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
HasPrefix(StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected 1 > 2, but received 1:1 <= 2:2."));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected 1 > 2, but received 1:1 <= 2:2.") !=
|
|
|
|
|
std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -141,9 +137,9 @@ TEST(ENFORCE_GE, FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_GE(1, 2);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
HasPrefix(StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected 1 >= 2, but received 1:1 < 2:2."));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected 1 >= 2, but received 1:1 < 2:2.") !=
|
|
|
|
|
std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -161,9 +157,9 @@ TEST(ENFORCE_LE, FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_GT(1, 2);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
HasPrefix(StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected 1 > 2, but received 1:1 <= 2:2."));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected 1 > 2, but received 1:1 <= 2:2.") !=
|
|
|
|
|
std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -179,9 +175,10 @@ TEST(ENFORCE_LT, FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_LT(1UL, 0.12);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(HasPrefix(StringPiece(error.what()),
|
|
|
|
|
"Enforce failed. Expected 1UL < 0.12, but "
|
|
|
|
|
"received 1UL:1 >= 0.12:0.12."));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("Expected 1UL < 0.12, but "
|
|
|
|
|
"received 1UL:1 >= 0.12:0.12.") !=
|
|
|
|
|
std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -198,7 +195,8 @@ TEST(ENFORCE_NOT_NULL, FAIL) {
|
|
|
|
|
PADDLE_ENFORCE_NOT_NULL(a);
|
|
|
|
|
} catch (paddle::platform::EnforceNotMet error) {
|
|
|
|
|
caught_exception = true;
|
|
|
|
|
EXPECT_TRUE(HasPrefix(StringPiece(error.what()), "a should not be null"));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("a should not be null") != std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_exception);
|
|
|
|
|
}
|
|
|
|
|
@ -251,7 +249,8 @@ TEST(EOF_EXCEPTION, THROW_EOF) {
|
|
|
|
|
PADDLE_THROW_EOF();
|
|
|
|
|
} catch (paddle::platform::EOFException error) {
|
|
|
|
|
caught_eof = true;
|
|
|
|
|
EXPECT_TRUE(HasPrefix(StringPiece(error.what()), "There is no next data."));
|
|
|
|
|
std::string ex_msg = error.what();
|
|
|
|
|
EXPECT_TRUE(ex_msg.find("There is no next data.") != std::string::npos);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_TRUE(caught_eof);
|
|
|
|
|
}
|
|
|
|
|
|