java_utils/
lib.rs

1#![warn(clippy::all, missing_docs, trivial_casts, trivial_numeric_casts)]
2//! Implementations of Java classes in pure Rust
3//!
4//! For interop with your old Java applications or the like
5
6/// Implentations from `java.lang.Object`
7///
8/// Only consists of the `HashCode` trait, since the
9/// other methods don't make sense to port to Rust
10pub mod object;
11/// Implements of classes from `java.util`
12pub mod util;
13
14pub use crate::object::HashCode;
15pub use crate::util::Random;
16
17#[cfg(test)]
18mod tests {
19    use crate::Random;
20    use crate::HashCode;
21    const RANDOMS: [i32; 64] = [
22        2992, 3717, 3763, 3320, 3762, 892, 2783, 1165, 321, 2041, 101, 3492, 2864, 3273, 3297, 1097, 619, 2353, 3787, 1722, 3128, 2937, 13, 2184,
23        3016, 1476, 3916, 1858, 3373, 529, 772, 2640, 1335, 1681, 3078, 774, 1148, 1847, 942, 2404, 3308, 3015, 3109, 1705, 3200, 1909, 3658, 1571,
24        2146, 3201, 210, 3536, 1420, 508, 1966, 2000, 3713, 742, 2336, 2204, 2284, 3441, 2341, 4063,
25    ];
26    const RANDOMS2: [i32; 64] = [
27        1130, 3485, 662, 3602, 558, 2973, 2899, 3534, 3023, 2378, 1110, 1529, 3209, 1193, 3207, 610, 3376, 2053, 1746, 3646, 4088, 2404, 138, 712,
28        2448, 1359, 1469, 744, 3838, 1962, 282, 3748, 3875, 3080, 2638, 311, 2934, 1084, 2032, 413, 0, 3776, 3639, 2840, 1359, 1152, 763, 2894, 1316,
29        3727, 800, 2731, 2211, 2522, 400, 1092, 3237, 2462, 34, 871, 3906, 3476, 802, 2946,
30    ];
31    #[allow(clippy::unreadable_literal)]
32    const STRINGS_TO_TEST: [(&str, i32); 4] = [
33        ("", 0),
34        ("hello", 99162322),
35        ("Þɪs ɪn jʉ͡u tiː ɛf eɪt", 1666277289),
36        ("Здразтвуйте", 1364145635),
37    ];
38
39    #[test]
40    fn random_ints_with_seed_4() {
41        let mut r = Random::new(4);
42        for (i, java_r) in RANDOMS.iter().enumerate() {
43            let k = r.next_int(4096);
44            println!("{}", k);
45            assert_eq!(k as i32, *java_r, "{}th iteration", i);
46        }
47    }
48    #[test]
49    fn random_ints_with_seed_4_non_power_of_two() {
50        let mut r = Random::new(4);
51        for (i, java_r) in RANDOMS2.iter().enumerate() {
52            let k = r.next_int(4097);
53            println!("{}", k);
54            assert_eq!(k as i32, *java_r, "{}th iteration", i);
55        }
56    }
57    #[test]
58    fn test_strings() {
59        for (s, n) in STRINGS_TO_TEST.iter() {
60            assert_eq!(s.hash_code(), *n, "string {:?}", s);
61        }
62    }
63    #[test]
64    fn test_ints() {
65        // java.lang.Byte
66        assert_eq!((127u8).hash_code(), 127);
67        assert_eq!((-1i8).hash_code(), -1);
68        assert_eq!((255u8).hash_code(), -1, "{}", (255u8).hash_code());
69        // java.lang.Short
70        assert_eq!((32767i16).hash_code(), 32767);
71        assert_eq!((-1i16).hash_code(), -1);
72        assert_eq!((65535u16).hash_code(), -1);
73        // java.lang.Integer
74        assert_eq!((2_147_483_647i32).hash_code(), 2_147_483_647);
75        assert_eq!((-1i32).hash_code(), -1);
76        assert_eq!((4_294_967_295u32).hash_code(), -1);
77        // java.lang.Long
78        assert_eq!((9_223_372_036_854_775_807i64).hash_code(), -2_147_483_648);
79        assert_eq!((-1i64).hash_code(), 0);
80        assert_eq!((18_446_744_073_709_551_615u64).hash_code(), 0);
81    }
82    #[test]
83    fn test_bools() {
84        assert_eq!(true.hash_code(), 1231);
85        assert_eq!(false.hash_code(), 1237);
86    }
87    use std::{f32, f64};
88    #[test]
89    fn test_floats() {
90        assert_eq!(4124.012f32.hash_code(), 1_166_073_881);
91        assert_eq!(4_124.041_241_235_123f64.hash_code(), -830_930_928);
92        assert_eq!(f32::NAN.hash_code(), 2_143_289_344);
93        assert_eq!(f64::NAN.hash_code(), 2_146_959_360);
94        assert_eq!(f32::NEG_INFINITY.hash_code(), -8_388_608);
95        assert_eq!(f64::NEG_INFINITY.hash_code(), -1_048_576);
96    }
97}