Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gtest/gtest.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <algorithm>
- #include <vector>
- using namespace std;
- class TextInfo {
- vector<string> strings;
- private:
- void readData(istream &in)
- {
- string buffer;
- while (getline(in, buffer))
- addString(buffer);
- }
- public:
- TextInfo() {}
- TextInfo(const vector<string> &strings) {
- this->strings = strings;
- }
- TextInfo(istream &in) {
- readData(in);
- }
- int getStringsCount() const {
- return strings.size();
- }
- string getString(const int n) const {
- return strings[n];
- }
- void setString(const string &str, const int n) {
- strings[n] = str;
- }
- void addString(const string &str) {
- strings.push_back(str);
- }
- void removeWordFromStrings(const string &wordToDelete) {
- for_each(strings.begin(), strings.end(), [&wordToDelete](string &str) {
- size_t pos;
- while ((pos = str.find(wordToDelete)) != str.npos)
- str.erase(pos, wordToDelete.length());
- });
- }
- void sortStrings() {
- sort(strings.begin(), strings.end(), [](const string& str1, const string& str2) {
- string a = str1, b = str2;
- transform(a.begin(), a.end(), a.begin(), ::tolower);
- transform(b.begin(), b.end(), b.begin(), ::tolower);
- return a < b;
- });
- }
- void saveToFile(ostream &out) const {
- for (const auto& str : strings)
- out << str << endl;
- }
- };
- TEST(TextInfoTests, ReadData) {
- string data = "str1 \nstr2\n str3\n str4";
- istringstream inputStream(data);
- TextInfo textInfo(inputStream);
- ASSERT_EQ(textInfo.getStringsCount(), 4);
- }
- TEST(TextInfoTests, AddString) {
- TextInfo textInfo;
- textInfo.addString("a");
- textInfo.addString("b");
- textInfo.addString("c");
- textInfo.addString("d");
- ASSERT_EQ(textInfo.getStringsCount(), 4);
- }
- TEST(TextInfoTests, GetString) {
- TextInfo textInfo;
- textInfo.addString("str");
- ASSERT_EQ(textInfo.getString(0), "str");
- }
- TEST(TextInfoTests, SetString) {
- TextInfo textInfo;
- textInfo.addString("str");
- textInfo.setString("new str", 0);
- ASSERT_EQ(textInfo.getString(0), "new str");
- }
- TEST(TextInfoTests, RemoveWord) {
- string data = "string1\nstring2\n3 string string3\nsome text\nstring";
- istringstream inputStream(data);
- TextInfo textInfo(inputStream);
- textInfo.removeWordFromStrings("string");
- string result;
- for (int i = 0; i < textInfo.getStringsCount(); i++)
- result += textInfo.getString(i);
- ASSERT_EQ("123 3some text", result);
- }
- TEST(TextInfoTests, SortStrings) {
- string data = "3\n2\n1\n";
- istringstream inputStream(data);
- TextInfo textInfo(inputStream);
- textInfo.sortStrings();
- string result = textInfo.getString(0) + textInfo.getString(1) + textInfo.getString(2);
- ASSERT_EQ("123", result);
- }
- TEST(TextInfoTests, SortStrings_CaseInsensetive) {
- string data = "B\nc\nD\na";
- istringstream inputStream(data);
- TextInfo textInfo(inputStream);
- textInfo.sortStrings();
- string result = textInfo.getString(0) + textInfo.getString(1) + textInfo.getString(2) + textInfo.getString(3);
- ASSERT_EQ("aBcD", result);
- }
- TEST(TextInfoTests, SaveToFile) {
- ofstream out("out.txt");
- TextInfo textInfo;
- textInfo.addString("3");
- textInfo.addString("2");
- textInfo.addString("1");
- textInfo.saveToFile(out);
- ifstream in("out.txt");
- string result, buffer;
- getline(in, result);
- getline(in, buffer);
- result += buffer;
- getline(in, buffer);
- result += buffer;
- ASSERT_EQ("321", result);
- }
- int main(int argc, wchar_t ** argv)
- {
- ::testing::InitGoogleTest(&argc, argv);
- RUN_ALL_TESTS();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement