blob: 4ed534620ba48c8341ccd96ebadd61b09002ab05 [file] [log] [blame]
Junxiao Shi9222f362019-04-16 15:15:23 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento22085362021-03-18 22:08:03 -04003 * Copyright (c) 2014-2021, 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
36namespace nfd {
37namespace cs {
38namespace tests {
39
40using namespace nfd::tests;
41
42#define CHECK_CS_FIND(expected) find([&] (uint32_t found) { BOOST_CHECK_EQUAL(expected, found); });
43
44class CsFixture : public GlobalIoTimeFixture
45{
46protected:
47 Name
48 insert(uint32_t id, const Name& name, const std::function<void(Data&)>& modifyData = nullptr,
49 bool isUnsolicited = false)
50 {
51 auto data = makeData(name);
52 data->setContent(reinterpret_cast<const uint8_t*>(&id), sizeof(id));
53
54 if (modifyData != nullptr) {
55 modifyData(*data);
56 }
57
58 data->wireEncode();
59 cs.insert(*data, isUnsolicited);
60
61 return data->getFullName();
62 }
63
64 Interest&
65 startInterest(const Name& name)
66 {
Davide Pesavento22085362021-03-18 22:08:03 -040067 interest = makeInterest(name);
Junxiao Shi9222f362019-04-16 15:15:23 +000068 return *interest;
69 }
70
71 void
72 find(const std::function<void(uint32_t)>& check)
73 {
74 bool hasResult = false;
75 cs.find(*interest,
Davide Pesavento22085362021-03-18 22:08:03 -040076 [&] (const Interest&, const Data& data) {
Junxiao Shi9222f362019-04-16 15:15:23 +000077 hasResult = true;
78 const Block& content = data.getContent();
79 uint32_t found = 0;
80 std::memcpy(&found, content.value(), sizeof(found));
81 check(found);
82 },
83 bind([&] {
84 hasResult = true;
85 check(0);
86 }));
87
88 // current Cs::find implementation is synchronous
89 BOOST_CHECK(hasResult);
90 }
91
92 size_t
93 erase(const Name& prefix, size_t limit)
94 {
95 optional<size_t> nErased;
96 cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; });
97
98 // current Cs::erase implementation is synchronous
99 // if callback was not invoked, bad_optional_access would occur
100 return *nErased;
101 }
102
103protected:
104 Cs cs;
105 shared_ptr<Interest> interest;
106};
107
108} // namespace tests
109} // namespace cs
110} // namespace nfd
111
112#endif // NFD_TESTS_DAEMON_TABLE_CS_FIXTURE_HPP