liserk_ope/
lib.rs

1pub mod hgd;
2pub mod ope;
3pub mod simplified_version;
4pub mod stats;
5pub mod utils;
6
7#[cfg(test)]
8mod tests {
9    use crate::ope::{Ope, ValueRange};
10
11    #[test]
12    fn test_value_range_new() {
13        assert!(ValueRange::new(5, 3).is_err());
14        assert_eq!(ValueRange::new(3, 5), Ok(ValueRange { start: 3, end: 5 }));
15    }
16
17    #[test]
18    fn test_value_range_size() {
19        let range = ValueRange::new(3, 5).unwrap();
20        assert_eq!(range.size(), 3);
21
22        let range = ValueRange::new(-3, -1).unwrap();
23        assert_eq!(range.size(), 3);
24    }
25
26    #[test]
27    fn test_value_range_contains() {
28        let range = ValueRange::new(3, 5).unwrap();
29        assert!(range.contains(4));
30        assert!(!range.contains(6));
31    }
32
33    #[test]
34    #[should_panic]
35    fn test_ope_new() {
36        let key = b"test_key";
37        assert!(Ope::new(key, Some(ValueRange::new(5, 3).unwrap()), None).is_err());
38        assert!(Ope::new(key, None, None).is_err());
39    }
40
41    #[test]
42    fn test_ope_encrypt_decrypt() {
43        let key = b"test_key";
44        let ope = Ope::new(key, None, None).unwrap();
45
46        let plaintext = 5;
47        let ciphertext = ope.encrypt(plaintext).unwrap();
48        assert!(ope.out_range.contains(ciphertext));
49
50        let decrypted = ope.decrypt(ciphertext).unwrap();
51        assert_eq!(plaintext, decrypted);
52    }
53
54    #[test]
55    fn test_ope_order_preserving_encryption() {
56        let key = b"test_key";
57        let ope = Ope::new(key, None, None).unwrap();
58
59        let mut plaintexts = vec![3, 1, 4, 5, 9, 2, 6, 5];
60        let mut ciphertexts = Vec::new();
61
62        // Encrypt each plaintext value
63        for &plaintext in plaintexts.iter() {
64            let ciphertext = ope.encrypt(plaintext).unwrap();
65            ciphertexts.push(ciphertext);
66        }
67
68        // Sort both plaintexts and ciphertexts
69        plaintexts.sort();
70        ciphertexts.sort();
71
72        // Check if the order is preserved
73        for (index, &plaintext) in plaintexts.iter().enumerate() {
74            let original_ciphertext = ope.encrypt(plaintext).unwrap();
75            assert_eq!(original_ciphertext, ciphertexts[index]);
76        }
77    }
78}