/* ***************************************************************************** MIT License Copyright (c) 2020 trindadegm Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ***************************************************************************** */ use voidbox::black_box::BlackBox; struct DummyDropZST { } impl Drop for DummyDropZST { fn drop(&mut self) { println!("Dropping DummyDropZST {:?}", self as *const DummyDropZST); } } struct DummyDropST { dummy_text: String, } impl Drop for DummyDropST { fn drop(&mut self) { println!("Dropping DummyDropST {:?}: {}", self as *const DummyDropST, self.dummy_text); } } #[test] fn creation_and_destruction_test() { let mut boxed: BlackBox = BlackBox::new(200_u32); assert_eq!(*boxed.get_ref::().unwrap(), 200_u32); *boxed.get_mut_ref::().unwrap() = 300; assert_ne!(*boxed.get_ref::().unwrap(), 200_u32); assert_eq!(*boxed.get_ref::().unwrap(), 300_u32); } #[test] fn dropping_prints_test() { let _boxed1: BlackBox = BlackBox::new(DummyDropZST { }); let _boxed2: BlackBox = BlackBox::new(DummyDropZST { }); let _boxed3: BlackBox = BlackBox::new(DummyDropZST { }); let _boxed4: BlackBox = BlackBox::new(DummyDropZST { }); let _boxed5: BlackBox = BlackBox::new(DummyDropST { dummy_text: String::from("Some dummy text 5") }); let _boxed6: BlackBox = BlackBox::new(DummyDropST { dummy_text: String::from("Some dummy text 6") }); let _boxed7: BlackBox = BlackBox::new(DummyDropST { dummy_text: String::from("Some dummy text 7") }); let _boxed8: BlackBox = BlackBox::new(DummyDropST { dummy_text: String::from("Some dummy text 8") }); } #[test] fn wrong_unboxing_test() { let mut boxed: BlackBox = BlackBox::new(128_u32); assert!(boxed.get_ref::().is_ok()); assert!(boxed.get_ref::().is_err()); assert!(boxed.get_ref::().is_err()); assert!(boxed.get_ref::>().is_err()); assert!(boxed.get_mut_ref::().is_ok()); assert!(boxed.get_mut_ref::().is_err()); assert!(boxed.get_mut_ref::().is_err()); assert!(boxed.get_mut_ref::>().is_err()); } #[test] fn unsafe_methods_test() { let mut boxed: BlackBox = BlackBox::new(512_u64); unsafe { assert_eq!(*boxed.get_ref_unchecked::(), 512_u64); } unsafe { assert_eq!(*boxed.get_mut_ref_unchecked::(), 512_u64); } } #[test] fn is_of_test() { let boxed: BlackBox = BlackBox::new(128_u32); assert!(boxed.is_of::()); assert!(!boxed.is_of::()); assert!(!boxed.is_of::()); assert!(!boxed.is_of::>()); }