File size: 5,832 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use regex::Regex;
use lazy_static::lazy_static;
use std::io;
use std::io::BufRead;

lazy_static! {
    // Extract the `term index` from `loda-cpp check` output.
    static ref EXTRACT_TERM_INDEX: Regex = Regex::new(
        "^(\\d+) \\d+$"
    ).unwrap();
}

fn parse_line<S: AsRef<str>>(line: S) -> Option<u32> {
    let line: &str = line.as_ref();
    let re = &EXTRACT_TERM_INDEX;
    let captures = match re.captures(line) {
        Some(value) => value,
        None => {
            return None;
        }
    };
    let capture1: &str = captures.get(1).map_or("", |m| m.as_str());
    let number_of_correct_terms: u32 = match capture1.parse::<u32>() {
        Ok(value) => value,
        Err(_error) => {
            return None;
        }
    };
    Some(number_of_correct_terms)
}

fn extract_number_of_correct_terms(input: &str) -> u32 {
    let mut input_u8: &[u8] = input.as_bytes();
    let reader: &mut dyn io::BufRead = &mut input_u8;
    let mut last_term_index: u32 = 0;
    let mut current_line_number: u32 = 0;
    for line in reader.lines() {
        current_line_number += 1;
        let line: String = match line {
            Ok(value) => value,
            Err(error) => {
                error!("Problem reading line #{:?}. {:?}", current_line_number, error);
                continue;
            }
        };
        if let Some(term_index) = parse_line(&line) {
            last_term_index = term_index;
        }
    }
    last_term_index + 1
}

#[derive(Debug, PartialEq)]
pub enum LodaCppCheckStatus {
    FullMatch,
    PartialMatch,
    Timeout,
}

#[derive(Debug)]
pub struct LodaCppCheckResult {
    pub status: LodaCppCheckStatus,
    pub number_of_correct_terms: u32,
}

impl LodaCppCheckResult {
    pub fn parse<S: AsRef<str>>(input_raw: S, process_did_timeout: bool) -> anyhow::Result<LodaCppCheckResult> {
        let input_raw: &str = input_raw.as_ref();
        let input_trimmed: &str = input_raw.trim();
        let number_of_correct_terms: u32 = extract_number_of_correct_terms(&input_trimmed);
        if input_trimmed.ends_with("error") || input_trimmed.contains("verflow") {
            // When it's an `error` or `Overflow in cell`
            return Ok(Self {
                status: LodaCppCheckStatus::PartialMatch,
                number_of_correct_terms: number_of_correct_terms,
            });
        }
        if input_trimmed.ends_with("ok") || input_trimmed.ends_with("warning") {
            // When the entire b-file has been matched.
            return Ok(Self {
                status: LodaCppCheckStatus::FullMatch,
                number_of_correct_terms: number_of_correct_terms,
            });
        }
        if process_did_timeout {
            // When the command `loda-cpp check` exceeded the time limit, eg. 2 minutes,
            // so it's undecided wether it's a full match or partial match.
            return Ok(Self {
                status: LodaCppCheckStatus::Timeout,
                number_of_correct_terms: number_of_correct_terms,
            });
        }
        // Fallback
        return Ok(Self {
            status: LodaCppCheckStatus::PartialMatch,
            number_of_correct_terms: number_of_correct_terms,
        });
    }
}


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

    #[test]
    fn test_10000_parse_line_some() {
        assert_eq!(parse_line("0 0"), Some(0));
        assert_eq!(parse_line("42 100"), Some(42));
        assert_eq!(parse_line("10000 1"), Some(10000));
    }

    #[test]
    fn test_10001_parse_line_none() {
        assert_eq!(parse_line("-1 100"), None);
        assert_eq!(parse_line("ok"), None);
        assert_eq!(parse_line("error"), None);
        assert_eq!(parse_line("123 456 -> expected 500"), None);
    }

    #[test]
    fn test_20000_parse_full_match() {
        // Arrange
        let content = 
r#"
0 1
1 30
2 21
3 77
4 93
5 87
ok
"#;

        // Act
        let result = LodaCppCheckResult::parse(content, false).expect("Should be able to parse");

        // Assert
        assert_eq!(result.status, LodaCppCheckStatus::FullMatch);
        assert_eq!(result.number_of_correct_terms, 6);
    }    

    #[test]
    fn test_20001_parse_full_match() {
        // Arrange
        let content = 
r#"
0 1
1 30
2 21
3 77
4 93
warning
"#;

        // Act
        let result = LodaCppCheckResult::parse(content, false).expect("Should be able to parse");

        // Assert
        assert_eq!(result.status, LodaCppCheckStatus::FullMatch);
        assert_eq!(result.number_of_correct_terms, 5);
    }    

    #[test]
    fn test_30000_parse_partial_match() {
        // Arrange
        let content = 
r#"
0 2
1 1
2 0
3 1
4 0
5 0
6 1
7 0
8 1
9 0
10 0
11 9 -> expected 5
error
"#;

        // Act
        let result = LodaCppCheckResult::parse(content, false).expect("Should be able to parse");

        // Assert
        assert_eq!(result.status, LodaCppCheckStatus::PartialMatch);
        assert_eq!(result.number_of_correct_terms, 11);
    }    

    #[test]
    fn test_30001_parse_partial_match() {
        // Arrange
        let content = 
r#"
0 2
1 1
2 30
3 600
4 379
5 601
Overflow in cell $2; last operation: mul $2,2
warning
"#;

        // Act
        let result = LodaCppCheckResult::parse(content, false).expect("Should be able to parse");

        // Assert
        assert_eq!(result.status, LodaCppCheckStatus::PartialMatch);
        assert_eq!(result.number_of_correct_terms, 6);
    }    

    #[test]
    fn test_40000_parse_timeout() {
        // Arrange
        let content = 
r#"
0 2
1 1
2 0
3 1
4 0
5 0
6 1
"#;

        // Act
        let result = LodaCppCheckResult::parse(content, true).expect("Should be able to parse");

        // Assert
        assert_eq!(result.status, LodaCppCheckStatus::Timeout);
        assert_eq!(result.number_of_correct_terms, 7);
    }    
}