|
| 1 | +// Copyright (C) 2019 Joseph Artsimovich <joseph.artsimovich@gmail.com>, 4lex4 <4lex49@zoho.com> |
| 2 | +// Use of this source code is governed by the GNU GPLv3 license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +#include <ImageId.h> |
| 5 | +#include <PageId.h> |
| 6 | +#include <PageInfo.h> |
| 7 | +#include <PageSequence.h> |
| 8 | + |
| 9 | +#include <boost/test/unit_test.hpp> |
| 10 | +#include <set> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace Tests { |
| 14 | + |
| 15 | +static PageInfo makePage(const char* path, const int imageIdx) { |
| 16 | + PageInfo info; |
| 17 | + info.setId(PageId(ImageId(path, imageIdx), PageId::SINGLE_PAGE)); |
| 18 | + return info; |
| 19 | +} |
| 20 | + |
| 21 | +BOOST_AUTO_TEST_SUITE(PageSequenceTestSuite) |
| 22 | + |
| 23 | +BOOST_AUTO_TEST_CASE(select_this_page_and_following_every_other_from_first) { |
| 24 | + PageSequence seq; |
| 25 | + std::vector<PageInfo> pages; |
| 26 | + for (int i = 0; i < 75; ++i) { |
| 27 | + pages.push_back(makePage("/scan", i)); |
| 28 | + seq.append(pages.back()); |
| 29 | + } |
| 30 | + |
| 31 | + const PageId base = pages[0].id(); |
| 32 | + const std::set<PageId> result = seq.selectThisPageAndFollowingEveryOther(base); |
| 33 | + |
| 34 | + BOOST_CHECK_EQUAL(result.size(), 38u); |
| 35 | + for (int i = 0; i < 75; i += 2) { |
| 36 | + BOOST_CHECK(result.count(pages[static_cast<size_t>(i)].id()) == 1); |
| 37 | + } |
| 38 | + for (int i = 1; i < 75; i += 2) { |
| 39 | + BOOST_CHECK(result.count(pages[static_cast<size_t>(i)].id()) == 0); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +BOOST_AUTO_TEST_CASE(select_this_page_and_following_every_other_from_second) { |
| 44 | + PageSequence seq; |
| 45 | + std::vector<PageInfo> pages; |
| 46 | + for (int i = 0; i < 75; ++i) { |
| 47 | + pages.push_back(makePage("/scan", i)); |
| 48 | + seq.append(pages.back()); |
| 49 | + } |
| 50 | + |
| 51 | + const PageId base = pages[1].id(); |
| 52 | + const std::set<PageId> result = seq.selectThisPageAndFollowingEveryOther(base); |
| 53 | + |
| 54 | + BOOST_CHECK_EQUAL(result.size(), 37u); |
| 55 | + for (int i = 1; i < 75; i += 2) { |
| 56 | + BOOST_CHECK(result.count(pages[static_cast<size_t>(i)].id()) == 1); |
| 57 | + } |
| 58 | + for (int i = 0; i < 75; i += 2) { |
| 59 | + BOOST_CHECK(result.count(pages[static_cast<size_t>(i)].id()) == 0); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +BOOST_AUTO_TEST_CASE(select_this_page_and_following_every_other_partitions_75_pages) { |
| 64 | + PageSequence seq; |
| 65 | + std::vector<PageInfo> pages; |
| 66 | + for (int i = 0; i < 75; ++i) { |
| 67 | + pages.push_back(makePage("/scan", i)); |
| 68 | + seq.append(pages.back()); |
| 69 | + } |
| 70 | + |
| 71 | + const std::set<PageId> oddFromFirst = seq.selectThisPageAndFollowingEveryOther(pages[0].id()); |
| 72 | + const std::set<PageId> evenFromSecond = seq.selectThisPageAndFollowingEveryOther(pages[1].id()); |
| 73 | + |
| 74 | + for (int i = 0; i < 75; ++i) { |
| 75 | + const bool inOdd = oddFromFirst.count(pages[static_cast<size_t>(i)].id()) != 0; |
| 76 | + const bool inEven = evenFromSecond.count(pages[static_cast<size_t>(i)].id()) != 0; |
| 77 | + BOOST_CHECK(inOdd != inEven); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +BOOST_AUTO_TEST_SUITE_END() |
| 82 | + |
| 83 | +} // namespace Tests |
0 commit comments