|
| 1 | +library; |
| 2 | + |
| 3 | +use std::bytes::Bytes; |
| 4 | +use ::utils::setup; |
| 5 | + |
| 6 | +#[test()] |
| 7 | +fn bytes_splice() { |
| 8 | + let (mut bytes, a, b, c) = setup(); |
| 9 | + bytes.push(11u8); |
| 10 | + bytes.push(13u8); |
| 11 | + // bytes = [5, 7, 9, 11, 13] |
| 12 | + |
| 13 | + // Remove [1..4), replace with nothing |
| 14 | + let spliced = bytes.splice(1, 4, Bytes::new()); |
| 15 | + |
| 16 | + // bytes => [5, 13] |
| 17 | + assert(bytes.len() == 2); |
| 18 | + assert(bytes.get(0).unwrap() == a); |
| 19 | + assert(bytes.get(1).unwrap() == 13u8); |
| 20 | + |
| 21 | + // spliced => [7, 9, 11] |
| 22 | + assert(spliced.len() == 3); |
| 23 | + assert(spliced.get(0).unwrap() == b); |
| 24 | + assert(spliced.get(1).unwrap() == c); |
| 25 | + assert(spliced.get(2).unwrap() == 11u8); |
| 26 | +} |
| 27 | + |
| 28 | +#[test()] |
| 29 | +fn bytes_splice_front() { |
| 30 | + let (mut bytes, a, b, c) = setup(); |
| 31 | + // Remove [0..2) => [5, 7], replace with nothing |
| 32 | + let spliced = bytes.splice(0, 2, Bytes::new()); |
| 33 | + |
| 34 | + // bytes => [9] |
| 35 | + assert(bytes.len() == 1); |
| 36 | + assert(bytes.get(0).unwrap() == c); |
| 37 | + |
| 38 | + // spliced => [5, 7] |
| 39 | + assert(spliced.len() == 2); |
| 40 | + assert(spliced.get(0).unwrap() == a); |
| 41 | + assert(spliced.get(1).unwrap() == b); |
| 42 | +} |
| 43 | + |
| 44 | +#[test()] |
| 45 | +fn bytes_splice_end() { |
| 46 | + let (mut bytes, a, b, c) = setup(); |
| 47 | + // Remove [1..3) => [7, 9], replace with nothing |
| 48 | + let spliced = bytes.splice(1, bytes.len(), Bytes::new()); |
| 49 | + |
| 50 | + // bytes => [5] |
| 51 | + assert(bytes.len() == 1); |
| 52 | + assert(bytes.get(0).unwrap() == a); |
| 53 | + |
| 54 | + // spliced => [7, 9] |
| 55 | + assert(spliced.len() == 2); |
| 56 | + assert(spliced.get(0).unwrap() == b); |
| 57 | + assert(spliced.get(1).unwrap() == c); |
| 58 | +} |
| 59 | + |
| 60 | +#[test()] |
| 61 | +fn bytes_splice_empty_range() { |
| 62 | + let (mut bytes, a, b, c) = setup(); |
| 63 | + // Remove [1..1) => nothing, replace with nothing |
| 64 | + let spliced = bytes.splice(1, 1, Bytes::new()); |
| 65 | + |
| 66 | + assert(bytes.len() == 3); |
| 67 | + assert(bytes.get(0).unwrap() == a); |
| 68 | + assert(bytes.get(1).unwrap() == b); |
| 69 | + assert(bytes.get(2).unwrap() == c); |
| 70 | + assert(spliced.len() == 0); |
| 71 | +} |
| 72 | + |
| 73 | +#[test()] |
| 74 | +fn bytes_splice_entire_range() { |
| 75 | + let (mut bytes, a, b, c) = setup(); |
| 76 | + // Remove [0..3) => [5, 7, 9], replace with nothing |
| 77 | + let spliced = bytes.splice(0, bytes.len(), Bytes::new()); |
| 78 | + |
| 79 | + assert(bytes.len() == 0); |
| 80 | + assert(bytes.is_empty()); |
| 81 | + assert(spliced.len() == 3); |
| 82 | + assert(spliced.get(0).unwrap() == a); |
| 83 | + assert(spliced.get(1).unwrap() == b); |
| 84 | + assert(spliced.get(2).unwrap() == c); |
| 85 | +} |
| 86 | + |
| 87 | +#[test(should_revert)] |
| 88 | +fn revert_bytes_splice_start_greater_than_end() { |
| 89 | + let (mut bytes, _, _, _) = setup(); |
| 90 | + let _spliced = bytes.splice(2, 1, Bytes::new()); |
| 91 | +} |
| 92 | + |
| 93 | +#[test(should_revert)] |
| 94 | +fn revert_bytes_splice_end_out_of_bounds() { |
| 95 | + let (mut bytes, _, _, _) = setup(); |
| 96 | + let _spliced = bytes.splice(0, bytes.len() + 1, Bytes::new()); |
| 97 | +} |
| 98 | + |
| 99 | +/// Additional tests for replacing a spliced range with different Byte lengths. |
| 100 | +#[test()] |
| 101 | +fn bytes_splice_replace_smaller() { |
| 102 | + let (mut bytes, a, b, c) = setup(); |
| 103 | + bytes.push(11u8); |
| 104 | + bytes.push(13u8); |
| 105 | + // bytes = [5, 7, 9, 11, 13] |
| 106 | + let mut replacement = Bytes::new(); |
| 107 | + replacement.push(42u8); |
| 108 | + // Remove [1..4) => [7, 9, 11], replace with [42] |
| 109 | + let spliced = bytes.splice(1, 4, replacement); |
| 110 | + |
| 111 | + // bytes => [5, 42, 13] |
| 112 | + assert(bytes.len() == 3); |
| 113 | + assert(bytes.get(0).unwrap() == a); |
| 114 | + assert(bytes.get(1).unwrap() == 42u8); |
| 115 | + assert(bytes.get(2).unwrap() == 13u8); |
| 116 | + |
| 117 | + // spliced => [7, 9, 11] |
| 118 | + assert(spliced.len() == 3); |
| 119 | + assert(spliced.get(0).unwrap() == b); |
| 120 | + assert(spliced.get(1).unwrap() == c); |
| 121 | + assert(spliced.get(2).unwrap() == 11u8); |
| 122 | +} |
| 123 | + |
| 124 | +#[test()] |
| 125 | +fn bytes_splice_replace_larger() { |
| 126 | + let (mut bytes, a, b, c) = setup(); |
| 127 | + // bytes = [5, 7, 9] |
| 128 | + let mut replacement = Bytes::new(); |
| 129 | + replacement.push(42u8); |
| 130 | + replacement.push(50u8); |
| 131 | + replacement.push(60u8); |
| 132 | + // Remove [1..2) => [7], replace with [42, 50, 60] |
| 133 | + let spliced = bytes.splice(1, 2, replacement); |
| 134 | + |
| 135 | + // bytes => [5, 42, 50, 60, 9] |
| 136 | + assert(bytes.len() == 5); |
| 137 | + assert(bytes.get(0).unwrap() == a); |
| 138 | + assert(bytes.get(1).unwrap() == 42u8); |
| 139 | + assert(bytes.get(2).unwrap() == 50u8); |
| 140 | + assert(bytes.get(3).unwrap() == 60u8); |
| 141 | + assert(bytes.get(4).unwrap() == c); |
| 142 | + |
| 143 | + // spliced => [7] |
| 144 | + assert(spliced.len() == 1); |
| 145 | + assert(spliced.get(0).unwrap() == b); |
| 146 | +} |
| 147 | + |
| 148 | +#[test()] |
| 149 | +fn bytes_splice_replace_same_length() { |
| 150 | + let (mut bytes, a, b, c) = setup(); |
| 151 | + // bytes = [5, 7, 9] |
| 152 | + let mut replacement = Bytes::new(); |
| 153 | + replacement.push(42u8); |
| 154 | + replacement.push(50u8); |
| 155 | + // Remove [1..3) => [7, 9], replace with [42, 50] (same length = 2) |
| 156 | + let spliced = bytes.splice(1, 3, replacement); |
| 157 | + |
| 158 | + // bytes => [5, 42, 50] |
| 159 | + assert(bytes.len() == 3); |
| 160 | + assert(bytes.get(0).unwrap() == a); |
| 161 | + assert(bytes.get(1).unwrap() == 42u8); |
| 162 | + assert(bytes.get(2).unwrap() == 50u8); |
| 163 | + |
| 164 | + // spliced => [7, 9] |
| 165 | + assert(spliced.len() == 2); |
| 166 | + assert(spliced.get(0).unwrap() == b); |
| 167 | + assert(spliced.get(1).unwrap() == c); |
| 168 | +} |
| 169 | + |
| 170 | +#[test()] |
| 171 | +fn bytes_splice_replace_empty_bytes() { |
| 172 | + let (mut bytes, a, b, c) = setup(); |
| 173 | + // bytes = [5, 7, 9] |
| 174 | + let replacement = Bytes::new(); |
| 175 | + // Remove [0..1) => [5], replace with [] |
| 176 | + let spliced = bytes.splice(0, 1, replacement); |
| 177 | + |
| 178 | + // bytes => [7, 9] |
| 179 | + assert(bytes.len() == 2); |
| 180 | + assert(bytes.get(0).unwrap() == b); |
| 181 | + assert(bytes.get(1).unwrap() == c); |
| 182 | + |
| 183 | + // spliced => [5] |
| 184 | + assert(spliced.len() == 1); |
| 185 | + assert(spliced.get(0).unwrap() == a); |
| 186 | +} |
| 187 | + |
| 188 | +#[test()] |
| 189 | +fn bytes_splice_replace_overlap() { |
| 190 | + let (mut bytes, a, b, c) = setup(); |
| 191 | + // bytes = [5, 7, 9] |
| 192 | + let mut replacement = Bytes::new(); |
| 193 | + replacement.push(10u8); |
| 194 | + replacement.push(12u8); |
| 195 | + // Remove [0..1) => [5], replace with [10, 12] |
| 196 | + let spliced = bytes.splice(0, 1, replacement); |
| 197 | + |
| 198 | + // Expect spliced to contain the removed element [5] |
| 199 | + assert(spliced.len() == 1); |
| 200 | + assert(spliced.get(0).unwrap() == a); |
| 201 | + |
| 202 | + // After replacement, bytes should now be [10, 12, 7, 9] |
| 203 | + assert(bytes.len() == 4); |
| 204 | + assert(bytes.get(0).unwrap() == 10u8); |
| 205 | + assert(bytes.get(1).unwrap() == 12u8); |
| 206 | + assert(bytes.get(2).unwrap() == b); |
| 207 | + assert(bytes.get(3).unwrap() == c); |
| 208 | +} |
0 commit comments