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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#![feature(specialization, atomic_min_max, try_from, assoc_unix_epoch)]
/// Welcome to the mutagen crate. Your entry point will probably be cargo-mutagen, so install it
/// right away.
#[macro_use]
extern crate lazy_static;

use std::env;
use std::sync::atomic::{AtomicUsize, Ordering};

include!(concat!(env!("OUT_DIR"), "/ops.rs"));

mod iterators;
pub mod bounded_loop;

mod coverage;
pub use coverage::report_coverage;

pub trait Defaulter: Sized {
    fn get_default(count: usize, flag: &AtomicUsize, mask: usize) -> Option<Self>;
}

impl<X> Defaulter for X {
    default fn get_default(_count: usize, _flag: &AtomicUsize, _mask: usize) -> Option<X> { None }
}

impl<X: Default> Defaulter for X {
    fn get_default(count: usize, flag: &AtomicUsize, mask: usize) -> Option<X> {
        if now(count, flag, mask) {
            Some(Default::default())
        } else {
            None
        }
    }
}

lazy_static! {
    static ref MU: Mutagen = {
        let count = env::var("MUTATION_COUNT").map(|s|s.parse().unwrap_or(0)).unwrap_or(0);
        Mutagen { x: AtomicUsize::new(count) }
    };
}

/// A global helper struct to keep a mutation count
///
/// This is used by the Mutagen crate to simplify the code it inserts.
/// You should have no business using it manually.
#[doc(hidden)]
pub struct Mutagen {
    x: AtomicUsize,
}

impl Mutagen {
    /// get the current mutation count
    pub fn get(&self) -> usize {
        self.x.load(Ordering::Relaxed)
    }

    /// check if the argument matches the current mutation count
    ///
    /// this simplifies operations like `if mutagen::MU.now(42) { .. }`
    pub fn now(&self, n: usize) -> bool {
        self.get() == n
    }

    pub fn diff(&self, n: usize) -> usize {
        self.get().wrapping_sub(n)
    }

    /// increment the mutation count
    pub fn next(&self) {
        self.x.fetch_add(1, Ordering::SeqCst);
    }

    /// use with if expressions, e.g. `if MU.t(..) { .. } else { .. }`
    pub fn t(&self, t: bool, n: usize) -> bool {
        match self.diff(n) {
            0 => true,
            1 => false,
            2 => !t,
            _ => t,
        }
    }

    /// use with while expressions, e.g. `while Mu.w(..) { .. }`
    ///
    /// this never leads to infinite loops
    pub fn w(&self, t: bool, n: usize) -> bool {
        if self.now(n) {
            false
        } else {
            t
        }
    }

    /// use instead of `==`
    pub fn eq<R, T: PartialEq<R>>(&self, x: T, y: R, n: usize) -> bool {
        match self.diff(n) {
            0 => true,
            1 => false,
            2 => x != y,
            _ => x == y,
        }
    }

    /// use instead of `!=`
    pub fn ne<R, T: PartialEq<R>>(&self, x: T, y: R, n: usize) -> bool {
        match self.diff(n) {
            0 => true,
            1 => false,
            2 => x == y,
            _ => x != y,
        }
    }

    /// use instead of `>` (or, switching operand order `<`)
    pub fn gt<R, T: PartialOrd<R>>(&self, x: &T, y: &R, n: usize) -> bool {
        match self.diff(n) {
            0 => false,
            1 => true,
            2 => x < y,
            3 => x <= y,
            4 => x >= y,
            5 => x == y,
            6 => x != y,
            _ => x > y,
        }
    }

    /// use instead of `>=` (or, switching operand order `<=`)
    pub fn ge<R, T: PartialOrd<R>>(&self, x: &T, y: &R, n: usize) -> bool {
        match self.diff(n) {
            0 => false,
            1 => true,
            2 => x < y,
            3 => x <= y,
            4 => x > y,
            5 => x == y,
            6 => x != y,
            _ => x >= y,
        }
    }

