refcountedstring/test.cpp

221 lines
5.5 KiB
C++

#include <iostream>
#include <sstream>
#include <type_traits>
#include <stdexcept>
#include <tuple>
#include "String.h"
#include <gtest/gtest.h>
///////////////////////////////////////////////////////////////
/// Testing helper classes ///
///////////////////////////////////////////////////////////////
using str3 = std::tuple<const char*, const char*, const char*>;
class addStringTest : public ::testing::TestWithParam<str3>
{};
using strcharstr = std::tuple<const char*, char, const char*>;
class addCharStringTest : public ::testing::TestWithParam<strcharstr>
{};
struct StreamStringTest : public ::testing::Test
{
std::stringstream _stream;
};
class SingleParamStringTest : public ::testing::TestWithParam<std::tuple<const char*>> {};
///////////////////////////////////////////////////////////////
/// Testing data ///
///////////////////////////////////////////////////////////////
static const std::vector<str3> testvalues1 =
{
std::make_tuple("cica", "sajt", "cicasajt"),
std::make_tuple("sajtos", " cica", "sajtos cica"),
std::make_tuple("szeretem", " a sajtot", "szeretem a sajtot"),
std::make_tuple("meg ", "a cicákat", "meg a cicákat"),
std::make_tuple("ennyi már ", "elég lesz", "ennyi már elég lesz")
};
static const std::vector<strcharstr> testvalues2 =
{
std::make_tuple("cicá", 'k', "cicák"),
std::make_tuple("sajto", 'k', "sajtok"),
std::make_tuple("C++1", '1', "C++11"),
std::make_tuple("", 'r', "sör"),
std::make_tuple("te", 'j', "tej")
};
static const std::vector<std::tuple<const char*>> testvalues3 =
{
std::make_tuple("cica"),
std::make_tuple("sajt"),
std::make_tuple("sör"),
std::make_tuple("C++11"),
std::make_tuple("suchInitializerList")
};
///////////////////////////////////////////////////////////////
/// Tests ///
///////////////////////////////////////////////////////////////
TEST(StringTest, defaultConstructible)
{
EXPECT_TRUE(std::is_default_constructible<String>::value);
}
TEST(StringTest, constcharConstructible)
{
// is_constructible with multiple template params cannot be passed to macro,
// TODO: should open an issue at GTest's GitHub
auto value = std::is_constructible<String, const char*>::value;
EXPECT_TRUE(value);
}
TEST(StringTest, copyConstructible)
{
EXPECT_TRUE(std::is_copy_constructible<String>::value);
}
TEST(StringTest, copyAssignable)
{
EXPECT_TRUE(std::is_copy_assignable<String>::value);
}
TEST(StringTest, moveConstructible)
{
EXPECT_TRUE(std::is_move_constructible<String>::value);
}
TEST(StringTest, moveAssignable)
{
EXPECT_TRUE(std::is_move_assignable<String>::value);
}
TEST(StringTest, cStrMethodWorks)
{
String str("sajtos cica");
EXPECT_STREQ("sajtos cica", str.c_str());
}
TEST(StringTest, uninitializedStringcStrThrows)
{
String str;
EXPECT_THROW(str.c_str(), std::runtime_error);
}
TEST_P(addStringTest, addable)
{
String str1(std::get<0>(GetParam()));
String str2(std::get<1>(GetParam()));
EXPECT_STREQ(std::get<2>(GetParam()), (str1+str2).c_str());
}
TEST_P(addStringTest, plusEqualsable)
{
String str1(std::get<0>(GetParam()));
String str2(std::get<1>(GetParam()));
str1 += str2;
EXPECT_STREQ(std::get<2>(GetParam()), str1.c_str());
}
INSTANTIATE_TEST_CASE_P(testGroup1, addStringTest, ::testing::ValuesIn(testvalues1));
TEST_P(addCharStringTest, addCharable)
{
String str(std::get<0>(GetParam()));
EXPECT_STREQ(std::get<2>(GetParam()), (str+std::get<1>(GetParam())).c_str());
}
TEST_P(addCharStringTest, plusEqualsCharable)
{
String str(std::get<0>(GetParam()));
str+=std::get<1>(GetParam());
EXPECT_STREQ(std::get<2>(GetParam()), str.c_str());
}
INSTANTIATE_TEST_CASE_P(testGroup1, addCharStringTest, ::testing::ValuesIn(testvalues2));
TEST_P(SingleParamStringTest, comparable)
{
String str1(std::get<0>(GetParam()));
String str2(std::get<0>(GetParam()));
EXPECT_EQ(str1, str2);
}
INSTANTIATE_TEST_CASE_P(testGroup1, SingleParamStringTest, ::testing::ValuesIn(testvalues3));
TEST(StringTest, uninitializedStringCompareThrows)
{
String str1;
String str2;
EXPECT_THROW(str1 == str2, std::runtime_error);
}
TEST(StringTest, indexOperatorWorks)
{
String str("cica");
char i = 'i';
EXPECT_EQ(i, str[1]);
}
TEST(StringTest, indexoperatorBoundCheckWorks)
{
String str("sajt");
EXPECT_THROW(str[4], std::out_of_range);
}
TEST(StringTest, uninitializedStringIndexingThrows)
{
String str;
EXPECT_THROW(str[0], std::runtime_error);
}
TEST(StringTest, copyOnWriteWorks)
{
String str1("cica");
String str2 = str1;
str2[0] = ' ';
EXPECT_STRNE(str1.c_str(), str2.c_str());
}
TEST(StringTest, resourceSharingWorks)
{
String str1("sajt");
String str2 = String("sajt");
EXPECT_EQ(str1.c_str(), str2.c_str());
}
TEST_F(StreamStringTest, ostreamOperatorWorks)
{
String str("cica");
_stream << str;
EXPECT_STREQ("cica", _stream.str().c_str());
}
TEST_F(StreamStringTest, istreamOperatorWorks)
{
String str;
_stream << "sajt";
_stream >> str;
EXPECT_STREQ("sajt", str.c_str());
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}