1 /*
2 * Copyright 2020, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6
7 #include "LRUCacheTest.h"
8
9 #include <stdio.h>
10
11 #include <HashString.h>
12 #include <String.h>
13
14 #include <cppunit/TestCaller.h>
15 #include <cppunit/TestSuite.h>
16
17 #include "LRUCache.h"
18
19
LRUCacheTest()20 LRUCacheTest::LRUCacheTest()
21 {
22 }
23
24
~LRUCacheTest()25 LRUCacheTest::~LRUCacheTest()
26 {
27 }
28
29
30 /*! This tests the insertion of various letters into the map and the subsequent
31 search for those values later using a binary search.
32 */
33
34 void
TestAddWithOverflow()35 LRUCacheTest::TestAddWithOverflow()
36 {
37 LRUCache<HashString, BString> map(5);
38
39 BString tmpKey;
40 BString tmpValue;
41
42 // ----------------------
43 for(char c = 'a'; c <= 'z'; c++) {
44 tmpKey.SetToFormat("%c", c);
45 tmpValue.SetToFormat("%c%c", c, c);
46 map.Put(HashString(tmpKey), tmpValue);
47 }
48 // ----------------------
49
50 CPPUNIT_ASSERT_EQUAL(5, map.Size());
51 // oldest entries have been removed.
52 CPPUNIT_ASSERT_EQUAL(BString(""), map.Get(HashString("a")));
53 CPPUNIT_ASSERT_EQUAL(BString(""), map.Get(HashString("u")));
54 // latter entries have been removed.
55 CPPUNIT_ASSERT_EQUAL(BString("zz"), map.Get(HashString("z")));
56 CPPUNIT_ASSERT_EQUAL(BString("yy"), map.Get(HashString("y")));
57 CPPUNIT_ASSERT_EQUAL(BString("xx"), map.Get(HashString("x")));
58 CPPUNIT_ASSERT_EQUAL(BString("ww"), map.Get(HashString("w")));
59 CPPUNIT_ASSERT_EQUAL(BString("vv"), map.Get(HashString("v")));
60 }
61
62
63 /*! This tests the insertion of various letters into the list, but during the
64 inserts, there are some get operations which will effect which are
65 considered to be the oldest entries.
66 */
67
68 void
TestAddWithOverflowWithGets()69 LRUCacheTest::TestAddWithOverflowWithGets()
70 {
71 LRUCache<HashString, BString> map(3);
72
73 // ----------------------
74 map.Put(HashString("Red"), "Rot");
75 map.Put(HashString("Yellow"), "Gelb");
76 map.Get(HashString("Red"));
77 map.Put(HashString("Green"), "Gruen");
78 map.Put(HashString("Purple"), "Lila");
79 // ----------------------
80
81 CPPUNIT_ASSERT_EQUAL(3, map.Size());
82 CPPUNIT_ASSERT_EQUAL(BString(""), map.Get(HashString("Yellow")));
83 CPPUNIT_ASSERT_EQUAL(BString("Rot"), map.Get(HashString("Red")));
84 CPPUNIT_ASSERT_EQUAL(BString("Gruen"), map.Get(HashString("Green")));
85 CPPUNIT_ASSERT_EQUAL(BString("Lila"), map.Get(HashString("Purple")));
86 }
87
88
89 void
TestRemove()90 LRUCacheTest::TestRemove()
91 {
92 LRUCache<HashString, BString> map(3);
93
94 // control value
95 map.Put(HashString("Town"), "Tirau");
96 map.Put(HashString("Lake"), "Taupo");
97
98 // ----------------------
99 BString resultOcean = map.Remove(HashString("Ocean"));
100 BString resultLake = map.Remove(HashString("Lake"));
101 // ----------------------
102
103 CPPUNIT_ASSERT_EQUAL(1, map.Size());
104 CPPUNIT_ASSERT_EQUAL(BString(""), resultOcean);
105 CPPUNIT_ASSERT_EQUAL(BString("Taupo"), resultLake);
106 CPPUNIT_ASSERT_EQUAL(BString("Tirau"), map.Get(HashString("Town")));
107 CPPUNIT_ASSERT_EQUAL(BString(""), map.Get(HashString("Lake")));
108 CPPUNIT_ASSERT_EQUAL(BString(""), map.Get(HashString("Ocean")));
109 }
110
111
112 /*static*/ void
AddTests(BTestSuite & parent)113 LRUCacheTest::AddTests(BTestSuite& parent)
114 {
115 CppUnit::TestSuite& suite = *new CppUnit::TestSuite(
116 "LRUCacheTest");
117
118 suite.addTest(
119 new CppUnit::TestCaller<LRUCacheTest>(
120 "LRUCacheTest::TestAddWithOverflow",
121 &LRUCacheTest::TestAddWithOverflow));
122 suite.addTest(
123 new CppUnit::TestCaller<LRUCacheTest>(
124 "LRUCacheTest::TestAddWithOverflowWithGets",
125 &LRUCacheTest::TestAddWithOverflowWithGets));
126 suite.addTest(
127 new CppUnit::TestCaller<LRUCacheTest>(
128 "LRUCacheTest::TestRemove",
129 &LRUCacheTest::TestRemove));
130
131 parent.addTest("LRUCacheTest", &suite);
132 }
133