Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | bb6146e | 2017-07-26 23:07:52 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/cs.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 29 | |
Junxiao Shi | 4e1b6ee | 2017-08-03 02:20:50 +0000 | [diff] [blame] | 30 | #include <cstring> |
| 31 | #include <ndn-cxx/lp/tags.hpp> |
| 32 | #include <ndn-cxx/util/sha256.hpp> |
| 33 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 34 | #define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); }); |
| 35 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 36 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 37 | namespace cs { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 38 | namespace tests { |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 39 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 40 | using namespace nfd::tests; |
| 41 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(Table) |
| 43 | BOOST_FIXTURE_TEST_SUITE(TestCs, BaseFixture) |
Junxiao Shi | 0fcb41e | 2014-01-24 10:29:43 -0700 | [diff] [blame] | 44 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 45 | class FindFixture : public UnitTestTimeFixture |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 46 | { |
| 47 | protected: |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 48 | Name |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 49 | insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 50 | { |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 51 | shared_ptr<Data> data = makeData(name); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 52 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 54 | if (modifyData != nullptr) { |
| 55 | modifyData(*data); |
| 56 | } |
| 57 | |
| 58 | data->wireEncode(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 59 | m_cs.insert(*data); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 60 | |
| 61 | return data->getFullName(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 62 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 63 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 64 | Interest& |
| 65 | startInterest(const Name& name) |
| 66 | { |
| 67 | m_interest = make_shared<Interest>(name); |
| 68 | return *m_interest; |
| 69 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 70 | |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 71 | void |
| 72 | find(const std::function<void(uint32_t)>& check) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 73 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 74 | bool hasResult = false; |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 75 | m_cs.find(*m_interest, |
| 76 | [&] (const Interest& interest, const Data& data) { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 77 | hasResult = true; |
| 78 | const Block& content = data.getContent(); |
Junxiao Shi | 4e1b6ee | 2017-08-03 02:20:50 +0000 | [diff] [blame] | 79 | uint32_t found = 0; |
| 80 | std::memcpy(&found, content.value(), sizeof(found)); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 81 | check(found); |
| 82 | }, |
| 83 | bind([&] { |
| 84 | hasResult = true; |
| 85 | check(0); |
| 86 | })); |
| 87 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame^] | 88 | // current Cs::find implementation is synchronous |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 89 | BOOST_CHECK(hasResult); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 91 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame^] | 92 | size_t |
| 93 | erase(const Name& prefix, size_t limit) |
| 94 | { |
| 95 | ndn::optional<size_t> nErased; |
| 96 | m_cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; }); |
| 97 | |
| 98 | // current Cs::erase implementation is synchronous |
| 99 | // if callback was not invoked, bad_optional_access would occur |
| 100 | return *nErased; |
| 101 | } |
| 102 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 103 | protected: |
| 104 | Cs m_cs; |
| 105 | shared_ptr<Interest> m_interest; |
| 106 | }; |
| 107 | |
| 108 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(EmptyDataName) |
| 111 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 112 | insert(1, "/"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 114 | startInterest("/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 115 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_CASE(EmptyInterestName) |
| 119 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 120 | insert(1, "/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 122 | startInterest("/"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 123 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(ExactName) |
| 127 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 128 | insert(1, "/"); |
| 129 | insert(2, "/A"); |
| 130 | insert(3, "/A/B"); |
| 131 | insert(4, "/A/C"); |
| 132 | insert(5, "/D"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 133 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 134 | startInterest("/A"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 135 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(FullName) |
| 139 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 140 | Name n1 = insert(1, "/A"); |
| 141 | Name n2 = insert(2, "/A"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 142 | |
| 143 | startInterest(n1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 144 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 145 | |
| 146 | startInterest(n2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 147 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(Leftmost) |
| 151 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 152 | insert(1, "/A"); |
| 153 | insert(2, "/B/p/1"); |
| 154 | insert(3, "/B/p/2"); |
| 155 | insert(4, "/B/q/1"); |
| 156 | insert(5, "/B/q/2"); |
| 157 | insert(6, "/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 158 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 159 | startInterest("/B"); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 160 | CHECK_CS_FIND(2); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_CASE(Rightmost) |
| 164 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 165 | insert(1, "/A"); |
| 166 | insert(2, "/B/p/1"); |
| 167 | insert(3, "/B/p/2"); |
| 168 | insert(4, "/B/q/1"); |
| 169 | insert(5, "/B/q/2"); |
| 170 | insert(6, "/C"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 171 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 172 | startInterest("/B") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 173 | .setChildSelector(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 174 | CHECK_CS_FIND(4); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 177 | BOOST_AUTO_TEST_CASE(MinSuffixComponents) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 178 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 179 | insert(1, "/"); |
| 180 | insert(2, "/A"); |
| 181 | insert(3, "/B/1"); |
| 182 | insert(4, "/C/1/2"); |
| 183 | insert(5, "/D/1/2/3"); |
| 184 | insert(6, "/E/1/2/3/4"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 185 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 186 | startInterest("/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 187 | .setMinSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 188 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 189 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 190 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 191 | .setMinSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 192 | CHECK_CS_FIND(1); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 193 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 194 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 195 | .setMinSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 196 | CHECK_CS_FIND(2); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 198 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 199 | .setMinSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 200 | CHECK_CS_FIND(3); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 201 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 202 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 203 | .setMinSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 204 | CHECK_CS_FIND(4); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 205 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 206 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 207 | .setMinSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 208 | CHECK_CS_FIND(5); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 209 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 210 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 211 | .setMinSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 212 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 213 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 214 | startInterest("/") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 215 | .setMinSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 216 | CHECK_CS_FIND(0); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_CASE(MaxSuffixComponents) |
| 220 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 221 | insert(1, "/"); |
| 222 | insert(2, "/A"); |
| 223 | insert(3, "/B/2"); |
| 224 | insert(4, "/C/2/3"); |
| 225 | insert(5, "/D/2/3/4"); |
| 226 | insert(6, "/E/2/3/4/5"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 227 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 228 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 229 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 230 | .setMaxSuffixComponents(0); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 231 | CHECK_CS_FIND(0); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 232 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 233 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 234 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 235 | .setMaxSuffixComponents(1); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 236 | CHECK_CS_FIND(1); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 237 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 238 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 239 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 240 | .setMaxSuffixComponents(2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 241 | CHECK_CS_FIND(2); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 242 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 243 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 244 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 245 | .setMaxSuffixComponents(3); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 246 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 247 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 248 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 249 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 250 | .setMaxSuffixComponents(4); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 251 | CHECK_CS_FIND(4); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 252 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 253 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 254 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 255 | .setMaxSuffixComponents(5); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 256 | CHECK_CS_FIND(5); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 257 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 258 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 259 | .setChildSelector(1) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 260 | .setMaxSuffixComponents(6); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 261 | CHECK_CS_FIND(6); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 262 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 263 | startInterest("/") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 264 | .setChildSelector(1) |
| 265 | .setMaxSuffixComponents(7); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 266 | CHECK_CS_FIND(6); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 269 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 270 | { |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 271 | insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 272 | insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(time::seconds(0)); }); |
| 273 | insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(time::seconds(1)); }); |
| 274 | insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(time::seconds(3600)); }); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 275 | |
| 276 | // lookup at exact same moment as insertion is not tested because this won't happen in reality |
| 277 | |
| 278 | this->advanceClocks(time::milliseconds(500)); // @500ms |
| 279 | startInterest("/A") |
| 280 | .setMustBeFresh(true); |
| 281 | CHECK_CS_FIND(3); |
| 282 | |
| 283 | this->advanceClocks(time::milliseconds(1500)); // @2s |
| 284 | startInterest("/A") |
| 285 | .setMustBeFresh(true); |
| 286 | CHECK_CS_FIND(4); |
| 287 | |
| 288 | this->advanceClocks(time::seconds(3500)); // @3502s |
| 289 | startInterest("/A") |
| 290 | .setMustBeFresh(true); |
| 291 | CHECK_CS_FIND(4); |
| 292 | |
| 293 | this->advanceClocks(time::seconds(3500)); // @7002s |
| 294 | startInterest("/A") |
| 295 | .setMustBeFresh(true); |
Eric Newberry | f4056d0 | 2017-05-26 17:31:53 +0000 | [diff] [blame] | 296 | CHECK_CS_FIND(0); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 299 | BOOST_AUTO_TEST_CASE(DigestOrder) |
| 300 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 301 | Name n1 = insert(1, "/A"); |
| 302 | Name n2 = insert(2, "/A"); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 303 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 304 | uint32_t expectedLeftmost = 0, expectedRightmost = 0; |
| 305 | if (n1 < n2) { |
| 306 | expectedLeftmost = 1; |
| 307 | expectedRightmost = 2; |
| 308 | } |
| 309 | else { |
| 310 | BOOST_CHECK_MESSAGE(n1 != n2, "implicit digest collision detected"); |
| 311 | expectedLeftmost = 2; |
| 312 | expectedRightmost = 1; |
| 313 | } |
| 314 | |
| 315 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 316 | .setChildSelector(0); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 317 | CHECK_CS_FIND(expectedLeftmost); |
| 318 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 319 | .setChildSelector(1); |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 320 | CHECK_CS_FIND(expectedRightmost); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | BOOST_AUTO_TEST_CASE(DigestExclude) |
| 324 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 325 | insert(1, "/A"); |
| 326 | Name n2 = insert(2, "/A"); |
| 327 | insert(3, "/A/B"); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 328 | |
Junxiao Shi | bb6146e | 2017-07-26 23:07:52 +0000 | [diff] [blame] | 329 | uint8_t digest00[ndn::util::Sha256::DIGEST_SIZE]; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 330 | std::fill_n(digest00, sizeof(digest00), 0x00); |
Junxiao Shi | bb6146e | 2017-07-26 23:07:52 +0000 | [diff] [blame] | 331 | uint8_t digestFF[ndn::util::Sha256::DIGEST_SIZE]; |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 332 | std::fill_n(digestFF, sizeof(digestFF), 0xFF); |
| 333 | |
| 334 | Exclude excludeDigest; |
| 335 | excludeDigest.excludeRange( |
| 336 | name::Component::fromImplicitSha256Digest(digest00, sizeof(digest00)), |
| 337 | name::Component::fromImplicitSha256Digest(digestFF, sizeof(digestFF))); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 338 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 339 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 340 | .setChildSelector(0) |
| 341 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 342 | CHECK_CS_FIND(3); |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 343 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 344 | startInterest("/A") |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 345 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 346 | .setExclude(excludeDigest); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 347 | CHECK_CS_FIND(3); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 348 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 349 | Exclude excludeGeneric; |
| 350 | excludeGeneric.excludeAfter(name::Component(static_cast<uint8_t*>(nullptr), 0)); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 351 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 352 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 353 | .setChildSelector(0) |
| 354 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 355 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 356 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 357 | startInterest("/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 358 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 359 | .setExclude(excludeGeneric); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 360 | find([] (uint32_t found) { BOOST_CHECK(found == 1 || found == 2); }); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 361 | |
| 362 | Exclude exclude2 = excludeGeneric; |
| 363 | exclude2.excludeOne(n2.get(-1)); |
| 364 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 365 | startInterest("/A") |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 366 | .setChildSelector(0) |
| 367 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 368 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 369 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 370 | startInterest("/A") |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 371 | .setChildSelector(1) |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 372 | .setExclude(exclude2); |
mzhang4 | eab7249 | 2015-02-25 11:16:09 -0600 | [diff] [blame] | 373 | CHECK_CS_FIND(1); |
Ilya Moiseenko | 96b80bb | 2014-04-05 20:53:27 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 376 | BOOST_AUTO_TEST_SUITE_END() // Find |
| 377 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame^] | 378 | BOOST_FIXTURE_TEST_CASE(Erase, FindFixture) |
| 379 | { |
| 380 | insert(1, "/A/B/1"); |
| 381 | insert(2, "/A/B/2"); |
| 382 | insert(3, "/A/C/3"); |
| 383 | insert(4, "/A/C/4"); |
| 384 | insert(5, "/D/5"); |
| 385 | insert(6, "/E/6"); |
| 386 | BOOST_CHECK_EQUAL(m_cs.size(), 6); |
| 387 | |
| 388 | BOOST_CHECK_EQUAL(erase("/A", 3), 3); |
| 389 | BOOST_CHECK_EQUAL(m_cs.size(), 3); |
| 390 | int nDataUnderA = 0; |
| 391 | startInterest("/A/B/1"); |
| 392 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 393 | startInterest("/A/B/2"); |
| 394 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 395 | startInterest("/A/C/3"); |
| 396 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 397 | startInterest("/A/C/4"); |
| 398 | find([&] (uint32_t found) { nDataUnderA += static_cast<int>(found > 0); }); |
| 399 | BOOST_CHECK_EQUAL(nDataUnderA, 1); |
| 400 | |
| 401 | BOOST_CHECK_EQUAL(erase("/D", 2), 1); |
| 402 | BOOST_CHECK_EQUAL(m_cs.size(), 2); |
| 403 | startInterest("/D/5"); |
| 404 | CHECK_CS_FIND(0); |
| 405 | |
| 406 | BOOST_CHECK_EQUAL(erase("/F", 2), 0); |
| 407 | BOOST_CHECK_EQUAL(m_cs.size(), 2); |
| 408 | } |
| 409 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 410 | // When the capacity limit is set to zero, Data cannot be inserted; |
| 411 | // this test case covers this situation. |
| 412 | // The behavior of non-zero capacity limit depends on the eviction policy, |
| 413 | // and is tested in policy test suites. |
| 414 | BOOST_FIXTURE_TEST_CASE(ZeroCapacity, FindFixture) |
| 415 | { |
| 416 | m_cs.setLimit(0); |
| 417 | |
| 418 | BOOST_CHECK_EQUAL(m_cs.getLimit(), 0); |
| 419 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 420 | BOOST_CHECK(m_cs.begin() == m_cs.end()); |
| 421 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 422 | insert(1, "/A"); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 423 | BOOST_CHECK_EQUAL(m_cs.size(), 0); |
| 424 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 425 | startInterest("/A"); |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 426 | CHECK_CS_FIND(0); |
| 427 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 428 | |
Junxiao Shi | 3d2049f | 2018-01-26 23:46:06 +0000 | [diff] [blame] | 429 | BOOST_FIXTURE_TEST_CASE(EnablementFlags, FindFixture) |
| 430 | { |
| 431 | BOOST_CHECK_EQUAL(m_cs.shouldAdmit(), true); |
| 432 | BOOST_CHECK_EQUAL(m_cs.shouldServe(), true); |
| 433 | |
| 434 | insert(1, "/A"); |
| 435 | m_cs.enableAdmit(false); |
| 436 | insert(2, "/B"); |
| 437 | m_cs.enableAdmit(true); |
| 438 | insert(3, "/C"); |
| 439 | |
| 440 | startInterest("/A"); |
| 441 | CHECK_CS_FIND(1); |
| 442 | startInterest("/B"); |
| 443 | CHECK_CS_FIND(0); |
| 444 | startInterest("/C"); |
| 445 | CHECK_CS_FIND(3); |
| 446 | |
| 447 | m_cs.enableServe(false); |
| 448 | startInterest("/A"); |
| 449 | CHECK_CS_FIND(0); |
| 450 | startInterest("/C"); |
| 451 | CHECK_CS_FIND(0); |
| 452 | |
| 453 | m_cs.enableServe(true); |
| 454 | startInterest("/A"); |
| 455 | CHECK_CS_FIND(1); |
| 456 | startInterest("/C"); |
| 457 | CHECK_CS_FIND(3); |
| 458 | } |
| 459 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 460 | BOOST_FIXTURE_TEST_CASE(CachePolicyNoCache, FindFixture) |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 461 | { |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 462 | insert(1, "/A", [] (Data& data) { |
| 463 | data.setTag(make_shared<lp::CachePolicyTag>( |
| 464 | lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE))); |
| 465 | }); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 466 | |
Junxiao Shi | 1fbdb54 | 2017-02-03 00:05:53 +0000 | [diff] [blame] | 467 | startInterest("/A"); |
| 468 | CHECK_CS_FIND(0); |
Junxiao Shi | 35b16b1 | 2015-02-16 22:14:57 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Junxiao Shi | fc20696 | 2015-01-16 11:12:22 -0700 | [diff] [blame] | 471 | BOOST_AUTO_TEST_CASE(Enumeration) |
| 472 | { |
| 473 | Cs cs; |
| 474 | |
| 475 | Name nameA("/A"); |
| 476 | Name nameAB("/A/B"); |
| 477 | Name nameABC("/A/B/C"); |
| 478 | Name nameD("/D"); |
| 479 | |
| 480 | BOOST_CHECK_EQUAL(cs.size(), 0); |
| 481 | BOOST_CHECK(cs.begin() == cs.end()); |
| 482 | |
| 483 | cs.insert(*makeData(nameABC)); |
| 484 | BOOST_CHECK_EQUAL(cs.size(), 1); |
| 485 | BOOST_CHECK(cs.begin() != cs.end()); |
| 486 | BOOST_CHECK(cs.begin()->getName() == nameABC); |
| 487 | BOOST_CHECK((*cs.begin()).getName() == nameABC); |
| 488 | |
| 489 | auto i = cs.begin(); |
| 490 | auto j = cs.begin(); |
| 491 | BOOST_CHECK(++i == cs.end()); |
| 492 | BOOST_CHECK(j++ == cs.begin()); |
| 493 | BOOST_CHECK(j == cs.end()); |
| 494 | |
| 495 | cs.insert(*makeData(nameA)); |
| 496 | cs.insert(*makeData(nameAB)); |
| 497 | cs.insert(*makeData(nameD)); |
| 498 | |
| 499 | std::set<Name> expected = {nameA, nameAB, nameABC, nameD}; |
| 500 | std::set<Name> actual; |
| 501 | for (const auto& csEntry : cs) { |
| 502 | actual.insert(csEntry.getName()); |
| 503 | } |
| 504 | BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 505 | } |
| 506 | |
Minsheng Zhang | ffe8bbb | 2016-03-10 13:40:37 -0700 | [diff] [blame] | 507 | BOOST_AUTO_TEST_SUITE_END() // TestCs |
| 508 | BOOST_AUTO_TEST_SUITE_END() // Table |
Alexander Afanasyev | b927a3a | 2014-01-24 10:41:47 -0800 | [diff] [blame] | 509 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 510 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 511 | } // namespace cs |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 512 | } // namespace nfd |