Shuo Chen | 9a43f16 | 2014-07-01 13:43:54 +0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "storage/skiplist.hpp" |
| 21 | |
| 22 | #include "../sqlite-fixture.hpp" |
| 23 | #include "../dataset-fixtures.hpp" |
| 24 | |
| 25 | #include <boost/test/unit_test.hpp> |
| 26 | #include <boost/concept_check.hpp> |
| 27 | #include <iostream> |
| 28 | |
| 29 | namespace repo { |
| 30 | namespace tests { |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(SkipList) |
| 33 | |
| 34 | template<class Dataset> |
| 35 | class Fixture : public Dataset |
| 36 | { |
| 37 | }; |
| 38 | |
| 39 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(NdnNameSkipList, T, DatasetFixtures, Fixture<T>) |
| 40 | { |
| 41 | repo::SkipList<ndn::Name, typename T::DataSetNameCompare> skipList; |
| 42 | //Insert |
| 43 | for (typename T::DataContainer::iterator i = this->data.begin(); |
| 44 | i != this->data.end(); ++i) { |
| 45 | skipList.insert((*i)->getName()); |
| 46 | } |
| 47 | |
| 48 | //find and erase |
| 49 | for (typename T::DataContainer::iterator i = this->data.begin(); |
| 50 | i != this->data.end(); ++i) { |
| 51 | typename repo::SkipList<ndn::Name, typename T::DataSetNameCompare>::iterator findIterator = |
| 52 | skipList.find((*i)->getName()); |
| 53 | skipList.erase(findIterator); |
| 54 | } |
| 55 | |
| 56 | //@todo test lower_bound |
| 57 | } |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(IntGtSkipList) |
| 60 | { |
| 61 | typedef repo::SkipList<int, std::greater<int> > IntGtSkipList; |
| 62 | IntGtSkipList sl; |
| 63 | BOOST_CONCEPT_ASSERT((boost::BidirectionalIterator<IntGtSkipList::iterator>)); |
| 64 | |
| 65 | // initial state |
| 66 | BOOST_CHECK_EQUAL(sl.size(), 0); |
| 67 | BOOST_CHECK(sl.begin() == sl.end()); |
| 68 | BOOST_CHECK(sl.lower_bound(10) == sl.end()); |
| 69 | |
| 70 | // initial contents |
| 71 | sl.insert(10); |
| 72 | sl.insert(20); |
| 73 | sl.insert(30); |
| 74 | BOOST_CHECK_EQUAL(sl.size(), 3); |
| 75 | // contents: [30,20,10] |
| 76 | |
| 77 | // iterators |
| 78 | IntGtSkipList::iterator it1 = sl.begin(); |
| 79 | IntGtSkipList::iterator it2 = sl.end(); |
| 80 | --it2; |
| 81 | BOOST_CHECK_EQUAL(*it1, 30); |
| 82 | BOOST_CHECK_EQUAL(*it2, 10); |
| 83 | ++it1; |
| 84 | BOOST_CHECK_EQUAL(*it1, 20); |
| 85 | IntGtSkipList::iterator it3 = it1; |
| 86 | ++it1; |
| 87 | BOOST_CHECK(it2 == it1); |
| 88 | BOOST_CHECK_EQUAL(*it3, 20); |
| 89 | |
| 90 | // lower_bound |
| 91 | IntGtSkipList::iterator found = sl.lower_bound(35); |
| 92 | BOOST_CHECK(found == sl.begin()); |
| 93 | BOOST_CHECK_EQUAL(*found, 30); |
| 94 | found = sl.lower_bound(30); |
| 95 | BOOST_CHECK_EQUAL(*found, 30); |
| 96 | found = sl.lower_bound(25); |
| 97 | BOOST_CHECK_EQUAL(*found, 20); |
| 98 | found = sl.lower_bound(20); |
| 99 | BOOST_CHECK_EQUAL(*found, 20); |
| 100 | found = sl.lower_bound(15); |
| 101 | BOOST_CHECK_EQUAL(*found, 10); |
| 102 | found = sl.lower_bound(10); |
| 103 | BOOST_CHECK_EQUAL(*found, 10); |
| 104 | found = sl.lower_bound(5); |
| 105 | BOOST_CHECK(found == sl.end()); |
| 106 | |
| 107 | // insert duplicate |
| 108 | std::pair<IntGtSkipList::iterator, bool> insertRes = sl.insert(10); |
| 109 | BOOST_CHECK_EQUAL(insertRes.second, false); |
| 110 | BOOST_CHECK_EQUAL(*(insertRes.first), 10); |
| 111 | BOOST_CHECK_EQUAL(sl.size(), 3); |
| 112 | insertRes = sl.insert(20); |
| 113 | BOOST_CHECK_EQUAL(insertRes.second, false); |
| 114 | BOOST_CHECK_EQUAL(*(insertRes.first), 20); |
| 115 | BOOST_CHECK_EQUAL(sl.size(), 3); |
| 116 | insertRes = sl.insert(30); |
| 117 | BOOST_CHECK_EQUAL(insertRes.second, false); |
| 118 | BOOST_CHECK_EQUAL(*(insertRes.first), 30); |
| 119 | BOOST_CHECK_EQUAL(sl.size(), 3); |
| 120 | |
| 121 | // insert non-duplicate |
| 122 | insertRes = sl.insert(5); |
| 123 | BOOST_CHECK_EQUAL(insertRes.second, true); |
| 124 | BOOST_CHECK_EQUAL(*(insertRes.first), 5); |
| 125 | BOOST_CHECK_EQUAL(sl.size(), 4); |
| 126 | insertRes = sl.insert(35); |
| 127 | BOOST_CHECK_EQUAL(insertRes.second, true); |
| 128 | BOOST_CHECK_EQUAL(*(insertRes.first), 35); |
| 129 | BOOST_CHECK_EQUAL(sl.size(), 5); |
| 130 | // contents: [35,30,20,10,5] |
| 131 | |
| 132 | // erase |
| 133 | it1 = sl.erase(sl.begin()); |
| 134 | // contents: [30,20,10,5] |
| 135 | BOOST_CHECK_EQUAL(*it1, 30); |
| 136 | BOOST_CHECK_EQUAL(sl.size(), 4); |
| 137 | it2 = sl.end(); |
| 138 | --it2; |
| 139 | it1 = sl.erase(it2); |
| 140 | // contents: [30,20,10] |
| 141 | BOOST_CHECK(it1 == sl.end()); |
| 142 | BOOST_CHECK_EQUAL(sl.size(), 3); |
| 143 | it2 = sl.lower_bound(20); |
| 144 | it1 = sl.erase(it2); |
| 145 | // contents: [30,10] |
| 146 | BOOST_CHECK_EQUAL(*it1, 10); |
| 147 | BOOST_CHECK_EQUAL(sl.size(), 2); |
| 148 | it3 = it1; |
| 149 | --it1; |
| 150 | BOOST_CHECK(it1 == sl.begin()); |
| 151 | BOOST_CHECK_EQUAL(*it1, 30); |
| 152 | ++it3; |
| 153 | BOOST_CHECK(it3 == sl.end()); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_SUITE_END() |
| 157 | |
| 158 | } // namespace tests |
| 159 | } // namespace repo |