Show More
@@ -1,367 +1,367 | |||||
1 | use byteorder::{BigEndian, ByteOrder}; |
|
1 | use byteorder::{BigEndian, ByteOrder}; | |
2 |
|
2 | |||
3 | /// A chunk of data to insert, delete or replace in a patch |
|
3 | /// A chunk of data to insert, delete or replace in a patch | |
4 | /// |
|
4 | /// | |
5 | /// A chunk is: |
|
5 | /// A chunk is: | |
6 | /// - an insertion when `!data.is_empty() && start == end` |
|
6 | /// - an insertion when `!data.is_empty() && start == end` | |
7 | /// - an deletion when `data.is_empty() && start < end` |
|
7 | /// - an deletion when `data.is_empty() && start < end` | |
8 | /// - a replacement when `!data.is_empty() && start < end` |
|
8 | /// - a replacement when `!data.is_empty() && start < end` | |
9 | /// - not doing anything when `data.is_empty() && start == end` |
|
9 | /// - not doing anything when `data.is_empty() && start == end` | |
10 | #[derive(Debug, Clone)] |
|
10 | #[derive(Debug, Clone)] | |
11 | struct PatchFrag<'a> { |
|
11 | struct PatchFrag<'a> { | |
12 | /// The start position of the chunk of data to replace |
|
12 | /// The start position of the chunk of data to replace | |
13 | start: i32, |
|
13 | start: i32, | |
14 | /// The end position of the chunk of data to replace (open end interval) |
|
14 | /// The end position of the chunk of data to replace (open end interval) | |
15 | end: i32, |
|
15 | end: i32, | |
16 | /// The data replacing the chunk |
|
16 | /// The data replacing the chunk | |
17 | data: &'a [u8], |
|
17 | data: &'a [u8], | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | impl<'a> PatchFrag<'a> { |
|
20 | impl<'a> PatchFrag<'a> { | |
21 | /// Adjusted start of the chunk to replace. |
|
21 | /// Adjusted start of the chunk to replace. | |
22 | /// |
|
22 | /// | |
23 | /// Offset allow to take into account the growth/shrinkage of data |
|
23 | /// Offset allow to take into account the growth/shrinkage of data | |
24 | /// induced by previously applied chunks. |
|
24 | /// induced by previously applied chunks. | |
25 | fn start_offseted_by(&self, offset: i32) -> i32 { |
|
25 | fn start_offseted_by(&self, offset: i32) -> i32 { | |
26 | self.start + offset |
|
26 | self.start + offset | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | /// Adjusted end of the chunk to replace. |
|
29 | /// Adjusted end of the chunk to replace. | |
30 | /// |
|
30 | /// | |
31 | /// Offset allow to take into account the growth/shrinkage of data |
|
31 | /// Offset allow to take into account the growth/shrinkage of data | |
32 | /// induced by previously applied chunks. |
|
32 | /// induced by previously applied chunks. | |
33 | fn end_offseted_by(&self, offset: i32) -> i32 { |
|
33 | fn end_offseted_by(&self, offset: i32) -> i32 { | |
34 | self.start_offseted_by(offset) + (self.data.len() as i32) |
|
34 | self.start_offseted_by(offset) + (self.data.len() as i32) | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | /// Length of the replaced chunk. |
|
37 | /// Length of the replaced chunk. | |
38 | fn replaced_len(&self) -> i32 { |
|
38 | fn replaced_len(&self) -> i32 { | |
39 | self.end - self.start |
|
39 | self.end - self.start | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | /// Length difference between the replacing data and the replaced data. |
|
42 | /// Length difference between the replacing data and the replaced data. | |
43 | fn len_diff(&self) -> i32 { |
|
43 | fn len_diff(&self) -> i32 { | |
44 | (self.data.len() as i32) - self.replaced_len() |
|
44 | (self.data.len() as i32) - self.replaced_len() | |
45 | } |
|
45 | } | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | /// The delta between two revisions data. |
|
48 | /// The delta between two revisions data. | |
49 | #[derive(Debug, Clone)] |
|
49 | #[derive(Debug, Clone)] | |
50 | pub struct PatchList<'a> { |
|
50 | pub struct PatchList<'a> { | |
51 | /// A collection of chunks to apply. |
|
51 | /// A collection of chunks to apply. | |
52 | /// |
|
52 | /// | |
53 | /// Those chunks are: |
|
53 | /// Those chunks are: | |
54 | /// - ordered from the left-most replacement to the right-most replacement |
|
54 | /// - ordered from the left-most replacement to the right-most replacement | |
55 | /// - non-overlapping, meaning that two chucks can not change the same |
|
55 | /// - non-overlapping, meaning that two chucks can not change the same | |
56 | /// chunk of the patched data |
|
56 | /// chunk of the patched data | |
57 | frags: Vec<PatchFrag<'a>>, |
|
57 | frags: Vec<PatchFrag<'a>>, | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | impl<'a> PatchList<'a> { |
|
60 | impl<'a> PatchList<'a> { | |
61 | /// Create a `PatchList` from bytes. |
|
61 | /// Create a `PatchList` from bytes. | |
62 | pub fn new(data: &'a [u8]) -> Self { |
|
62 | pub fn new(data: &'a [u8]) -> Self { | |
63 | let mut frags = vec![]; |
|
63 | let mut frags = vec![]; | |
64 | let mut data = data; |
|
64 | let mut data = data; | |
65 | while !data.is_empty() { |
|
65 | while !data.is_empty() { | |
66 | let start = BigEndian::read_i32(&data[0..]); |
|
66 | let start = BigEndian::read_i32(&data[0..]); | |
67 | let end = BigEndian::read_i32(&data[4..]); |
|
67 | let end = BigEndian::read_i32(&data[4..]); | |
68 | let len = BigEndian::read_i32(&data[8..]); |
|
68 | let len = BigEndian::read_i32(&data[8..]); | |
69 | assert!(0 <= start && start <= end && len >= 0); |
|
69 | assert!(0 <= start && start <= end && len >= 0); | |
70 | frags.push(PatchFrag { |
|
70 | frags.push(PatchFrag { | |
71 | start, |
|
71 | start, | |
72 | end, |
|
72 | end, | |
73 | data: &data[12..12 + (len as usize)], |
|
73 | data: &data[12..12 + (len as usize)], | |
74 | }); |
|
74 | }); | |
75 | data = &data[12 + (len as usize)..]; |
|
75 | data = &data[12 + (len as usize)..]; | |
76 | } |
|
76 | } | |
77 | PatchList { frags } |
|
77 | PatchList { frags } | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | /// Return the final length of data after patching |
|
80 | /// Return the final length of data after patching | |
81 | /// given its initial length . |
|
81 | /// given its initial length . | |
82 | fn size(&self, initial_size: i32) -> i32 { |
|
82 | fn size(&self, initial_size: i32) -> i32 { | |
83 | self.frags |
|
83 | self.frags | |
84 | .iter() |
|
84 | .iter() | |
85 | .fold(initial_size, |acc, frag| acc + frag.len_diff()) |
|
85 | .fold(initial_size, |acc, frag| acc + frag.len_diff()) | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | /// Apply the patch to some data. |
|
88 | /// Apply the patch to some data. | |
89 | pub fn apply(&self, initial: &[u8]) -> Vec<u8> { |
|
89 | pub fn apply(&self, initial: &[u8]) -> Vec<u8> { | |
90 | let mut last: usize = 0; |
|
90 | let mut last: usize = 0; | |
91 | let mut vec = |
|
91 | let mut vec = | |
92 | Vec::with_capacity(self.size(initial.len() as i32) as usize); |
|
92 | Vec::with_capacity(self.size(initial.len() as i32) as usize); | |
93 | for PatchFrag { start, end, data } in self.frags.iter() { |
|
93 | for PatchFrag { start, end, data } in self.frags.iter() { | |
94 | vec.extend(&initial[last..(*start as usize)]); |
|
94 | vec.extend(&initial[last..(*start as usize)]); | |
95 | vec.extend(data.iter()); |
|
95 | vec.extend(data.iter()); | |
96 | last = *end as usize; |
|
96 | last = *end as usize; | |
97 | } |
|
97 | } | |
98 | vec.extend(&initial[last..]); |
|
98 | vec.extend(&initial[last..]); | |
99 | vec |
|
99 | vec | |
100 | } |
|
100 | } | |
101 |
|
101 | |||
102 | /// Combine two patch lists into a single patch list. |
|
102 | /// Combine two patch lists into a single patch list. | |
103 | /// |
|
103 | /// | |
104 | /// Applying consecutive patches can lead to waste of time and memory |
|
104 | /// Applying consecutive patches can lead to waste of time and memory | |
105 | /// as the changes introduced by one patch can be overridden by the next. |
|
105 | /// as the changes introduced by one patch can be overridden by the next. | |
106 | /// Combining patches optimizes the whole patching sequence. |
|
106 | /// Combining patches optimizes the whole patching sequence. | |
107 | fn combine(&mut self, other: &mut Self) -> Self { |
|
107 | fn combine(&mut self, other: &mut Self) -> Self { | |
108 | let mut frags = vec![]; |
|
108 | let mut frags = vec![]; | |
109 |
|
109 | |||
110 | // Keep track of each growth/shrinkage resulting from applying a chunk |
|
110 | // Keep track of each growth/shrinkage resulting from applying a chunk | |
111 | // in order to adjust the start/end of subsequent chunks. |
|
111 | // in order to adjust the start/end of subsequent chunks. | |
112 | let mut offset = 0i32; |
|
112 | let mut offset = 0i32; | |
113 |
|
113 | |||
114 | // Keep track of the chunk of self.chunks to process. |
|
114 | // Keep track of the chunk of self.chunks to process. | |
115 | let mut pos = 0; |
|
115 | let mut pos = 0; | |
116 |
|
116 | |||
117 | // For each chunk of `other`, chunks of `self` are processed |
|
117 | // For each chunk of `other`, chunks of `self` are processed | |
118 | // until they start after the end of the current chunk. |
|
118 | // until they start after the end of the current chunk. | |
119 | for PatchFrag { start, end, data } in other.frags.iter() { |
|
119 | for PatchFrag { start, end, data } in other.frags.iter() { | |
120 | // Add chunks of `self` that start before this chunk of `other` |
|
120 | // Add chunks of `self` that start before this chunk of `other` | |
121 | // without overlap. |
|
121 | // without overlap. | |
122 | while pos < self.frags.len() |
|
122 | while pos < self.frags.len() | |
123 | && self.frags[pos].end_offseted_by(offset) <= *start |
|
123 | && self.frags[pos].end_offseted_by(offset) <= *start | |
124 | { |
|
124 | { | |
125 | let first = self.frags[pos].clone(); |
|
125 | let first = self.frags[pos].clone(); | |
126 | offset += first.len_diff(); |
|
126 | offset += first.len_diff(); | |
127 | frags.push(first); |
|
127 | frags.push(first); | |
128 | pos += 1; |
|
128 | pos += 1; | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | // The current chunk of `self` starts before this chunk of `other` |
|
131 | // The current chunk of `self` starts before this chunk of `other` | |
132 | // with overlap. |
|
132 | // with overlap. | |
133 | // The left-most part of data is added as an insertion chunk. |
|
133 | // The left-most part of data is added as an insertion chunk. | |
134 | // The right-most part data is kept in the chunk. |
|
134 | // The right-most part data is kept in the chunk. | |
135 | if pos < self.frags.len() |
|
135 | if pos < self.frags.len() | |
136 | && self.frags[pos].start_offseted_by(offset) < *start |
|
136 | && self.frags[pos].start_offseted_by(offset) < *start | |
137 | { |
|
137 | { | |
138 | let first = &mut self.frags[pos]; |
|
138 | let first = &mut self.frags[pos]; | |
139 |
|
139 | |||
140 | let (data_left, data_right) = first.data.split_at( |
|
140 | let (data_left, data_right) = first.data.split_at( | |
141 | (*start - first.start_offseted_by(offset)) as usize, |
|
141 | (*start - first.start_offseted_by(offset)) as usize, | |
142 | ); |
|
142 | ); | |
143 | let left = PatchFrag { |
|
143 | let left = PatchFrag { | |
144 | start: first.start, |
|
144 | start: first.start, | |
145 | end: first.start, |
|
145 | end: first.start, | |
146 | data: data_left, |
|
146 | data: data_left, | |
147 | }; |
|
147 | }; | |
148 |
|
148 | |||
149 | first.data = data_right; |
|
149 | first.data = data_right; | |
150 |
|
150 | |||
151 | offset += left.len_diff(); |
|
151 | offset += left.len_diff(); | |
152 |
|
152 | |||
153 | frags.push(left); |
|
153 | frags.push(left); | |
154 |
|
154 | |||
155 | // There is no index incrementation because the right-most part |
|
155 | // There is no index incrementation because the right-most part | |
156 | // needs further examination. |
|
156 | // needs further examination. | |
157 | } |
|
157 | } | |
158 |
|
158 | |||
159 | // At this point remaining chunks of `self` starts after |
|
159 | // At this point remaining chunks of `self` starts after | |
160 | // the current chunk of `other`. |
|
160 | // the current chunk of `other`. | |
161 |
|
161 | |||
162 | // `start_offset` will be used to adjust the start of the current |
|
162 | // `start_offset` will be used to adjust the start of the current | |
163 | // chunk of `other`. |
|
163 | // chunk of `other`. | |
164 | // Offset tracking continues with `end_offset` to adjust the end |
|
164 | // Offset tracking continues with `end_offset` to adjust the end | |
165 | // of the current chunk of `other`. |
|
165 | // of the current chunk of `other`. | |
166 | let mut next_offset = offset; |
|
166 | let mut next_offset = offset; | |
167 |
|
167 | |||
168 | // Discard the chunks of `self` that are totally overridden |
|
168 | // Discard the chunks of `self` that are totally overridden | |
169 | // by the current chunk of `other` |
|
169 | // by the current chunk of `other` | |
170 | while pos < self.frags.len() |
|
170 | while pos < self.frags.len() | |
171 | && self.frags[pos].end_offseted_by(next_offset) <= *end |
|
171 | && self.frags[pos].end_offseted_by(next_offset) <= *end | |
172 | { |
|
172 | { | |
173 | let first = &self.frags[pos]; |
|
173 | let first = &self.frags[pos]; | |
174 | next_offset += first.len_diff(); |
|
174 | next_offset += first.len_diff(); | |
175 | pos += 1; |
|
175 | pos += 1; | |
176 | } |
|
176 | } | |
177 |
|
177 | |||
178 | // Truncate the left-most part of chunk of `self` that overlaps |
|
178 | // Truncate the left-most part of chunk of `self` that overlaps | |
179 | // the current chunk of `other`. |
|
179 | // the current chunk of `other`. | |
180 | if pos < self.frags.len() |
|
180 | if pos < self.frags.len() | |
181 | && self.frags[pos].start_offseted_by(next_offset) < *end |
|
181 | && self.frags[pos].start_offseted_by(next_offset) < *end | |
182 | { |
|
182 | { | |
183 | let first = &mut self.frags[pos]; |
|
183 | let first = &mut self.frags[pos]; | |
184 |
|
184 | |||
185 | let how_much_to_discard = |
|
185 | let how_much_to_discard = | |
186 | *end - first.start_offseted_by(next_offset); |
|
186 | *end - first.start_offseted_by(next_offset); | |
187 |
|
187 | |||
188 | first.data = &first.data[(how_much_to_discard as usize)..]; |
|
188 | first.data = &first.data[(how_much_to_discard as usize)..]; | |
189 |
|
189 | |||
190 | next_offset += how_much_to_discard; |
|
190 | next_offset += how_much_to_discard; | |
191 | } |
|
191 | } | |
192 |
|
192 | |||
193 | // Add the chunk of `other` with adjusted position. |
|
193 | // Add the chunk of `other` with adjusted position. | |
194 | frags.push(PatchFrag { |
|
194 | frags.push(PatchFrag { | |
195 | start: *start - offset, |
|
195 | start: *start - offset, | |
196 | end: *end - next_offset, |
|
196 | end: *end - next_offset, | |
197 | data, |
|
197 | data, | |
198 | }); |
|
198 | }); | |
199 |
|
199 | |||
200 | // Go back to normal offset tracking for the next `o` chunk |
|
200 | // Go back to normal offset tracking for the next `o` chunk | |
201 | offset = next_offset; |
|
201 | offset = next_offset; | |
202 | } |
|
202 | } | |
203 |
|
203 | |||
204 | // Add remaining chunks of `self`. |
|
204 | // Add remaining chunks of `self`. | |
205 | for elt in &self.frags[pos..] { |
|
205 | for elt in &self.frags[pos..] { | |
206 | frags.push(elt.clone()); |
|
206 | frags.push(elt.clone()); | |
207 | } |
|
207 | } | |
208 | PatchList { frags } |
|
208 | PatchList { frags } | |
209 | } |
|
209 | } | |
210 | } |
|
210 | } | |
211 |
|
211 | |||
212 | /// Combine a list of patch list into a single patch optimized patch list. |
|
212 | /// Combine a list of patch list into a single patch optimized patch list. | |
213 | pub fn fold_patch_lists<'a>(lists: &[PatchList<'a>]) -> PatchList<'a> { |
|
213 | pub fn fold_patch_lists<'a>(lists: &[PatchList<'a>]) -> PatchList<'a> { | |
214 | if lists.len() <= 1 { |
|
214 | if lists.len() <= 1 { | |
215 | if lists.is_empty() { |
|
215 | if lists.is_empty() { | |
216 | PatchList { frags: vec![] } |
|
216 | PatchList { frags: vec![] } | |
217 | } else { |
|
217 | } else { | |
218 | lists[0].clone() |
|
218 | lists[0].clone() | |
219 | } |
|
219 | } | |
220 | } else { |
|
220 | } else { | |
221 | let (left, right) = lists.split_at(lists.len() / 2); |
|
221 | let (left, right) = lists.split_at(lists.len() / 2); | |
222 | let mut left_res = fold_patch_lists(left); |
|
222 | let mut left_res = fold_patch_lists(left); | |
223 | let mut right_res = fold_patch_lists(right); |
|
223 | let mut right_res = fold_patch_lists(right); | |
224 | left_res.combine(&mut right_res) |
|
224 | left_res.combine(&mut right_res) | |
225 | } |
|
225 | } | |
226 | } |
|
226 | } | |
227 |
|
227 | |||
228 | #[cfg(test)] |
|
228 | #[cfg(test)] | |
229 | mod tests { |
|
229 | mod tests { | |
230 | use super::*; |
|
230 | use super::*; | |
231 |
|
231 | |||
232 | struct PatchDataBuilder { |
|
232 | struct PatchDataBuilder { | |
233 | data: Vec<u8>, |
|
233 | data: Vec<u8>, | |
234 | } |
|
234 | } | |
235 |
|
235 | |||
236 | impl PatchDataBuilder { |
|
236 | impl PatchDataBuilder { | |
237 | pub fn new() -> Self { |
|
237 | pub fn new() -> Self { | |
238 | Self { data: vec![] } |
|
238 | Self { data: vec![] } | |
239 | } |
|
239 | } | |
240 |
|
240 | |||
241 | pub fn replace( |
|
241 | pub fn replace( | |
242 | &mut self, |
|
242 | &mut self, | |
243 | start: usize, |
|
243 | start: usize, | |
244 | end: usize, |
|
244 | end: usize, | |
245 | data: &[u8], |
|
245 | data: &[u8], | |
246 | ) -> &mut Self { |
|
246 | ) -> &mut Self { | |
247 | assert!(start <= end); |
|
247 | assert!(start <= end); | |
248 | self.data.extend(&(start as i32).to_be_bytes()); |
|
248 | self.data.extend(&(start as i32).to_be_bytes()); | |
249 | self.data.extend(&(end as i32).to_be_bytes()); |
|
249 | self.data.extend(&(end as i32).to_be_bytes()); | |
250 | self.data.extend(&(data.len() as i32).to_be_bytes()); |
|
250 | self.data.extend(&(data.len() as i32).to_be_bytes()); | |
251 | self.data.extend(data.iter()); |
|
251 | self.data.extend(data.iter()); | |
252 | self |
|
252 | self | |
253 | } |
|
253 | } | |
254 |
|
254 | |||
255 | pub fn get(&mut self) -> &[u8] { |
|
255 | pub fn get(&mut self) -> &[u8] { | |
256 |
&self.data |
|
256 | &self.data | |
257 | } |
|
257 | } | |
258 | } |
|
258 | } | |
259 |
|
259 | |||
260 | #[test] |
|
260 | #[test] | |
261 | fn test_ends_before() { |
|
261 | fn test_ends_before() { | |
262 | let data = vec![0u8, 0u8, 0u8]; |
|
262 | let data = vec![0u8, 0u8, 0u8]; | |
263 | let mut patch1_data = PatchDataBuilder::new(); |
|
263 | let mut patch1_data = PatchDataBuilder::new(); | |
264 | patch1_data.replace(0, 1, &[1, 2]); |
|
264 | patch1_data.replace(0, 1, &[1, 2]); | |
265 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
265 | let mut patch1 = PatchList::new(patch1_data.get()); | |
266 |
|
266 | |||
267 | let mut patch2_data = PatchDataBuilder::new(); |
|
267 | let mut patch2_data = PatchDataBuilder::new(); | |
268 | patch2_data.replace(2, 4, &[3, 4]); |
|
268 | patch2_data.replace(2, 4, &[3, 4]); | |
269 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
269 | let mut patch2 = PatchList::new(patch2_data.get()); | |
270 |
|
270 | |||
271 | let patch = patch1.combine(&mut patch2); |
|
271 | let patch = patch1.combine(&mut patch2); | |
272 |
|
272 | |||
273 | let result = patch.apply(&data); |
|
273 | let result = patch.apply(&data); | |
274 |
|
274 | |||
275 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
275 | assert_eq!(result, vec![1u8, 2, 3, 4]); | |
276 | } |
|
276 | } | |
277 |
|
277 | |||
278 | #[test] |
|
278 | #[test] | |
279 | fn test_starts_after() { |
|
279 | fn test_starts_after() { | |
280 | let data = vec![0u8, 0u8, 0u8]; |
|
280 | let data = vec![0u8, 0u8, 0u8]; | |
281 | let mut patch1_data = PatchDataBuilder::new(); |
|
281 | let mut patch1_data = PatchDataBuilder::new(); | |
282 | patch1_data.replace(2, 3, &[3]); |
|
282 | patch1_data.replace(2, 3, &[3]); | |
283 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
283 | let mut patch1 = PatchList::new(patch1_data.get()); | |
284 |
|
284 | |||
285 | let mut patch2_data = PatchDataBuilder::new(); |
|
285 | let mut patch2_data = PatchDataBuilder::new(); | |
286 | patch2_data.replace(1, 2, &[1, 2]); |
|
286 | patch2_data.replace(1, 2, &[1, 2]); | |
287 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
287 | let mut patch2 = PatchList::new(patch2_data.get()); | |
288 |
|
288 | |||
289 | let patch = patch1.combine(&mut patch2); |
|
289 | let patch = patch1.combine(&mut patch2); | |
290 |
|
290 | |||
291 | let result = patch.apply(&data); |
|
291 | let result = patch.apply(&data); | |
292 |
|
292 | |||
293 | assert_eq!(result, vec![0u8, 1, 2, 3]); |
|
293 | assert_eq!(result, vec![0u8, 1, 2, 3]); | |
294 | } |
|
294 | } | |
295 |
|
295 | |||
296 | #[test] |
|
296 | #[test] | |
297 | fn test_overridden() { |
|
297 | fn test_overridden() { | |
298 | let data = vec![0u8, 0, 0]; |
|
298 | let data = vec![0u8, 0, 0]; | |
299 | let mut patch1_data = PatchDataBuilder::new(); |
|
299 | let mut patch1_data = PatchDataBuilder::new(); | |
300 | patch1_data.replace(1, 2, &[3, 4]); |
|
300 | patch1_data.replace(1, 2, &[3, 4]); | |
301 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
301 | let mut patch1 = PatchList::new(patch1_data.get()); | |
302 |
|
302 | |||
303 | let mut patch2_data = PatchDataBuilder::new(); |
|
303 | let mut patch2_data = PatchDataBuilder::new(); | |
304 | patch2_data.replace(1, 4, &[1, 2, 3]); |
|
304 | patch2_data.replace(1, 4, &[1, 2, 3]); | |
305 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
305 | let mut patch2 = PatchList::new(patch2_data.get()); | |
306 |
|
306 | |||
307 | let patch = patch1.combine(&mut patch2); |
|
307 | let patch = patch1.combine(&mut patch2); | |
308 |
|
308 | |||
309 | let result = patch.apply(&data); |
|
309 | let result = patch.apply(&data); | |
310 |
|
310 | |||
311 | assert_eq!(result, vec![0u8, 1, 2, 3]); |
|
311 | assert_eq!(result, vec![0u8, 1, 2, 3]); | |
312 | } |
|
312 | } | |
313 |
|
313 | |||
314 | #[test] |
|
314 | #[test] | |
315 | fn test_right_most_part_is_overridden() { |
|
315 | fn test_right_most_part_is_overridden() { | |
316 | let data = vec![0u8, 0, 0]; |
|
316 | let data = vec![0u8, 0, 0]; | |
317 | let mut patch1_data = PatchDataBuilder::new(); |
|
317 | let mut patch1_data = PatchDataBuilder::new(); | |
318 | patch1_data.replace(0, 1, &[1, 3]); |
|
318 | patch1_data.replace(0, 1, &[1, 3]); | |
319 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
319 | let mut patch1 = PatchList::new(patch1_data.get()); | |
320 |
|
320 | |||
321 | let mut patch2_data = PatchDataBuilder::new(); |
|
321 | let mut patch2_data = PatchDataBuilder::new(); | |
322 | patch2_data.replace(1, 4, &[2, 3, 4]); |
|
322 | patch2_data.replace(1, 4, &[2, 3, 4]); | |
323 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
323 | let mut patch2 = PatchList::new(patch2_data.get()); | |
324 |
|
324 | |||
325 | let patch = patch1.combine(&mut patch2); |
|
325 | let patch = patch1.combine(&mut patch2); | |
326 |
|
326 | |||
327 | let result = patch.apply(&data); |
|
327 | let result = patch.apply(&data); | |
328 |
|
328 | |||
329 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
329 | assert_eq!(result, vec![1u8, 2, 3, 4]); | |
330 | } |
|
330 | } | |
331 |
|
331 | |||
332 | #[test] |
|
332 | #[test] | |
333 | fn test_left_most_part_is_overridden() { |
|
333 | fn test_left_most_part_is_overridden() { | |
334 | let data = vec![0u8, 0, 0]; |
|
334 | let data = vec![0u8, 0, 0]; | |
335 | let mut patch1_data = PatchDataBuilder::new(); |
|
335 | let mut patch1_data = PatchDataBuilder::new(); | |
336 | patch1_data.replace(1, 3, &[1, 3, 4]); |
|
336 | patch1_data.replace(1, 3, &[1, 3, 4]); | |
337 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
337 | let mut patch1 = PatchList::new(patch1_data.get()); | |
338 |
|
338 | |||
339 | let mut patch2_data = PatchDataBuilder::new(); |
|
339 | let mut patch2_data = PatchDataBuilder::new(); | |
340 | patch2_data.replace(0, 2, &[1, 2]); |
|
340 | patch2_data.replace(0, 2, &[1, 2]); | |
341 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
341 | let mut patch2 = PatchList::new(patch2_data.get()); | |
342 |
|
342 | |||
343 | let patch = patch1.combine(&mut patch2); |
|
343 | let patch = patch1.combine(&mut patch2); | |
344 |
|
344 | |||
345 | let result = patch.apply(&data); |
|
345 | let result = patch.apply(&data); | |
346 |
|
346 | |||
347 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
347 | assert_eq!(result, vec![1u8, 2, 3, 4]); | |
348 | } |
|
348 | } | |
349 |
|
349 | |||
350 | #[test] |
|
350 | #[test] | |
351 | fn test_mid_is_overridden() { |
|
351 | fn test_mid_is_overridden() { | |
352 | let data = vec![0u8, 0, 0]; |
|
352 | let data = vec![0u8, 0, 0]; | |
353 | let mut patch1_data = PatchDataBuilder::new(); |
|
353 | let mut patch1_data = PatchDataBuilder::new(); | |
354 | patch1_data.replace(0, 3, &[1, 3, 3, 4]); |
|
354 | patch1_data.replace(0, 3, &[1, 3, 3, 4]); | |
355 | let mut patch1 = PatchList::new(patch1_data.get()); |
|
355 | let mut patch1 = PatchList::new(patch1_data.get()); | |
356 |
|
356 | |||
357 | let mut patch2_data = PatchDataBuilder::new(); |
|
357 | let mut patch2_data = PatchDataBuilder::new(); | |
358 | patch2_data.replace(1, 3, &[2, 3]); |
|
358 | patch2_data.replace(1, 3, &[2, 3]); | |
359 | let mut patch2 = PatchList::new(patch2_data.get()); |
|
359 | let mut patch2 = PatchList::new(patch2_data.get()); | |
360 |
|
360 | |||
361 | let patch = patch1.combine(&mut patch2); |
|
361 | let patch = patch1.combine(&mut patch2); | |
362 |
|
362 | |||
363 | let result = patch.apply(&data); |
|
363 | let result = patch.apply(&data); | |
364 |
|
364 | |||
365 | assert_eq!(result, vec![1u8, 2, 3, 4]); |
|
365 | assert_eq!(result, vec![1u8, 2, 3, 4]); | |
366 | } |
|
366 | } | |
367 | } |
|
367 | } |
General Comments 0
You need to be logged in to leave comments.
Login now