Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void TestExcludeStopWordsFromAddedDocumentContent() {
- const int doc_id = 42;
- const string content = "cat in the city"s;
- const vector<int> ratings = {1, 2, 3};
- // Сначала убеждаемся, что поиск слова, не входящего в список стоп-слов,
- // находит нужный документ
- {
- SearchServer server;
- server.AddDocument(doc_id, content, DocumentStatus::ACTUAL, ratings);
- const auto found_docs = server.FindTopDocuments("in"s);
- assert(found_docs.size() == 1);
- const Document& doc0 = found_docs[0];
- assert(doc0.id == doc_id);
- }
- // Затем убеждаемся, что поиск этого же слова, входящего в список стоп-слов,
- // возвращает пустой результат
- {
- //SearchServer server;
- //server.SetStopWords("in the"s);
- //server.AddDocument(doc_id, content, DocumentStatus::ACTUAL, ratings);
- //assert(server.FindTopDocuments("in"s).empty());
- }
- }
- void TestMinusWordsAndRatingsAndRelevance() {
- const vector<int> ratings = {1, 2, 3};
- {
- SearchServer server;
- server.AddDocument(0, "cat in the city"s, DocumentStatus::ACTUAL, ratings);
- server.AddDocument(1, "cat in the village"s, DocumentStatus::ACTUAL, ratings);
- const auto found_docs = server.FindTopDocuments("-cat"s);
- assert(found_docs.size() == 0);
- }
- {
- SearchServer server;
- server.AddDocument(0, "cat in the city"s, DocumentStatus::ACTUAL, ratings);
- server.AddDocument(1, "red cat on the bed"s, DocumentStatus::ACTUAL, ratings);
- const auto found_docs = server.FindTopDocuments("cat"s);
- assert(found_docs.size() == 2);
- const Document& doc0 = found_docs[0];
- assert(doc0.rating == 2);
- assert(doc0.relevance == 0.0); //idf = 0 tf = 0.25
- }
- }
- void TestLambda(){
- SearchServer server;
- server.AddDocument(0, "белый кот и модный ошейник"s, DocumentStatus::ACTUAL, {8, -3});
- server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, {7, 2, 7});
- server.AddDocument(2, "ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
- server.AddDocument(3, "ухоженный скворец евгений"s, DocumentStatus::BANNED, {9});
- for(const Document& document : server.FindTopDocuments("пушистый ухоженный кот"s, [](int document_id, DocumentStatus status[[maybe_unused]], int rating[[maybe_unused]]) { return document_id % 2 == 0; }))
- assert(document.id % 2 == 0);
- }
- void TestStatusFilter() {
- SearchServer server;
- server.AddDocument(0, "текст белый кот и модный ошейник"s, DocumentStatus::ACTUAL, {8, -3});
- server.AddDocument(1, "текст пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, {7, 2, 7});
- server.AddDocument(2, "текст ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
- server.AddDocument(3, "текст ухоженный скворец евгений"s, DocumentStatus::BANNED, {9});
- const auto found_docs = server.FindTopDocuments("текст"s, DocumentStatus::BANNED);
- assert(found_docs.size() == 1);
- const Document& doc0 = found_docs[0];
- assert(doc0.id == 3);
- }
- void TestRelevanceSort() {
- SearchServer server;
- server.AddDocument(0, "белый кот и модный ошейник"s, DocumentStatus::ACTUAL, {8, -3});
- server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, {7, 2, 7});
- server.AddDocument(2, "ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
- server.AddDocument(3, "ухоженный скворец евгений"s, DocumentStatus::BANNED, {9});
- const auto found_docs = server.FindTopDocuments("кот модный"s);
- const Document& doc0 = found_docs[0];
- assert(doc0.id == 0);
- const Document& doc1 = found_docs[1];
- assert(doc1.id == 1);
- }
- void TestRelevance() {
- const vector<int> ratings = {1, 2, 3};
- // проверяем вычиление релевантности
- const double e = 1e-5; //погрешность
- double standard = 0.0;
- SearchServer server;
- server.AddDocument(0, "cat in the village"s, DocumentStatus::ACTUAL, ratings);
- const auto found_docs = server.FindTopDocuments("cat"s);
- const Document& doc0 = found_docs[0];
- assert(fabs(doc0.relevance - standard) < e); //idf = 0 tf = 0.25
- server.AddDocument(1, "cat in the city"s, DocumentStatus::ACTUAL, ratings);
- server.AddDocument(2, "dog in the car"s, DocumentStatus::ACTUAL, ratings);
- const auto found_docs1 = server.FindTopDocuments("cat village"s);
- const Document& doc1 = found_docs1[0];
- standard = 0.376019;
- assert(fabs(doc1.relevance - standard) < e);
- }
- void TestMatching() {
- SearchServer server;
- vector<string> words, words0;
- DocumentStatus status;
- server.AddDocument(0, "zero one two three"s, DocumentStatus::ACTUAL, {10, 1});
- tie(words, status) = server.MatchDocument("one two three four"s, 0);
- //tuple<vector<string>, DocumentStatus> T = server.MatchDocument("one two three four"s, 0);
- //const auto [words, status] = server.MatchDocument("one two three four"s, 0);
- assert(words.size() == 3);
- assert(find(words.begin(), words.end(), "one"s) != words.end());
- assert(find(words.begin(), words.end(), "two"s) != words.end());
- assert(find(words.begin(), words.end(), "three"s) != words.end());
- //tie(words0, status) = server.MatchDocument("one -two three four"s, 0);
- //assert(words0.size() == 0);
- }
- // Функция TestSearchServer является точкой входа для запуска тестов
- void TestSearchServer() {
- TestExcludeStopWordsFromAddedDocumentContent();
- TestMinusWordsAndRatingsAndRelevance();
- TestStatusFilter();
- TestRelevance();
- TestLambda();
- TestMatching();
- TestRelevanceSort();
- }
Advertisement
Add Comment
Please, Sign In to add comment