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
49using ACG::Histogram;
50using ACG::HistogramT;
51
52class HistogramTest : public testing::Test {
53
54public:
56 {
57 }
58
59protected:
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
69TEST_F(HistogramTest, simpleDouble ) {
70 std::vector<double> v {1.0, 1.1, 2.0};
71 auto hist = ACG::create_histogram_auto(v,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(), 2u);
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}
88
89#if 0 // not yet implemented
90TEST_F(HistogramTest, simpleInt ) {
91 std::vector<int> v {0, 1, 1, 2, 5};
92 HistogramT<int> hist(v.begin(), v.end(), 3);
93 // should be: 0-1, 2-3, 4-5
94 // counts: 3 1 1
95
96 std::vector<size_t> correct_bins {2, 1, 1};
97 EXPECT_EQ(hist.getBins(), correct_bins);
98
99
100 auto widths = hist.getBinWidths();
101 ASSERT_EQ(widths.size(), 3);
102 EXPECT_EQ(widths[0], 2);
103 EXPECT_EQ(widths[1], 2);
104 EXPECT_EQ(widths[2], 2);
105
106 int width_sum = std::accumulate(widths.begin(), widths.end(), 0.0);
107 int total_width = hist.getTotalWidth();
108 EXPECT_EQ(total_width, 6);
109 EXPECT_EQ(total_width, width_sum);
110
111 std::vector<int> correct_boundaries {0, 2, 4, 5};
112 auto boundaries = hist.getBinBoundaries();
113 ASSERT_EQ(boundaries, correct_boundaries);
114}
115#endif