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