Advertisement
Argentum_02

Paginator

Aug 3rd, 2020 (edited)
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include "test_runner.h"
  2. #include <typeinfo>
  3. #include <numeric>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <exception>
  8. using namespace std;
  9.  
  10. // Реализуйте шаблон класса Paginator
  11.  
  12. template <typename it>
  13. struct itrange{
  14.     it first,last;
  15.     size_t size() const{
  16.         return last - first;
  17.     }
  18.     it begin() const{
  19.         return first;
  20.     }
  21.  
  22.     it end() const{
  23.         return last;
  24.     }
  25. };
  26.  
  27. template <typename Iterator>
  28. class Paginator {
  29. public:
  30.     Paginator(Iterator begin, Iterator end, size_t page_size){
  31.         pag_begin = begin;
  32.         pag_end = end;
  33.         this->page_size = page_size;
  34.         Iterator head = begin;
  35.         Iterator tail = begin + page_size;
  36.             while(end > tail){
  37.                 page.push_back({head,tail});
  38.                 head = tail;
  39.                 tail +=page_size;
  40.             }
  41.                 page.push_back({head,end});
  42.     }
  43.  
  44.     auto begin(){
  45.         return page.begin();
  46.     }
  47.      auto end() {
  48.         return page.end();
  49.     }
  50.  
  51.     size_t size() const{
  52.         return page.size();
  53.     }
  54.  
  55. private:
  56.  
  57.     Iterator pag_begin,pag_end;
  58.     vector<itrange<Iterator>> page;
  59.     size_t page_size;
  60. };
  61.  
  62. template <typename C>
  63. auto Paginate(C& c, size_t page_size){
  64.  
  65.     return Paginator<decltype(c.begin())>(c.begin(), c.end(), page_size);
  66.   // Реализуйте этот шаблон функции
  67. }
  68.  
  69. void TestPageCounts() {
  70.   vector<int> v(15);
  71.  
  72.   ASSERT_EQUAL(Paginate(v, 1).size(), v.size());
  73.   ASSERT_EQUAL(Paginate(v, 3).size(), 5u);
  74.   ASSERT_EQUAL(Paginate(v, 5).size(), 3u);
  75.   ASSERT_EQUAL(Paginate(v, 4).size(), 4u);
  76.   ASSERT_EQUAL(Paginate(v, 15).size(), 1u);
  77.   ASSERT_EQUAL(Paginate(v, 150).size(), 1u);
  78.   ASSERT_EQUAL(Paginate(v, 14).size(), 2u);
  79. }
  80.  
  81. void TestLooping() {
  82.   vector<int> v(15);
  83.   iota(begin(v), end(v), 1);
  84.  
  85.   Paginator<vector<int>::iterator> paginate_v(v.begin(), v.end(), 6);
  86.   ostringstream os;
  87.   for (const auto& page : paginate_v) {
  88.     for (int x : page) {
  89.       os << x << ' ';
  90.     }
  91.     os << '\n';
  92.   }
  93.  
  94.   ASSERT_EQUAL(os.str(), "1 2 3 4 5 6 \n7 8 9 10 11 12 \n13 14 15 \n");
  95. }
  96.  
  97. void TestModification() {
  98.   vector<string> vs = {"one", "two", "three", "four", "five"};
  99.   for (auto page : Paginate(vs, 2)) {
  100.     for (auto& word : page) {
  101.       word[0] = toupper(word[0]);
  102.     }
  103.   }
  104.  
  105.   const vector<string> expected = {"One", "Two", "Three", "Four", "Five"};
  106.   ASSERT_EQUAL(vs, expected);
  107. }
  108.  
  109. void TestPageSizes() {
  110.   string letters(26, ' ');
  111.  
  112.   Paginator <string::iterator>letters_pagination(letters.begin(), letters.end(), 11);
  113.   vector<size_t> page_sizes;
  114.   for (const auto& page : letters_pagination) {
  115.     page_sizes.push_back(page.size());
  116.   }
  117.  
  118.   const vector<size_t> expected = {11, 11, 4};
  119.   ASSERT_EQUAL(page_sizes, expected);
  120. }
  121.  
  122. void TestConstContainer() {
  123.   const string letters = "abcdefghijklmnopqrstuvwxyz";
  124.  
  125.   vector<string> pages;
  126.   for (const auto& page : Paginate(letters, 10)) {
  127.     pages.push_back(string(page.begin(), page.end()));
  128.   }
  129.  
  130.   const vector<string> expected = {"abcdefghij", "klmnopqrst", "uvwxyz"};
  131.   ASSERT_EQUAL(pages, expected);
  132. }
  133.  
  134. void TestPagePagination() {
  135.   vector<int> v(22);
  136.   iota(begin(v), end(v), 1);
  137.  
  138.   vector<vector<int>> lines;
  139.   for (const auto& split_by_9 : Paginate(v, 9)) {
  140.     for (const auto& split_by_4 : Paginate(split_by_9, 4)) {
  141.       lines.push_back({});
  142.       for (int item : split_by_4) {
  143.         lines.back().push_back(item);
  144.       }
  145.     }
  146.   }
  147.  
  148.   const vector<vector<int>> expected = {
  149.       {1, 2, 3, 4},
  150.       {5, 6, 7, 8},
  151.       {9},
  152.       {10, 11, 12, 13},
  153.       {14, 15, 16, 17},
  154.       {18},
  155.       {19, 20, 21, 22}
  156.   };
  157.   ASSERT_EQUAL(lines, expected);
  158. }
  159.  
  160. int main() {
  161.   TestRunner tr;
  162.   RUN_TEST(tr, TestPageCounts);
  163.   RUN_TEST(tr, TestLooping);
  164.   RUN_TEST(tr, TestModification);
  165.   RUN_TEST(tr, TestPageSizes);
  166.   RUN_TEST(tr, TestConstContainer);
  167.   RUN_TEST(tr, TestPagePagination);
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement