Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 10b24be | 2017-07-12 23:23:46 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/ims/in-memory-storage.hpp" |
| 23 | #include "ndn-cxx/ims/in-memory-storage-fifo.hpp" |
| 24 | #include "ndn-cxx/ims/in-memory-storage-lfu.hpp" |
| 25 | #include "ndn-cxx/ims/in-memory-storage-lru.hpp" |
| 26 | #include "ndn-cxx/ims/in-memory-storage-persistent.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "ndn-cxx/util/sha256.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
| 30 | #include "tests/unit/io-fixture.hpp" |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 32 | #include <boost/mpl/vector.hpp> |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 35 | namespace tests { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 37 | using namespace ndn::tests; |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Ims) |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(TestInMemoryStorage) |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 42 | using InMemoryStorages = boost::mpl::vector<InMemoryStoragePersistent, |
| 43 | InMemoryStorageFifo, |
| 44 | InMemoryStorageLfu, |
| 45 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 46 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 47 | BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion, T, InMemoryStorages) |
| 48 | { |
| 49 | T ims; |
| 50 | |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 51 | ims.insert(*makeData("/a")); |
| 52 | ims.insert(*makeData("/b")); |
| 53 | ims.insert(*makeData("/c")); |
| 54 | ims.insert(*makeData("/d")); |
| 55 | |
| 56 | BOOST_CHECK_EQUAL(ims.size(), 4); |
| 57 | } |
| 58 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 59 | BOOST_AUTO_TEST_CASE_TEMPLATE(Insertion2, T, InMemoryStorages) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 60 | { |
| 61 | T ims; |
| 62 | |
| 63 | Name name("/a"); |
| 64 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 65 | const uint8_t content1[] = {1, 2, 3, 4}; |
| 66 | auto data1 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 67 | data1->setFreshnessPeriod(99999_ms); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 68 | data1->setContent(content1); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 69 | signData(data1); |
| 70 | ims.insert(*data1); |
| 71 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 72 | const uint8_t content2[] = {5, 6, 7, 8}; |
| 73 | auto data2 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 74 | data2->setFreshnessPeriod(99999_ms); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 75 | data2->setContent(content2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 76 | signData(data2); |
| 77 | ims.insert(*data2); |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion, T, InMemoryStorages) |
| 83 | { |
| 84 | T ims; |
| 85 | |
| 86 | shared_ptr<Data> data0 = makeData("/insert/smth"); |
| 87 | ims.insert(*data0); |
| 88 | |
| 89 | shared_ptr<Data> data = makeData("/insert/duplicate"); |
| 90 | ims.insert(*data); |
| 91 | |
| 92 | ims.insert(*data); |
| 93 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE_TEMPLATE(DuplicateInsertion2, T, InMemoryStorages) |
| 97 | { |
| 98 | T ims; |
| 99 | |
| 100 | shared_ptr<Data> data = makeData("/insert/duplicate"); |
| 101 | ims.insert(*data); |
| 102 | |
| 103 | ims.insert(*data); |
| 104 | BOOST_CHECK_EQUAL(ims.size(), 1); |
| 105 | |
| 106 | shared_ptr<Data> data2 = makeData("/insert/original"); |
| 107 | ims.insert(*data2); |
| 108 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFind, T, InMemoryStorages) |
| 112 | { |
| 113 | T ims; |
| 114 | |
| 115 | Name name("/insert/and/find"); |
| 116 | |
| 117 | shared_ptr<Data> data = makeData(name); |
| 118 | ims.insert(*data); |
| 119 | |
| 120 | shared_ptr<Interest> interest = makeInterest(name); |
| 121 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 122 | auto found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 123 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(data->getName(), found->getName()); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFind, T, InMemoryStorages) |
| 128 | { |
| 129 | T ims; |
| 130 | |
| 131 | Name name("/insert/and/find"); |
| 132 | shared_ptr<Data> data = makeData(name); |
| 133 | ims.insert(*data); |
| 134 | |
| 135 | Name name2("/not/find"); |
| 136 | shared_ptr<Interest> interest = makeInterest(name2); |
| 137 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 138 | auto found = ims.find(*interest); |
| 139 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByName, T, InMemoryStorages) |
| 143 | { |
| 144 | T ims; |
| 145 | |
| 146 | Name name("/insert/and/find"); |
| 147 | |
| 148 | shared_ptr<Data> data = makeData(name); |
| 149 | ims.insert(*data); |
| 150 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 151 | auto found = ims.find(name); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 152 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(data->getName(), found->getName()); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndFindByFullName, T, InMemoryStorages) |
| 157 | { |
| 158 | T ims; |
| 159 | |
| 160 | Name name("/insert/and/find"); |
| 161 | |
| 162 | shared_ptr<Data> data = makeData(name); |
| 163 | ims.insert(*data); |
| 164 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 165 | auto found = ims.find(data->getFullName()); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 166 | BOOST_CHECK(found != nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(data->getFullName(), found->getFullName()); |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByName, T, InMemoryStorages) |
| 171 | { |
| 172 | T ims; |
| 173 | |
| 174 | Name name("/insert/and/find"); |
| 175 | shared_ptr<Data> data = makeData(name); |
| 176 | ims.insert(*data); |
| 177 | |
| 178 | Name name2("/not/find"); |
| 179 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 180 | auto found = ims.find(name2); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 181 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndNotFindByFullName, T, InMemoryStorages) |
| 185 | { |
| 186 | T ims; |
| 187 | |
| 188 | Name name("/a"); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 189 | const uint8_t content1[] = {1, 2, 3, 4}; |
| 190 | auto data1 = makeData(name); |
| 191 | data1->setContent(content1); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 192 | signData(data1); |
| 193 | ims.insert(*data1); |
| 194 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 195 | const uint8_t content2[] = {5, 6, 7, 8}; |
| 196 | auto data2 = makeData(name); |
| 197 | data2->setContent(content2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 198 | signData(data2); |
| 199 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 200 | auto found = ims.find(data2->getFullName()); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 201 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByName, T, InMemoryStorages) |
| 205 | { |
| 206 | T ims; |
| 207 | |
| 208 | Name name("/insertandremovebyname"); |
| 209 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 210 | const uint8_t content1[] = {1, 2, 3, 4}; |
| 211 | auto data1 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 212 | data1->setFreshnessPeriod(99999_ms); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 213 | data1->setContent(content1); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 214 | signData(data1); |
| 215 | ims.insert(*data1); |
| 216 | |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 217 | const uint8_t content2[] = {5, 6, 7, 8}; |
| 218 | auto data2 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 219 | data2->setFreshnessPeriod(99999_ms); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 220 | data2->setContent(content2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 221 | signData(data2); |
| 222 | ims.insert(*data2); |
| 223 | |
| 224 | shared_ptr<Data> data3 = makeData("/insertandremovebyname/1"); |
| 225 | ims.insert(*data3); |
| 226 | |
| 227 | shared_ptr<Data> data4 = makeData("/insertandremovebyname/2"); |
| 228 | ims.insert(*data4); |
| 229 | |
| 230 | BOOST_CHECK_EQUAL(ims.size(), 4); |
| 231 | |
| 232 | ims.erase(data1->getFullName(), false); |
| 233 | BOOST_CHECK_EQUAL(ims.size(), 3); |
| 234 | } |
| 235 | |
| 236 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEraseByPrefix, T, InMemoryStorages) |
| 237 | { |
| 238 | T ims; |
| 239 | |
| 240 | shared_ptr<Data> data = makeData("/a"); |
| 241 | ims.insert(*data); |
| 242 | |
| 243 | shared_ptr<Data> data2 = makeData("/b"); |
| 244 | ims.insert(*data2); |
| 245 | |
| 246 | shared_ptr<Data> data3 = makeData("/c"); |
| 247 | ims.insert(*data3); |
| 248 | |
| 249 | shared_ptr<Data> data4 = makeData("/d"); |
| 250 | ims.insert(*data4); |
| 251 | |
| 252 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 253 | ims.insert(*data5); |
| 254 | |
| 255 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 256 | ims.insert(*data6); |
| 257 | |
| 258 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 259 | ims.insert(*data7); |
| 260 | |
| 261 | BOOST_CHECK_EQUAL(ims.size(), 7); |
| 262 | |
| 263 | Name name("/c"); |
| 264 | ims.erase(name); |
| 265 | BOOST_CHECK_EQUAL(ims.size(), 3); |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 266 | BOOST_CHECK_EQUAL(ims.getCapacity(), 16); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_CASE_TEMPLATE(DigestCalculation, T, InMemoryStorages) |
| 270 | { |
| 271 | shared_ptr<Data> data = makeData("/digest/compute"); |
| 272 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 273 | auto digest1 = util::Sha256::computeDigest(data->wireEncode()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 274 | BOOST_CHECK_EQUAL(digest1->size(), 32); |
| 275 | |
Alexander Afanasyev | 3e2c3a0 | 2017-01-19 13:55:56 -0800 | [diff] [blame] | 276 | InMemoryStorageEntry entry; |
| 277 | entry.setData(*data); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 278 | BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(), |
Alexander Afanasyev | 3e2c3a0 | 2017-01-19 13:55:56 -0800 | [diff] [blame] | 279 | entry.getFullName()[-1].value_begin(), |
| 280 | entry.getFullName()[-1].value_end()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages) |
| 284 | { |
| 285 | T ims; |
| 286 | |
| 287 | BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>)); |
| 288 | |
| 289 | for (int i = 0; i < 10; i++) { |
| 290 | std::ostringstream convert; |
| 291 | convert << i; |
| 292 | Name name("/" + convert.str()); |
| 293 | shared_ptr<Data> data = makeData(name); |
| 294 | ims.insert(*data); |
| 295 | } |
| 296 | |
| 297 | InMemoryStorage::const_iterator it = ims.begin(); |
| 298 | InMemoryStorage::const_iterator tmp1 = it; |
| 299 | BOOST_REQUIRE(tmp1 == it); |
| 300 | InMemoryStorage::const_iterator tmp2 = tmp1++; |
| 301 | BOOST_REQUIRE(tmp2 != tmp1); |
| 302 | tmp2 = ++tmp1; |
| 303 | BOOST_REQUIRE(tmp2 == tmp1); |
| 304 | |
| 305 | int i = 0; |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 306 | for (; it != ims.end(); it++) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 307 | std::ostringstream convert; |
| 308 | convert << i; |
| 309 | Name name("/" + convert.str()); |
| 310 | BOOST_CHECK_EQUAL(it->getName(), name); |
| 311 | BOOST_CHECK_EQUAL((*it).getName(), name); |
| 312 | i++; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages) |
| 317 | { |
| 318 | T ims; |
| 319 | |
| 320 | shared_ptr<Data> data = makeData("/a"); |
| 321 | ims.insert(*data); |
| 322 | |
| 323 | shared_ptr<Data> data2 = makeData("/b"); |
| 324 | ims.insert(*data2); |
| 325 | |
| 326 | shared_ptr<Data> data3 = makeData("/c"); |
| 327 | ims.insert(*data3); |
| 328 | |
| 329 | shared_ptr<Data> data4 = makeData("/d"); |
| 330 | ims.insert(*data4); |
| 331 | |
| 332 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 333 | ims.insert(*data5); |
| 334 | |
| 335 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 336 | ims.insert(*data6); |
| 337 | |
| 338 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 339 | ims.insert(*data7); |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 340 | |
| 341 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 342 | BOOST_CHECK(true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages) |
| 346 | { |
| 347 | T ims; |
| 348 | |
| 349 | shared_ptr<Data> data = makeData("/a"); |
| 350 | ims.insert(*data); |
| 351 | |
| 352 | shared_ptr<Data> data2 = makeData("/b"); |
| 353 | ims.insert(*data2); |
| 354 | |
| 355 | shared_ptr<Data> data3 = makeData("/c"); |
| 356 | ims.insert(*data3); |
| 357 | |
| 358 | shared_ptr<Data> data4 = makeData("/d"); |
| 359 | ims.insert(*data4); |
| 360 | |
| 361 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 362 | ims.insert(*data5); |
| 363 | |
| 364 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 365 | ims.insert(*data6); |
| 366 | |
| 367 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 368 | ims.insert(*data7); |
| 369 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 370 | auto digest1 = util::Sha256::computeDigest(data->wireEncode()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 371 | |
| 372 | Name name("/a"); |
| 373 | ims.erase(name); |
| 374 | BOOST_CHECK_EQUAL(ims.size(), 6); |
| 375 | } |
| 376 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 377 | using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo, |
| 378 | InMemoryStorageLfu, |
| 379 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 380 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 381 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 382 | { |
| 383 | T ims; |
| 384 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 385 | ims.setCapacity(18); |
| 386 | for (int i = 1; i < 19; ++i) { |
| 387 | ims.insert(*makeData(to_string(i))); |
| 388 | } |
| 389 | BOOST_CHECK_EQUAL(ims.size(), 18); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 390 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 391 | ims.setCapacity(16); |
| 392 | BOOST_CHECK_EQUAL(ims.size(), 16); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited) |
| 396 | { |
| 397 | T ims(10000); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 398 | BOOST_CHECK_EQUAL(ims.getLimit(), 10000); |
| 399 | |
| 400 | T ims2(4); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 401 | BOOST_CHECK_EQUAL(ims2.getLimit(), 4); |
| 402 | } |
| 403 | |
| 404 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited) |
| 405 | { |
| 406 | T ims(40); |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 407 | size_t initialCapacity = ims.getCapacity(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 408 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 409 | for (size_t i = 0; i < initialCapacity + 1; i++) { |
| 410 | shared_ptr<Data> data = makeData(to_string(i)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 411 | data->setFreshnessPeriod(5000_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 412 | signData(data); |
| 413 | ims.insert(*data); |
| 414 | } |
| 415 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 416 | BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1); |
| 417 | BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited) |
| 421 | { |
| 422 | T ims(2); |
| 423 | |
| 424 | Name name("/insert/1"); |
| 425 | shared_ptr<Data> data = makeData(name); |
| 426 | ims.insert(*data); |
| 427 | |
| 428 | Name name2("/insert/2"); |
| 429 | shared_ptr<Data> data2 = makeData(name2); |
| 430 | ims.insert(*data2); |
| 431 | |
| 432 | Name name3("/insert/3"); |
| 433 | shared_ptr<Data> data3 = makeData(name3); |
| 434 | ims.insert(*data3); |
| 435 | |
| 436 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 437 | |
| 438 | shared_ptr<Interest> interest = makeInterest(name); |
| 439 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 440 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 443 | // Find function is implemented at the base case, so it's sufficient to test for one derived class. |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 444 | class FindFixture : public IoFixture |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 445 | { |
| 446 | protected: |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 447 | Name |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 448 | insert(uint32_t id, const Name& name, |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 449 | const std::function<void(Data&)>& modifyData = nullptr, |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 450 | const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 451 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 452 | auto data = makeData(name); |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 453 | data->setContent(make_span(reinterpret_cast<const uint8_t*>(&id), sizeof(id))); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 454 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 455 | if (modifyData != nullptr) { |
| 456 | modifyData(*data); |
| 457 | } |
| 458 | |
| 459 | data->wireEncode(); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 460 | m_ims.insert(*data, freshWindow); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 461 | |
| 462 | return data->getFullName(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | Interest& |
| 466 | startInterest(const Name& name) |
| 467 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 468 | m_interest = makeInterest(name, false); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 469 | return *m_interest; |
| 470 | } |
| 471 | |
| 472 | uint32_t |
| 473 | find() |
| 474 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 475 | auto found = m_ims.find(*m_interest); |
| 476 | if (found == nullptr) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | const Block& content = found->getContent(); |
| 480 | if (content.value_size() != sizeof(uint32_t)) { |
| 481 | return 0; |
| 482 | } |
Junxiao Shi | baab732 | 2017-07-27 01:22:49 +0000 | [diff] [blame] | 483 | uint32_t id = 0; |
| 484 | std::memcpy(&id, content.value(), sizeof(id)); |
| 485 | return id; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | protected: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 489 | InMemoryStoragePersistent m_ims{m_io}; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 490 | shared_ptr<Interest> m_interest; |
| 491 | }; |
| 492 | |
| 493 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 494 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 495 | BOOST_AUTO_TEST_CASE(ExactName) |
| 496 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 497 | insert(1, "/"); |
| 498 | insert(2, "/A"); |
| 499 | insert(3, "/A/B"); |
| 500 | insert(4, "/A/C"); |
| 501 | insert(5, "/D"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 502 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 503 | startInterest("/A"); |
| 504 | BOOST_CHECK_EQUAL(find(), 2); |
| 505 | } |
| 506 | |
| 507 | BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix) |
| 508 | { |
| 509 | insert(1, "/"); |
| 510 | insert(2, "/A"); |
| 511 | insert(3, "/A/B"); |
| 512 | insert(4, "/A/C"); |
| 513 | insert(5, "/D"); |
| 514 | |
| 515 | startInterest("/A") |
| 516 | .setCanBePrefix(true); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 517 | BOOST_CHECK_EQUAL(find(), 2); |
| 518 | } |
| 519 | |
| 520 | BOOST_AUTO_TEST_CASE(FullName) |
| 521 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 522 | Name n1 = insert(1, "/A"); |
| 523 | Name n2 = insert(2, "/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 524 | |
| 525 | startInterest(n1); |
| 526 | BOOST_CHECK_EQUAL(find(), 1); |
| 527 | |
| 528 | startInterest(n2); |
| 529 | BOOST_CHECK_EQUAL(find(), 2); |
| 530 | } |
| 531 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 532 | BOOST_AUTO_TEST_CASE(FullName_EmptyDataName) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 533 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 534 | Name n1 = insert(1, "/"); |
| 535 | Name n2 = insert(2, "/"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 536 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 537 | startInterest(n1); |
| 538 | BOOST_CHECK_EQUAL(find(), 1); |
| 539 | |
| 540 | startInterest(n2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 541 | BOOST_CHECK_EQUAL(find(), 2); |
| 542 | } |
| 543 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 544 | BOOST_AUTO_TEST_CASE(PrefixName) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 545 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 546 | insert(1, "/A"); |
| 547 | insert(2, "/B/p/1"); |
| 548 | insert(3, "/B/p/2"); |
| 549 | insert(4, "/B/q/1"); |
| 550 | insert(5, "/B/q/2"); |
| 551 | insert(6, "/C"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 552 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 553 | startInterest("/B") |
| 554 | .setCanBePrefix(true); |
| 555 | BOOST_CHECK_EQUAL(find(), 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 558 | BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 559 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 560 | insert(1, "/B/p/1"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 561 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 562 | startInterest("/B"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 563 | BOOST_CHECK_EQUAL(find(), 0); |
| 564 | } |
| 565 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 566 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 567 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 568 | insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms |
| 569 | insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); }); |
| 570 | insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s); |
| 571 | insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 572 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 573 | // lookup at exact same moment as insertion is not tested because this won't happen in reality |
| 574 | |
| 575 | advanceClocks(500_ms); // @500ms |
| 576 | startInterest("/A") |
| 577 | .setCanBePrefix(true) |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 578 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 579 | BOOST_CHECK_EQUAL(find(), 3); |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 580 | |
| 581 | advanceClocks(1500_ms); // @2s |
| 582 | startInterest("/A") |
| 583 | .setCanBePrefix(true) |
| 584 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 585 | BOOST_CHECK_EQUAL(find(), 4); |
| 586 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 587 | advanceClocks(3500_s); // @3502s |
| 588 | startInterest("/A") |
| 589 | .setCanBePrefix(true) |
| 590 | .setMustBeFresh(true); |
| 591 | BOOST_CHECK_EQUAL(find(), 4); |
| 592 | |
| 593 | advanceClocks(3500_s); // @7002s |
| 594 | startInterest("/A") |
| 595 | .setCanBePrefix(true) |
| 596 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 597 | BOOST_CHECK_EQUAL(find(), 0); |
| 598 | } |
| 599 | |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 600 | BOOST_AUTO_TEST_SUITE_END() // Find |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 601 | BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 602 | BOOST_AUTO_TEST_SUITE_END() // Ims |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 603 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 604 | } // namespace tests |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 605 | } // namespace ndn |