| 1 | use enclose::enc; |
| 2 | use enclose::enclose; |
| 3 | use std::sync::Arc; |
| 4 | use std::sync::Mutex; |
| 5 | use std::sync::MutexGuard; |
| 6 | use std::sync::RwLock; |
| 7 | use std::thread; |
| 8 | |
| 9 | struct MutexSafeData(Mutex<usize>); |
| 10 | |
| 11 | impl MutexSafeData { |
| 12 | #[inline] |
| 13 | pub fn new(def: usize) -> Self { |
| 14 | MutexSafeData(Mutex::new(def)) |
| 15 | } |
| 16 | #[allow(dead_code)] |
| 17 | #[inline] |
| 18 | pub fn set(&self, size: usize) { |
| 19 | *self.get_mut() = size; |
| 20 | } |
| 21 | #[allow(dead_code)] |
| 22 | #[inline] |
| 23 | pub fn add(&self, size: usize) { |
| 24 | *self.get_mut() += size; |
| 25 | } |
| 26 | #[inline] |
| 27 | pub fn get_mut(&self) -> MutexGuard<usize> { |
| 28 | match self.0.lock() { |
| 29 | Ok(a) => a, |
| 30 | Err(e) => e.into_inner(), |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn clone_enc_macros() { |
| 37 | let mutex_data = Arc::new(MutexSafeData::new(0)); |
| 38 | let mutex_data2 = Arc::new(MutexSafeData::new(0)); |
| 39 | |
| 40 | // one value |
| 41 | enc!( |
| 42 | (mutex_data) || { |
| 43 | mutex_data.add(1); |
| 44 | } |
| 45 | )(); |
| 46 | |
| 47 | // two value |
| 48 | enc!( |
| 49 | (mutex_data, mutex_data2) || { |
| 50 | mutex_data.add(1); |
| 51 | mutex_data2.add(1); |
| 52 | } |
| 53 | )(); |
| 54 | |
| 55 | // one alias |
| 56 | enc!((mutex_data => m1) || { |
| 57 | m1.add(1); |
| 58 | })(); |
| 59 | |
| 60 | // two alias |
| 61 | enc!((mutex_data => m1, mutex_data2 => m2) || { |
| 62 | m1.add(1); |
| 63 | m2.add(1); |
| 64 | })(); |
| 65 | |
| 66 | // combination |
| 67 | enc!((mutex_data, mutex_data2 => m2) || { |
| 68 | mutex_data.add(1); |
| 69 | m2.add(1); |
| 70 | })(); |
| 71 | |
| 72 | assert_eq!(*mutex_data.get_mut(), 5); |
| 73 | assert_eq!(*mutex_data2.get_mut(), 3); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_unk_operations() { |
| 78 | let mutex_data = Arc::new(MutexSafeData::new(0)); |
| 79 | let mutex_data2 = Arc::new(MutexSafeData::new(0)); |
| 80 | |
| 81 | // one |
| 82 | enc!((@mutex_data.clone() => m1) || { |
| 83 | m1.add(1); |
| 84 | })(); |
| 85 | |
| 86 | // two |
| 87 | enc!((@mutex_data.clone() => m1, @mutex_data2.clone() => m2) || { |
| 88 | m1.add(1); |
| 89 | m2.add(1); |
| 90 | })(); |
| 91 | |
| 92 | // unk operations |
| 93 | enc!((@{ mutex_data2.set(1); mutex_data2.clone() } => m2) || { |
| 94 | m2.add(1); |
| 95 | })(); |
| 96 | |
| 97 | assert_eq!(*mutex_data.get_mut(), 2); |
| 98 | assert_eq!(*mutex_data2.get_mut(), 2); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn threadpool_twovalue_and_alias() { |
| 103 | let v1 = Arc::new(MutexSafeData::new(1)); |
| 104 | assert_eq!(*v1.get_mut(), 1); |
| 105 | |
| 106 | let v2 = Arc::new(RwLock::new((0, 2, 3, 4))); |
| 107 | |
| 108 | let count_thread = 5; |
| 109 | let mut join_all = Vec::with_capacity(count_thread); |
| 110 | |
| 111 | for _a in 0..count_thread { |
| 112 | join_all.push({ |
| 113 | thread::spawn(enclose!((v1, v2, count_thread => alias_cthreads,) move || { |
| 114 | assert_eq!(alias_cthreads, 5); |
| 115 | |
| 116 | *v1.get_mut() += 1; |
| 117 | |
| 118 | let mut lock_v2 = match v2.write() { |
| 119 | Ok(a) => a, |
| 120 | Err(e) => e.into_inner(), |
| 121 | }; |
| 122 | lock_v2.0 += 1; |
| 123 | })) |
| 124 | }); |
| 125 | } |
| 126 | for a in join_all { |
| 127 | a.join().unwrap(); |
| 128 | } |
| 129 | assert_eq!(*v1.get_mut(), 6); |
| 130 | { |
| 131 | let lock_v2 = match v2.write() { |
| 132 | Ok(a) => a, |
| 133 | Err(e) => e.into_inner(), |
| 134 | }; |
| 135 | assert_eq!(lock_v2.0, 5); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn selfalias() { |
| 141 | #[derive(Debug, Clone, PartialEq, PartialOrd)] |
| 142 | struct CheckData { |
| 143 | a: u32, |
| 144 | } |
| 145 | |
| 146 | impl CheckData { |
| 147 | #[inline] |
| 148 | pub const fn new(a: u32) -> Self { |
| 149 | Self { a } |
| 150 | } |
| 151 | |
| 152 | pub fn calculate(&self, mul_num: &u32) -> u32 { |
| 153 | enc!((*mul_num, self.a => mut num0,) move || { |
| 154 | // (*mul_num, self.a => mut num0) -> |
| 155 | // let mul_num = *mul_num; |
| 156 | // let num0 = self.a.clone(); |
| 157 | |
| 158 | num0 *= mul_num; |
| 159 | num0 += 1024; |
| 160 | |
| 161 | num0 |
| 162 | })() |
| 163 | |
| 164 | // run_enclose |
| 165 | // |
| 166 | // let mut enclose = $crate::enclose!( $($tt)* ); |
| 167 | // enclose() |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | let data = CheckData::new(1024); |
| 172 | |
| 173 | // (data.a * 2) + 1024 |
| 174 | assert_eq!(data.calculate(&2), 3072); |
| 175 | |
| 176 | // check data |
| 177 | assert_eq!(data.a, 1024); |
| 178 | } |