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
mod common;
mod id_generator;

#[cfg(feature = "ptr")]
pub mod ptr;

#[cfg(feature = "time")]
pub mod time;

#[cfg(feature = "sync")]
pub mod sync;

#[cfg(feature = "fs")]
pub mod fs;

pub use common::*;
pub use id_generator::*;

#[cfg(test)]
mod test {
    use crate::*;
    use std::sync::Arc;

    #[test]
    fn test_hex() {
        let data: Vec<u8> = vec![101, 201, 30, 40, 50, 60, 70, 80];
        assert_eq!(
            "65c91e28323c4650",
            data.to_hex().as_str(),
            "test_hex failed"
        )
    }
    #[test]
    fn test_base64_url() {
        let data = "@hello, wo/-rld*";
        let ciphertext = data.base64_encode_url().expect("base64_encode_url error");
        assert_eq!(
            r#"QGhlbGxvLCB3by8tcmxkKg"#,
            ciphertext.as_str(),
            "test_base64_url encode failed"
        );
        let plaintext = ciphertext
            .try_base64_decode_url()
            .expect("try_base64_decode_url error");
        assert_eq!(Vec::from(data), plaintext, "test_base64_url decode failed");
    }

    #[test]
    fn test_base64_std() {
        let data = "@hello, wo/-rld*";
        let ciphertext = data.base64_encode_std();
        assert_eq!(
            r#"QGhlbGxvLCB3by8tcmxkKg=="#,
            ciphertext.as_str(),
            "test_base64_std encode failed"
        );
        let plaintext = ciphertext
            .try_base64_decode_std()
            .expect("try_base64_decode_std error");
        assert_eq!(Vec::from(data), plaintext, "test_base64_std decode failed");
    }

    #[test]
    fn test_md5() {
        let data = "hello world";
        let md5 = data.md5().to_hex();
        assert_eq!(
            r#"5eb63bbbe01eeed093cb22bb8f5acdc3"#,
            md5.as_str(),
            "test_md5 failed"
        );
    }

    #[test]
    fn test_pf() {
        let data = 10u8;
        assert_eq!(data.ok(), Ok::<u8, ()>(10), "test_pf ok");
        assert_eq!(data.err(), Err::<(), u8>(10), "test_pf err");
        assert_eq!(data.to_box(), Box::new(10), "test_pf box");
        assert_eq!(data.arc(), Arc::new(10), "test_pf arc");
        assert_eq!(data.some(), Some(10), "test_pf option");
    }

    #[test]
    fn test_ptr() {
        let src = 129u8;
        let des = ptr::force_arc_to_var::<_, i8>(Arc::new(src));
        assert_eq!(des, -127, "force_arc_to_var failed");
        let des = ptr::force_box_to_var::<_, i8>(src.to_box());
        assert_eq!(des, -127, "force_box_to_var failed");
    }

    #[test]
    fn test_snowflake() {
        let id = snowflake_id();
        println!("id --> {}", id);
    }

    #[test]
    fn test_uuid_v4_v5() {
        let uuid = uuid::v4();
        println!("uuid v4 --> {}", uuid);
        let uuid = uuid::v5(uuid::UuidV5Namespace::DNS, b"hello world");
        println!("uuid v5 --> {}", uuid);
    }

    #[test]
    fn test_time_utc_timestamp() {
        let ts = time::utc_timestamp();
        println!("{}", ts);
        let mts = time::utc_timestamp_millis();
        println!("{}", mts)
    }

    #[test]
    fn test_less_lock() {
        let lkv = sync::CopyLock::new(0);
        let one = lkv.share();
        assert_eq!(Arc::new(0), one, "test_less_lock one failed");
        lkv.update(|i| i + 1);
        assert_eq!(Arc::new(1), lkv.share(), "test_less_lock two failed");
        lkv.update(|i| i + 1);
        assert_eq!(Arc::new(2), lkv.share(), "test_less_lock three failed");
    }

    // #[derive(Clone, Eq, PartialEq, Debug, Default)]
    // struct NLTest(usize);
    // impl Drop for NLTest {
    //     fn drop(&mut self) {
    //         println!("drop NLTest {}", self.0)
    //     }
    // }

    // #[test]
    // fn test_null_lock(){
    //     let nl = NullLock::<NLTest>::new();
    //     let nu = nl.get();
    //     assert_eq!(None,nu,"test_null_lock null failed");
    //
    //     let nu = nl.get_unwrap();
    //     assert_eq!(NLTest::default(),nu,"test_null_lock default failed");
    //
    //     nl.init(NLTest(1));
    //     let i = nl.map(|x| {
    //         x.0 + 1
    //     }).unwrap();
    //     assert_eq!(2,i,"test_null_lock non null failed")
    // }
}