shizaero

Unit-test

Feb 3rd, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.35 KB | None | 0 0
  1. void TestExcludeStopWordsFromAddedDocumentContent() {
  2.     const int doc_id = 42;
  3.     const string content = "cat in the city"s;
  4.     const vector<int> ratings = {1, 2, 3};
  5.     // Сначала убеждаемся, что поиск слова, не входящего в список стоп-слов,
  6.     // находит нужный документ
  7.     {
  8.         SearchServer server;
  9.         server.AddDocument(doc_id, content, DocumentStatus::ACTUAL, ratings);
  10.         const auto found_docs = server.FindTopDocuments("in"s);
  11.         assert(found_docs.size() == 1);
  12.         const Document& doc0 = found_docs[0];
  13.         assert(doc0.id == doc_id);
  14.     }
  15.  
  16.     // Затем убеждаемся, что поиск этого же слова, входящего в список стоп-слов,
  17.     // возвращает пустой результат
  18.     {
  19.         //SearchServer server;
  20.         //server.SetStopWords("in the"s);
  21.         //server.AddDocument(doc_id, content, DocumentStatus::ACTUAL, ratings);
  22.         //assert(server.FindTopDocuments("in"s).empty());
  23.     }
  24. }
  25.  
  26. void TestMinusWordsAndRatingsAndRelevance() {
  27.     const vector<int> ratings = {1, 2, 3};
  28.     {
  29.         SearchServer server;
  30.         server.AddDocument(0, "cat in the city"s, DocumentStatus::ACTUAL, ratings);
  31.         server.AddDocument(1, "cat in the village"s, DocumentStatus::ACTUAL, ratings);
  32.         const auto found_docs = server.FindTopDocuments("-cat"s);
  33.         assert(found_docs.size() == 0);
  34.     }
  35.  
  36.     {
  37.         SearchServer server;
  38.         server.AddDocument(0, "cat in the city"s, DocumentStatus::ACTUAL, ratings);
  39.         server.AddDocument(1, "red cat on the bed"s, DocumentStatus::ACTUAL, ratings);
  40.         const auto found_docs = server.FindTopDocuments("cat"s);
  41.         assert(found_docs.size() == 2);
  42.         const Document& doc0 = found_docs[0];
  43.         assert(doc0.rating == 2);
  44.         assert(doc0.relevance == 0.0); //idf = 0 tf = 0.25
  45.     }
  46. }
  47.  
  48. void TestLambda(){
  49.     SearchServer server;
  50.     server.AddDocument(0, "белый кот и модный ошейник"s,        DocumentStatus::ACTUAL, {8, -3});
  51.     server.AddDocument(1, "пушистый кот пушистый хвост"s,       DocumentStatus::ACTUAL, {7, 2, 7});
  52.     server.AddDocument(2, "ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
  53.     server.AddDocument(3, "ухоженный скворец евгений"s,         DocumentStatus::BANNED, {9});
  54.  
  55.     for(const Document& document : server.FindTopDocuments("пушистый ухоженный кот"s, [](int document_id, DocumentStatus status[[maybe_unused]], int rating[[maybe_unused]]) { return document_id % 2 == 0; }))
  56.     assert(document.id % 2 == 0);
  57. }
  58.  
  59. void TestStatusFilter() {
  60.     SearchServer server;
  61.     server.AddDocument(0, "текст белый кот и модный ошейник"s,        DocumentStatus::ACTUAL, {8, -3});
  62.     server.AddDocument(1, "текст пушистый кот пушистый хвост"s,       DocumentStatus::ACTUAL, {7, 2, 7});
  63.     server.AddDocument(2, "текст ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
  64.     server.AddDocument(3, "текст ухоженный скворец евгений"s,         DocumentStatus::BANNED, {9});
  65.  
  66.     const auto found_docs = server.FindTopDocuments("текст"s, DocumentStatus::BANNED);
  67.     assert(found_docs.size() == 1);
  68.     const Document& doc0 = found_docs[0];
  69.     assert(doc0.id == 3);
  70. }
  71.  
  72. void TestRelevanceSort() {
  73.     SearchServer server;
  74.     server.AddDocument(0, "белый кот и модный ошейник"s,        DocumentStatus::ACTUAL, {8, -3});
  75.     server.AddDocument(1, "пушистый кот пушистый хвост"s,       DocumentStatus::ACTUAL, {7, 2, 7});
  76.     server.AddDocument(2, "ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
  77.     server.AddDocument(3, "ухоженный скворец евгений"s,         DocumentStatus::BANNED, {9});
  78.  
  79.     const auto found_docs = server.FindTopDocuments("кот модный"s);
  80.     const Document& doc0 = found_docs[0];
  81.     assert(doc0.id == 0);
  82.     const Document& doc1 = found_docs[1];
  83.     assert(doc1.id == 1);
  84. }
  85.  
  86. void TestRelevance() {
  87.     const vector<int> ratings = {1, 2, 3};
  88.     // проверяем вычиление релевантности
  89.         const double e = 1e-5; //погрешность
  90.         double standard = 0.0;
  91.         SearchServer server;
  92.         server.AddDocument(0, "cat in the village"s, DocumentStatus::ACTUAL, ratings);
  93.         const auto found_docs = server.FindTopDocuments("cat"s);
  94.         const Document& doc0 = found_docs[0];
  95.         assert(fabs(doc0.relevance - standard) < e); //idf = 0 tf = 0.25
  96.         server.AddDocument(1, "cat in the city"s, DocumentStatus::ACTUAL, ratings);
  97.         server.AddDocument(2, "dog in the car"s, DocumentStatus::ACTUAL, ratings);
  98.         const auto found_docs1 = server.FindTopDocuments("cat village"s);
  99.         const Document& doc1 = found_docs1[0];
  100.         standard = 0.376019;
  101.         assert(fabs(doc1.relevance - standard) < e);
  102. }
  103.  
  104. void TestMatching() {
  105.     SearchServer server;
  106.     vector<string> words, words0;
  107.     DocumentStatus status;
  108.     server.AddDocument(0, "zero one two three"s,        DocumentStatus::ACTUAL, {10, 1});
  109.     tie(words, status) = server.MatchDocument("one two three four"s, 0);
  110.     //tuple<vector<string>, DocumentStatus> T = server.MatchDocument("one two three four"s, 0);
  111.     //const auto [words, status] = server.MatchDocument("one two three four"s, 0);
  112.     assert(words.size() == 3);
  113.     assert(find(words.begin(), words.end(), "one"s) != words.end());
  114.     assert(find(words.begin(), words.end(), "two"s) != words.end());
  115.     assert(find(words.begin(), words.end(), "three"s) != words.end());
  116.     //tie(words0, status) = server.MatchDocument("one -two three four"s, 0);
  117.     //assert(words0.size() == 0);
  118. }
  119.  
  120. // Функция TestSearchServer является точкой входа для запуска тестов
  121. void TestSearchServer() {
  122.     TestExcludeStopWordsFromAddedDocumentContent();
  123.     TestMinusWordsAndRatingsAndRelevance();
  124.     TestStatusFilter();
  125.     TestRelevance();
  126.     TestLambda();
  127.     TestMatching();
  128.     TestRelevanceSort();
  129. }
Advertisement
Add Comment
Please, Sign In to add comment