@@ -129,3 +129,76 @@ impl<V: ?Sized + fmt::Debug + Any + 'static> Clone for PtrMapCell<V> where Box<V
129129 cell
130130 }
131131}
132+
133+ #[ cfg( test) ]
134+ mod test {
135+ use std:: any:: TypeId ;
136+ use super :: * ;
137+
138+ #[ test]
139+ fn test_opt_cell_set ( ) {
140+ let one: OptCell < u32 > = OptCell :: new ( None ) ;
141+ one. set ( 1 ) ;
142+ assert_eq ! ( * one, Some ( 1 ) ) ;
143+ }
144+
145+ #[ test]
146+ fn test_opt_cell_clone ( ) {
147+ let one: OptCell < u32 > = OptCell :: new ( Some ( 3 ) ) ;
148+ let stored = * one. clone ( ) ;
149+ assert_eq ! ( stored, Some ( 3 ) ) ;
150+ }
151+
152+
153+ #[ test]
154+ fn test_ptr_map_cell_none ( ) {
155+ let type_id = TypeId :: of :: < u32 > ( ) ;
156+ let pm: PtrMapCell < u32 > = PtrMapCell :: new ( ) ;
157+ assert_eq ! ( pm. get( type_id) , None ) ;
158+ }
159+
160+ #[ test]
161+ fn test_ptr_map_cell_one ( ) {
162+ let type_id = TypeId :: of :: < String > ( ) ;
163+ let pm: PtrMapCell < String > = PtrMapCell :: new ( ) ;
164+ unsafe { pm. insert ( type_id, Box :: new ( "a" . to_string ( ) ) ) ; }
165+ assert_eq ! ( pm. get( type_id) , Some ( & "a" . to_string( ) ) ) ;
166+ assert_eq ! ( unsafe { pm. one( ) } , "a" ) ;
167+ }
168+
169+ #[ test]
170+ fn test_ptr_map_cell_two ( ) {
171+ let type_id = TypeId :: of :: < String > ( ) ;
172+ let type_id2 = TypeId :: of :: < Vec < u8 > > ( ) ;
173+ let pm: PtrMapCell < String > = PtrMapCell :: new ( ) ;
174+ unsafe { pm. insert ( type_id, Box :: new ( "a" . to_string ( ) ) ) ; }
175+ unsafe { pm. insert ( type_id2, Box :: new ( "b" . to_string ( ) ) ) ; }
176+ assert_eq ! ( pm. get( type_id) , Some ( & "a" . to_string( ) ) ) ;
177+ assert_eq ! ( pm. get( type_id2) , Some ( & "b" . to_string( ) ) ) ;
178+ }
179+
180+ #[ test]
181+ fn test_ptr_map_cell_many ( ) {
182+ let id1 = TypeId :: of :: < String > ( ) ;
183+ let id2 = TypeId :: of :: < Vec < u8 > > ( ) ;
184+ let id3 = TypeId :: of :: < OptCell < String > > ( ) ;
185+ let pm: PtrMapCell < String > = PtrMapCell :: new ( ) ;
186+ unsafe { pm. insert ( id1, Box :: new ( "a" . to_string ( ) ) ) ; }
187+ unsafe { pm. insert ( id2, Box :: new ( "b" . to_string ( ) ) ) ; }
188+ unsafe { pm. insert ( id3, Box :: new ( "c" . to_string ( ) ) ) ; }
189+ assert_eq ! ( pm. get( id1) , Some ( & "a" . to_string( ) ) ) ;
190+ assert_eq ! ( pm. get( id2) , Some ( & "b" . to_string( ) ) ) ;
191+ assert_eq ! ( pm. get( id3) , Some ( & "c" . to_string( ) ) ) ;
192+ }
193+
194+
195+ #[ test]
196+ fn test_ptr_map_cell_clone ( ) {
197+ let type_id = TypeId :: of :: < String > ( ) ;
198+ let pm: PtrMapCell < String > = PtrMapCell :: new ( ) ;
199+ unsafe { pm. insert ( type_id, Box :: new ( "a" . to_string ( ) ) ) ; }
200+ let cloned = pm. clone ( ) ;
201+ assert_eq ! ( cloned. get( type_id) , Some ( & "a" . to_string( ) ) ) ;
202+ }
203+
204+ }
0 commit comments