SHOW:
|
|
- or go back to the newest paste.
| 1 | // -------- Начало модульных тестов поисковой системы ---------- | |
| 2 | ||
| 3 | // Тест проверяет, что поисковая система исключает стоп-слова при добавлении документов | |
| 4 | void TestExcludeStopWordsFromAddedDocumentContent() {
| |
| 5 | const int doc_id = 42; | |
| 6 | const string content = "cat in the city"s; | |
| 7 | const vector<int> ratings = {1, 2, 3};
| |
| 8 | {
| |
| 9 | SearchServer server; | |
| 10 | server.AddDocument(doc_id, content, DocumentStatus::ACTUAL, ratings); | |
| 11 | const auto found_docs = server.FindTopDocuments("in"s);
| |
| 12 | ASSERT_EQUAL(found_docs.size(), 1u); | |
| 13 | const Document& doc0 = found_docs[0]; | |
| 14 | ASSERT_EQUAL(doc0.id, doc_id); | |
| 15 | } | |
| 16 | ||
| 17 | {
| |
| 18 | SearchServer server; | |
| 19 | server.SetStopWords("in the"s);
| |
| 20 | server.AddDocument(doc_id, content, DocumentStatus::ACTUAL, ratings); | |
| 21 | ASSERT_HINT(server.FindTopDocuments("in"s).empty(),
| |
| 22 | "Stop words must be excluded from documents"s); | |
| 23 | } | |
| 24 | } | |
| 25 | /* | |
| 26 | Разместите код остальных тестов здесь | |
| 27 | */ | |
| 28 | void TestMatchQuery(){
| |
| 29 | string query1 = "what happend with this city"; | |
| 30 | string query2 = "i fucked up this city"; | |
| 31 | ||
| 32 | vector<int> ratings1 = {-1, -2, -5, -7, 11};
| |
| 33 | vector<int> ratings2 = {-11, -22, -55, -76, 10};
| |
| 34 | ||
| 35 | DocumentStatus status1 = DocumentStatus::ACTUAL; | |
| 36 | DocumentStatus status2 = DocumentStatus::BANNED; | |
| 37 | ||
| 38 | SearchServer search_server; | |
| 39 | search_server.AddDocument(1, query1, status1, ratings1); | |
| 40 | search_server.AddDocument(2, query2, status2, ratings2); | |
| 41 | ||
| 42 | vector<Document> document = search_server.FindTopDocuments("what happend with this city");
| |
| 43 | ASSERT_EQUAL_HINT(document.size(), 1, "The number of matching documents is incorrect"); | |
| 44 | } | |
| 45 | ||
| 46 | void TestMinusWords() {
| |
| 47 | SearchServer search_server; | |
| 48 | ||
| 49 | int document_id = 1; | |
| 50 | vector<int> rating = {-1, -2, -5, -7, 11};
| |
| 51 | DocumentStatus status = DocumentStatus::ACTUAL; | |
| 52 | ||
| 53 | string document_match = "tabby cat with big eyes"; | |
| 54 | search_server.AddDocument(document_id, document_match, status, rating); | |
| 55 | ||
| 56 | document_id = 2; | |
| 57 | string document_minus = "small dog and tabby cat"; | |
| 58 | search_server.AddDocument(document_id, document_minus, status, rating); | |
| 59 | ||
| 60 | string query = "-small -dog walks tabby cat"; | |
| 61 | vector<Document> found_documents = search_server.FindTopDocuments(query); | |
| 62 | ||
| 63 | ASSERT_EQUAL_HINT(found_documents.size(), 1, "One document must be found"); | |
| 64 | ASSERT_EQUAL_HINT(found_documents[0].id, 1, "Document id does not match"); | |
| 65 | } | |
| 66 | ||
| 67 | void TestMatchWords(){
| |
| 68 | SearchServer search_server; | |
| 69 | ||
| 70 | string document1 = "tabby cat with big eyes"s; | |
| 71 | vector<string> document_split = {"tabby"s, "cat"s, "with"s, "big"s, "eyes"s};
| |
| 72 | sort(document_split.begin(), document_split.end()); | |
| 73 | ||
| 74 | search_server.AddDocument(1, document1, DocumentStatus::ACTUAL, {13});
| |
| 75 | tuple<vector<string>, DocumentStatus> matched_words1 = search_server.MatchDocument("tabby cat with big eyes"s, 1);
| |
| 76 | ||
| 77 | ASSERT_EQUAL_HINT(document_split, get<vector<string>>(matched_words1), "Matching words must also be in the ethalon vector"); | |
| 78 | ||
| 79 | string document2 = "small dog and tabby cat"s; | |
| 80 | search_server.AddDocument(2, document2, DocumentStatus::ACTUAL, {10});
| |
| 81 | tuple<vector<string>, DocumentStatus> matched_words = search_server.MatchDocument("-small -dog and tabby cat"s, 2);
| |
| 82 | const auto [string_, doc_] = matched_words; | |
| 83 | ASSERT_HINT(string_.empty(), "Matching words should not be found"); | |
| 84 | } | |
| 85 | ||
| 86 | void TestSortByRelevance(){
| |
| 87 | SearchServer search_server; | |
| 88 | int document_id = 1; | |
| 89 | vector<int> rating = {1, 2, 3};
| |
| 90 | DocumentStatus status = DocumentStatus::ACTUAL; | |
| 91 | ||
| 92 | string document1 = "big elephant with pink super duper tail"; | |
| 93 | search_server.AddDocument(document_id, document1, status, rating); | |
| 94 | document_id = 2; | |
| 95 | string document2 = "orange pig fucked in the pink mouth"; | |
| 96 | search_server.AddDocument(document_id, document2, status, rating); | |
| 97 | document_id = 3; | |
| 98 | string document3 = "super pink dog with blue tail"; | |
| 99 | search_server.AddDocument(document_id, document3, status, rating); | |
| 100 | document_id = 4; | |
| 101 | string document4 = "super pink carpet"; | |
| 102 | search_server.AddDocument(document_id, document4, status, rating); | |
| 103 | ||
| 104 | string query = "pink super duper tail"; | |
| 105 | ||
| 106 | vector<Document> found_documents = search_server.FindTopDocuments(query); | |
| 107 | ASSERT_EQUAL_HINT(found_documents[0].id, 1, "Document id does not match (relevance)"); | |
| 108 | ASSERT_EQUAL_HINT(found_documents[1].id, 3, "Document id does not match (relevance)"); | |
| 109 | ASSERT_EQUAL_HINT(found_documents[2].id, 4, "Document id does not match (relevance)"); | |
| 110 | ASSERT_EQUAL_HINT(found_documents[3].id, 2, "Document id does not match (relevance)"); | |
| 111 | } | |
| 112 | ||
| 113 | void TestRating() {
| |
| 114 | SearchServer search_server; | |
| 115 | DocumentStatus status = DocumentStatus::ACTUAL; | |
| 116 | ||
| 117 | int document_id = 1; | |
| 118 | vector<int> rating1 = {1, 1, 1, 1, 1};
| |
| 119 | string document1 = "big elephant with pink super duper tail"; | |
| 120 | search_server.AddDocument(document_id, document1, status, rating1); | |
| 121 | document_id = 2; | |
| 122 | vector<int> rating2 = {2, 2, 2, 2, 2};
| |
| 123 | string document2 = "orange pig fucked in the pink mouth"; | |
| 124 | search_server.AddDocument(document_id, document2, status, rating2); | |
| 125 | document_id = 3; | |
| 126 | vector<int> rating3 = {3, 3, 3, 3, 3};
| |
| 127 | string document3 = "pink dog with blue collar and super tail"; | |
| 128 | search_server.AddDocument(document_id, document3, status, rating3); | |
| 129 | document_id = 4; | |
| 130 | vector<int> rating4 = {4, 4, 4, 4, 4};
| |
| 131 | string document4 = "super pink carpet"; | |
| 132 | search_server.AddDocument(document_id, document4, status, rating4); | |
| 133 | document_id = 5; | |
| 134 | vector<int> rating5 = {5, 5, 5, 5, 5};
| |
| 135 | string document5 = "green baby with pink big dick and sweet ass"; | |
| 136 | search_server.AddDocument(document_id, document5, status, rating5); | |
| 137 | ||
| 138 | string query = "pink"; | |
| 139 | ||
| 140 | vector<Document> documents_to_rating = search_server.FindTopDocuments(query); | |
| 141 | ASSERT_EQUAL_HINT(documents_to_rating[0].rating, 5, "Incorrect rating"); | |
| 142 | ASSERT_EQUAL_HINT(documents_to_rating[1].rating, 4, "Incorrect rating"); | |
| 143 | ASSERT_EQUAL_HINT(documents_to_rating[2].rating, 3, "Incorrect rating"); | |
| 144 | ASSERT_EQUAL_HINT(documents_to_rating[3].rating, 2, "Incorrect rating"); | |
| 145 | ASSERT_EQUAL_HINT(documents_to_rating[4].rating, 1, "Incorrect rating"); | |
| 146 | ||
| 147 | } | |
| 148 | ||
| 149 | void TestFindDocumentByPredicate(){
| |
| 150 | SearchServer search_server; | |
| 151 | search_server.AddDocument(1, "tabby cat with big eyes"s, DocumentStatus::REMOVED, {13});
| |
| 152 | search_server.AddDocument(2, "small dog and tabby cat"s, DocumentStatus::ACTUAL, {20});
| |
| 153 | vector<Document> found_document = search_server.FindTopDocuments("cat", []([[maybe_unused]] int document_id, DocumentStatus status, [[maybe_unused]] int rating) { return status == DocumentStatus::ACTUAL;});
| |
| 154 | ASSERT_EQUAL_HINT(found_document[0].id, 2, "Incorrect document id (predicate)"); | |
| 155 | } | |
| 156 | ||
| 157 | void TestFindDocumentByStatus(){
| |
| 158 | SearchServer search_server; | |
| 159 | int document_id = 1; | |
| 160 | vector<int> rating = {1, 2, 3, 4, 5};
| |
| 161 | DocumentStatus status1 = DocumentStatus::ACTUAL; | |
| 162 | string document1 = "big elephant with pink tail"; | |
| 163 | search_server.AddDocument(document_id, document1, status1, rating); | |
| 164 | document_id = 2; | |
| 165 | string document2 = "pink pig fucked in the mouth"; | |
| 166 | DocumentStatus status2 = DocumentStatus::BANNED; | |
| 167 | search_server.AddDocument(document_id, document2, status2, rating); | |
| 168 | document_id = 3; | |
| 169 | string document3 = "pink dog with blue collar"; | |
| 170 | DocumentStatus status3 = DocumentStatus::IRRELEVANT; | |
| 171 | search_server.AddDocument(document_id, document3, status3, rating); | |
| 172 | document_id = 4; | |
| 173 | string document4 = "green baby with big pink dick and sweet ass"; | |
| 174 | DocumentStatus status4 = DocumentStatus::REMOVED; | |
| 175 | search_server.AddDocument(document_id, document4, status4, rating); | |
| 176 | ||
| 177 | string query = "pink tail"; | |
| 178 | ||
| 179 | vector<Document> documents_actual = search_server.FindTopDocuments(query, DocumentStatus::ACTUAL); | |
| 180 | ASSERT_EQUAL_HINT(documents_actual[0].id, 1, "Document id does not match (status)"); | |
| 181 | ||
| 182 | vector<Document> documents_banned = search_server.FindTopDocuments(query, DocumentStatus::BANNED); | |
| 183 | ASSERT_EQUAL_HINT(documents_banned[0].id, 2, "Document id does not match (status)"); | |
| 184 | ||
| 185 | vector<Document> documents_irrelevant = search_server.FindTopDocuments(query, DocumentStatus::IRRELEVANT); | |
| 186 | ASSERT_EQUAL_HINT(documents_irrelevant[0].id, 3, "Document id does not match (status)"); | |
| 187 | ||
| 188 | vector<Document> documents_removed = search_server.FindTopDocuments(query, DocumentStatus::REMOVED); | |
| 189 | ASSERT_EQUAL_HINT(documents_removed[0].id, 4, "Document id does not match (status)"); | |
| 190 | } | |
| 191 | ||
| 192 | void TestAccurateRelevance(){
| |
| 193 | SearchServer search_server; | |
| 194 | int document_id = 1; | |
| 195 | vector<int> rating1 = { 1, 7, 13 };
| |
| 196 | DocumentStatus status = DocumentStatus::ACTUAL; | |
| 197 | string document1 = "big cute cat"s; | |
| 198 | search_server.AddDocument(document_id, document1, status, rating1); | |
| 199 | ||
| 200 | document_id = 2; | |
| 201 | vector<int> rating2 = { 2, 4, 10 };
| |
| 202 | string document2 = "small tabby bird"s; | |
| 203 | search_server.AddDocument(document_id, document2, status, rating2); | |
| 204 | vector<Document> document_ = search_server.FindTopDocuments("cat"s);
| |
| 205 | ASSERT_EQUAL_HINT(document_[0].relevance, (log(2/1))*1/3, "Relevance does not match"); | |
| 206 | } | |
| 207 | ||
| 208 | // Функция TestSearchServer является точкой входа для запуска тестов | |
| 209 | void TestSearchServer() {
| |
| 210 | RUN_TEST(TestExcludeStopWordsFromAddedDocumentContent); | |
| 211 | RUN_TEST(TestMatchQuery); | |
| 212 | RUN_TEST(TestMinusWords); | |
| 213 | RUN_TEST(TestMatchWords); | |
| 214 | RUN_TEST(TestSortByRelevance); | |
| 215 | RUN_TEST(TestRating); | |
| 216 | RUN_TEST(TestFindDocumentByPredicate); | |
| 217 | RUN_TEST(TestFindDocumentByStatus); | |
| 218 | RUN_TEST(TestAccurateRelevance); | |
| 219 | // Не забудьте вызывать остальные тесты здесь | |
| 220 | } | |
| 221 | ||
| 222 | // --------- Окончание модульных тестов поисковой системы ----------- | |
| 223 | ||
| 224 | int main() {
| |
| 225 | TestSearchServer(); | |
| 226 | // Если вы видите эту строку, значит все тесты прошли успешно | |
| 227 | cout << "Search server testing finished"s << endl; | |
| 228 | } |