This source file includes following definitions.
- CheckEqual
- Negate
- return_zero
- return_zero_as_double
- num_calls
- Init
- RunTests
- TestToString
- TestPassingComparisons
- TestFailingComparisons
- TestEvaluateOnce
#include "ppapi/tests/test_test_internals.h"
#include <vector>
namespace {
std::string CheckEqual(const std::string& expected, const std::string& actual) {
if (expected != actual) {
return std::string("Expected : \"") + expected + "\", got : \"" + actual +
"\"";
}
PASS();
}
std::string Negate(const std::string& result) {
if (result.empty())
return std::string("FAIL: String was empty.");
return std::string();
}
class CallCounter {
public:
CallCounter() : num_calls_(0) {}
int return_zero() {
++num_calls_;
return 0;
}
double return_zero_as_double() {
++num_calls_;
return 0.0;
}
int num_calls() const { return num_calls_; }
private:
int num_calls_;
};
}
REGISTER_TEST_CASE(TestInternals);
bool TestTestInternals::Init() {
return true;
}
void TestTestInternals::RunTests(const std::string& filter) {
RUN_TEST(ToString, filter);
RUN_TEST(PassingComparisons, filter);
RUN_TEST(FailingComparisons, filter);
RUN_TEST(EvaluateOnce, filter);
}
#define WRAP_LEFT_PARAM(a) \
internal::ParameterWrapper<IS_NULL_LITERAL(a)>::WrapValue(a)
std::string TestTestInternals::TestToString() {
ASSERT_SUBTEST_SUCCESS(CheckEqual(WRAP_LEFT_PARAM(NULL).ToString(), "0"));
ASSERT_SUBTEST_SUCCESS(CheckEqual(WRAP_LEFT_PARAM(0).ToString(), "0"));
ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(5), "5"));
int32_t x = 5;
ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(x + 1), "6"));
std::string str = "blah";
ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(str + "blah"),
"blahblah"));
std::vector<int> vec;
ASSERT_SUBTEST_SUCCESS(CheckEqual(internal::ToString(vec), std::string()));
PASS();
}
#define COMPARE_DOUBLE_EQ(a, b) \
internal::CompareDoubleEq( \
internal::ParameterWrapper<IS_NULL_LITERAL(a)>::WrapValue(a), \
(b), #a, #b, __FILE__, __LINE__)
std::string TestTestInternals::TestPassingComparisons() {
{
const std::string* const kNull = NULL;
const std::string* const kDeadBeef =
reinterpret_cast<const std::string*>(0xdeadbeef);
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, NULL, kNull));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, kDeadBeef, kDeadBeef));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, NULL, kDeadBeef));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, kDeadBeef, kNull));
} {
const int64_t zero_int32 = 0;
const int64_t zero_int64 = 0;
const int32_t zero_uint32 = 0;
const int64_t zero_uint64 = 0;
const int32_t one_int32 = 1;
const int64_t one_int64 = 1;
const int32_t one_uint32 = 1;
const int64_t one_uint64 = 1;
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 0, zero_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(EQ, 1, one_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 1, zero_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(NE, 0, one_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LT, 0, one_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, zero_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(LE, 0, one_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GT, 1, zero_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, zero_uint64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_int32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_uint32));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_int64));
ASSERT_SUBTEST_SUCCESS(COMPARE_BINARY_INTERNAL(GE, 1, one_uint64));
} {
ASSERT_SUBTEST_SUCCESS(
COMPARE_BINARY_INTERNAL(EQ, "hello", std::string("hello")));
std::vector<int> int_vector1(10, 10);
std::vector<int> int_vector2(int_vector1);
ASSERT_SUBTEST_SUCCESS(
COMPARE_BINARY_INTERNAL(EQ, int_vector1, int_vector2));
} {
const double kZeroDouble = 0.0;
const double kPositiveDouble = 1.1;
ASSERT_SUBTEST_SUCCESS(
COMPARE_BINARY_INTERNAL(LT, kZeroDouble, kPositiveDouble));
ASSERT_SUBTEST_SUCCESS(
COMPARE_BINARY_INTERNAL(GT, kPositiveDouble, kZeroDouble));
ASSERT_SUBTEST_SUCCESS(COMPARE_DOUBLE_EQ(0.0, kZeroDouble));
ASSERT_SUBTEST_SUCCESS(COMPARE_DOUBLE_EQ(1.0 + 0.1, kPositiveDouble));
}
PASS();
}
#define ASSERT_SUBTEST_FAILURE(param) ASSERT_SUBTEST_SUCCESS(Negate(param))
std::string TestTestInternals::TestFailingComparisons() {
{
const std::string* const kNull = NULL;
const std::string* const kDeadBeef =
reinterpret_cast<const std::string*>(0xdeadbeef);
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, NULL, kNull));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, kDeadBeef, kDeadBeef));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, NULL, kDeadBeef));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, kDeadBeef, kNull));
}
{
const int64_t zero_int32 = 0;
const int64_t zero_int64 = 0;
const int32_t zero_uint32 = 0;
const int64_t zero_uint64 = 0;
const int32_t one_int32 = 1;
const int64_t one_int64 = 1;
const int32_t one_uint32 = 1;
const int64_t one_uint64 = 1;
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 1, zero_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(EQ, 0, one_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 0, zero_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(NE, 1, one_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LT, 1, one_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(LE, 1, zero_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GT, 0, zero_uint64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_int32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_uint32));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_int64));
ASSERT_SUBTEST_FAILURE(COMPARE_BINARY_INTERNAL(GE, 0, one_uint64));
} {
ASSERT_SUBTEST_FAILURE(
COMPARE_BINARY_INTERNAL(EQ, "goodbye", std::string("hello")));
std::vector<int> int_vector1(10, 10);
std::vector<int> int_vector2;
ASSERT_SUBTEST_FAILURE(
COMPARE_BINARY_INTERNAL(EQ, int_vector1, int_vector2));
} {
const double kZeroDouble = 0.0;
const double kPositiveDouble = 1.1;
ASSERT_SUBTEST_FAILURE(
COMPARE_BINARY_INTERNAL(GT, kZeroDouble, kPositiveDouble));
ASSERT_SUBTEST_FAILURE(
COMPARE_BINARY_INTERNAL(LT, kPositiveDouble, kZeroDouble));
ASSERT_SUBTEST_FAILURE(COMPARE_DOUBLE_EQ(1.1, kZeroDouble));
ASSERT_SUBTEST_FAILURE(COMPARE_DOUBLE_EQ(0.0, kPositiveDouble));
}
PASS();
}
#undef COMPARE
#undef COMPARE_DOUBLE_EQ
std::string TestTestInternals::TestEvaluateOnce() {
{
CallCounter call_counter1;
CallCounter call_counter2;
ASSERT_EQ(call_counter1.return_zero(), call_counter2.return_zero());
assert(call_counter1.num_calls() == 1);
assert(call_counter2.num_calls() == 1);
} {
CallCounter call_counter1;
CallCounter call_counter2;
ASSERT_DOUBLE_EQ(call_counter1.return_zero_as_double(),
call_counter2.return_zero_as_double());
assert(call_counter1.num_calls() == 1);
assert(call_counter2.num_calls() == 1);
}
PASS();
}