-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCropTest.m
137 lines (110 loc) · 5.61 KB
/
CropTest.m
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
classdef CropTest < matlab.unittest.TestCase
properties
testCube
end
properties (ClassSetupParameter)
height = struct('row', 1, ...
'small', 3, ...
'large', 10 ...
)
width = struct('column', 1, ...
'small', 3, ...
'large', 10 ...
)
nbands = struct('single', 1, ...
'rgb', 3, ...
'multi', 5, ...
'hyper', 100 ...
);
end
methods (TestClassSetup)
function createCube(testCase, width, height, nbands)
data = gallery('uniformdata', [height, width, nbands], 1);
% Suppress default value warnings for clearer test output
warning('off', 'Cube:DefaultQuantity');
warning('off', 'Cube:DefaultWavelengthUnit');
warning('off', 'Cube:DefaultWavelength');
warning('off', 'Cube:DefaultFWHM');
testCase.testCube = Cube(data);
% Turn warnings back on
warning('on', 'all');
end
end
methods (Test)
function cxParams(testCase)
% crop should accept either a position vector or two corner
% positions with identical results
c = testCase.testCube;
% Not testable on single-pixel data
testCase.assumeGreaterThan(c.Width, 1);
testCase.assumeGreaterThan(c.Height, 1);
pos = [1,1,1,1];
tl = [1,1];
br = [2,2];
testCase.verifyEqual(c.crop(pos).Data, c.crop(tl, br).Data);
end
function cxSize(testCase)
% crop should error if not supplied with a single position
% vector [x,y,w,h] or two corner positions [x,y], [x,y]
c = testCase.testCube;
testCase.verifyError(@()c.crop([1,1,1]), 'Cube:InvalidCoordinateSize');
testCase.verifyError(@()c.crop([1,1,1,1,1]), 'Cube:InvalidCoordinateSize');
testCase.verifyError(@()c.crop(1,[1,1]), 'Cube:InvalidCoordinateSize');
testCase.verifyError(@()c.crop([1,1],1), 'Cube:InvalidCoordinateSize');
testCase.verifyError(@()c.crop([1,1,1],[1,1]), 'Cube:InvalidCoordinateSize');
testCase.verifyError(@()c.crop([1,1],[1,1,1]), 'Cube:InvalidCoordinateSize');
end
function unnaturalCx(testCase)
% Coordinates must be integers greater than zero
c = testCase.testCube;
testCase.verifyError(@()c.crop([0 1], [1 1]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1 0], [1 1]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1 1], [0 1]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1 1], [1 0]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1.5 1], [1 1]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1 1.5], [1 1]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1 1], [1.5 1]), 'Cube:UnnaturalCoordinates');
testCase.verifyError(@()c.crop([1 1], [1 1.5]), 'Cube:UnnaturalCoordinates');
end
function cornerOutOfBounds(testCase)
% crop should error if given corner is not valid
c = testCase.testCube;
% The Cube width and height are the maximum valid coords
testCase.verifyError(@()c.crop([c.Width+1, 1], [1 1]), 'Cube:CornerOutOfBounds');
testCase.verifyError(@()c.crop([1, 1], [c.Width+1 1]), 'Cube:CornerOutOfBounds');
testCase.verifyError(@()c.crop([1, c.Height+1], [1 1]), 'Cube:CornerOutOfBounds');
testCase.verifyError(@()c.crop([1, 1], [1 c.Height+1]), 'Cube:CornerOutOfBounds');
end
function invalidCorner(testCase)
% crop should error if the first corner is not to the top left
% of the second corner (or equal)
c = testCase.testCube;
% Not testable on single-pixel data
testCase.assumeGreaterThan(c.Width, 1);
testCase.assumeGreaterThan(c.Height, 1);
testCase.verifyError(@()c.crop([2 1], [1 1]), 'Cube:InvalidCornerOrder');
testCase.verifyError(@()c.crop([1 2], [1 1]), 'Cube:InvalidCornerOrder');
testCase.verifyError(@()c.crop([2 2], [1 1]), 'Cube:InvalidCornerOrder');
end
function croppedWidth(testCase)
% Width of the cropped image should match the distance of the
% corner x-coordinates + 1 (inclusive)
c = testCase.testCube;
testCase.assumeGreaterThan(c.Width, 1);
w = c.Width;
h = c.Height;
testCase.verifyEqual(c.crop([1, 1], [w-1, h]).Width, w-1);
testCase.verifyEqual(c.crop([2, 1], [w, h]).Width, w-1);
end
function croppedHeight(testCase)
% Width of the cropped image should match the distance of the
% corner x-coordinates + 1 (inclusive)
c = testCase.testCube;
testCase.assumeGreaterThan(c.Height, 1);
w = c.Width;
h = c.Height;
testCase.verifyEqual(c.crop([1, 1], [w, h-1]).Height, h-1);
testCase.verifyEqual(c.crop([1, 2], [w, h]).Height, h-1);
end
end
end