|
| 1 | +use loro_fractional_index::FractionalIndex; |
| 2 | +use rand::{Error, RngCore}; |
| 3 | +use serde_json::json; |
| 4 | +use std::panic::{catch_unwind, set_hook, take_hook, AssertUnwindSafe}; |
| 5 | + |
| 6 | +struct ConstantByteRng(u8); |
| 7 | + |
| 8 | +impl RngCore for ConstantByteRng { |
| 9 | + fn next_u32(&mut self) -> u32 { |
| 10 | + u32::from(self.0) * 0x0101_0101 |
| 11 | + } |
| 12 | + |
| 13 | + fn next_u64(&mut self) -> u64 { |
| 14 | + u64::from(self.0) * 0x0101_0101_0101_0101 |
| 15 | + } |
| 16 | + |
| 17 | + fn fill_bytes(&mut self, dest: &mut [u8]) { |
| 18 | + dest.fill(self.0); |
| 19 | + } |
| 20 | + |
| 21 | + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { |
| 22 | + self.fill_bytes(dest); |
| 23 | + Ok(()) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fn idx(hex: &str) -> FractionalIndex { |
| 28 | + FractionalIndex::from_hex_string(hex) |
| 29 | +} |
| 30 | + |
| 31 | +fn maybe_hex(value: Option<FractionalIndex>) -> serde_json::Value { |
| 32 | + match value { |
| 33 | + Some(value) => json!(value.to_string()), |
| 34 | + None => serde_json::Value::Null, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn maybe_hex_or_panic(value: impl FnOnce() -> Option<FractionalIndex>) -> serde_json::Value { |
| 39 | + let hook = take_hook(); |
| 40 | + set_hook(Box::new(|_| {})); |
| 41 | + let result = catch_unwind(AssertUnwindSafe(value)); |
| 42 | + set_hook(hook); |
| 43 | + |
| 44 | + match result { |
| 45 | + Ok(value) => json!({ |
| 46 | + "panics": false, |
| 47 | + "value": maybe_hex(value), |
| 48 | + }), |
| 49 | + Err(_) => json!({ |
| 50 | + "panics": true, |
| 51 | + "value": null, |
| 52 | + }), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn evenly( |
| 57 | + lower: Option<&FractionalIndex>, |
| 58 | + upper: Option<&FractionalIndex>, |
| 59 | + n: usize, |
| 60 | +) -> serde_json::Value { |
| 61 | + match FractionalIndex::generate_n_evenly(lower, upper, n) { |
| 62 | + Some(values) => json!(values |
| 63 | + .into_iter() |
| 64 | + .map(|value| value.to_string()) |
| 65 | + .collect::<Vec<_>>()), |
| 66 | + None => serde_json::Value::Null, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fn evenly_jitter( |
| 71 | + lower: Option<&FractionalIndex>, |
| 72 | + upper: Option<&FractionalIndex>, |
| 73 | + n: usize, |
| 74 | + jitter: u8, |
| 75 | + byte: u8, |
| 76 | +) -> serde_json::Value { |
| 77 | + let mut rng = ConstantByteRng(byte); |
| 78 | + match FractionalIndex::generate_n_evenly_jitter(lower, upper, n, &mut rng, jitter) { |
| 79 | + Some(values) => json!(values |
| 80 | + .into_iter() |
| 81 | + .map(|value| value.to_string()) |
| 82 | + .collect::<Vec<_>>()), |
| 83 | + None => serde_json::Value::Null, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn main() { |
| 88 | + let default = FractionalIndex::default(); |
| 89 | + let before = FractionalIndex::new_before(&default); |
| 90 | + let after = FractionalIndex::new_after(&default); |
| 91 | + |
| 92 | + let mut after_chain = Vec::new(); |
| 93 | + let mut current = default.clone(); |
| 94 | + for _ in 0..32 { |
| 95 | + current = FractionalIndex::new_after(¤t); |
| 96 | + after_chain.push(current.to_string()); |
| 97 | + } |
| 98 | + |
| 99 | + let mut before_chain = Vec::new(); |
| 100 | + let mut current = default.clone(); |
| 101 | + for _ in 0..32 { |
| 102 | + current = FractionalIndex::new_before(¤t); |
| 103 | + before_chain.push(current.to_string()); |
| 104 | + } |
| 105 | + |
| 106 | + let new_cases = vec![ |
| 107 | + json!({"lower": null, "upper": null, "value": maybe_hex(FractionalIndex::new(None, None))}), |
| 108 | + json!({"lower": default.to_string(), "upper": null, "value": maybe_hex(FractionalIndex::new(Some(&default), None))}), |
| 109 | + json!({"lower": null, "upper": default.to_string(), "value": maybe_hex(FractionalIndex::new(None, Some(&default)))}), |
| 110 | + json!({"lower": before.to_string(), "upper": default.to_string(), "value": maybe_hex(FractionalIndex::new(Some(&before), Some(&default)))}), |
| 111 | + json!({"lower": default.to_string(), "upper": after.to_string(), "value": maybe_hex(FractionalIndex::new(Some(&default), Some(&after)))}), |
| 112 | + json!({"lower": default.to_string(), "upper": default.to_string(), "value": maybe_hex(FractionalIndex::new(Some(&default), Some(&default)))}), |
| 113 | + json!({"lower": after.to_string(), "upper": default.to_string(), "value": maybe_hex(FractionalIndex::new(Some(&after), Some(&default)))}), |
| 114 | + ]; |
| 115 | + |
| 116 | + let between_pairs = [ |
| 117 | + ("80", "8180"), |
| 118 | + ("7F80", "80"), |
| 119 | + ("80", "80"), |
| 120 | + ("8180", "80"), |
| 121 | + ("7080", "9080"), |
| 122 | + ("7F80", "8080"), |
| 123 | + ("8180", "8280"), |
| 124 | + ("10", "1080"), |
| 125 | + ("8080", "80"), |
| 126 | + ("80", "80FF"), |
| 127 | + ("80FF", "8180"), |
| 128 | + ("0080", "0180"), |
| 129 | + ("0080", "008180"), |
| 130 | + ("FE80", "FF80"), |
| 131 | + ]; |
| 132 | + let between = between_pairs |
| 133 | + .into_iter() |
| 134 | + .map(|(left, right)| { |
| 135 | + let left_index = idx(left); |
| 136 | + let right_index = idx(right); |
| 137 | + let result = |
| 138 | + maybe_hex_or_panic(|| FractionalIndex::new_between(&left_index, &right_index)); |
| 139 | + json!({ |
| 140 | + "left": left, |
| 141 | + "right": right, |
| 142 | + "panics": result["panics"], |
| 143 | + "value": result["value"], |
| 144 | + }) |
| 145 | + }) |
| 146 | + .collect::<Vec<_>>(); |
| 147 | + |
| 148 | + let mut evenly_cases = Vec::new(); |
| 149 | + for n in 0..=20 { |
| 150 | + evenly_cases.push(json!({ |
| 151 | + "lower": null, |
| 152 | + "upper": null, |
| 153 | + "n": n, |
| 154 | + "value": evenly(None, None, n), |
| 155 | + })); |
| 156 | + } |
| 157 | + for n in 1..=12 { |
| 158 | + evenly_cases.push(json!({ |
| 159 | + "lower": before.to_string(), |
| 160 | + "upper": after.to_string(), |
| 161 | + "n": n, |
| 162 | + "value": evenly(Some(&before), Some(&after), n), |
| 163 | + })); |
| 164 | + } |
| 165 | + evenly_cases.push(json!({ |
| 166 | + "lower": default.to_string(), |
| 167 | + "upper": default.to_string(), |
| 168 | + "n": 3, |
| 169 | + "value": evenly(Some(&default), Some(&default), 3), |
| 170 | + })); |
| 171 | + evenly_cases.push(json!({ |
| 172 | + "lower": after.to_string(), |
| 173 | + "upper": before.to_string(), |
| 174 | + "n": 3, |
| 175 | + "value": evenly(Some(&after), Some(&before), 3), |
| 176 | + })); |
| 177 | + |
| 178 | + let mut rng = ConstantByteRng(0xAB); |
| 179 | + let jitter_default_3 = FractionalIndex::jitter_default(&mut rng, 3); |
| 180 | + let mut rng = ConstantByteRng(0x01); |
| 181 | + let before_jitter = FractionalIndex::new_before_jitter(&default, &mut rng, 2); |
| 182 | + let mut rng = ConstantByteRng(0x02); |
| 183 | + let after_jitter = FractionalIndex::new_after_jitter(&default, &mut rng, 2); |
| 184 | + let mut rng = ConstantByteRng(0x03); |
| 185 | + let between_jitter = FractionalIndex::new_between_jitter(&default, &after, &mut rng, 2); |
| 186 | + let mut rng = ConstantByteRng(0x04); |
| 187 | + let new_jitter_none = FractionalIndex::new_jitter(None, None, &mut rng, 2); |
| 188 | + let mut rng = ConstantByteRng(0x05); |
| 189 | + let new_jitter_after = FractionalIndex::new_jitter(Some(&default), None, &mut rng, 2); |
| 190 | + let mut rng = ConstantByteRng(0x06); |
| 191 | + let new_jitter_before = FractionalIndex::new_jitter(None, Some(&default), &mut rng, 2); |
| 192 | + |
| 193 | + let fixture = json!({ |
| 194 | + "basic": { |
| 195 | + "terminator": 128, |
| 196 | + "default": default.to_string(), |
| 197 | + "beforeDefault": before.to_string(), |
| 198 | + "afterDefault": after.to_string(), |
| 199 | + "fromHexOddLength": FractionalIndex::from_hex_string("80ffA").to_string(), |
| 200 | + }, |
| 201 | + "chains": { |
| 202 | + "after": after_chain, |
| 203 | + "before": before_chain, |
| 204 | + }, |
| 205 | + "newCases": new_cases, |
| 206 | + "between": between, |
| 207 | + "evenly": evenly_cases, |
| 208 | + "jitter": { |
| 209 | + "defaultJitter0": { |
| 210 | + "jitter": 0, |
| 211 | + "byte": 0xAB, |
| 212 | + "value": FractionalIndex::jitter_default(&mut ConstantByteRng(0xAB), 0).to_string(), |
| 213 | + }, |
| 214 | + "defaultJitter3": { |
| 215 | + "jitter": 3, |
| 216 | + "byte": 0xAB, |
| 217 | + "value": jitter_default_3.to_string(), |
| 218 | + }, |
| 219 | + "before": { |
| 220 | + "input": default.to_string(), |
| 221 | + "jitter": 2, |
| 222 | + "byte": 0x01, |
| 223 | + "value": before_jitter.to_string(), |
| 224 | + }, |
| 225 | + "after": { |
| 226 | + "input": default.to_string(), |
| 227 | + "jitter": 2, |
| 228 | + "byte": 0x02, |
| 229 | + "value": after_jitter.to_string(), |
| 230 | + }, |
| 231 | + "between": { |
| 232 | + "lower": default.to_string(), |
| 233 | + "upper": after.to_string(), |
| 234 | + "jitter": 2, |
| 235 | + "byte": 0x03, |
| 236 | + "value": maybe_hex(between_jitter), |
| 237 | + }, |
| 238 | + "newNoneNone": { |
| 239 | + "lower": null, |
| 240 | + "upper": null, |
| 241 | + "jitter": 2, |
| 242 | + "byte": 0x04, |
| 243 | + "value": maybe_hex(new_jitter_none), |
| 244 | + }, |
| 245 | + "newAfter": { |
| 246 | + "lower": default.to_string(), |
| 247 | + "upper": null, |
| 248 | + "jitter": 2, |
| 249 | + "byte": 0x05, |
| 250 | + "value": maybe_hex(new_jitter_after), |
| 251 | + }, |
| 252 | + "newBefore": { |
| 253 | + "lower": null, |
| 254 | + "upper": default.to_string(), |
| 255 | + "jitter": 2, |
| 256 | + "byte": 0x06, |
| 257 | + "value": maybe_hex(new_jitter_before), |
| 258 | + }, |
| 259 | + "generateN": { |
| 260 | + "lower": null, |
| 261 | + "upper": null, |
| 262 | + "n": 7, |
| 263 | + "jitter": 1, |
| 264 | + "byte": 0xCC, |
| 265 | + "value": evenly_jitter(None, None, 7, 1, 0xCC), |
| 266 | + } |
| 267 | + } |
| 268 | + }); |
| 269 | + |
| 270 | + println!("{}", serde_json::to_string_pretty(&fixture).unwrap()); |
| 271 | +} |
0 commit comments