]>
git.proxmox.com Git - cargo.git/blob - vendor/lazy_static-0.2.9/tests/test.rs
1 #![cfg_attr(feature="nightly", feature(const_fn))]
4 extern crate lazy_static
;
5 use std
::collections
::HashMap
;
9 pub static ref NUMBER
: u32 = times_two(3);
11 static ref ARRAY_BOXES
: [Box
<u32>; 3] = [Box
::new(1), Box
::new(2), Box
::new(3)];
13 /// More documentation!
14 #[allow(unused_variables)]
15 #[derive(Copy, Clone, Debug)]
16 pub static ref STRING
: String
= "hello".to_string();
18 static ref HASHMAP
: HashMap
<u32, &'
static str> = {
19 let mut m
= HashMap
::new();
26 // This should not compile if the unsafe is removed.
27 static ref UNSAFE
: u32 = unsafe {
28 std
::mem
::transmute
::<i32, u32>(-1)
31 // This *should* triggger warn(dead_code) by design.
32 static ref UNUSED
: () = ();
37 static ref S1
: &'
static str = "a";
38 static ref S2
: &'
static str = "b";
41 static ref S3
: String
= [*S1
, *S2
].join("");
46 assert_eq
!(&*S3
, "ab");
49 fn times_two(n
: u32) -> u32 {
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
);
65 assert_eq
!(*NUMBER
, 6);
66 assert_eq
!(*NUMBER
, 6);
67 assert_eq
!(*NUMBER
, 6);
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 _
!= ©_of_string
as *const _
);
77 // this would not compile if STRING were not marked #[derive(Debug)]
78 assert_eq
!(format
!("{:?}", STRING
), "STRING { __private_field: () }".to_string());
83 pub static ref FOO
: Box
<u32> = Box
::new(0);
84 static ref BAR
: Box
<u32> = Box
::new(98);
90 assert_eq
!(**BAR
, 98);
95 fn test_visibility() {
96 assert_eq
!(*visibility
::FOO
, Box
::new(0));
99 // This should not cause a warning about a missing Copy implementation
101 pub static ref VAR
: i32 = { 0 }
;
104 #[derive(Copy, Clone, Debug, PartialEq)]
107 const ONCE_INIT
: Once
= Once(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 }
115 // All these names should not be shadowed
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()
127 fn item_name_shadowing() {
128 assert_eq
!(*ITEM_NAME_TEST
, X
);
131 use std
::sync
::atomic
::AtomicBool
;
132 use std
::sync
::atomic
::ATOMIC_BOOL_INIT
;
133 use std
::sync
::atomic
::Ordering
::SeqCst
;
135 static PRE_INIT_FLAG
: AtomicBool
= ATOMIC_BOOL_INIT
;
138 static ref PRE_INIT
: () = {
139 PRE_INIT_FLAG
.store(true, SeqCst
);
146 assert_eq
!(PRE_INIT_FLAG
.load(SeqCst
), false);
147 lazy_static
::initialize(&PRE_INIT
);
148 assert_eq
!(PRE_INIT_FLAG
.load(SeqCst
), true);
152 static ref LIFETIME_NAME
: for<'a
> fn(&'a
u8) = { fn f(_: &u8) {} f
};
157 let _
= LIFETIME_NAME
;