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.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotEquals;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Arrays;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 public class TiffRasterStatisticsTest {
32
33 int width = 11;
34 int height = 10;
35 float[] data;
36 TiffRasterData raster;
37 float meanValue;
38 TiffRasterStatistics stat0;
39 TiffRasterStatistics stat1;
40
41 public TiffRasterStatisticsTest() {
42 double sum = 0;
43 data = new float[width * height];
44 int k = 0;
45 for (int i = 0; i < width; i++) {
46 for (int j = 0; j < height; j++) {
47 data[k] = k;
48 sum += k;
49 k++;
50 }
51 }
52 data[width * height / 2] = Float.NaN;
53 raster = new TiffRasterDataFloat(width, height, data);
54 meanValue = (float) (sum / k);
55 stat0 = raster.getSimpleStatistics();
56 stat1 = raster.getSimpleStatistics(stat0.getMaxValue());
57 }
58
59
60
61
62 @Test
63 public void testGetCountOfNulls() {
64 assertEquals(1, stat0.getCountOfNulls());
65 assertEquals(1, stat1.getCountOfNulls());
66 }
67
68
69
70
71 @Test
72 public void testGetCountOfSamples() {
73 assertEquals(width * height - 1, stat0.getCountOfSamples());
74 assertEquals(width * height - 2, stat1.getCountOfSamples());
75 }
76
77
78
79
80 @Test
81 public void testGetExcludedValue() {
82 assertTrue(Float.isNaN(stat0.getExcludedValue()));
83 assertEquals(width * height - 1, stat1.getExcludedValue());
84 }
85
86
87
88
89 @Test
90 public void testGetMaxValue() {
91 assertEquals(width * height - 1, stat0.getMaxValue());
92 assertEquals(width * height - 2, stat1.getMaxValue());
93 }
94
95
96
97
98 @Test
99 public void testGetMeanValue() {
100 assertNotEquals(0, stat0.getMeanValue());
101
102 final float[] zero = new float[100];
103 Arrays.fill(zero, 10);
104 final TiffRasterData zeroData = new TiffRasterDataFloat(10, 10, zero);
105 final TiffRasterStatistics zeroStat = zeroData.getSimpleStatistics(10);
106 assertEquals(0.0f, zeroStat.getMeanValue(), "Invalid mean data for excluded value");
107 }
108
109
110
111
112 @Test
113 public void testGetMinValue() {
114 assertEquals(0, stat0.getMinValue());
115 assertEquals(0, stat1.getMinValue());
116 }
117
118
119
120
121 @Test
122 public void testIsAnExcludedValueSet() {
123 assertFalse(stat0.isAnExcludedValueSet());
124 assertTrue(stat1.isAnExcludedValueSet());
125 }
126
127 }