File: test.rs

package info (click to toggle)
rust-linear-map 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 176 kB
  • sloc: sh: 13; makefile: 4
file content (278 lines) | stat: -rw-r--r-- 7,012 bytes parent folder | download
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#[macro_use]
extern crate linear_map;

use linear_map::LinearMap;
use linear_map::Entry::{Occupied, Vacant};

const TEST_CAPACITY: usize = 10;

#[test]
fn test_new() {
    let map: LinearMap<i32, i32> = LinearMap::new();
    assert_eq!(map.capacity(), 0);
    assert_eq!(map.len(), 0);
    assert!(map.is_empty());
}

#[test]
fn test_with_capacity() {
    let map: LinearMap<i32, i32> = LinearMap::with_capacity(TEST_CAPACITY);
    assert!(map.capacity() >= TEST_CAPACITY);
}

#[test]
fn test_capacity() {
    let mut map = LinearMap::new();
    map.insert(1, 2);
    assert!(map.capacity() >= 1);
    map.remove(&1);
    assert!(map.capacity() >= 1);
    map.reserve(TEST_CAPACITY);
    let capacity = map.capacity();
    assert!(capacity >= TEST_CAPACITY);
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    assert_eq!(capacity, map.capacity());
}

#[test]
fn test_reserve() {
    let mut map = LinearMap::new();
    map.reserve(TEST_CAPACITY);
    assert!(map.capacity() >= TEST_CAPACITY);
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    map.reserve(TEST_CAPACITY);
    assert!(map.capacity() >= 2 * TEST_CAPACITY);

    let mut map = LinearMap::new();
    map.reserve(TEST_CAPACITY);
    assert!(map.capacity() >= TEST_CAPACITY);
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    map.reserve(TEST_CAPACITY);
    assert!(map.capacity() >= 2 * TEST_CAPACITY);
}

#[test]
fn test_shrink_to_fit() {
    let mut map = LinearMap::new();
    map.shrink_to_fit();
    assert_eq!(map.capacity(), 0);
    map.reserve(TEST_CAPACITY);
    map.shrink_to_fit();
    assert_eq!(map.capacity(), 0);
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    map.shrink_to_fit();
    assert_eq!(map.len(), TEST_CAPACITY);
    assert!(map.capacity() >= TEST_CAPACITY);
}

#[test]
fn test_len_and_is_empty() {
    let mut map = LinearMap::new();
    assert_eq!(map.len(), 0);
    assert!(map.is_empty());
    map.insert(100, 100);
    assert_eq!(map.len(), 1);
    assert!(!map.is_empty());
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    assert_eq!(map.len(), 1 + TEST_CAPACITY);
    assert!(!map.is_empty());
    assert!(map.remove(&100).is_some());
    assert_eq!(map.len(), TEST_CAPACITY);
    assert!(!map.is_empty());
}

#[test]
fn test_clear() {
    let mut map = LinearMap::new();
    map.clear();
    assert_eq!(map.len(), 0);
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    map.clear();
    assert_eq!(map.len(), 0);
    assert!(map.capacity() > 0);
}

#[test]
fn test_iterators() {
    const ONE:   i32 = 0b0001;
    const TWO:   i32 = 0b0010;
    const THREE: i32 = 0b0100;
    const FOUR:  i32 = 0b1000;
    const ALL:   i32 = 0b1111;
    let mut map = LinearMap::new();
    assert!(map.insert(ONE, TWO).is_none());
    assert!(map.insert(TWO, THREE).is_none());
    assert!(map.insert(THREE, FOUR).is_none());
    assert!(map.insert(FOUR, ONE).is_none());

    {
        let mut result_k = 0;
        let mut result_v = 0;
        for (&k, &v) in map.iter() {
            result_k ^= k;
            result_v ^= v;
            assert_eq!(((k << 1) & ALL) | ((k >> 3) & ALL), v);
        }
        assert_eq!(result_k, ALL);
        assert_eq!(result_v, ALL);
    }
    {
        let mut result_k = 0;
        let mut result_v = 0;
        for (&k, &mut v) in map.iter_mut() {
            result_k ^= k;
            result_v ^= v;
            assert_eq!(((k << 1) & ALL) | ((k >> 3) & ALL), v);
        }
        assert_eq!(result_k, ALL);
        assert_eq!(result_v, ALL);
    }
    {
        let mut result = 0;
        for &k in map.keys() {
            result ^= k;
        }
        assert_eq!(result, ALL);
    }
    {
        let mut result = 0;
        for &v in map.values() {
            result ^= v;
        }
        assert_eq!(result, ALL);
    }
}

