View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Provides unit tests for the TIFF simple-statistics class
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       * Test of getCountOfNulls method, of class TiffRasterStatistics.
61       */
62      @Test
63      public void testGetCountOfNulls() {
64          assertEquals(1, stat0.getCountOfNulls());
65          assertEquals(1, stat1.getCountOfNulls());
66      }
67  
68      /**
69       * Test of getCountOfSamples method, of class TiffRasterStatistics.
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       * Test of getExcludedValue method, of class TiffRasterStatistics.
79       */
80      @Test
81      public void testGetExcludedValue() {
82          assertTrue(Float.isNaN(stat0.getExcludedValue()));
83          assertEquals(width * height - 1, stat1.getExcludedValue());
84      }
85  
86      /**
87       * Test of getMaxValue method, of class TiffRasterStatistics.
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       * Test of getMeanValue method, of class TiffRasterStatistics.
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      * Test of getMinValue method, of class TiffRasterStatistics.
111      */
112     @Test
113     public void testGetMinValue() {
114         assertEquals(0, stat0.getMinValue());
115         assertEquals(0, stat1.getMinValue());
116     }
117 
118     /**
119      * Test of isAnExcludedValueSet method, of class TiffRasterStatistics.
120      */
121     @Test
122     public void testIsAnExcludedValueSet() {
123         assertFalse(stat0.isAnExcludedValueSet());
124         assertTrue(stat1.isAnExcludedValueSet());
125     }
126 
127 }