swh_graph/front_coded_list/
mod.rs

1// Copyright (C) 2023  The Software Heritage developers
2// See the AUTHORS file at the top-level directory of this distribution
3// License: GNU General Public License version 3, or any later version
4// See top-level LICENSE file for more information
5
6//! Structure to read the same data structures as `it.unimi.dsi.fastutil.chars.CharArrayFrontCodedList`,
7//! which are used to store labels in graphs generated by the Java implementation of swh-graph.
8
9mod read;
10pub use read::*;
11mod write;
12pub use write::*;
13
14#[test]
15fn test_push_int() {
16    let mut v = Vec::new();
17    for i in 0..1000 {
18        // all 1-byte varints, and the small 2-byte varints
19        push_int(&mut v, i).unwrap();
20    }
21    for i in 16000..17000 {
22        // large 2-byte varints and small 3-byte varints
23        push_int(&mut v, i).unwrap();
24    }
25    for i in 2097100..2097200 {
26        // large 3-byte varints and small 4-byte varints
27        push_int(&mut v, i).unwrap();
28    }
29    for i in 268435400..268435455 {
30        // largest varints (4-byte)
31        push_int(&mut v, i).unwrap();
32    }
33
34    let mut r = v.as_ref();
35    for i in 0..1000 {
36        let (i2, r2) = decode_int(r);
37        assert_eq!(
38            i,
39            i2,
40            "Could not read back {i}, got {i2} instead. Start of slice: {:?}",
41            &r[0..2]
42        );
43        r = r2;
44    }
45
46    for i in 16000..17000 {
47        let (i2, r2) = decode_int(r);
48        assert_eq!(
49            i,
50            i2,
51            "Could not read back {i}, got {i2} instead. Start of slice: {:?}",
52            &r[0..3]
53        );
54        r = r2;
55    }
56
57    for i in 2097100..2097200 {
58        let (i2, r2) = decode_int(r);
59        assert_eq!(
60            i,
61            i2,
62            "Could not read back {i}, got {i2} instead. Start of slice: {:?}",
63            &r[0..4]
64        );
65        r = r2;
66    }
67
68    for i in 268435400..268435455 {
69        let (i2, r2) = decode_int(r);
70        assert_eq!(
71            i,
72            i2,
73            "Could not read back {i}, got {i2} instead. Start of slice: {:?}",
74            &r[0..5]
75        );
76        r = r2;
77    }
78
79    assert_eq!(r.len(), 0, "Leftovers in the slice: {r:?}");
80}