Developer Documentation

Histogram_test.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43 #include <gtest/gtest.h>
44
45 #include <ACG/Utils/Histogram.hh>
46
47 #include <numeric>
48
49 using ACG::Histogram;
50 using ACG::HistogramT;
51
52 class HistogramTest : public testing::Test {
53
54 public:
55 HistogramTest()
56 {
57 }
58
59 protected:
60 // This function is called before each test is run
61 virtual void SetUp() {
62 }
63
64 // This function is called after all tests are through
65 virtual void TearDown() {
66 }
67 };
68
69 TEST_F(HistogramTest, simpleDouble ) {
70 std::vector<double> v {1.0, 1.1, 2.0};
71 HistogramT<double> hist(v.begin(), v.end(), 2);
72
73 std::vector<size_t> correct_bins {2, 1};
74 EXPECT_EQ(hist.getBins(), correct_bins);
75
76
77 auto widths = hist.getBinWidths();
78 ASSERT_EQ(widths.size(), 2);
79 EXPECT_DOUBLE_EQ(widths[0], 0.5);
80 EXPECT_DOUBLE_EQ(widths[1], 0.5);
81
82 double width_sum = std::accumulate(widths.begin(), widths.end(), 0.0);
83 double total_width = hist.getTotalWidth();
84 EXPECT_DOUBLE_EQ(total_width, 1.0);
85 EXPECT_DOUBLE_EQ(total_width, width_sum);
86
87 auto boundaries = hist.getBinBoundaries();
88 ASSERT_EQ(boundaries.size(), 3);
89 EXPECT_DOUBLE_EQ(boundaries.at(0), 1);
90 EXPECT_DOUBLE_EQ(boundaries.at(1), 1.5);
91 EXPECT_DOUBLE_EQ(boundaries.at(2), 2);
92 }
93
94 #if 0 // not yet implemented
95 TEST_F(HistogramTest, simpleInt ) {
96 std::vector<int> v {0, 1, 1, 2, 5};
97 HistogramT<int> hist(v.begin(), v.end(), 3);
98 // should be: 0-1, 2-3, 4-5
99 // counts: 3 1 1
100
101 std::vector<size_t> correct_bins {2, 1, 1};
102 EXPECT_EQ(hist.getBins(), correct_bins);
103
104
105 auto widths = hist.getBinWidths();
106 ASSERT_EQ(widths.size(), 3);
107 EXPECT_EQ(widths[0], 2);
108 EXPECT_EQ(widths[1], 2);
109 EXPECT_EQ(widths[2], 2);
110
111 int width_sum = std::accumulate(widths.begin(), widths.end(), 0.0);
112 int total_width = hist.getTotalWidth();
113 EXPECT_EQ(total_width, 6);
114 EXPECT_EQ(total_width, width_sum);
115
116 std::vector<int> correct_boundaries {0, 2, 4, 5};
117 auto boundaries = hist.getBinBoundaries();
118 ASSERT_EQ(boundaries, correct_boundaries);
119 }
120 #endif
ACG::HistogramT
Definition: Histogram.hh:85
ACG::Histogram
Definition: Histogram.hh:57
HistogramTest
Definition: Histogram_test.cc:52