rust_3d/
test_helper.rs

1/*
2Copyright 2017 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! helper functions for testing (these functions unwrap and panic, only use for tests)
24
25use std::{
26    fs::File,
27    io::{prelude::*, BufReader},
28};
29
30use crate::{io::*, *};
31
32//------------------------------------------------------------------------------
33
34/// Ensures the content of two files is equal
35pub fn assert_files_equal(filepath1: &str, filepath2: &str) {
36    let mut f1 = File::open(filepath1).unwrap();
37    let mut f2 = File::open(filepath2).unwrap();
38
39    let mut s1 = String::new();
40    f1.read_to_string(&mut s1).unwrap();
41
42    let mut s2 = String::new();
43    f2.read_to_string(&mut s2).unwrap();
44
45    assert!(s1 == s2);
46}
47
48/// Tests a 2D filter by comparing the result of its usage on the test quare vs. its expected result
49pub fn test_filter_2d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
50where
51    F: IsFilter<P>,
52    P: IsBuildable2D + Clone,
53{
54    let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
55    let filter = FilterRandomAccessible::new(f);
56
57    let mut view = View::Full;
58    let mut pc = PointCloud2D::<P>::new();
59    let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
60    load_xy(&mut f, &mut pc).unwrap();
61
62    filter.filter(&pc, &mut view);
63
64    pc.apply_view(&view).unwrap();
65
66    let mut f_tmp = File::create(&path_tmp).unwrap();
67    save_xy(&mut f_tmp, &pc, " ", "\n").unwrap();
68    assert_files_equal(path_expected, &path_tmp);
69}
70
71/// Tests a 3D filter by comparing the result of its usage on the test cube vs. its expected result
72pub fn test_filter_3d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
73where
74    F: IsFilter<P>,
75    P: IsBuildable3D + Clone,
76{
77    let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
78    let filter = FilterRandomAccessible::new(f);
79
80    let mut view = View::Full;
81    let mut pc = PointCloud3D::<P>::new();
82    let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
83    load_xyz(&mut f, &mut pc).unwrap();
84
85    filter.filter(&pc, &mut view);
86
87    pc.apply_view(&view).unwrap();
88
89    let mut f_tmp = File::create(&path_tmp).unwrap();
90    save_xyz(&mut f_tmp, &pc, " ", "\n").unwrap();
91    assert_files_equal(path_expected, &path_tmp);
92}
93
94/// Can be used to write the expected result of a 2D filter to later compare against in a test
95pub fn write_expected_filter_2d<F, P>(f: F, path_expected: &str)
96where
97    F: IsFilter<P>,
98    P: IsBuildable2D + Clone,
99{
100    let filter = FilterRandomAccessible::new(f);
101
102    let mut view = View::Full;
103    let mut pc = PointCloud2D::<P>::new();
104    let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
105    load_xy(&mut f, &mut pc).unwrap();
106
107    filter.filter(&pc, &mut view);
108
109    let mut f_expected = File::create(path_expected).unwrap();
110    pc.apply_view(&view).unwrap();
111    save_xy(&mut f_expected, &pc, " ", "\n").unwrap();
112}
113
114/// Can be used to write the expected result of a 3D filter to later compare against in a test
115pub fn write_expected_filter_3d<F, P>(f: F, path_expected: &str)
116where
117    F: IsFilter<P>,
118    P: IsBuildable3D + Clone,
119{
120    let filter = FilterRandomAccessible::new(f);
121
122    let mut view = View::Full;
123    let mut pc = PointCloud3D::<P>::new();
124    let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
125    load_xyz(&mut f, &mut pc).unwrap();
126
127    filter.filter(&pc, &mut view);
128
129    let mut f_expected = File::create(path_expected).unwrap();
130    pc.apply_view(&view).unwrap();
131    save_xyz(&mut f_expected, &pc, " ", "\n").unwrap();
132}