summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uluru/src/tests.rs
blob: 978e129449e8baf2de4f3ef131b2cbd1d8284ddf (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
extern crate std;

use self::std::vec::Vec;
use super::*;

type TestCache = LRUCache<i32, 4>;

/// Convenience function for test assertions
fn items<T, const N: usize>(cache: &mut LRUCache<T, N>) -> Vec<T>
where
    T: Clone,
{
    let mut v = Vec::new();
    let mut iter = cache.iter_mut();
    while let Some((_idx, val)) = iter.next() {
        v.push(val.clone())
    }
    v
}

#[test]
fn empty() {
    let mut cache = TestCache::default();
    assert_eq!(cache.is_empty(), true);
    assert_eq!(items(&mut cache), []);
    cache.insert(1);
    assert_eq!(cache.is_empty(), false);
}

#[test]
fn len() {
    let mut cache = TestCache::default();
    cache.insert(1);
    assert_eq!(cache.len(), 1);
    assert_eq!(items(&mut cache), [1]);
}

#[test]
fn insert() {
    let mut cache = TestCache::default();
    cache.insert(1);
    assert_eq!(cache.len(), 1);
    cache.insert(2);
    assert_eq!(cache.len(), 2);
    cache.insert(3);
    assert_eq!(cache.len(), 3);
    cache.insert(4);
    assert_eq!(cache.len(), 4);
    assert_eq!(
        items(&mut cache),
        [4, 3, 2, 1],
        "Ordered from most- to least-recent."
    );

    cache.insert(5);
    assert_eq!(cache.len(), 4);
    assert_eq!(
        items(&mut cache),
        [5, 4, 3, 2],
        "Least-recently-used item evicted."
    );

    cache.insert(6);
    cache.insert(7);
    cache.insert(8);
    cache.insert(9);
    assert_eq!(
        items(&mut cache),
        [9, 8, 7, 6],
        "Least-recently-used item evicted."
    );
}

#[test]
fn lookup() {
    let mut cache = TestCache::default();
    cache.insert(1);
    cache.insert(2);
    cache.insert(3);
    cache.insert(4);

    let result = cache.lookup(|x| if *x == 5 { Some(()) } else { None });
    assert_eq!(result, None, "Cache miss.");
    assert_eq!(items(&mut cache), [4, 3, 2, 1], "Order not changed.");

    // Cache hit
    let result = cache.lookup(|x| if *x == 3 { Some(*x * 2) } else { None });
    assert_eq!(result, Some(6), "Cache hit.");
    assert_eq!(
        items(&mut cache),
        [3, 4, 2, 1],
        "Matching item moved to front."
    );
}

#[test]
fn clear() {
    let mut cache = TestCache::default();
    cache.insert(1);
    cache.clear();
    assert_eq!(items(&mut cache), [], "all items evicted");

    cache.insert(1);
    cache.insert(2);
    cache.insert(3);
    cache.insert(4);
    assert_eq!(items(&mut cache), [4, 3, 2, 1]);
    cache.clear();
    assert_eq!(items(&mut cache), [], "all items evicted again");
}

#[test]
fn touch() {
    let mut cache = TestCache::default();

    cache.insert(0);
    cache.insert(1);
    cache.insert(2);
    cache.insert(3);

    cache.touch(|x| *x == 5);

    assert_eq!(items(&mut cache), [3, 2, 1, 0], "Nothing is touched.");

    cache.touch(|x| *x == 1);

    assert_eq!(
        items(&mut cache),
        [1, 3, 2, 0],
        "Touched item is moved to front."
    );
}

#[test]
fn find() {
    let mut cache = TestCache::default();

    cache.insert(0);
    cache.insert(1);
    cache.insert(2);
    cache.insert(3);

    let result = cache.find(|x| *x == 5).copied();

    assert_eq!(result, None);
    assert_eq!(items(&mut cache), [3, 2, 1, 0], "Nothing is touched.");

    let result = cache.find(|x| *x == 1).copied();

    assert_eq!(result, Some(1));
    assert_eq!(
        items(&mut cache),
        [1, 3, 2, 0],
        "Retrieved item is moved to front."
    );
}

#[test]
fn front() {
    let mut cache = TestCache::default();

    assert_eq!(cache.front(), None, "Nothing is in the front.");

    cache.insert(0);
    cache.insert(1);

    assert_eq!(
        cache.front(),
        Some(&1),
        "The last inserted item should be in the front."
    );

    cache.touch(|x| *x == 0);

    assert_eq!(
        cache.front(),
        Some(&0),
        "Touched item should be in the front."
    );
}

#[test]
fn get() {
    let mut cache = TestCache::default();

    assert_eq!(cache.get(0), None, "Nothing at index 0.");

    cache.insert(42);
    cache.insert(64);

    assert_eq!(
        cache.get(0),
        Some(&64),
        "The last inserted item should be at index 0."
    );

    assert_eq!(
        cache.get(1),
        Some(&42),
        "The first inserted item should be at index 1."
    );

    cache.touch(|x| *x == 42);

    assert_eq!(
        cache.get(0),
        Some(&42),
        "The last touched item should be at index 0."
    );

    assert_eq!(
        cache.get(1),
        Some(&64),
        "The previously front item should be at index 1."
    );
}