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
use rand::Rng; use super::traits::*; #[derive(PartialEq, Eq, Clone, Copy, Hash, Debug, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum SquareDirection { North, East, South, West, } impl DirectionTrait for SquareDirection { fn variants() -> &'static [SquareDirection] { static VARIANTS: &'static [SquareDirection] = &[SquareDirection::North, SquareDirection::East, SquareDirection::South, SquareDirection::West]; VARIANTS } } #[derive(PartialEq, Eq, Clone, Copy, Hash, Debug, Serialize, Deserialize)] pub struct SquareVector { pub x: isize, pub y: isize, } impl VectorTrait for SquareVector { type Direction = SquareDirection; fn distance(&self, other: &SquareVector) -> usize { let xdist = (self.x - other.x).abs(); let ydist = (self.y - other.y).abs(); (xdist + ydist) as usize } fn neighbour(&self, direction: &SquareDirection) -> SquareVector { match *direction { SquareDirection::North => { SquareVector { x: self.x, y: self.y - 1, } } SquareDirection::East => { SquareVector { x: self.x + 1, y: self.y, } } SquareDirection::South => { SquareVector { x: self.x, y: self.y + 1, } } SquareDirection::West => { SquareVector { x: self.x - 1, y: self.y, } } } } fn neighbours(&self) -> Vec<Self> { SquareDirection::variants() .into_iter() .map(|d| self.neighbour(d)) .collect() } } #[derive(PartialEq, Eq, Clone, Copy, Hash, Debug, Serialize, Deserialize)] pub struct SquareGrid { pub width: usize, pub height: usize, } impl SquareGrid { #[allow(dead_code)] pub fn new(width: usize, height: usize) -> SquareGrid { SquareGrid { width: width, height: height, } } } impl GridTrait for SquareGrid { type Vector = SquareVector; fn dimensions(&self) -> Vec<isize> { vec![self.width as isize, self.height as isize] } fn is_within_bounds(&self, v: SquareVector) -> bool { v.x >= 0 && v.x < (self.width as isize) && v.y >= 0 && v.y < (self.height as isize) } fn cells(&self) -> Vec<SquareVector> { unimplemented!(); } fn random_cell<R: Rng>(&self, rng: &mut R) -> SquareVector { let isize_width = self.width as isize; let isize_height = self.height as isize; SquareVector { x: rng.gen_range(0, isize_width), y: rng.gen_range(0, isize_height), } } } #[cfg(test)] mod tests { use quickcheck::{Gen, Arbitrary, quickcheck}; use super::*; use rand::OsRng; impl Arbitrary for SquareVector { fn arbitrary<G: Gen>(g: &mut G) -> SquareVector { let (x, y) = Arbitrary::arbitrary(g); return SquareVector { x: x, y: y }; } } impl Arbitrary for SquareDirection { fn arbitrary<G: Gen>(g: &mut G) -> SquareDirection { let i: usize = g.gen_range(0, 4); SquareDirection::variants()[i].clone() } } impl Arbitrary for SquareGrid { fn arbitrary<G: Gen>(g: &mut G) -> SquareGrid { let (mut width, mut height) = Arbitrary::arbitrary(g); if width == 0 { width = 1; } if height == 0 { height = 1; } return SquareGrid { width: width, height: height, }; } } fn identity_of_indescernibles_prop(v: SquareVector) -> bool { v.distance(&v) == 0 } #[test] fn identity_of_indescernibles() { quickcheck(identity_of_indescernibles_prop as fn(SquareVector) -> bool); } fn triangle_inequality_prop(u: SquareVector, v: SquareVector, w: SquareVector) -> bool { u.distance(&w) <= u.distance(&v) + v.distance(&w) } #[test] fn triangle_inequality() { quickcheck(triangle_inequality_prop as fn(SquareVector, SquareVector, SquareVector) -> bool); } fn symmetry_prop(v: SquareVector, w: SquareVector) -> bool { v.distance(&w) == w.distance(&v) } #[test] fn symmetry() { quickcheck(symmetry_prop as fn(SquareVector, SquareVector) -> bool); } fn neighbour_adjacency_prop(v: SquareVector, d: SquareDirection) -> bool { v.distance(&v.neighbour(&d)) == 1 } #[test] fn neighbour_adjacency() { quickcheck(neighbour_adjacency_prop as fn(SquareVector, SquareDirection) -> bool); } fn random_cells_within_bounds_prop(g: SquareGrid) -> bool { let mut osrng = OsRng::new().unwrap(); for _ in 0..1000 { let random_cell = g.random_cell(&mut osrng); if !g.is_within_bounds(random_cell) { return false; } } return true; } #[test] fn random_cells_within_bounds() { quickcheck(random_cells_within_bounds_prop as fn(SquareGrid) -> bool); } }