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