1
1
use ordered_varint::Variable;
2
use rand::prelude::SliceRandom;
3
use rand::thread_rng;
4

            
5
1
fn main() {
6
1
    // Generate some random values
7
1
    let mut rng = thread_rng();
8
1
    let mut original_values = vec![];
9
124
    for power in 1..124 {
10
123
        original_values.push(2_u128.pow(power) - 1);
11
123
    }
12

            
13
1
    original_values.shuffle(&mut rng);
14
1

            
15
1
    // Encode the values.
16
1
    let mut encoded = original_values
17
1
        .iter()
18
123
        .map(|value| value.to_variable_vec().unwrap())
19
1
        .collect::<Vec<_>>();
20
1

            
21
1
    // Sort the original vec and the encoded vec.
22
1
    original_values.sort_unstable();
23
1
    encoded.sort();
24
1

            
25
1
    // Decode the encoded values.
26
1
    let decoded = encoded
27
1
        .iter()
28
123
        .map(|encoded| u128::decode_variable(encoded.as_slice()).unwrap())
29
1
        .collect::<Vec<_>>();
30
1

            
31
1
    // This assert proves that the encoded values are encoded such that sorting
32
1
    // works identially on the encoded and decoded values.
33
1
    assert_eq!(original_values, decoded);
34

            
35
    // Print some summary information
36
123
    let total_encoded_bytes: usize = encoded.iter().map(|encoded| encoded.len()).sum();
37
1
    let total_original_bytes = original_values.len() * std::mem::size_of::<u128>();
38
1
    println!("Original bytes: {total_original_bytes}");
39
1
    println!("Encoded bytes: {total_encoded_bytes}");
40
123
    for (original, encoded) in original_values.into_iter().zip(encoded.into_iter()) {
41
123
        println!("{original} encodes as {encoded:02x?}");
42
123
    }
43
1
}
44

            
45
1
#[test]
46
1
fn runs() {
47
1
    main();
48
1
}