blob: bc34c1541b2b97667d3cd011026791bfac47cde1 [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "table/name-tree.hpp"
8#include <boost/test/unit_test.hpp>
9
10namespace nfd {
11
12using name_tree::Entry;
13
14BOOST_AUTO_TEST_SUITE(TableNameTree)
15
16BOOST_AUTO_TEST_CASE (Entry)
17{
18 Name prefix("ndn:/named-data/research/abc/def/ghi");
19
20 name_tree::Entry npe = name_tree::Entry(prefix);
21 BOOST_CHECK_EQUAL(npe.getPrefix(), prefix);
22
23 // examine all the get methods
24
25 uint32_t hash = npe.getHash();
26 BOOST_CHECK_EQUAL(hash, 0);
27
28 shared_ptr<name_tree::Entry> parent = npe.getParent();
29 BOOST_CHECK(!static_cast<bool>(parent));
30
31 std::vector<shared_ptr<name_tree::Entry> >& childList = npe.getChildren();
32 BOOST_CHECK_EQUAL(childList.size(), 0);
33
34 shared_ptr<fib::Entry> fib = npe.getFibEntry();
35 BOOST_CHECK(!static_cast<bool>(fib));
36
37 std::vector< shared_ptr<pit::Entry> >& pitList = npe.getPitEntries();
38 BOOST_CHECK_EQUAL(pitList.size(), 0);
39
40 // examine all the set method
41
42 npe.setHash(12345);
43 BOOST_CHECK_EQUAL(npe.getHash(), 12345);
44
45 Name parentName("ndn:/named-data/research/abc/def");
46 parent = make_shared<name_tree::Entry>(parentName);
47 npe.setParent(parent);
48 BOOST_CHECK_EQUAL(npe.getParent(), parent);
49
50 // Insert FIB
51
52 shared_ptr<fib::Entry> fibEntry(new fib::Entry(prefix));
53 shared_ptr<fib::Entry> fibEntryParent(new fib::Entry(parentName));
54
55 npe.setFibEntry(fibEntry);
56 BOOST_CHECK_EQUAL(npe.getFibEntry(), fibEntry);
57
58 // Erase a FIB that does not exist
59 BOOST_CHECK_EQUAL(npe.
60 eraseFibEntry(fibEntryParent), false);
61 BOOST_CHECK_EQUAL(npe.getFibEntry(), fibEntry);
62
63 // Erase the FIB that exists
64 BOOST_CHECK_EQUAL(npe.
65 eraseFibEntry(fibEntry), true);
66 BOOST_CHECK(!static_cast<bool>(npe.getFibEntry()));
67
68 // Insert a PIT
69
70 shared_ptr<pit::Entry> PitEntry(make_shared<pit::Entry>(prefix));
71 shared_ptr<pit::Entry> PitEntry2(make_shared<pit::Entry>(parentName));
72
73 Name prefix3("ndn:/named-data/research/abc/def");
74 shared_ptr<pit::Entry> PitEntry3(make_shared<pit::Entry>(prefix3));
75
76 npe.insertPitEntry(PitEntry);
77 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
78
79 npe.insertPitEntry(PitEntry2);
80 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 2);
81
82 BOOST_CHECK_EQUAL(npe.
83 erasePitEntry(PitEntry), true);
84 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
85
86 // erase a PIT Entry that does not exist
87
88 BOOST_CHECK_EQUAL(npe.
89 erasePitEntry(PitEntry3), false);
90 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 1);
91
92 BOOST_CHECK_EQUAL(npe.
93 erasePitEntry(PitEntry2), true);
94 BOOST_CHECK_EQUAL(npe.getPitEntries().size(), 0);
95
96 // erase a PIT Entry that does not exist any more
97
98 BOOST_CHECK_EQUAL(npe.
99 erasePitEntry(PitEntry2), false);
100}
101
102BOOST_AUTO_TEST_CASE (NameTreeBasic)
103{
104 size_t nBuckets = 16;
105 NameTree nt(nBuckets);
106
107 BOOST_CHECK_EQUAL(nt.size(), 0);
108 BOOST_CHECK_EQUAL(nt.getNBuckets(), nBuckets);
109
110 Name nameABC = ("ndn:/a/b/c");
111 shared_ptr<name_tree::Entry> npeABC = nt.lookup(nameABC);
112 BOOST_CHECK_EQUAL(nt.size(), 4);
113
114 Name nameABD = ("/a/b/d");
115 shared_ptr<name_tree::Entry> npeABD = nt.lookup(nameABD);
116 BOOST_CHECK_EQUAL(nt.size(), 5);
117
118 Name nameAE = ("/a/e/");
119 shared_ptr<name_tree::Entry> npeAE = nt.lookup(nameAE);
120 BOOST_CHECK_EQUAL(nt.size(), 6);
121
122 Name nameF = ("/f");
123 shared_ptr<name_tree::Entry> npeF = nt.lookup(nameF);
124 BOOST_CHECK_EQUAL(nt.size(), 7);
125
126 // validate lookup() and findExactMatch()
127
128 Name nameAB ("/a/b");
129 BOOST_CHECK_EQUAL(npeABC->getParent(), nt.findExactMatch(nameAB));
130 BOOST_CHECK_EQUAL(npeABD->getParent(), nt.findExactMatch(nameAB));
131
132 Name nameA ("/a");
133 BOOST_CHECK_EQUAL(npeAE->getParent(), nt.findExactMatch(nameA));
134
135 Name nameRoot ("/");
136 BOOST_CHECK_EQUAL(npeF->getParent(), nt.findExactMatch(nameRoot));
137 BOOST_CHECK_EQUAL(nt.size(), 7);
138
139 Name name0 = ("/does/not/exist");
140 shared_ptr<name_tree::Entry> npe0 = nt.findExactMatch(name0);
141 BOOST_CHECK(!static_cast<bool>(npe0));
142
143
144 // Longest Prefix Matching
145
146 shared_ptr<name_tree::Entry> temp;
147 Name nameABCLPM("/a/b/c/def/asdf/nlf");
148 temp = nt.findLongestPrefixMatch(nameABCLPM);
149 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABC));
150
151 Name nameABDLPM("/a/b/d/def/asdf/nlf");
152 temp = nt.findLongestPrefixMatch(nameABDLPM);
153 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameABD));
154
155 Name nameABLPM("/a/b/hello/world");
156 temp = nt.findLongestPrefixMatch(nameABLPM);
157 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAB));
158
159 Name nameAELPM("/a/e/hello/world");
160 temp = nt.findLongestPrefixMatch(nameAELPM);
161 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameAE));
162
163 Name nameALPM("/a/hello/world");
164 temp = nt.findLongestPrefixMatch(nameALPM);
165 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameA));
166
167 Name nameFLPM("/f/hello/world");
168 temp = nt.findLongestPrefixMatch(nameFLPM);
169 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameF));
170
171 Name nameRootLPM("/does_not_exist");
172 temp = nt.findLongestPrefixMatch(nameRootLPM);
173 BOOST_CHECK_EQUAL(temp, nt.findExactMatch(nameRoot));
174
175 // nt.dump(std::cout);
176
177 bool eraseResult = false;
178 temp = nt.findExactMatch(nameABC);
179 if (static_cast<bool>(temp))
180 eraseResult = nt.
181 eraseEntryIfEmpty(temp);
182 BOOST_CHECK_EQUAL(nt.size(), 6);
183 BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC)));
184 BOOST_CHECK_EQUAL(eraseResult, true);
185
186 eraseResult = false;
187 temp = nt.findExactMatch(nameABCLPM);
188 if (static_cast<bool>(temp))
189 eraseResult = nt.
190 eraseEntryIfEmpty(temp);
191 BOOST_CHECK(!static_cast<bool>(temp));
192 BOOST_CHECK_EQUAL(nt.size(), 6);
193 BOOST_CHECK_EQUAL(eraseResult, false);
194
195 // nt.dump(std::cout);
196
197 nt.lookup(nameABC);
198 BOOST_CHECK_EQUAL(nt.size(), 7);
199
200 eraseResult = false;
201 temp = nt.findExactMatch(nameABC);
202 if (static_cast<bool>(temp))
203 eraseResult = nt.
204 eraseEntryIfEmpty(temp);
205 BOOST_CHECK_EQUAL(nt.size(), 6);
206 BOOST_CHECK_EQUAL(eraseResult, true);
207 BOOST_CHECK(!static_cast<bool>(nt.findExactMatch(nameABC)));
208
209 // nt.dump(std::cout);
210
211 BOOST_CHECK_EQUAL(nt.getNBuckets(), 16);
212
213 // should resize now
214 Name nameABCD("a/b/c/d");
215 nt.lookup(nameABCD);
216 Name nameABCDE("a/b/c/d/e");
217 nt.lookup(nameABCDE);
218 BOOST_CHECK_EQUAL(nt.size(), 9);
219 BOOST_CHECK_EQUAL(nt.getNBuckets(), 32);
220
221 // nt.dump(std::cout);
222
223 // try to erase /a/b/c, should return false
224 temp = nt.findExactMatch(nameABC);
225 BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC);
226 eraseResult = nt.
227 eraseEntryIfEmpty(temp);
228 BOOST_CHECK_EQUAL(eraseResult, false);
229 temp = nt.findExactMatch(nameABC);
230 BOOST_CHECK_EQUAL(temp->getPrefix(), nameABC);
231
232 temp = nt.findExactMatch(nameABD);
233 if (static_cast<bool>(temp))
234 nt.
235 eraseEntryIfEmpty(temp);
236 BOOST_CHECK_EQUAL(nt.size(), 8);
237
238 // nt.dump(std::cout);
239
240 shared_ptr<std::vector<shared_ptr<name_tree::Entry> > > fullList;
241 fullList = nt.fullEnumerate();
242 BOOST_CHECK_EQUAL(fullList->size(), 8);
243 // for (size_t j = 0; j < (*fullList).size(); j++)
244 // {
245 // temp = (*fullList)[j];
246 // std::cout << temp->getPrefix().toUri() << std::endl;
247 // }
248
249 shared_ptr<std::vector<shared_ptr<name_tree::Entry> > > partialList;
250 partialList = nt.partialEnumerate(nameA);
251 BOOST_CHECK_EQUAL(partialList->size(), 6);
252 // for (size_t j = 0; j < (*partialList).size(); j++)
253 // {
254 // temp = (*partialList)[j];
255 // std::cout << temp->getPrefix().toUri() << std::endl;
256 // }
257
258 partialList = nt.partialEnumerate(nameRoot);
259 BOOST_CHECK_EQUAL(partialList->size(), 8);
260 // for (size_t j = 0; j < (*partialList).size(); j++)
261 // {
262 // temp = (*partialList)[j];
263 // std::cout << temp->getPrefix().toUri() << std::endl;
264 // }
265}
266
267BOOST_AUTO_TEST_SUITE_END()
268
269} // namespace nfd
270
271