1
exercism fetch rust dominoes

tests/dominoes.rs

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
extern crate dominoes;

use CheckResult::*;

type Domino = (usize, usize);

#[derive(Debug)]
enum CheckResult {
    GotInvalid,             // chain returned None
    Correct,
    ChainingFailure(Vec<Domino>), // failure to match the dots at the right side of one domino with
    // the one on the left side of the next
    LengthMismatch(Vec<Domino>),
    DominoMismatch(Vec<Domino>), // different dominoes are used in input and output
}

fn normalize(d: &Domino) -> Domino {
    match d {
        &(m, n) if m > n => (n, m),
        &(m, n) => (m, n)
    }
}

fn check(input: &Vec<Domino>) -> CheckResult {
    let output = match dominoes::chain(input) {
        None => return GotInvalid,
        Some(o) => o
    };
    if input.len() != output.len() {
        return LengthMismatch(output);
    }
    else if input.len() == 0 { // and thus output.len() == 0
        return Correct;
    }

    let mut output_sorted = output.iter().map(|d| normalize(d)).collect::<Vec<Domino>>();
    output_sorted.sort();
    let mut input_sorted = input.iter().map(|d| normalize(d)).collect::<Vec<Domino>>();
    input_sorted.sort();
    if input_sorted != output_sorted {
        return DominoMismatch(output);
    }

    // both input and output have at least 1 element
    // This essentially puts the first element after the last one, thereby making it
    // easy to check whether the domino chains "wraps around".
    let mut fail = false;
    {
        let mut n = output[0].1;
        let iter = output.iter().skip(1).chain(output.iter().take(1));
        for &(first, second) in iter {
            if n != first {
                fail = true;
                break
            }
            n = second
        }
    }
    if fail {
        ChainingFailure(output)
    }
    else {
        Correct
    }
}

fn assert_correct(input: &Vec<Domino>) {
    match check(&input) {
        Correct => (),
        GotInvalid => panic!("Unexpectedly got invalid on input {:?}", input),
        ChainingFailure(output) => panic!("Chaining failure for input {:?}, output {:?}", input, output),
        LengthMismatch(output) => panic!("Length mismatch for input {:?}, output {:?}", input, output),
        DominoMismatch(output) => panic!("Domino mismatch for input {:?}, output {:?}", input, output),
    }
}

#[test]
fn empty_input_empty_output() {
    let input = vec!();
    assert_eq!(dominoes::chain(&input), Some(vec!()));
}

#[test]
#[ignore]
fn singleton_input_singleton_output() {
    let input = vec!((1, 1));
    assert_correct(&input);
}

#[test]
#[ignore]
fn singleton_that_cant_be_chained() {
    let input = vec![(1, 2)];
    assert_eq!(dominoes::chain(&input), None);
}

#[test]
#[ignore]
fn no_repeat_numbers() {
    let input = vec!((1, 2), (3, 1), (2, 3));
    assert_correct(&input);
}

#[test]
#[ignore]
fn can_reverse_dominoes() {
    let input = vec![(1, 2), (1, 3), (2, 3)];
    assert_correct(&input);
}

#[test]
#[ignore]
fn no_chains() {
    let input = vec!((1, 2), (4, 1), (2, 3));
    assert_eq!(dominoes::chain(&input), None);
}

#[test]
#[ignore]
fn disconnected_simple() {
    let input = vec![(1, 1), (2, 2)];
    assert_eq!(dominoes::chain(&input), None);
}

#[test]
#[ignore]
fn disconnected_double_loop() {
    let input = vec![(1, 2), (2, 1), (3, 4), (4, 3)];
    assert_eq!(dominoes::chain(&input), None);
}

#[test]
#[ignore]
fn disconnected_single_isolated() {
    let input = vec![(1, 2), (2, 3), (3, 1), (4, 4)];
    assert_eq!(dominoes::chain(&input), None);
}

#[test]
#[ignore]
fn need_backtrack() {
    let input = vec![(1, 2), (2, 3), (3, 1), (2, 4), (2, 4)];
    assert_correct(&input);
}

#[test]
#[ignore]
fn separate_loops() {
    let input = vec![(1, 2), (2, 3), (3, 1), (1, 1), (2, 2), (3, 3)];
    assert_correct(&input);
}

#[test]
#[ignore]
fn nine_elements() {
    let input = vec!((1, 2), (5, 3), (3, 1), (1, 2), (2, 4), (1, 6), (2, 3), (3, 4), (5, 6));
    assert_correct(&input);
}