blob: 20a7948e5f3e939233d60000224bd321ce2e0703 [file] [log] [blame]
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * This file is part of NAC (Name-Based Access Control for NDN).
6 * See AUTHORS.md for complete list of NAC authors and contributors.
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07009 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070013 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
Alexander Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070018 *
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070020 */
21
22#include "repetitive-interval.hpp"
23#include "boost-test.hpp"
24
25namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040026namespace nac {
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070027namespace tests {
28
29using namespace boost::posix_time;
30
31BOOST_AUTO_TEST_SUITE(TestRepetitiveInterval)
32
33BOOST_AUTO_TEST_CASE(Construction)
34{
35 RepetitiveInterval repetitiveInterval1(from_iso_string("20150825T000000"),
36 from_iso_string("20150825T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070037 5,
38 10);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070039
40 BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval1.getStartDate()), "20150825T000000");
41 BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval1.getEndDate()), "20150825T000000");
42 BOOST_CHECK_EQUAL(repetitiveInterval1.getIntervalStartHour(), 5);
43 BOOST_CHECK_EQUAL(repetitiveInterval1.getIntervalEndHour(), 10);
44
45 RepetitiveInterval repetitiveInterval2(from_iso_string("20150825T000000"),
46 from_iso_string("20150827T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070047 5,
48 10,
49 1,
50 RepetitiveInterval::RepeatUnit::DAY);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070051
52 BOOST_CHECK_EQUAL(repetitiveInterval2.getNRepeats(), 1);
53 BOOST_CHECK(repetitiveInterval2.getRepeatUnit() == RepetitiveInterval::RepeatUnit::DAY);
54
55 RepetitiveInterval repetitiveInterval3(from_iso_string("20150825T000000"),
56 from_iso_string("20151227T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070057 5,
58 10,
59 2,
60 RepetitiveInterval::RepeatUnit::MONTH);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070061
62 BOOST_CHECK_EQUAL(repetitiveInterval3.getNRepeats(), 2);
63 BOOST_CHECK(repetitiveInterval3.getRepeatUnit() == RepetitiveInterval::RepeatUnit::MONTH);
64
65 RepetitiveInterval repetitiveInterval4(from_iso_string("20150825T000000"),
66 from_iso_string("20301227T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070067 5,
68 10,
69 5,
70 RepetitiveInterval::RepeatUnit::YEAR);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070071
72 BOOST_CHECK_EQUAL(repetitiveInterval4.getNRepeats(), 5);
73 BOOST_CHECK(repetitiveInterval4.getRepeatUnit() == RepetitiveInterval::RepeatUnit::YEAR);
74
75 RepetitiveInterval repetitiveInterval5;
76
77 BOOST_CHECK_EQUAL(repetitiveInterval5.getNRepeats(), 0);
78 BOOST_CHECK(repetitiveInterval5.getRepeatUnit() == RepetitiveInterval::RepeatUnit::NONE);
79}
80
81BOOST_AUTO_TEST_CASE(CheckCoverTimePoint)
82{
83 ///////////////////////////////////////////// with the repeat unit DAY
84
85 RepetitiveInterval repetitiveInterval1(from_iso_string("20150825T000000"),
86 from_iso_string("20150925T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070087 5,
88 10,
89 2,
90 RepetitiveInterval::RepeatUnit::DAY);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070091 Interval resultInterval;
92 bool isPositive = false;
93
94 TimeStamp tp1 = from_iso_string("20150825T050000");
95
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070096 std::tie(isPositive, resultInterval) = repetitiveInterval1.getInterval(tp1);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070097 BOOST_CHECK_EQUAL(isPositive, true);
98 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T050000");
99 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000");
100
101 TimeStamp tp2 = from_iso_string("20150902T060000");
102
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700103 std::tie(isPositive, resultInterval) = repetitiveInterval1.getInterval(tp2);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700104 BOOST_CHECK_EQUAL(isPositive, true);
105 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150902T050000");
106 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150902T100000");
107
108 TimeStamp tp3 = from_iso_string("20150929T040000");
109
110 BOOST_CHECK(std::get<0>(repetitiveInterval1.getInterval(tp3)) == false);
111
112 ///////////////////////////////////////////// with the repeat unit MONTH
113
114 RepetitiveInterval repetitiveInterval2(from_iso_string("20150825T000000"),
115 from_iso_string("20160825T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700116 5,
117 10,
118 2,
119 RepetitiveInterval::RepeatUnit::MONTH);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700120
121 TimeStamp tp4 = from_iso_string("20150825T050000");
122
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700123 std::tie(isPositive, resultInterval) = repetitiveInterval2.getInterval(tp4);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700124 BOOST_CHECK_EQUAL(isPositive, true);
125 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T050000");
126 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000");
127
128 TimeStamp tp5 = from_iso_string("20151025T060000");
129
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700130 std::tie(isPositive, resultInterval) = repetitiveInterval2.getInterval(tp5);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700131 BOOST_CHECK_EQUAL(isPositive, true);
132 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20151025T050000");
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700133 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20151025T100000");
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700134
135 TimeStamp tp6 = from_iso_string("20151226T050000");
136
137 BOOST_CHECK(std::get<0>(repetitiveInterval2.getInterval(tp6)) == false);
138
139 TimeStamp tp7 = from_iso_string("20151225T040000");
140
141 BOOST_CHECK(std::get<0>(repetitiveInterval2.getInterval(tp7)) == false);
142
143 ///////////////////////////////////////////// with the repeat unit YEAR
144
145 RepetitiveInterval repetitiveInterval3(from_iso_string("20150825T000000"),
146 from_iso_string("20300825T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700147 5,
148 10,
149 3,
150 RepetitiveInterval::RepeatUnit::YEAR);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700151
152 TimeStamp tp8 = from_iso_string("20150825T050000");
153
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700154 std::tie(isPositive, resultInterval) = repetitiveInterval3.getInterval(tp8);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700155 BOOST_CHECK_EQUAL(isPositive, true);
156 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20150825T050000");
157 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20150825T100000");
158
159 TimeStamp tp9 = from_iso_string("20180825T060000");
160
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700161 std::tie(isPositive, resultInterval) = repetitiveInterval3.getInterval(tp9);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700162 BOOST_CHECK_EQUAL(isPositive, true);
163 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getStartTime()), "20180825T050000");
164 BOOST_CHECK_EQUAL(to_iso_string(resultInterval.getEndTime()), "20180825T100000");
165
166 TimeStamp tp10 = from_iso_string("20180826T050000");
167 BOOST_CHECK(std::get<0>(repetitiveInterval3.getInterval(tp10)) == false);
168
169 TimeStamp tp11 = from_iso_string("20210825T040000");
170 BOOST_CHECK(std::get<0>(repetitiveInterval3.getInterval(tp11)) == false);
171
172 TimeStamp tp12 = from_iso_string("20300825T040000");
173 BOOST_CHECK(std::get<0>(repetitiveInterval3.getInterval(tp12)) == false);
174}
175
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700176const uint8_t REPETITIVE_INTERVAL[] = {0x8c, 0x2e, // RepetitiveInterval
177 0x86, 0x0f, 0x32, 0x30, 0x31, 0x35, 0x30, 0x38, 0x32, 0x35,
178 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x87, 0x0f, 0x32,
179 0x30, 0x31, 0x35, 0x30, 0x39, 0x32, 0x31, 0x54, 0x30, 0x30,
180 0x30, 0x30, 0x30, 0x30, 0x88, 0x01, 0x05, 0x89, 0x01, 0x0a,
181 0x8a, 0x01, 0x04, 0x8b, 0x01, 0x01};
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700182
183BOOST_AUTO_TEST_CASE(EncodeAndDecode)
184{
185 RepetitiveInterval repetitiveInterval1(from_iso_string("20150825T000000"),
186 from_iso_string("20150921T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700187 5,
188 10,
189 4,
190 RepetitiveInterval::RepeatUnit::DAY);
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700191
192 Block block1 = repetitiveInterval1.wireEncode();
193 Block block2(REPETITIVE_INTERVAL, sizeof(REPETITIVE_INTERVAL));
194
195 BOOST_CHECK(block1 == block2);
196
197 RepetitiveInterval RepetitiveInterval2(block1);
198
199 BOOST_CHECK_EQUAL(to_iso_string(RepetitiveInterval2.getStartDate()), "20150825T000000");
200 BOOST_CHECK_EQUAL(to_iso_string(RepetitiveInterval2.getEndDate()), "20150921T000000");
201 BOOST_CHECK_EQUAL(RepetitiveInterval2.getIntervalStartHour(), 5);
202 BOOST_CHECK_EQUAL(RepetitiveInterval2.getIntervalEndHour(), 10);
203 BOOST_CHECK_EQUAL(RepetitiveInterval2.getNRepeats(), 4);
204 BOOST_CHECK(RepetitiveInterval2.getRepeatUnit() == RepetitiveInterval::RepeatUnit::DAY);
205
206 RepetitiveInterval repetitiveInterval3(block2);
207
208 BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval3.getStartDate()), "20150825T000000");
209 BOOST_CHECK_EQUAL(to_iso_string(repetitiveInterval3.getEndDate()), "20150921T000000");
210 BOOST_CHECK_EQUAL(repetitiveInterval3.getIntervalStartHour(), 5);
211 BOOST_CHECK_EQUAL(repetitiveInterval3.getIntervalEndHour(), 10);
212 BOOST_CHECK_EQUAL(repetitiveInterval3.getNRepeats(), 4);
213 BOOST_CHECK(repetitiveInterval3.getRepeatUnit() == RepetitiveInterval::RepeatUnit::DAY);
214}
215
Yingdi Yua717b882016-03-09 17:56:42 -0800216static bool
217check(const RepetitiveInterval& small, const RepetitiveInterval& big)
218{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700219 return (small < big && !(big < small));
Yingdi Yua717b882016-03-09 17:56:42 -0800220}
221
222BOOST_AUTO_TEST_CASE(Comparison)
223{
224 BOOST_CHECK(check(RepetitiveInterval(from_iso_string("20150825T000000"),
225 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700226 5,
227 10,
228 2,
229 RepetitiveInterval::RepeatUnit::DAY),
Yingdi Yua717b882016-03-09 17:56:42 -0800230 RepetitiveInterval(from_iso_string("20150826T000000"),
231 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700232 5,
233 10,
234 2,
235 RepetitiveInterval::RepeatUnit::DAY)));
Yingdi Yua717b882016-03-09 17:56:42 -0800236
237 BOOST_CHECK(check(RepetitiveInterval(from_iso_string("20150825T000000"),
238 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700239 5,
240 10,
241 2,
242 RepetitiveInterval::RepeatUnit::DAY),
Yingdi Yua717b882016-03-09 17:56:42 -0800243 RepetitiveInterval(from_iso_string("20150825T000000"),
244 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700245 6,
246 10,
247 2,
248 RepetitiveInterval::RepeatUnit::DAY)));
Yingdi Yua717b882016-03-09 17:56:42 -0800249
250 BOOST_CHECK(check(RepetitiveInterval(from_iso_string("20150825T000000"),
251 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700252 5,
253 10,
254 2,
255 RepetitiveInterval::RepeatUnit::DAY),
Yingdi Yua717b882016-03-09 17:56:42 -0800256 RepetitiveInterval(from_iso_string("20150825T000000"),
257 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700258 5,
259 11,
260 2,
261 RepetitiveInterval::RepeatUnit::DAY)));
Yingdi Yua717b882016-03-09 17:56:42 -0800262
263 BOOST_CHECK(check(RepetitiveInterval(from_iso_string("20150825T000000"),
264 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700265 5,
266 10,
267 2,
268 RepetitiveInterval::RepeatUnit::DAY),
Yingdi Yua717b882016-03-09 17:56:42 -0800269 RepetitiveInterval(from_iso_string("20150825T000000"),
270 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700271 5,
272 10,
273 3,
274 RepetitiveInterval::RepeatUnit::DAY)));
Yingdi Yua717b882016-03-09 17:56:42 -0800275
276 BOOST_CHECK(check(RepetitiveInterval(from_iso_string("20150825T000000"),
277 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700278 5,
279 10,
280 2,
281 RepetitiveInterval::RepeatUnit::DAY),
Yingdi Yua717b882016-03-09 17:56:42 -0800282 RepetitiveInterval(from_iso_string("20150825T000000"),
283 from_iso_string("20150828T000000"),
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700284 5,
285 10,
286 2,
287 RepetitiveInterval::RepeatUnit::MONTH)));
Yingdi Yua717b882016-03-09 17:56:42 -0800288}
289
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700290BOOST_AUTO_TEST_SUITE_END()
291
292} // namespace tests
Alexander Afanasyev9091d832018-04-18 17:21:08 -0400293} // namespace nac
Zhiyi Zhangcea58d52015-08-26 10:19:56 -0700294} // namespace ndn