]> git.proxmox.com Git - cargo.git/blob - vendor/lazy_static-0.2.9/tests/test.rs
New upstream version 0.23.0
[cargo.git] / vendor / lazy_static-0.2.9 / tests / test.rs
1 #![cfg_attr(feature="nightly", feature(const_fn))]
2
3 #[macro_use]
4 extern crate lazy_static;
5 use std::collections::HashMap;
6
7 lazy_static! {
8 /// Documentation!
9 pub static ref NUMBER: u32 = times_two(3);
10
11 static ref ARRAY_BOXES: [Box<u32>; 3] = [Box::new(1), Box::new(2), Box::new(3)];
12
13 /// More documentation!
14 #[allow(unused_variables)]
15 #[derive(Copy, Clone, Debug)]
16 pub static ref STRING: String = "hello".to_string();
17
18 static ref HASHMAP: HashMap<u32, &'static str> = {
19 let mut m = HashMap::new();
20 m.insert(0, "abc");
21 m.insert(1, "def");
22 m.insert(2, "ghi");
23 m
24 };
25
26 // This should not compile if the unsafe is removed.
27 static ref UNSAFE: u32 = unsafe {
28 std::mem::transmute::<i32, u32>(-1)
29 };
30
31 // This *should* triggger warn(dead_code) by design.
32 static ref UNUSED: () = ();
33
34 }
35
36 lazy_static! {
37 static ref S1: &'static str = "a";
38 static ref S2: &'static str = "b";
39 }
40 lazy_static! {
41 static ref S3: String = [*S1, *S2].join("");
42 }
43
44 #[test]
45 fn s3() {
46 assert_eq!(&*S3, "ab");
47 }
48
49 fn times_two(n: u32) -> u32 {
50 n * 2
51 }
52
53 #[test]
54 fn test_basic() {
55 assert_eq!(&**STRING, "hello");
56 assert_eq!(*NUMBER, 6);
57 assert!(HASHMAP.get(&1).is_some());
58 assert!(HASHMAP.get(&3).is_none());
59 assert_eq!(&*ARRAY_BOXES, &[Box::new(1), Box::new(2), Box::new(3)]);
60 assert_eq!(*UNSAFE, std::u32::MAX);
61 }
62
63 #[test]
64 fn test_repeat() {
65 assert_eq!(*NUMBER, 6);
66 assert_eq!(*NUMBER, 6);
67 assert_eq!(*NUMBER, 6);
68 }
69
70 #[test]
71 fn test_meta() {
72 // this would not compile if STRING were not marked #[derive(Copy, Clone)]
73 let copy_of_string = STRING;
74 // just to make sure it was copied
75 assert!(&STRING as *const _ != &copy_of_string as *const _);
76
77 // this would not compile if STRING were not marked #[derive(Debug)]
78 assert_eq!(format!("{:?}", STRING), "STRING { __private_field: () }".to_string());
79 }
80
81 mod visibility {
82 lazy_static! {
83 pub static ref FOO: Box<u32> = Box::new(0);
84 static ref BAR: Box<u32> = Box::new(98);
85 }
86
87 #[test]
88 fn sub_test() {
89 assert_eq!(**FOO, 0);
90 assert_eq!(**BAR, 98);
91 }
92 }
93
94 #[test]
95 fn test_visibility() {
96 assert_eq!(*visibility::FOO, Box::new(0));
97 }
98
99 // This should not cause a warning about a missing Copy implementation
100 lazy_static! {
101 pub static ref VAR: i32 = { 0 };
102 }
103
104 #[derive(Copy, Clone, Debug, PartialEq)]
105 struct X;
106 struct Once(X);
107 const ONCE_INIT: Once = Once(X);
108 static DATA: X = X;
109 static ONCE: X = X;
110 fn require_sync() -> X { X }
111 fn transmute() -> X { X }
112 fn __static_ref_initialize() -> X { X }
113 fn test(_: Vec<X>) -> X { X }
114
115 // All these names should not be shadowed
116 lazy_static! {
117 static ref ITEM_NAME_TEST: X = {
118 test(vec![X, Once(X).0, ONCE_INIT.0, DATA, ONCE,
119 require_sync(), transmute(),
120 // Except this, which will sadly be shadowed by internals:
121 // __static_ref_initialize()
122 ])
123 };
124 }
125
126 #[test]
127 fn item_name_shadowing() {
128 assert_eq!(*ITEM_NAME_TEST, X);
129 }
130
131 use std::sync::atomic::AtomicBool;
132 use std::sync::atomic::ATOMIC_BOOL_INIT;
133 use std::sync::atomic::Ordering::SeqCst;
134
135 static PRE_INIT_FLAG: AtomicBool = ATOMIC_BOOL_INIT;
136
137 lazy_static! {
138 static ref PRE_INIT: () = {
139 PRE_INIT_FLAG.store(true, SeqCst);
140 ()
141 };
142 }
143
144 #[test]
145 fn pre_init() {
146 assert_eq!(PRE_INIT_FLAG.load(SeqCst), false);
147 lazy_static::initialize(&PRE_INIT);
148 assert_eq!(PRE_INIT_FLAG.load(SeqCst), true);
149 }
150
151 lazy_static! {
152 static ref LIFETIME_NAME: for<'a> fn(&'a u8) = { fn f(_: &u8) {} f };
153 }
154
155 #[test]
156 fn lifetime_name() {
157 let _ = LIFETIME_NAME;
158 }