blob: 7f605c8c9d384b57b2ca5040dfcddfc785a2617c [file] [log] [blame]
spirosmastorakis0e2b1972016-03-27 20:54:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent 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* nTorrent 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 nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21
22#include "boost-test.hpp"
23#include "stats-table.hpp"
24
25#include <ndn-cxx/name.hpp>
26
27namespace ndn {
28namespace ntorrent {
29namespace tests {
30
31BOOST_AUTO_TEST_SUITE(TestStatsTable)
32
33BOOST_AUTO_TEST_CASE(TestCoreAPI)
34{
35 StatsTable table(Name("linux15.01"));
36 table.insert(Name("isp1"));
37 table.insert(Name("isp2"));
38 table.insert(Name("isp3"));
39
40 BOOST_CHECK(table.find(Name("isp1")) != table.end());
41 BOOST_CHECK(table.find(Name("isp2")) != table.end());
42 BOOST_CHECK(table.find(Name("isp3")) != table.end());
43 BOOST_CHECK(table.find(Name("isp4")) == table.end());
44
45 BOOST_CHECK_EQUAL(table.erase(Name("isp2")), true);
46 BOOST_CHECK_EQUAL(table.erase(Name("isp4")), false);
47
48 BOOST_CHECK(table.find(Name("isp1")) != table.end());
49 BOOST_CHECK(table.find(Name("isp2")) == table.end());
50 BOOST_CHECK(table.find(Name("isp3")) != table.end());
51 BOOST_CHECK(table.find(Name("isp4")) == table.end());
52}
53
54BOOST_AUTO_TEST_CASE(TestStatsTableModification)
55{
56 StatsTable table(Name("linux15.01"));
57 table.insert(Name("isp1"));
58 table.insert(Name("isp2"));
59 table.insert(Name("isp3"));
60
61 BOOST_CHECK_EQUAL(table.size(), 3);
62
63 auto entry = table.find(Name("isp2"));
64 entry->incrementSentInterests();
65 entry->incrementReceivedData();
66
67 auto i = table.begin();
68 BOOST_CHECK(!(entry == table.end()));
69 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
70 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
71
72 i++;
73 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
74 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
75
76 i++;
77 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
78 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
79
80 entry = table.find(Name("isp1"));
81 BOOST_CHECK(!(entry == table.end()));
82 entry->incrementSentInterests();
83 entry->incrementReceivedData();
84 entry->incrementSentInterests();
85
86 i = table.begin();
87 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
88 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.5);
89
90 i++;
91 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
92 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
93
94 i++;
95 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
96 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
97
98 entry = table.find(Name("isp3"));
99 BOOST_CHECK(!(entry == table.end()));
100 entry->incrementSentInterests();
101 entry->incrementReceivedData();
102 entry->incrementSentInterests();
103 entry->incrementSentInterests();
104 entry->incrementSentInterests();
105
106 i = table.begin();
107 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
108 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.5);
109
110 i++;
111 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
112 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
113
114 i++;
115 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
116 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.25);
117}
118
119BOOST_AUTO_TEST_CASE(TestTableSort)
120{
121 StatsTable table(Name("linux15.01"));
122 table.insert(Name("isp1"));
123 table.insert(Name("isp2"));
124 table.insert(Name("isp3"));
125
126 BOOST_CHECK_EQUAL(table.size(), 3);
127
128 auto entry = table.find(Name("isp2"));
129 entry->incrementSentInterests();
130 entry->incrementReceivedData();
131
132 auto i = table.begin();
133 BOOST_CHECK(!(entry == table.end()));
134 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
135 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
136
137 i++;
138 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
139 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
140
141 i++;
142 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
143 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
144
145 entry = table.find(Name("isp1"));
146 BOOST_CHECK(!(entry == table.end()));
147 entry->incrementSentInterests();
148 entry->incrementReceivedData();
149 entry->incrementSentInterests();
150
151 i = table.begin();
152 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
153 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.5);
154
155 i++;
156 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
157 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
158
159 i++;
160 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
161 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
162
163 entry = table.find(Name("isp3"));
164 BOOST_CHECK(!(entry == table.end()));
165 entry->incrementSentInterests();
166 entry->incrementReceivedData();
167 entry->incrementSentInterests();
168 entry->incrementSentInterests();
169 entry->incrementSentInterests();
170
171 i = table.begin();
172 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
173 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.5);
174
175 i++;
176 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
177 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
178
179 i++;
180 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
181 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.25);
182
183 table.sort();
184
185 i = table.begin();
186 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
187 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 1);
188 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
189
190 i++;
191 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
192 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.5);
193
194 i++;
195 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
196 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.25);
197
198 BOOST_CHECK_EQUAL(table.erase(Name("isp1")), true);
199 BOOST_CHECK_EQUAL(table.size(), 2);
200
201 i = table.begin();
202 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
203 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 1);
204
205 i++;
206 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
207 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.25);
208
209 entry = table.find(Name("isp2"));
210 BOOST_CHECK(!(entry == table.end()));
211 entry->incrementSentInterests();
212 entry->incrementSentInterests();
213 entry->incrementSentInterests();
214
215 table.sort();
216
217 i = table.begin();
218 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
219 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.25);
220
221 i++;
222 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
223 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0.25);
224}
225
226BOOST_AUTO_TEST_SUITE_END()
227
228} // namespace tests
229} // namespace ntorrent
230} // namespace ndn