| 1 | use core::sync::atomic::AtomicU8; |
| 2 | use core::sync::atomic::Ordering; |
| 3 | use enclose::enc; |
| 4 | |
| 5 | #[test] |
| 6 | fn clone_count_operations() { |
| 7 | static CHECK_COUNT_OPERATIONS: AtomicU8 = AtomicU8::new(0); |
| 8 | struct CleverType; |
| 9 | |
| 10 | impl Clone for CleverType { |
| 11 | #[inline] |
| 12 | fn clone(&self) -> Self { |
| 13 | let _del = CHECK_COUNT_OPERATIONS.fetch_add(1, Ordering::SeqCst); |
| 14 | |
| 15 | CleverType |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | let data = CleverType; |
| 20 | |
| 21 | assert_eq!(CHECK_COUNT_OPERATIONS.load(Ordering::SeqCst), 0); //Checking the number of operations |
| 22 | enc!((data => d) || { |
| 23 | assert_eq!(CHECK_COUNT_OPERATIONS.load(Ordering::SeqCst), 1); //Checking the number of operations |
| 24 | |
| 25 | std::thread::spawn(enc!((d,) move || { |
| 26 | assert_eq!(CHECK_COUNT_OPERATIONS.load(Ordering::SeqCst), 2); //Checking the number of operations |
| 27 | |
| 28 | enc!((d) || { |
| 29 | enc!((d => _d, d => _d2) move || { //'Move 'is not mandatory here, but since the semantics of the macro are different, we will leave it here for tests. |
| 30 | |
| 31 | })(); |
| 32 | })(); |
| 33 | assert_eq!(CHECK_COUNT_OPERATIONS.load(Ordering::SeqCst), 5); //Checking the number of operations |
| 34 | |
| 35 | |
| 36 | })).join().unwrap(); |
| 37 | |
| 38 | })(); |
| 39 | |
| 40 | let _manclone = data.clone(); |
| 41 | |
| 42 | //Checking the number of operations, |
| 43 | //Closure = 2 |
| 44 | assert_eq!(CHECK_COUNT_OPERATIONS.load(Ordering::SeqCst), 6); |
| 45 | } |