diff options
author | Mathias Stearn <mathias@10gen.com> | 2012-09-28 16:25:42 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2012-09-30 21:13:50 -0400 |
commit | b897ff2d1d6c32e43f4efbcbd4956b645bd3eb8b (patch) | |
tree | 023ddb4b34eb3e9ddb9fd255dc1484a29d6aba84 /src/mongo/base | |
parent | 7ce347287c153ef15067e0e2f8039ea33f1d90df (diff) | |
download | mongo-b897ff2d1d6c32e43f4efbcbd4956b645bd3eb8b.tar.gz |
Support templated unittest test suites.
Diffstat (limited to 'src/mongo/base')
-rw-r--r-- | src/mongo/base/parse_number_test.cpp | 70 |
1 files changed, 24 insertions, 46 deletions
diff --git a/src/mongo/base/parse_number_test.cpp b/src/mongo/base/parse_number_test.cpp index d5afb89157e..d43e8eaffac 100644 --- a/src/mongo/base/parse_number_test.cpp +++ b/src/mongo/base/parse_number_test.cpp @@ -40,13 +40,15 @@ namespace mongo { namespace { - template <typename _NumberType> + // MakeUnique argument is to allow testing types that are aliases like int and int32_t + template <typename _NumberType, int MakeUnique = 0> class CommonNumberParsingTests { - public: + TEMPLATE_SUITE_BOILERPLATE; + typedef _NumberType NumberType; typedef std::numeric_limits<NumberType> Limits; - static void TestRejectingBadBases() { + TEMPLATE_SUITE_TEST(CommonNumberParsingTests, TestRejectingBadBases) { NumberType ignored; ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("0", -1, &ignored)); ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("10", 1, &ignored)); @@ -56,14 +58,14 @@ namespace { ASSERT_EQUALS(ErrorCodes::BadValue, parseNumberFromStringWithBase("^%", -1, &ignored)); } - static void TestParsingNonNegatives() { + TEMPLATE_SUITE_TEST(CommonNumberParsingTests, TestParsingNonNegatives) { ASSERT_PARSES(NumberType, "10", 10); ASSERT_PARSES(NumberType, "0", 0); ASSERT_PARSES(NumberType, "0xff", 0xff); ASSERT_PARSES(NumberType, "077", 077); } - static void TestParsingNegatives() { + TEMPLATE_SUITE_TEST(CommonNumberParsingTests, TestParsingNegatives) { if (Limits::is_signed) { ASSERT_PARSES(NumberType, "-10", -10); ASSERT_PARSES(NumberType, "-0xff", -0xff); @@ -77,7 +79,7 @@ namespace { } } - static void TestParsingGarbage() { + TEMPLATE_SUITE_TEST(CommonNumberParsingTests, TestParsingGarbage) { NumberType ignored; ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("", &ignored)); ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString(" ", &ignored)); @@ -85,7 +87,7 @@ namespace { ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromString("15b", &ignored)); } - static void TestParsingWithExplicitBase() { + TEMPLATE_SUITE_TEST(CommonNumberParsingTests, TestParsingWithExplicitBase) { NumberType ignored; ASSERT_PARSES_WITH_BASE(NumberType, "15b", 16, 0x15b); ASSERT_PARSES_WITH_BASE(NumberType, "77", 8, 077); @@ -94,7 +96,7 @@ namespace { ASSERT_EQUALS(ErrorCodes::FailedToParse, parseNumberFromStringWithBase("80", 8, &ignored)); } - static void TestParsingLimits() { + TEMPLATE_SUITE_TEST(CommonNumberParsingTests, TestParsingLimits) { using namespace mongoutils; NumberType ignored; ASSERT_PARSES(NumberType, (str::stream() << Limits::max()), Limits::max()); @@ -109,44 +111,20 @@ namespace { } }; -#define GENERAL_NUMBER_TESTS(SHORT_NAME, TYPE) \ - class ParseNumberTests##SHORT_NAME : public unittest::Test { \ - public: \ - typedef CommonNumberParsingTests<TYPE> TestFns; \ - }; \ - TEST_F(ParseNumberTests##SHORT_NAME, RejectBadBases) { \ - TestFns::TestRejectingBadBases(); \ - } \ - TEST_F(ParseNumberTests##SHORT_NAME, ParseNonNegatives) { \ - TestFns::TestParsingNonNegatives(); \ - } \ - TEST_F(ParseNumberTests##SHORT_NAME, ParseNegatives) { \ - TestFns::TestParsingNegatives(); \ - } \ - TEST_F(ParseNumberTests##SHORT_NAME, ParseGarbage) { \ - TestFns::TestParsingGarbage(); \ - } \ - TEST_F(ParseNumberTests##SHORT_NAME, ParseWithExplicitBase) { \ - TestFns::TestParsingWithExplicitBase(); \ - } \ - TEST_F(ParseNumberTests##SHORT_NAME, TestParsingLimits) { \ - TestFns::TestParsingLimits(); \ - } - - GENERAL_NUMBER_TESTS(Short, short) - GENERAL_NUMBER_TESTS(Int, int) - GENERAL_NUMBER_TESTS(Long, long) - GENERAL_NUMBER_TESTS(LongLong, long long) - GENERAL_NUMBER_TESTS(UnsignedShort, unsigned short) - GENERAL_NUMBER_TESTS(UnsignedInt, unsigned int) - GENERAL_NUMBER_TESTS(UnsignedLong, unsigned long) - GENERAL_NUMBER_TESTS(UnsignedLongLong, unsigned long long) - GENERAL_NUMBER_TESTS(Int16, int16_t); - GENERAL_NUMBER_TESTS(Int32, int32_t); - GENERAL_NUMBER_TESTS(Int64, int64_t); - GENERAL_NUMBER_TESTS(UInt16, uint16_t); - GENERAL_NUMBER_TESTS(UInt32, uint32_t); - GENERAL_NUMBER_TESTS(UInt64, uint64_t); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, short); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, int); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, long); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, long long); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, unsigned short); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, unsigned int); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, unsigned long); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, unsigned long long); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, int16_t, 1); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, int32_t, 1); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, int64_t, 1); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, uint16_t, 1); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, uint32_t, 1); + TEMPLATE_SUITE_INSTANCE(CommonNumberParsingTests, uint64_t, 1); } // namespace } // namespace mongo |