#[test]
fn test_insert_remove_get() {
    let mut map = LinearMap::new();
    assert!(map.insert(100, 101).is_none());
    assert!(map.contains_key(&100));
    assert_eq!(map.get(&100), Some(&101));
    assert_eq!(map.get_mut(&100), Some(&mut 101));
    for i in 0..TEST_CAPACITY as i32 {
        assert!(map.insert(i, i).is_none());
    }
    assert_eq!(map.insert(100, 102), Some(101));
    assert_eq!(map.remove(&100), Some(102));
    assert_eq!(map.remove(&100), None);
    assert_eq!(map.remove(&1000), None);
}

#[test]
fn test_entry() {
    let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];

    let mut map = LinearMap::new();

    for &(k, v) in &xs {
        map.insert(k, v);
    }

    // Existing key (insert)
    match map.entry(1) {
        Vacant(_) => unreachable!(),
        Occupied(mut view) => {
            assert_eq!(view.get(), &10);
            assert_eq!(view.insert(100), 10);
        }
    }
    assert_eq!(map.get(&1).unwrap(), &100);
    assert_eq!(map.len(), 6);


    // Existing key (update)
    match map.entry(2) {
        Vacant(_) => unreachable!(),
        Occupied(mut view) => {
            let v = view.get_mut();
            let new_v = (*v) * 10;
            *v = new_v;
        }
    }
    assert_eq!(map.get(&2).unwrap(), &200);
    assert_eq!(map.len(), 6);

    // Existing key (take)
    match map.entry(3) {
        Vacant(_) => unreachable!(),
        Occupied(view) => {
            assert_eq!(view.remove(), 30);
        }
    }
    assert_eq!(map.get(&3), None);
    assert_eq!(map.len(), 5);


    // Inexistent key (insert)
    match map.entry(10) {
        Occupied(_) => unreachable!(),
        Vacant(view) => {
            assert_eq!(*view.insert(1000), 1000);
        }
    }
    assert_eq!(map.get(&10).unwrap(), &1000);
    assert_eq!(map.len(), 6);
}

#[test]
fn test_eq() {
    let kvs = vec![('a', 1), ('b', 2), ('c', 3)];

    let mut m1: LinearMap<_, _> = kvs.clone().into_iter().collect();
    let m2: LinearMap<_, _> = kvs.into_iter().rev().collect();
    assert_eq!(m1, m2);

    m1.insert('a', 11);
    assert!(m1 != m2);

    m1.insert('a', 1);
    assert_eq!(m1, m2);

    m1.remove(&'a');
    assert!(m1 != m2);
}

#[test]
fn test_macro() {
    let names = linear_map!{
        1 => "one",
        2 => "two",
    };
    assert_eq!(names.len(), 2);
    assert_eq!(names.capacity(), 2);
    assert_eq!(names[&1], "one");
    assert_eq!(names[&2], "two");
    assert_eq!(names.get(&3), None);

    let empty: LinearMap<i32, i32> = linear_map!{};
    assert_eq!(empty.len(), 0);
    assert_eq!(empty.capacity(), 0);

    let _nested_compiles = linear_map!{
        1 => linear_map!{0 => 1 + 2,},
        2 => linear_map!{1 => 1,},
    };
}

#[test]
fn test_retain() {
    let mut map: LinearMap<isize, isize> = (0..100).map(|x|(x, x*10)).collect();
    map.retain(|&k, _| k % 2 == 0);
    assert_eq!(map.len(), 50);
    assert_eq!(map[&2], 20);
    assert_eq!(map[&4], 40);
    assert_eq!(map[&6], 60);
}