-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveToFrontListTimedTests.java
More file actions
164 lines (145 loc) · 4.35 KB
/
Copy pathMoveToFrontListTimedTests.java
File metadata and controls
164 lines (145 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.rules.Timeout;
public class MoveToFrontListTimedTests {
@Rule
public Timeout globalTimeout = new Timeout(1000);
@Test
public void testSimpleRank() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("the");
l.incrementCount("cheese");
assertEquals(0, l.rank("cheese"));
assertEquals(1, l.rank("the"));
assertEquals(2, l.rank("bacon"));
}
@Test
public void testSimpleMoveToFront() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("the");
l.incrementCount("cheese");
assertEquals(0, l.rank("cheese"));
assertEquals(1, l.rank("the"));
assertEquals(2, l.rank("bacon"));
l.incrementCount("the");
assertEquals(0, l.rank("the"));
assertEquals(1, l.rank("cheese"));
assertEquals(2, l.rank("bacon"));
}
@Test
public void testFindSimpleMoveToFront() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("the");
l.incrementCount("cheese");
assertNotNull( l.find("cheese"));
assertNotNull( l.find("the"));
assertNull( l.find("bacon"));
}
@Test
public void testSpliceOut() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("the");
l.incrementCount("cheese");
assertNotNull( l.find("cheese"));
assertNotNull( l.find("the"));
assertNull( l.find("bacon"));
assertEquals(2, l.size());
l.incrementCount("the");
StringCountElement s = l.find("cheese");
l.spliceOut(s);
assertNull( l.find("cheese"));
assertNotNull( l.find("the"));
assertNull( l.find("bacon"));
assertEquals(1, l.size());
}
@Test
public void testSpliceOutMiddle() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("the");
l.incrementCount("cheese");
l.incrementCount("cat");
assertNotNull( l.find("cheese"));
assertNotNull( l.find("the"));
assertNotNull( l.find("cat"));
assertNull( l.find("bacon"));
assertEquals(3, l.size());
l.incrementCount("cheese");
StringCountElement s = l.find("cheese");
l.spliceOut(s);
assertNull( l.find("cheese"));
assertNotNull( l.find("the"));
assertNotNull( l.find("cat"));
assertNull( l.find("bacon"));
assertEquals(2, l.size());
}
@Test
public void testSpliceOutTail() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("the");
l.incrementCount("cheese");
l.incrementCount("cat");
assertNotNull( l.find("cheese"));
assertNotNull( l.find("the"));
assertNotNull( l.find("cat"));
assertNull( l.find("bacon"));
assertEquals(3, l.size());
StringCountElement s = l.find("the");
l.spliceOut(s);
assertNotNull( l.find("cheese"));
assertNull( l.find("the"));
assertNotNull( l.find("cat"));
assertNull( l.find("bacon"));
assertEquals(2, l.size());
}
@Test
public void testSpliceHead() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("cheese");
l.incrementCount("potato");
assertEquals(2, l.size());
assertNull( l.find("the"));
StringCountElement s = new StringCountElement();
s.key = "the";
s.count = 1;
l.spliceIn(s, 0);
StringCountElement one = l.find("the");
StringCountElement two = l.find("potato");
StringCountElement three = l.find("cheese");
assertNotNull(one);
assertNotNull(two);
assertNotNull(three);
// assertNull(one.prev); // don't check -- if using sentinels, this may not be true
// assertNull(three.next); // don't check -- if using sentinels, this may not be true
assertEquals(two, one.next);
assertEquals(three, two.next);
assertEquals(two, three.prev);
assertEquals(one, two.prev);
assertEquals(3, l.size());
}
@Test
public void testSpliceMiddle() {
MoveToFrontList l = new MoveToFrontList();
l.incrementCount("cheese");
l.incrementCount("potato");
assertEquals(2, l.size());
assertNull( l.find("the"));
StringCountElement s = new StringCountElement();
s.key = "the";
s.count = 1;
l.spliceIn(s, 1);
StringCountElement one = l.find("potato");
StringCountElement two = l.find("the");
StringCountElement three = l.find("cheese");
assertNotNull(one);
assertNotNull(two);
assertNotNull(three);
// assertNull(one.prev); // don't check -- if using sentinels, this may not be true
// assertNull(three.next); // don't check -- if using sentinels, this may not be true
assertEquals(two, one.next);
assertEquals(three, two.next);
assertEquals(two, three.prev);
assertEquals(one, two.prev);
assertEquals(3, l.size());
}
}