tis_100/node/
test.rs

1use std::collections::LinkedList;
2use super::{Node, TestNode, TestState};
3use super::TestState::*;
4use core::Port::*;
5use image::Image;
6use io::IoBusView;
7
8#[derive(Debug)]
9pub struct TestInputNode {
10    test_data: LinkedList<isize>,
11    blocked: bool,
12}
13
14impl TestInputNode {
15    pub fn with_data(test_data: &Vec<isize>) -> TestInputNode {
16        TestInputNode {
17            test_data: test_data.iter().map(|&i| i).collect::<LinkedList<_>>(),
18            blocked: false,
19        }
20    }
21}
22
23impl Node for TestInputNode {
24    fn step(&mut self, io: &mut IoBusView) {
25        if !self.blocked {
26            if let Some(&val) = self.test_data.front() {
27                io.write(DOWN, val);
28                self.blocked = true;
29            }
30        }
31    }
32
33    fn sync(&mut self, io: &mut IoBusView) {
34        if !io.is_blocked() {
35            self.test_data.pop_front();
36            self.blocked = false;
37        }
38    }
39}
40
41#[derive(Debug)]
42pub struct TestOutputNode {
43    test_data: LinkedList<isize>,
44    results: Vec<(isize, isize)>,
45}
46
47impl TestOutputNode {
48    pub fn with_data(test_data: &Vec<isize>) -> TestOutputNode {
49        TestOutputNode {
50            test_data: test_data.iter().map(|&i| i).collect::<LinkedList<_>>(),
51            results: Vec::new(),
52        }
53    }
54}
55
56impl Node for TestOutputNode {
57    fn step(&mut self, io: &mut IoBusView) {
58        if let Some(val) = io.read(UP) {
59            if let Some(expected) = self.test_data.pop_front() {
60                self.results.push((expected, val));
61            }
62        }
63    }
64}
65
66impl TestNode for TestOutputNode {
67    fn state(&self) -> TestState {
68        if !self.test_data.is_empty() {
69            Testing
70        } else {
71            if self.results.iter().all(|&(e, a)| e == a) {
72                Passed
73            } else {
74                Failed
75            }
76        }
77    }
78}
79
80#[derive(Debug)]
81pub struct TestImageNode {
82    test_image: Image,
83    image: Image,
84}
85
86impl TestImageNode {
87    pub fn with_data(data: &Vec<isize>, width: usize, height: usize) -> TestImageNode {
88        TestImageNode {
89            test_image: Image::with_data(data, width, height),
90            image: Image::new(width, height),
91        }
92    }
93}
94
95impl Node for TestImageNode {
96    fn step(&mut self, io: &mut IoBusView) {
97        if let Some(val) = io.read(UP) {
98            self.image.write(val);
99        }
100    }
101}
102
103impl TestNode for TestImageNode {
104    fn state(&self) -> TestState {
105        if self.test_image == self.image {
106            Passed
107        } else {
108            Testing
109        }
110    }
111}