    pub fn forloop<'a, I: Iterator + 'a>(&self, i: I, n: usize) -> Box<Iterator<Item=I::Item> + 'a> {
        match self.diff(n) {
            0 => Box::new(iterators::NoopIterator{inner: i}),
            1 => Box::new(i.skip(1)),
            2 => Box::new(iterators::SkipLast::new(i)),
            3 => Box::new(iterators::SkipLast::new(i.skip(1))),
            _ => Box::new(i),
        }
    }
}

/// get the current mutation count
pub fn get() -> usize {
    MU.get()
}

/// check if the argument matches the current mutation count
///
/// this simplifies operations like `if mutagen::MU.now(42) { .. }`
pub fn now(n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 1), flag, mask);
    MU.now(n)
}

/// get the unsigned wrapping difference between the current mutation count and the given count
pub fn diff(n: usize, len: usize, flag: &AtomicUsize, mask: usize) -> usize {
    report_coverage(n..(n + len), flag, mask);
    MU.diff(n)
}

/// increment the mutation count
pub fn next() {
    MU.next()
}

/// use with if expressions, e.g. `if MU.t(..) { .. } else { .. }`
pub fn t(t: bool, n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 3), flag, mask);
    MU.t(t, n)
}

/// use with while expressions, e.g. `while Mu.w(..) { .. }`
///
/// this never leads to infinite loops
pub fn w(t: bool, n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 1), flag, mask);
    MU.w(t, n)
}

/// use instead of `==`
pub fn eq<R, T: PartialEq<R>>(x: T, y: R, n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 3), flag, mask);
    MU.eq(x, y, n)
}

/// use instead of `!=`
pub fn ne<R, T: PartialEq<R>>(x: T, y: R, n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 3), flag, mask);
    MU.ne(x, y, n)
}

/// use instead of `>` (or, switching operand order `<`)
pub fn gt<R, T: PartialOrd<R>>(x: &T, y: &R, n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 7), flag, mask);
    MU.gt(x, y, n)
}

/// use instead of `>=` (or, switching operand order `<=`)
pub fn ge<R, T: PartialOrd<R>>(x: &T, y: &R, n: usize, flag: &AtomicUsize, mask: usize) -> bool {
    report_coverage(n..(n + 7), flag, mask);
    MU.ge(x, y, n)
}

pub fn forloop<'a, I: Iterator + 'a>(i: I, n: usize, flag: &AtomicUsize, mask: usize) -> Box<Iterator<Item=I::Item > + 'a> {
    report_coverage(n..(n + 4), flag, mask);
    MU.forloop(i, n)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn eq_mutation() {
        let mu = Mutagen {
            x: AtomicUsize::new(0),
        };

        // Always true
        assert_eq!(true, mu.eq(&1, &0, 0));

        // Always false
        mu.next();
        assert_eq!(false, mu.eq(&1, &0, 0));

        // Checks inequality
        mu.next();
        assert_eq!(true, mu.eq(&0, &1, 0));
        assert_eq!(false, mu.eq(&1, &1, 0));

        // Checks equality
        mu.next();
        assert_eq!(true, mu.eq(&0, &0, 0));
        assert_eq!(false, mu.eq(&1, &0, 0));
    }

    #[test]
    fn ne_mutation() {
        let mu = Mutagen {
            x: AtomicUsize::new(0),
        };

        // Always true
        assert_eq!(true, mu.ne(&1, &0, 0));

        // Always false
        mu.next();
        assert_eq!(false, mu.ne(&1, &0, 0));

        // Checks equality
        mu.next();
        assert_eq!(false, mu.ne(&0, &1, 0));
        assert_eq!(true, mu.ne(&1, &1, 0));

        // Checks inequality
        mu.next();
        assert_eq!(false, mu.ne(&0, &0, 0));
        assert_eq!(true, mu.ne(&1, &0, 0));
    }

    #[test]
    fn eq_with_references() {
        let n = "test".as_bytes();
        let mu = Mutagen {
            x: AtomicUsize::new(0),
        };

        assert_eq!(true, mu.eq(&*n, &*b"test", 0));
    }
}