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
/*
Copyright 2017 Martin Buck

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall
be included all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//! helper functions for testing (these functions unwrap and panic, only use for tests)

use std::{
    fs::File,
    io::{prelude::*, BufReader},
};

use crate::{io::*, *};

//------------------------------------------------------------------------------

/// Ensures the content of two files is equal
pub fn assert_files_equal(filepath1: &str, filepath2: &str) {
    let mut f1 = File::open(filepath1).unwrap();
    let mut f2 = File::open(filepath2).unwrap();

    let mut s1 = String::new();
    f1.read_to_string(&mut s1).unwrap();

    let mut s2 = String::new();
    f2.read_to_string(&mut s2).unwrap();

    assert!(s1 == s2);
}

/// Tests a 2D filter by comparing the result of its usage on the test quare vs. its expected result
pub fn test_filter_2d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
where
    F: IsFilter<P>,
    P: IsBuildable2D + Clone,
{
    let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
    let filter = FilterRandomAccessible::new(f);

    let mut view = View::Full;
    let mut pc = PointCloud2D::<P>::new();
    let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
    load_xy(&mut f, &mut pc).unwrap();

    filter.filter(&pc, &mut view);

    pc.apply_view(&view).unwrap();

    let mut f_tmp = File::create(&path_tmp).unwrap();
    save_xy(&mut f_tmp, &pc, " ", "\n").unwrap();
    assert_files_equal(path_expected, &path_tmp);
}

/// Tests a 3D filter by comparing the result of its usage on the test cube vs. its expected result
pub fn test_filter_3d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
where
    F: IsFilter<P>,
    P: IsBuildable3D + Clone,
{
    let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
    let filter = FilterRandomAccessible::new(f);

    let mut view = View::Full;
    let mut pc = PointCloud3D::<P>::new();
    let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
    load_xyz(&mut f, &mut pc).unwrap();

    filter.filter(&pc, &mut view);

    pc.apply_view(&view).unwrap();

    let mut f_tmp = File::create(&path_tmp).unwrap();
    save_xyz(&mut f_tmp, &pc, " ", "\n").unwrap();
    assert_files_equal(path_expected, &path_tmp);
}

/// Can be used to write the expected result of a 2D filter to later compare against in a test
pub fn write_expected_filter_2d<F, P>(f: F, path_expected: &str)
where
    F: IsFilter<P>,
    P: IsBuildable2D + Clone,
{
    let filter = FilterRandomAccessible::new(f);

    let mut view = View::Full;
    let mut pc = PointCloud2D::<P>::new();
    let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
    load_xy(&mut f, &mut pc).unwrap();

    filter.filter(&pc, &mut view);

    let mut f_expected = File::create(path_expected).unwrap();
    pc.apply_view(&view).unwrap();
    save_xy(&mut f_expected, &pc, " ", "\n").unwrap();
}

/// Can be used to write the expected result of a 3D filter to later compare against in a test
pub fn write_expected_filter_3d<F, P>(f: F, path_expected: &str)
where
    F: IsFilter<P>,
    P: IsBuildable3D + Clone,
{
    let filter = FilterRandomAccessible::new(f);

    let mut view = View::Full;
    let mut pc = PointCloud3D::<P>::new();
    let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
    load_xyz(&mut f, &mut pc).unwrap();

    filter.filter(&pc, &mut view);

    let mut f_expected = File::create(path_expected).unwrap();
    pc.apply_view(&view).unwrap();
    save_xyz(&mut f_expected, &pc, " ", "\n").unwrap();
}