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 | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
| 65 | uint32_t content1 = 1; |
| 66 | shared_ptr<Data> data1 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 67 | data1->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 68 | data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1)); |
| 69 | signData(data1); |
| 70 | ims.insert(*data1); |
| 71 | |
| 72 | uint32_t content2 = 2; |
| 73 | shared_ptr<Data> data2 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 74 | data2->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 75 | data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2)); |
| 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"); |
| 189 | uint32_t content1 = 1; |
| 190 | shared_ptr<Data> data1 = makeData(name); |
| 191 | data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1)); |
| 192 | signData(data1); |
| 193 | ims.insert(*data1); |
| 194 | |
| 195 | uint32_t content2 = 2; |
| 196 | shared_ptr<Data> data2 = makeData(name); |
| 197 | data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2)); |
| 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 | |
| 210 | uint32_t content1 = 1; |
| 211 | shared_ptr<Data> data1 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 212 | data1->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 213 | data1->setContent(reinterpret_cast<const uint8_t*>(&content1), sizeof(content1)); |
| 214 | signData(data1); |
| 215 | ims.insert(*data1); |
| 216 | |
| 217 | uint32_t content2 = 2; |
| 218 | shared_ptr<Data> data2 = makeData(name); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 219 | data2->setFreshnessPeriod(99999_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 220 | data2->setContent(reinterpret_cast<const uint8_t*>(&content2), sizeof(content2)); |
| 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 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 273 | ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size()); |
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 | |
| 279 | BOOST_CHECK_EQUAL_COLLECTIONS(digest1->begin(), digest1->end(), |
Alexander Afanasyev | 3e2c3a0 | 2017-01-19 13:55:56 -0800 | [diff] [blame] | 280 | entry.getFullName()[-1].value_begin(), |
| 281 | entry.getFullName()[-1].value_end()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | BOOST_AUTO_TEST_CASE_TEMPLATE(Iterator, T, InMemoryStorages) |
| 285 | { |
| 286 | T ims; |
| 287 | |
| 288 | BOOST_CONCEPT_ASSERT((boost::InputIterator<InMemoryStorage::const_iterator>)); |
| 289 | |
| 290 | for (int i = 0; i < 10; i++) { |
| 291 | std::ostringstream convert; |
| 292 | convert << i; |
| 293 | Name name("/" + convert.str()); |
| 294 | shared_ptr<Data> data = makeData(name); |
| 295 | ims.insert(*data); |
| 296 | } |
| 297 | |
| 298 | InMemoryStorage::const_iterator it = ims.begin(); |
| 299 | InMemoryStorage::const_iterator tmp1 = it; |
| 300 | BOOST_REQUIRE(tmp1 == it); |
| 301 | InMemoryStorage::const_iterator tmp2 = tmp1++; |
| 302 | BOOST_REQUIRE(tmp2 != tmp1); |
| 303 | tmp2 = ++tmp1; |
| 304 | BOOST_REQUIRE(tmp2 == tmp1); |
| 305 | |
| 306 | int i = 0; |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 307 | for (; it != ims.end(); it++) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 308 | std::ostringstream convert; |
| 309 | convert << i; |
| 310 | Name name("/" + convert.str()); |
| 311 | BOOST_CHECK_EQUAL(it->getName(), name); |
| 312 | BOOST_CHECK_EQUAL((*it).getName(), name); |
| 313 | i++; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertCanonical, T, InMemoryStorages) |
| 318 | { |
| 319 | T ims; |
| 320 | |
| 321 | shared_ptr<Data> data = makeData("/a"); |
| 322 | ims.insert(*data); |
| 323 | |
| 324 | shared_ptr<Data> data2 = makeData("/b"); |
| 325 | ims.insert(*data2); |
| 326 | |
| 327 | shared_ptr<Data> data3 = makeData("/c"); |
| 328 | ims.insert(*data3); |
| 329 | |
| 330 | shared_ptr<Data> data4 = makeData("/d"); |
| 331 | ims.insert(*data4); |
| 332 | |
| 333 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 334 | ims.insert(*data5); |
| 335 | |
| 336 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 337 | ims.insert(*data6); |
| 338 | |
| 339 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 340 | ims.insert(*data7); |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 341 | |
| 342 | // avoid "test case [...] did not check any assertions" message from Boost.Test |
| 343 | BOOST_CHECK(true); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | BOOST_AUTO_TEST_CASE_TEMPLATE(EraseCanonical, T, InMemoryStorages) |
| 347 | { |
| 348 | T ims; |
| 349 | |
| 350 | shared_ptr<Data> data = makeData("/a"); |
| 351 | ims.insert(*data); |
| 352 | |
| 353 | shared_ptr<Data> data2 = makeData("/b"); |
| 354 | ims.insert(*data2); |
| 355 | |
| 356 | shared_ptr<Data> data3 = makeData("/c"); |
| 357 | ims.insert(*data3); |
| 358 | |
| 359 | shared_ptr<Data> data4 = makeData("/d"); |
| 360 | ims.insert(*data4); |
| 361 | |
| 362 | shared_ptr<Data> data5 = makeData("/c/c/1/2/3/4/5/6"); |
| 363 | ims.insert(*data5); |
| 364 | |
| 365 | shared_ptr<Data> data6 = makeData("/c/c/1/2/3"); |
| 366 | ims.insert(*data6); |
| 367 | |
| 368 | shared_ptr<Data> data7 = makeData("/c/c/1"); |
| 369 | ims.insert(*data7); |
| 370 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 371 | ConstBufferPtr digest1 = util::Sha256::computeDigest(data->wireEncode().wire(), data->wireEncode().size()); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 372 | |
| 373 | Name name("/a"); |
| 374 | ims.erase(name); |
| 375 | BOOST_CHECK_EQUAL(ims.size(), 6); |
| 376 | } |
| 377 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 378 | using InMemoryStoragesLimited = boost::mpl::vector<InMemoryStorageFifo, |
| 379 | InMemoryStorageLfu, |
| 380 | InMemoryStorageLru>; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 381 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 382 | BOOST_AUTO_TEST_CASE_TEMPLATE(SetCapacity, T, InMemoryStoragesLimited) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 383 | { |
| 384 | T ims; |
| 385 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 386 | ims.setCapacity(18); |
| 387 | for (int i = 1; i < 19; ++i) { |
| 388 | ims.insert(*makeData(to_string(i))); |
| 389 | } |
| 390 | BOOST_CHECK_EQUAL(ims.size(), 18); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 391 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 392 | ims.setCapacity(16); |
| 393 | BOOST_CHECK_EQUAL(ims.size(), 16); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_CASE_TEMPLATE(GetLimit, T, InMemoryStoragesLimited) |
| 397 | { |
| 398 | T ims(10000); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 399 | BOOST_CHECK_EQUAL(ims.getLimit(), 10000); |
| 400 | |
| 401 | T ims2(4); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(ims2.getLimit(), 4); |
| 403 | } |
| 404 | |
| 405 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndDouble, T, InMemoryStoragesLimited) |
| 406 | { |
| 407 | T ims(40); |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 408 | size_t initialCapacity = ims.getCapacity(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 409 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 410 | for (size_t i = 0; i < initialCapacity + 1; i++) { |
| 411 | shared_ptr<Data> data = makeData(to_string(i)); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 412 | data->setFreshnessPeriod(5000_ms); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 413 | signData(data); |
| 414 | ims.insert(*data); |
| 415 | } |
| 416 | |
Ashlesh Gawande | 1bbce6d | 2018-11-13 15:31:04 -0600 | [diff] [blame] | 417 | BOOST_CHECK_EQUAL(ims.size(), initialCapacity + 1); |
| 418 | BOOST_CHECK_EQUAL(ims.getCapacity(), initialCapacity * 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | BOOST_AUTO_TEST_CASE_TEMPLATE(InsertAndEvict, T, InMemoryStoragesLimited) |
| 422 | { |
| 423 | T ims(2); |
| 424 | |
| 425 | Name name("/insert/1"); |
| 426 | shared_ptr<Data> data = makeData(name); |
| 427 | ims.insert(*data); |
| 428 | |
| 429 | Name name2("/insert/2"); |
| 430 | shared_ptr<Data> data2 = makeData(name2); |
| 431 | ims.insert(*data2); |
| 432 | |
| 433 | Name name3("/insert/3"); |
| 434 | shared_ptr<Data> data3 = makeData(name3); |
| 435 | ims.insert(*data3); |
| 436 | |
| 437 | BOOST_CHECK_EQUAL(ims.size(), 2); |
| 438 | |
| 439 | shared_ptr<Interest> interest = makeInterest(name); |
| 440 | shared_ptr<const Data> found = ims.find(*interest); |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 441 | BOOST_CHECK(found == nullptr); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 444 | // 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^] | 445 | class FindFixture : public IoFixture |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 446 | { |
| 447 | protected: |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 448 | Name |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 449 | insert(uint32_t id, const Name& name, |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 450 | const std::function<void(Data&)>& modifyData = nullptr, |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 451 | const time::milliseconds& freshWindow = InMemoryStorage::INFINITE_WINDOW) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 452 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 453 | auto data = makeData(name); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 454 | data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id)); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 455 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 456 | if (modifyData != nullptr) { |
| 457 | modifyData(*data); |
| 458 | } |
| 459 | |
| 460 | data->wireEncode(); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 461 | m_ims.insert(*data, freshWindow); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 462 | |
| 463 | return data->getFullName(); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | Interest& |
| 467 | startInterest(const Name& name) |
| 468 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 469 | m_interest = makeInterest(name, false); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 470 | return *m_interest; |
| 471 | } |
| 472 | |
| 473 | uint32_t |
| 474 | find() |
| 475 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame^] | 476 | auto found = m_ims.find(*m_interest); |
| 477 | if (found == nullptr) { |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | const Block& content = found->getContent(); |
| 481 | if (content.value_size() != sizeof(uint32_t)) { |
| 482 | return 0; |
| 483 | } |
Junxiao Shi | baab732 | 2017-07-27 01:22:49 +0000 | [diff] [blame] | 484 | uint32_t id = 0; |
| 485 | std::memcpy(&id, content.value(), sizeof(id)); |
| 486 | return id; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | protected: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame^] | 490 | InMemoryStoragePersistent m_ims{m_io}; |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 491 | shared_ptr<Interest> m_interest; |
| 492 | }; |
| 493 | |
| 494 | BOOST_FIXTURE_TEST_SUITE(Find, FindFixture) |
| 495 | |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 496 | BOOST_AUTO_TEST_CASE(ExactName) |
| 497 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 498 | insert(1, "/"); |
| 499 | insert(2, "/A"); |
| 500 | insert(3, "/A/B"); |
| 501 | insert(4, "/A/C"); |
| 502 | insert(5, "/D"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 503 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 504 | startInterest("/A"); |
| 505 | BOOST_CHECK_EQUAL(find(), 2); |
| 506 | } |
| 507 | |
| 508 | BOOST_AUTO_TEST_CASE(ExactName_CanBePrefix) |
| 509 | { |
| 510 | insert(1, "/"); |
| 511 | insert(2, "/A"); |
| 512 | insert(3, "/A/B"); |
| 513 | insert(4, "/A/C"); |
| 514 | insert(5, "/D"); |
| 515 | |
| 516 | startInterest("/A") |
| 517 | .setCanBePrefix(true); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 518 | BOOST_CHECK_EQUAL(find(), 2); |
| 519 | } |
| 520 | |
| 521 | BOOST_AUTO_TEST_CASE(FullName) |
| 522 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 523 | Name n1 = insert(1, "/A"); |
| 524 | Name n2 = insert(2, "/A"); |
Alexander Afanasyev | 8cf2f2c | 2015-01-31 21:33:46 -0800 | [diff] [blame] | 525 | |
| 526 | startInterest(n1); |
| 527 | BOOST_CHECK_EQUAL(find(), 1); |
| 528 | |
| 529 | startInterest(n2); |
| 530 | BOOST_CHECK_EQUAL(find(), 2); |
| 531 | } |
| 532 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 533 | BOOST_AUTO_TEST_CASE(FullName_EmptyDataName) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 534 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 535 | Name n1 = insert(1, "/"); |
| 536 | Name n2 = insert(2, "/"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 537 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 538 | startInterest(n1); |
| 539 | BOOST_CHECK_EQUAL(find(), 1); |
| 540 | |
| 541 | startInterest(n2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 542 | BOOST_CHECK_EQUAL(find(), 2); |
| 543 | } |
| 544 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 545 | BOOST_AUTO_TEST_CASE(PrefixName) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 546 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 547 | insert(1, "/A"); |
| 548 | insert(2, "/B/p/1"); |
| 549 | insert(3, "/B/p/2"); |
| 550 | insert(4, "/B/q/1"); |
| 551 | insert(5, "/B/q/2"); |
| 552 | insert(6, "/C"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 553 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 554 | startInterest("/B") |
| 555 | .setCanBePrefix(true); |
| 556 | BOOST_CHECK_EQUAL(find(), 2); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 559 | BOOST_AUTO_TEST_CASE(PrefixName_NoCanBePrefix) |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 560 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 561 | insert(1, "/B/p/1"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 562 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 563 | startInterest("/B"); |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 564 | BOOST_CHECK_EQUAL(find(), 0); |
| 565 | } |
| 566 | |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 567 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 568 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 569 | insert(1, "/A/1"); // omitted FreshnessPeriod means FreshnessPeriod = 0 ms |
| 570 | insert(2, "/A/2", [] (Data& data) { data.setFreshnessPeriod(0_s); }); |
| 571 | insert(3, "/A/3", [] (Data& data) { data.setFreshnessPeriod(1_s); }, 1_s); |
| 572 | insert(4, "/A/4", [] (Data& data) { data.setFreshnessPeriod(1_h); }, 1_h); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 573 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 574 | // lookup at exact same moment as insertion is not tested because this won't happen in reality |
| 575 | |
| 576 | advanceClocks(500_ms); // @500ms |
| 577 | startInterest("/A") |
| 578 | .setCanBePrefix(true) |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 579 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 580 | BOOST_CHECK_EQUAL(find(), 3); |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 581 | |
| 582 | advanceClocks(1500_ms); // @2s |
| 583 | startInterest("/A") |
| 584 | .setCanBePrefix(true) |
| 585 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 586 | BOOST_CHECK_EQUAL(find(), 4); |
| 587 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 588 | advanceClocks(3500_s); // @3502s |
| 589 | startInterest("/A") |
| 590 | .setCanBePrefix(true) |
| 591 | .setMustBeFresh(true); |
| 592 | BOOST_CHECK_EQUAL(find(), 4); |
| 593 | |
| 594 | advanceClocks(3500_s); // @7002s |
| 595 | startInterest("/A") |
| 596 | .setCanBePrefix(true) |
| 597 | .setMustBeFresh(true); |
Yingdi Yu | 404eafd | 2016-03-06 14:54:25 -0800 | [diff] [blame] | 598 | BOOST_CHECK_EQUAL(find(), 0); |
| 599 | } |
| 600 | |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 601 | BOOST_AUTO_TEST_SUITE_END() // Find |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 602 | BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage |
Junxiao Shi | c542f63 | 2017-07-18 14:20:32 +0000 | [diff] [blame] | 603 | BOOST_AUTO_TEST_SUITE_END() // Ims |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 604 | |
Junxiao Shi | 85d9083 | 2016-08-04 03:19:46 +0000 | [diff] [blame] | 605 | } // namespace tests |
Jiewen Tan | 9913596 | 2014-09-20 02:18:53 -0700 | [diff] [blame] | 606 | } // namespace ndn |