blob: 30273d3bb40637fca7d799b65fcdc102b022cc31 [file] [log] [blame]
Junxiao Shi9222f362019-04-16 15:15:23 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi9222f362019-04-16 15:15:23 +00004 * 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.
10 *
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/>.
24 */
25
26#ifndef NFD_TESTS_DAEMON_TABLE_CS_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_TABLE_CS_FIXTURE_HPP
28
29#include "table/cs.hpp"
30
31#include "tests/test-common.hpp"
32#include "tests/daemon/global-io-fixture.hpp"
33
34#include <cstring>
35
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Junxiao Shi9222f362019-04-16 15:15:23 +000037
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038#define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); })
Junxiao Shi9222f362019-04-16 15:15:23 +000039
40class CsFixture : public GlobalIoTimeFixture
41{
42protected:
43 Name
44 insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr,
45 bool isUnsolicited = false)
46 {
47 auto data = makeData(name);
Davide Pesaventoa599d2a2022-02-16 18:52:43 -050048 data->setContent(ndn::make_span(reinterpret_cast<const uint8_t*>(&id), sizeof(id)));
Junxiao Shi9222f362019-04-16 15:15:23 +000049
50 if (modifyData != nullptr) {
51 modifyData(*data);
52 }
53
54 data->wireEncode();
55 cs.insert(*data, isUnsolicited);
56
57 return data->getFullName();
58 }
59
60 Interest&
61 startInterest(const Name& name)
62 {
Davide Pesavento22085362021-03-18 22:08:03 -040063 interest = makeInterest(name);
Junxiao Shi9222f362019-04-16 15:15:23 +000064 return *interest;
65 }
66
67 void
68 find(const std::function<void(uint32_t)>& check)
69 {
70 bool hasResult = false;
71 cs.find(*interest,
Davide Pesavento22085362021-03-18 22:08:03 -040072 [&] (const Interest&, const Data& data) {
Junxiao Shi9222f362019-04-16 15:15:23 +000073 hasResult = true;
74 const Block& content = data.getContent();
75 uint32_t found = 0;
76 std::memcpy(&found, content.value(), sizeof(found));
77 check(found);
78 },
Davide Pesavento412c9822021-07-02 00:21:05 -040079 [&] (auto&&...) {
Junxiao Shi9222f362019-04-16 15:15:23 +000080 hasResult = true;
81 check(0);
Davide Pesavento412c9822021-07-02 00:21:05 -040082 });
Junxiao Shi9222f362019-04-16 15:15:23 +000083
84 // current Cs::find implementation is synchronous
85 BOOST_CHECK(hasResult);
86 }
87
88 size_t
89 erase(const Name& prefix, size_t limit)
90 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040091 std::optional<size_t> nErased;
92 cs.erase(prefix, limit, [&] (size_t n) { nErased = n; });
Junxiao Shi9222f362019-04-16 15:15:23 +000093
94 // current Cs::erase implementation is synchronous
95 // if callback was not invoked, bad_optional_access would occur
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040096 return nErased.value();
Junxiao Shi9222f362019-04-16 15:15:23 +000097 }
98
99protected:
100 Cs cs;
101 shared_ptr<Interest> interest;
102};
103
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400104} // namespace nfd::tests
Junxiao Shi9222f362019-04-16 15:15:23 +0000105
106#endif // NFD_TESTS_DAEMON_TABLE_CS_FIXTURE_HPP