made some more tests parametric & made some cosmetic changes to the

tests
This commit is contained in:
Kjistóf 2016-10-24 21:26:45 +02:00
parent 3149e1a653
commit af4fede463

View File

@ -12,20 +12,23 @@
/// Testing helper classes /// /// Testing helper classes ///
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
using str3 = std::tuple<std::string, std::string, std::string>; using str3 = std::tuple<std::string, std::string, std::string>;
class addStringTest : public ::testing::TestWithParam<str3> class AddStringTest : public ::testing::TestWithParam<str3>
{}; {};
using strcharstr = std::tuple<std::string, char, std::string>; using strcharstr = std::tuple<std::string, char, std::string>;
class addCharStringTest : public ::testing::TestWithParam<strcharstr> class AddCharStringTest : public ::testing::TestWithParam<strcharstr>
{}; {};
struct StreamStringTest : public ::testing::Test class ComparableStringTest : public ::testing::TestWithParam<std::string> {};
class ResourceSharingStringTest : public ::testing::TestWithParam<std::string> {};
class StreamStringTest : public ::testing::TestWithParam<std::string>
{ {
public:
std::stringstream _stream; std::stringstream _stream;
}; };
class SingleParamStringTest : public ::testing::TestWithParam<std::string> {};
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
/// Testing data /// /// Testing data ///
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
@ -53,6 +56,7 @@ static const std::vector<std::string> testvalues3 =
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
/// Tests /// /// Tests ///
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
// Basic tests
TEST(StringTest, defaultConstructible) TEST(StringTest, defaultConstructible)
{ {
EXPECT_TRUE(std::is_default_constructible<String>::value); EXPECT_TRUE(std::is_default_constructible<String>::value);
@ -100,7 +104,9 @@ TEST(StringTest, uninitializedStringcStrThrows)
EXPECT_THROW(str.c_str(), std::runtime_error); EXPECT_THROW(str.c_str(), std::runtime_error);
} }
TEST_P(addStringTest, addable)
// add operator tests
TEST_P(AddStringTest, addable)
{ {
String str1(std::get<0>(GetParam()).c_str()); String str1(std::get<0>(GetParam()).c_str());
String str2(std::get<1>(GetParam()).c_str()); String str2(std::get<1>(GetParam()).c_str());
@ -108,7 +114,7 @@ TEST_P(addStringTest, addable)
EXPECT_STREQ(std::get<2>(GetParam()).c_str(), (str1+str2).c_str()); EXPECT_STREQ(std::get<2>(GetParam()).c_str(), (str1+str2).c_str());
} }
TEST_P(addStringTest, plusEqualsable) TEST_P(AddStringTest, plusEqualsable)
{ {
String str1(std::get<0>(GetParam()).c_str()); String str1(std::get<0>(GetParam()).c_str());
String str2(std::get<1>(GetParam()).c_str()); String str2(std::get<1>(GetParam()).c_str());
@ -116,32 +122,34 @@ TEST_P(addStringTest, plusEqualsable)
EXPECT_STREQ(std::get<2>(GetParam()).c_str(), str1.c_str()); EXPECT_STREQ(std::get<2>(GetParam()).c_str(), str1.c_str());
} }
INSTANTIATE_TEST_CASE_P(testGroup1, addStringTest, ::testing::ValuesIn(testvalues1)); INSTANTIATE_TEST_CASE_P(tests, AddStringTest, ::testing::ValuesIn(testvalues1));
TEST_P(addCharStringTest, addCharable) TEST_P(AddCharStringTest, addCharable)
{ {
String str(std::get<0>(GetParam()).c_str()); String str(std::get<0>(GetParam()).c_str());
EXPECT_STREQ(std::get<2>(GetParam()).c_str(), (str+std::get<1>(GetParam())).c_str()); EXPECT_STREQ(std::get<2>(GetParam()).c_str(), (str+std::get<1>(GetParam())).c_str());
} }
TEST_P(addCharStringTest, plusEqualsCharable) TEST_P(AddCharStringTest, plusEqualsCharable)
{ {
String str(std::get<0>(GetParam()).c_str()); String str(std::get<0>(GetParam()).c_str());
str+=std::get<1>(GetParam()); str+=std::get<1>(GetParam());
EXPECT_STREQ(std::get<2>(GetParam()).c_str(), str.c_str()); EXPECT_STREQ(std::get<2>(GetParam()).c_str(), str.c_str());
} }
INSTANTIATE_TEST_CASE_P(testGroup1, addCharStringTest, ::testing::ValuesIn(testvalues2)); INSTANTIATE_TEST_CASE_P(tests, AddCharStringTest, ::testing::ValuesIn(testvalues2));
TEST_P(SingleParamStringTest, comparable)
// Comparison tests
TEST_P(ComparableStringTest, comparable)
{ {
String str1(GetParam().c_str()); String str1(GetParam().c_str());
String str2(GetParam().c_str()); String str2(GetParam().c_str());
EXPECT_EQ(str1, str2); EXPECT_EQ(str1, str2);
} }
INSTANTIATE_TEST_CASE_P(testGroup1, SingleParamStringTest, ::testing::ValuesIn(testvalues3)); INSTANTIATE_TEST_CASE_P(tests, ComparableStringTest, ::testing::ValuesIn(testvalues3));
TEST(StringTest, uninitializedStringCompareThrows) TEST(StringTest, uninitializedStringCompareThrows)
{ {
@ -151,6 +159,8 @@ TEST(StringTest, uninitializedStringCompareThrows)
EXPECT_THROW(str1 == str2, std::runtime_error); EXPECT_THROW(str1 == str2, std::runtime_error);
} }
// Index operator tests
TEST(StringTest, indexOperatorWorks) TEST(StringTest, indexOperatorWorks)
{ {
String str("cica"); String str("cica");
@ -173,6 +183,8 @@ TEST(StringTest, uninitializedStringIndexingThrows)
EXPECT_THROW(str[0], std::runtime_error); EXPECT_THROW(str[0], std::runtime_error);
} }
// COW & resource sharing tests
TEST(StringTest, copyOnWriteWorks) TEST(StringTest, copyOnWriteWorks)
{ {
String str1("cica"); String str1("cica");
@ -182,30 +194,34 @@ TEST(StringTest, copyOnWriteWorks)
EXPECT_STRNE(str1.c_str(), str2.c_str()); EXPECT_STRNE(str1.c_str(), str2.c_str());
} }
TEST(StringTest, resourceSharingWorks) TEST_P(ResourceSharingStringTest, resourceSharingWorks)
{ {
String str1("sajt"); String str1(GetParam().c_str());
String str2 = String("sajt"); String str2 = String(GetParam().c_str());
EXPECT_EQ(str1.c_str(), str2.c_str()); EXPECT_EQ(str1.c_str(), str2.c_str());
} }
INSTANTIATE_TEST_CASE_P(tests, ResourceSharingStringTest, ::testing::ValuesIn(testvalues3));
TEST_F(StreamStringTest, ostreamOperatorWorks)
// Stream tests
TEST_P(StreamStringTest, ostreamOperatorWorks)
{ {
String str("cica"); String str(GetParam().c_str());
_stream << str; _stream << str;
EXPECT_STREQ("cica", _stream.str().c_str()); EXPECT_STREQ(GetParam().c_str(), _stream.str().c_str());
} }
TEST_F(StreamStringTest, istreamOperatorWorks) TEST_P(StreamStringTest, istreamOperatorWorks)
{ {
String str; String str;
_stream << "sajt"; _stream << GetParam().c_str();
_stream >> str; _stream >> str;
EXPECT_STREQ("sajt", str.c_str()); EXPECT_STREQ(GetParam().c_str(), str.c_str());
} }
INSTANTIATE_TEST_CASE_P(tests, StreamStringTest, ::testing::ValuesIn(testvalues3));