1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
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 *=========================================================================*/
18
19 #include "itkSparseFrequencyContainer2.h"
20
21
itkSparseFrequencyContainer2Test(int,char * [])22 int itkSparseFrequencyContainer2Test(int, char* [] )
23 {
24 std::cout << "SparseFrequencyContainer2 Test \n \n";
25
26 using SparseFrequencyContainer2Type = itk::Statistics::SparseFrequencyContainer2;
27
28
29 SparseFrequencyContainer2Type::Pointer container =
30 SparseFrequencyContainer2Type::New();
31
32 using AbsoluteFrequencyType = SparseFrequencyContainer2Type::AbsoluteFrequencyType;
33
34 constexpr unsigned int numberOfBins = 1250;
35
36 container->Initialize( numberOfBins );
37
38
39 // Test the SetFrequency() / GetFrequency() methods
40 {
41 std::cout << "Testing Set/Get Frequency methods...";
42 for( unsigned int bin=0; bin < numberOfBins; bin++ )
43 {
44 // Compute any value as frequency just to test the SetFrequency() method
45 const auto frequency = static_cast<AbsoluteFrequencyType>( bin * bin );
46 container->SetFrequency( bin, frequency );
47 }
48
49 for( unsigned int bin=0; bin < numberOfBins; bin++ )
50 {
51 // Test if the values can be read back
52 const auto frequency = static_cast<AbsoluteFrequencyType>( bin * bin );
53 const AbsoluteFrequencyType stored = container->GetFrequency( bin );
54 if( stored != frequency )
55 {
56 std::cout << "Failed !" << std::endl;
57 std::cout << "Stored Frequency in bin " << bin << " doesn't match value" << std::endl;
58 std::cout << "Value is = " << stored << " value should be " << frequency << std::endl;
59 return EXIT_FAILURE;
60 }
61 }
62 std::cout << " PASSED !" << std::endl;
63
64 } // end of SetFrequency() / GetFrequency() test
65
66 //Set all the bins to zero and check the values
67 container->SetToZero();
68 for( unsigned int bin=0; bin < numberOfBins; bin++ )
69 {
70 if( container->GetFrequency( bin ) != itk::NumericTraits< AbsoluteFrequencyType >::ZeroValue() )
71 {
72 std::cout << "Failed !" << std::endl;
73 std::cout << "Stored Frequency in bin is not zero after SetToZero() method invocation"
74 << std::endl;
75 return EXIT_FAILURE;
76 }
77 }
78
79 // Test the IncreaseFrequency() method
80 {
81 std::cout << "Testing IncreaseFrequency method...";
82 // Try not to depend on previous tests....So, we initialize the histogram again.
83 for( unsigned int bin=0; bin < numberOfBins; bin++ )
84 {
85 // Compute any value as frequency just to test the SetFrequency() method
86 const auto frequency = static_cast<AbsoluteFrequencyType>( bin * bin );
87 container->SetFrequency( bin, frequency );
88 }
89
90 // Now increment by a number (we use "bin", but any other will do it...)
91 for( unsigned int bin=0; bin < numberOfBins; bin++ )
92 {
93 // Compute any value as frequency just to test the IncreaseFrequency() method
94 const auto frequency = static_cast<AbsoluteFrequencyType>( bin );
95 container->IncreaseFrequency( bin, frequency );
96 }
97
98
99 // Test if the values can be read back
100 for( unsigned int bin=0; bin < numberOfBins; bin++ )
101 {
102 const auto frequency = static_cast<AbsoluteFrequencyType>( bin * bin + bin );
103 const AbsoluteFrequencyType stored = container->GetFrequency( bin );
104 if( stored != frequency )
105 {
106 std::cout << "Failed !" << std::endl;
107 std::cout << "Stored Frequency in bin " << bin << " doesn't match value" << std::endl;
108 std::cout << "Value is = " << stored << " value should be " << frequency << std::endl;
109 return EXIT_FAILURE;
110 }
111 }
112 std::cout << " PASSED !" << std::endl;
113 } // end of SetFrequency() / GetFrequency() test
114
115 std::cout << "Test passed." << std::endl;
116 return EXIT_SUCCESS;
117
118 }
119