1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use arrayvec::ArrayVec;
use std::cmp::{max, min};
use std::io::Write;
const DEFAULT_BUFFER_SIZE: usize = 1024;
#[derive(Debug, Clone)]
pub struct UndoBuffer {
buffer: ArrayVec<[u8; DEFAULT_BUFFER_SIZE]>,
original: ArrayVec<[u8; DEFAULT_BUFFER_SIZE]>,
dirty: Option<(usize, usize)>,
}
impl UndoBuffer {
pub fn new(buf: &[u8]) -> Self {
let mut original = ArrayVec::<[u8; DEFAULT_BUFFER_SIZE]>::new();
let mut buffer = ArrayVec::<[u8; DEFAULT_BUFFER_SIZE]>::new();
(&mut original)
.write_all(buf)
.expect("Failed to copy into UndoBuffer");
(&mut buffer)
.write_all(buf)
.expect("Failed to copy into UndoBuffer");
Self {
original,
buffer,
dirty: None,
}
}
pub fn len(&self) -> usize {
self.buffer.len()
}
pub fn is_empty(&self) -> bool {
self.buffer.len() == 0
}
pub fn get_mut(&mut self) -> &mut [u8] {
self.dirty = Some((0, self.buffer.len()));
&mut self.buffer[..]
}
pub fn get_mut_range(&mut self, start: usize, end: usize) -> &mut [u8] {
let end = min(self.buffer.len(), end);
self.dirty = match self.dirty {
Some(range) => {
Some((min(range.0, start), max(range.1, end)))
}
None => Some((start, end)),
};
&mut self.buffer[start..end]
}
pub fn read(&self) -> &[u8] {
&self.buffer[..]
}
pub fn undo(&mut self) {
let (start, end) = match self.dirty {
None => {
return;
}
Some(range) => range,
};
(&mut self.buffer[start..end])
.write_all(&self.original[start..end])
.expect("Failed to write");
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mutators::bitflipper::BitFlipper;
#[test]
fn mutate_and_reset() {
let mut buffer = UndoBuffer::new(b"foo");
BitFlipper::mutate(buffer.get_mut(), 0, 1);
assert_eq!(buffer.read(), b"goo");
buffer.undo();
assert_eq!(buffer.read(), b"foo");
}
#[test]
fn mutate_reset_range() {
let (min, max) = (2, 3);
let mut buffer = UndoBuffer::new(b"foo");
let range = buffer.get_mut_range(min, max);
BitFlipper::mutate(range, 0, 1);
assert_ne!(buffer.read()[0..3], b"foo"[..]);
buffer.undo();
assert_eq!(buffer.read()[0..3], b"foo"[..]);
}
}