2016-10-23 01:46:48 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <type_traits>
|
2016-10-23 12:14:41 +00:00
|
|
|
#include <stdexcept>
|
2016-10-23 01:46:48 +00:00
|
|
|
#include "String.h"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST(StringTest, defaultConstructible)
|
|
|
|
{
|
2016-10-23 12:14:41 +00:00
|
|
|
EXPECT_TRUE(std::is_default_constructible<String>::value);
|
2016-10-23 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-10-23 12:14:41 +00:00
|
|
|
EXPECT_TRUE(value);
|
2016-10-23 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, copyConstructible)
|
|
|
|
{
|
2016-10-23 12:14:41 +00:00
|
|
|
EXPECT_TRUE(std::is_copy_constructible<String>::value);
|
2016-10-23 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, copyAssignable)
|
|
|
|
{
|
2016-10-23 12:14:41 +00:00
|
|
|
EXPECT_TRUE(std::is_copy_assignable<String>::value);
|
2016-10-23 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, moveConstructible)
|
|
|
|
{
|
2016-10-23 12:14:41 +00:00
|
|
|
EXPECT_TRUE(std::is_move_constructible<String>::value);
|
2016-10-23 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, moveAssignable)
|
|
|
|
{
|
2016-10-23 12:14:41 +00:00
|
|
|
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);
|
2016-10-23 01:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, addable)
|
|
|
|
{
|
|
|
|
String str1("cica");
|
|
|
|
String str2("sajt");
|
|
|
|
|
|
|
|
EXPECT_STREQ("cicasajt", (str1+str2).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, plusequalsAddable)
|
|
|
|
{
|
|
|
|
String str1("cica");
|
|
|
|
String str2("sajt");
|
|
|
|
str1 += str2;
|
|
|
|
|
|
|
|
EXPECT_STREQ("cicasajt", str1.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, addableChar)
|
|
|
|
{
|
|
|
|
String str("cicá");
|
|
|
|
|
|
|
|
EXPECT_STREQ("cicák", (str+'k').c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, plusequalsAddableChar)
|
|
|
|
{
|
|
|
|
String str("sajto");
|
|
|
|
str+='k';
|
|
|
|
|
|
|
|
EXPECT_STREQ("sajtok", str.c_str());
|
|
|
|
}
|
|
|
|
|
2016-10-23 12:14:41 +00:00
|
|
|
TEST(StringTest, comparable)
|
|
|
|
{
|
|
|
|
String str1("cica");
|
|
|
|
String str2("cica");
|
|
|
|
|
|
|
|
EXPECT_EQ(str1, str2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(StringTest, uninitializedStringCompareThrows)
|
|
|
|
{
|
|
|
|
String str1;
|
|
|
|
String str2;
|
|
|
|
|
|
|
|
EXPECT_THROW(str1 == str2, std::runtime_error);
|
|
|
|
}
|
|
|
|
|
2016-10-23 01:46:48 +00:00
|
|
|
TEST(StringTest, indexOperatorWorks)
|
|
|
|
{
|
|
|
|
String str("cica");
|
|
|
|
char i = 'i';
|
|
|
|
|
|
|
|
EXPECT_EQ(i, str[1]);
|
|
|
|
}
|
|
|
|
|
2016-10-23 12:14:41 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-10-23 01:46:48 +00:00
|
|
|
TEST(StringTest, resourceSharingWorks)
|
|
|
|
{
|
|
|
|
String str1("sajt");
|
|
|
|
String str2 = String("sajt");
|
|
|
|
|
|
|
|
EXPECT_EQ(str1.c_str(), str2.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamStringTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
std::stringstream _stream;
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|