blob: 15bf89e602dfc0b50598cdb797834b0a07af7581 [file] [log] [blame]
Junxiao Shi9222f362019-04-16 15:15:23 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2019, Regents of the University of California,
4 * 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 {
67 interest = make_shared<Interest>(name);
68 interest->setCanBePrefix(false);
69 return *interest;
70 }
71
72 void
73 find(const std::function<void(uint32_t)>& check)
74 {
75 bool hasResult = false;
76 cs.find(*interest,
77 [&] (const Interest& interest, const Data& data) {
78 hasResult = true;
79 const Block& content = data.getContent();
80 uint32_t found = 0;
81 std::memcpy(&found, content.value(), sizeof(found));
82 check(found);
83 },
84 bind([&] {
85 hasResult = true;
86 check(0);
87 }));
88
89 // current Cs::find implementation is synchronous
90 BOOST_CHECK(hasResult);
91 }
92
93 size_t
94 erase(const Name& prefix, size_t limit)
95 {
96 optional<size_t> nErased;
97 cs.erase(prefix, limit, [&] (size_t nErased1) { nErased = nErased1; });
98
99 // current Cs::erase implementation is synchronous
100 // if callback was not invoked, bad_optional_access would occur
101 return *nErased;
102 }
103
104protected:
105 Cs cs;
106 shared_ptr<Interest> interest;
107};
108
109} // namespace tests
110} // namespace cs
111} // namespace nfd
112
113#endif // NFD_TESTS_DAEMON_TABLE_CS_FIXTURE_HPP