1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.tiff;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 public class TiffRasterDataTest {
30
31 int width = 11;
32 int height = 10;
33 float[] data;
34 TiffRasterData raster;
35 float meanValue;
36
37 public TiffRasterDataTest() {
38 double sum = 0;
39 data = new float[width * height];
40 int k = 0;
41 for (int i = 0; i < width; i++) {
42 for (int j = 0; j < height; j++) {
43 data[k] = k;
44 sum += k;
45 k++;
46 }
47 }
48 raster = new TiffRasterDataFloat(width, height, data);
49 meanValue = (float) (sum / k);
50 }
51
52
53
54
55 @Test
56 public void testBadConstructor() {
57 assertThrows(IllegalArgumentException.class, () -> new TiffRasterDataFloat(-1, 10), "Constructor did not detect bad width");
58 assertThrows(IllegalArgumentException.class, () -> new TiffRasterDataFloat(10, -1), "Constructor did not detect bad height");
59 assertThrows(IllegalArgumentException.class, () -> new TiffRasterDataFloat(1, 1, 0), "Constructor did not detect bad samplesPerPixel");
60
61 final float[] f = new float[10];
62 assertThrows(IllegalArgumentException.class, () -> new TiffRasterDataFloat(2, 10, f), "Constructor did not detect insufficient input array size");
63 assertThrows(IllegalArgumentException.class, () -> new TiffRasterDataFloat(2, 3, 2, f), "Constructor did not detect insufficient input array size");
64 }
65
66
67
68
69 @Test
70 public void testBadCoordinates() {
71 final float[] f = new float[100];
72 final TiffRasterData instance = new TiffRasterDataFloat(10, 10, 1, f);
73 assertThrows(IllegalArgumentException.class, () -> instance.getValue(11, 11), "Access method getValue() did not detect bad coordinates");
74 assertThrows(IllegalArgumentException.class, () -> instance.setValue(11, 11, 5.0f), "Access method setValue() did not detect bad coordinates");
75 assertThrows(IllegalArgumentException.class, () -> instance.getValue(1, 1, 2), "Access method setValue() did not detect bad sample index");
76 }
77
78
79
80
81 @Test
82 public void testGetData() {
83 final float[] result = raster.getData();
84 assertArrayEquals(data, result);
85 final int samplesPerPixel = raster.getSamplesPerPixel();
86 assertEquals(1, samplesPerPixel, "Incorrect number of samples per pixel");
87 }
88
89
90
91
92 @Test
93 public void testGetDataType() {
94 final TiffRasterDataType dataType = raster.getDataType();
95 assertTrue(dataType == TiffRasterDataType.FLOAT, "Unexpected data type " + dataType.name());
96 }
97
98
99
100
101 @Test
102 public void testGetHeight() {
103 assertEquals(width, raster.getWidth(), "Improper height stored");
104 }
105
106
107
108
109 @Test
110 public void testGetIntData() {
111 for (int y = 0; y < height; y++) {
112 for (int x = 0; x < width; x++) {
113 final int index = y * width + x;
114 final int test = (int) data[index];
115 assertEquals(index, test, "Integer array access test failed at (" + x + "," + y + ")");
116 }
117 }
118 }
119
120
121
122
123 @Test
124 public void testGetSimpleStatistics_0args() {
125
126 final TiffRasterStatistics result = raster.getSimpleStatistics();
127 assertEquals(0, result.getMinValue(), "Min value failure");
128 assertEquals(width * height - 1, result.getMaxValue(), "Max value failure");
129 assertEquals(meanValue, result.getMeanValue(), "Mean value failure");
130 }
131
132
133
134
135 @Test
136 public void testGetSimpleStatistics_float() {
137
138
139 final TiffRasterStatistics result = raster.getSimpleStatistics(width * height - 1);
140 assertEquals(width * height - 2, result.getMaxValue(), "Max value failure");
141 }
142
143
144
145
146 @Test
147 public void testGetValue() {
148 for (int y = 0; y < height; y++) {
149 for (int x = 0; x < width; x++) {
150 final int index = y * width + x;
151 final int test = (int) raster.getValue(x, y);
152 assertEquals(index, test, "Get into source data test failed at (" + x + "," + y + ")");
153 final int iTest = raster.getIntValue(x, y);
154 assertEquals(index, iTest, "Get into source data test failed at (" + x + "," + y + ")");
155 }
156 }
157 }
158
159
160
161
162 @Test
163 public void testGetValue2() {
164 for (int y = 0; y < height; y++) {
165 for (int x = 0; x < width; x++) {
166 final int index = y * width + x;
167 final int test = (int) raster.getValue(x, y, 0);
168 assertEquals(index, test, "Get into source data test failed at (" + x + "," + y + ")");
169 final int iTest = raster.getIntValue(x, y, 0);
170 assertEquals(index, iTest, "Get into source data test failed at (" + x + "," + y + ")");
171 }
172 }
173 }
174
175
176
177
178 @Test
179 public void testGetWidth() {
180 assertEquals(width, raster.getWidth(), "Improper width stored");
181 }
182
183
184
185
186 @Test
187 public void testSetValue() {
188 final TiffRasterData instance = new TiffRasterDataFloat(width, height);
189 for (int y = 0; y < height; y++) {
190 for (int x = 0; x < width; x++) {
191 final int index = y * width + height;
192 instance.setValue(x, y, index);
193 final int test = (int) instance.getValue(x, y);
194 assertEquals(index, test, "Set/get value test failed at (" + x + "," + y + ")");
195 instance.setIntValue(x, y, index);
196 final int iTest = instance.getIntValue(x, y);
197 assertEquals(index, iTest, "Get/set value test failed at (" + x + "," + y + ")");
198 }
199 }
200 }
201
202
203
204
205 @Test
206 public void testSetValue2() {
207 final TiffRasterData instance = new TiffRasterDataFloat(width, height, 2);
208 for (int y = 0; y < height; y++) {
209 for (int x = 0; x < width; x++) {
210 final int index = y * width + height;
211 instance.setValue(x, y, 1, index);
212 final int test = (int) instance.getValue(x, y, 1);
213 assertEquals(index, test, "Set/get value test failed at (" + x + "," + y + ")");
214 instance.setIntValue(x, y, 1, index);
215 final int iTest = instance.getIntValue(x, y, 1);
216 assertEquals(index, iTest, "Get/set value test failed at (" + x + "," + y + ")");
217 }
218 }
219 }
220
221 }