Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <gtest/gtest.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. class TextInfo {
  11. vector<string> strings;
  12. private:
  13. void readData(istream &in)
  14. {
  15. string buffer;
  16. while (getline(in, buffer))
  17. addString(buffer);
  18. }
  19. public:
  20. TextInfo() {}
  21.  
  22. TextInfo(const vector<string> &strings) {
  23. this->strings = strings;
  24. }
  25.  
  26. TextInfo(istream &in) {
  27. readData(in);
  28. }
  29.  
  30. int getStringsCount() const {
  31. return strings.size();
  32. }
  33.  
  34. string getString(const int n) const {
  35. return strings[n];
  36. }
  37.  
  38. void setString(const string &str, const int n) {
  39. strings[n] = str;
  40. }
  41.  
  42. void addString(const string &str) {
  43. strings.push_back(str);
  44. }
  45.  
  46. void removeWordFromStrings(const string &wordToDelete) {
  47. for_each(strings.begin(), strings.end(), [&wordToDelete](string &str) {
  48. size_t pos;
  49. while ((pos = str.find(wordToDelete)) != str.npos)
  50. str.erase(pos, wordToDelete.length());
  51. });
  52. }
  53.  
  54. void sortStrings() {
  55. sort(strings.begin(), strings.end(), [](const string& str1, const string& str2) {
  56. string a = str1, b = str2;
  57. transform(a.begin(), a.end(), a.begin(), ::tolower);
  58. transform(b.begin(), b.end(), b.begin(), ::tolower);
  59. return a < b;
  60. });
  61. }
  62.  
  63. void saveToFile(ostream &out) const {
  64. for (const auto& str : strings)
  65. out << str << endl;
  66. }
  67. };
  68.  
  69. TEST(TextInfoTests, ReadData) {
  70. string data = "str1 \nstr2\n str3\n str4";
  71. istringstream inputStream(data);
  72.  
  73. TextInfo textInfo(inputStream);
  74. ASSERT_EQ(textInfo.getStringsCount(), 4);
  75. }
  76.  
  77. TEST(TextInfoTests, AddString) {
  78. TextInfo textInfo;
  79. textInfo.addString("a");
  80. textInfo.addString("b");
  81. textInfo.addString("c");
  82. textInfo.addString("d");
  83. ASSERT_EQ(textInfo.getStringsCount(), 4);
  84. }
  85.  
  86.  
  87. TEST(TextInfoTests, GetString) {
  88. TextInfo textInfo;
  89. textInfo.addString("str");
  90. ASSERT_EQ(textInfo.getString(0), "str");
  91. }
  92.  
  93. TEST(TextInfoTests, SetString) {
  94. TextInfo textInfo;
  95. textInfo.addString("str");
  96. textInfo.setString("new str", 0);
  97. ASSERT_EQ(textInfo.getString(0), "new str");
  98. }
  99.  
  100. TEST(TextInfoTests, RemoveWord) {
  101. string data = "string1\nstring2\n3 string string3\nsome text\nstring";
  102. istringstream inputStream(data);
  103. TextInfo textInfo(inputStream);
  104. textInfo.removeWordFromStrings("string");
  105.  
  106. string result;
  107. for (int i = 0; i < textInfo.getStringsCount(); i++)
  108. result += textInfo.getString(i);
  109. ASSERT_EQ("123 3some text", result);
  110. }
  111.  
  112. TEST(TextInfoTests, SortStrings) {
  113. string data = "3\n2\n1\n";
  114. istringstream inputStream(data);
  115. TextInfo textInfo(inputStream);
  116. textInfo.sortStrings();
  117. string result = textInfo.getString(0) + textInfo.getString(1) + textInfo.getString(2);
  118. ASSERT_EQ("123", result);
  119. }
  120.  
  121. TEST(TextInfoTests, SortStrings_CaseInsensetive) {
  122. string data = "B\nc\nD\na";
  123. istringstream inputStream(data);
  124. TextInfo textInfo(inputStream);
  125. textInfo.sortStrings();
  126. string result = textInfo.getString(0) + textInfo.getString(1) + textInfo.getString(2) + textInfo.getString(3);
  127. ASSERT_EQ("aBcD", result);
  128. }
  129.  
  130. TEST(TextInfoTests, SaveToFile) {
  131. ofstream out("out.txt");
  132. TextInfo textInfo;
  133. textInfo.addString("3");
  134. textInfo.addString("2");
  135. textInfo.addString("1");
  136. textInfo.saveToFile(out);
  137.  
  138. ifstream in("out.txt");
  139. string result, buffer;
  140. getline(in, result);
  141.  
  142. getline(in, buffer);
  143. result += buffer;
  144. getline(in, buffer);
  145. result += buffer;
  146. ASSERT_EQ("321", result);
  147. }
  148.  
  149. int main(int argc, wchar_t ** argv)
  150. {
  151. ::testing::InitGoogleTest(&argc, argv);
  152. RUN_ALL_TESTS();
  153. system("pause");
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement