blob: 5c37e56ffb6d30fe887daa9d8b91aa8818eb27b9 [file] [log] [blame]
Jiewen Tan99135962014-09-20 02:18:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Jiewen Tan99135962014-09-20 02:18:53 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library 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 * ndn-cxx library 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 ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/in-memory-storage-lfu.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070023
24#include "boost-test.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080025#include "../make-interest-data.hpp"
Jiewen Tan99135962014-09-20 02:18:53 -070026
27namespace ndn {
28namespace util {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Jiewen Tan99135962014-09-20 02:18:53 -070030
Junxiao Shi85d90832016-08-04 03:19:46 +000031using namespace ndn::tests;
32
33BOOST_AUTO_TEST_SUITE(Util)
34BOOST_AUTO_TEST_SUITE(TestInMemoryStorage)
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070035BOOST_AUTO_TEST_SUITE(Lfu)
Jiewen Tan99135962014-09-20 02:18:53 -070036
37BOOST_AUTO_TEST_CASE(FrequencyQueue)
38{
39 InMemoryStorageLfu ims;
40
41 Name name1("/insert/1");
42 shared_ptr<Data> data1 = makeData(name1);
43 ims.insert(*data1);
44
45 Name name2("/insert/2");
46 shared_ptr<Data> data2 = makeData(name2);
47 ims.insert(*data2);
48
49 Name name3("/insert/3");
50 shared_ptr<Data> data3 = makeData(name3);
51 ims.insert(*data3);
52
53 shared_ptr<Interest> interest1 = makeInterest(name1);
54 shared_ptr<Interest> interest2 = makeInterest(name2);
55 shared_ptr<Interest> interest3 = makeInterest(name3);
56
57 ims.find(*interest1);
58 ims.find(*interest2);
59 ims.find(*interest3);
60 ims.find(*interest1);
61 ims.find(*interest3);
62 ims.find(*interest3);
63
64 ims.evictItem();
65 BOOST_CHECK_EQUAL(ims.size(), 2);
66
67 shared_ptr<const Data> found2 = ims.find(*interest2);
Junxiao Shi85d90832016-08-04 03:19:46 +000068 BOOST_CHECK(found2 == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -070069
70 shared_ptr<const Data> found1 = ims.find(*interest1);
71 BOOST_CHECK_EQUAL(found1->getName(), name1);
72 shared_ptr<const Data> found3 = ims.find(*interest3);
73 BOOST_CHECK_EQUAL(found3->getName(), name3);
74}
75
76BOOST_AUTO_TEST_CASE(FrequencyQueue2)
77{
78 InMemoryStorageLfu ims;
79
80 Name name1("/insert/1");
81 shared_ptr<Data> data1 = makeData(name1);
82 ims.insert(*data1);
83
84 Name name2("/insert/2");
85 shared_ptr<Data> data2 = makeData(name2);
86 ims.insert(*data2);
87
88 Name name3("/insert/3");
89 shared_ptr<Data> data3 = makeData(name3);
90 ims.insert(*data3);
91
92 shared_ptr<Interest> interest1 = makeInterest(name1);
93 shared_ptr<Interest> interest2 = makeInterest(name2);
94 shared_ptr<Interest> interest3 = makeInterest(name3);
95
96 ims.find(*interest1);
97 ims.find(*interest2);
98 ims.find(*interest3);
99 ims.find(*interest1);
100 ims.find(*interest3);
101 ims.find(*interest3);
102
103 ims.evictItem();
104 BOOST_CHECK_EQUAL(ims.size(), 2);
105
106 shared_ptr<const Data> found2 = ims.find(*interest2);
Junxiao Shi85d90832016-08-04 03:19:46 +0000107 BOOST_CHECK(found2 == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700108
109 shared_ptr<const Data> found1 = ims.find(*interest1);
110 BOOST_CHECK_EQUAL(found1->getName(), name1);
111 shared_ptr<const Data> found3 = ims.find(*interest3);
112 BOOST_CHECK_EQUAL(found3->getName(), name3);
113
114 Name name4("/insert/4");
115 shared_ptr<Data> data4 = makeData(name4);
116 ims.insert(*data4);
117
118 shared_ptr<Interest> interest4 = makeInterest(name4);
119 ims.find(*interest4);
120
121 ims.evictItem();
122 BOOST_CHECK_EQUAL(ims.size(), 2);
123
124 shared_ptr<const Data> found4 = ims.find(*interest4);
Junxiao Shi85d90832016-08-04 03:19:46 +0000125 BOOST_CHECK(found4 == nullptr);
Jiewen Tan99135962014-09-20 02:18:53 -0700126
127 found1 = ims.find(*interest1);
128 BOOST_CHECK_EQUAL(found1->getName(), name1);
129 found3 = ims.find(*interest3);
130 BOOST_CHECK_EQUAL(found3->getName(), name3);
131}
132
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -0700133BOOST_AUTO_TEST_SUITE_END() // Lfu
Junxiao Shi85d90832016-08-04 03:19:46 +0000134BOOST_AUTO_TEST_SUITE_END() // TestInMemoryStorage
135BOOST_AUTO_TEST_SUITE_END() // Util
Jiewen Tan99135962014-09-20 02:18:53 -0700136
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800137} // namespace tests
Junxiao Shi85d90832016-08-04 03:19:46 +0000138} // namespace util
Jiewen Tan99135962014-09-20 02:18:53 -0700139} // namespace ndn