OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestHistogram.cpp
Go to the documentation of this file.
1
44#include <gtest/gtest.h>
45#include <cmath>
46#include <vector>
48
49class HistogramTest : public ::testing::Test {
50protected:
51 void SetUp() override {
52 // Test setup
53 }
54};
55
56TEST_F(HistogramTest, UniformRanges) {
59
60 EXPECT_EQ(gsl_histogram_bins(h), 10);
61
62 // Check range boundaries
63 double lower, upper;
64 gsl_histogram_get_range(h, 0, &lower, &upper);
65 EXPECT_DOUBLE_EQ(lower, 0.0);
66 EXPECT_DOUBLE_EQ(upper, 1.0);
67
68 gsl_histogram_get_range(h, 9, &lower, &upper);
69 EXPECT_DOUBLE_EQ(lower, 9.0);
70 EXPECT_DOUBLE_EQ(upper, 10.0);
71
73}
74
75TEST_F(HistogramTest, IncrementAndGet) {
78
79 // Increment some values
83
84 EXPECT_DOUBLE_EQ(gsl_histogram_get(h, 1), 2.0); // 1.5 and 1.7 in bin 1
85 EXPECT_DOUBLE_EQ(gsl_histogram_get(h, 2), 1.0); // 2.5 in bin 2
86
88}
89
90TEST_F(HistogramTest, CustomRanges) {
92 std::vector<double> ranges = {0.0, 1.0, 2.0, 5.0, 10.0, 20.0};
93 gsl_histogram_set_ranges(h, ranges.data(), ranges.size());
94
98
99 EXPECT_DOUBLE_EQ(gsl_histogram_get(h, 1), 1.0); // 1.5 in bin 1
100 EXPECT_DOUBLE_EQ(gsl_histogram_get(h, 2), 1.0); // 3.0 in bin 2
101 EXPECT_DOUBLE_EQ(gsl_histogram_get(h, 3), 1.0); // 7.5 in bin 3
102
104}
105
106TEST_F(HistogramTest, OutOfRange) {
109
110 // Values outside range should not increment any bin
113
114 // All bins should be zero
115 for (size_t i = 0; i < 10; ++i) {
116 EXPECT_DOUBLE_EQ(gsl_histogram_get(h, i), 0.0);
117 }
118
120}
121
122TEST_F(HistogramTest, Histogram2D) {
124 gsl_histogram2d_set_ranges_uniform(h, 0.0, 5.0, 0.0, 5.0);
125
126 EXPECT_EQ(gsl_histogram2d_nx(h), 5);
127 EXPECT_EQ(gsl_histogram2d_ny(h), 5);
128
129 // Increment some values
130 gsl_histogram2d_increment(h, 1.5, 2.5);
131 gsl_histogram2d_increment(h, 1.7, 2.3);
132
133 EXPECT_DOUBLE_EQ(gsl_histogram2d_get(h, 1, 2), 2.0);
134
136}
137
138TEST_F(HistogramTest, Histogram2DCustomRanges) {
140
141 std::vector<double> xranges = {0.0, 1.0, 3.0, 6.0};
142 std::vector<double> yranges = {0.0, 2.0, 4.0, 8.0};
143
144 gsl_histogram2d_set_ranges(h, xranges.data(), xranges.size(), yranges.data(), yranges.size());
145
146 gsl_histogram2d_increment(h, 2.0, 3.0);
147 gsl_histogram2d_increment(h, 4.0, 5.0);
148
149 EXPECT_DOUBLE_EQ(gsl_histogram2d_get(h, 1, 1), 1.0);
150 EXPECT_DOUBLE_EQ(gsl_histogram2d_get(h, 2, 2), 1.0);
151
153}
154
155TEST_F(HistogramTest, Histogram2DOutOfRange) {
157 gsl_histogram2d_set_ranges_uniform(h, 0.0, 5.0, 0.0, 5.0);
158
159 // Values outside range
160 gsl_histogram2d_increment(h, -1.0, 2.0);
161 gsl_histogram2d_increment(h, 2.0, -1.0);
162 gsl_histogram2d_increment(h, 6.0, 2.0);
163 gsl_histogram2d_increment(h, 2.0, 6.0);
164
165 // All bins should be zero
166 for (size_t i = 0; i < 5; ++i) {
167 for (size_t j = 0; j < 5; ++j) {
168 EXPECT_DOUBLE_EQ(gsl_histogram2d_get(h, i, j), 0.0);
169 }
170 }
171
173}
gsl_histogram2d * gsl_histogram2d_alloc(size_t nx, size_t ny)
Allocate a 2D histogram with nx by ny bins.
void gsl_histogram2d_increment(gsl_histogram2d *h, double x, double y)
Increment the bin containing x,y by 1.
void gsl_histogram_free(gsl_histogram *h)
Free a histogram allocated by gsl_histogram_alloc.
int gsl_histogram2d_set_ranges(gsl_histogram2d *h, const double *xrange, size_t nx, const double *yrange, size_t ny)
Set explicit x/y bin edges.
int gsl_histogram_get_range(const gsl_histogram *h, size_t i, double *lower, double *upper)
Get the range for a bin.
void gsl_histogram_increment(gsl_histogram *h, double x)
Increment the bin containing x by 1.
double gsl_histogram2d_get(const gsl_histogram2d *h, size_t i, size_t j)
Get the bin count for index .
size_t gsl_histogram2d_nx(const gsl_histogram2d *h)
Number of x bins.
void gsl_histogram_set_ranges_uniform(gsl_histogram *h, double xmin, double xmax)
Set uniform bin edges over .
size_t gsl_histogram2d_ny(const gsl_histogram2d *h)
Number of y bins.
gsl_histogram * gsl_histogram_alloc(size_t n)
Allocate a 1D histogram with n bins.
int gsl_histogram_set_ranges(gsl_histogram *h, const double *range, size_t n)
Set explicit bin edges for a histogram.
void gsl_histogram2d_set_ranges_uniform(gsl_histogram2d *h, double xmin, double xmax, double ymin, double ymax)
Set uniform x/y bin edges.
double gsl_histogram_get(const gsl_histogram *h, size_t i)
Get the bin count for index i.
void gsl_histogram2d_free(gsl_histogram2d *h)
Free a 2D histogram.
size_t gsl_histogram_bins(const gsl_histogram *h)
Number of bins in the histogram.
TEST_F(HistogramTest, UniformRanges)
void SetUp() override
2D histogram with explicit x/y bin edges.
1D histogram with explicit bin edges.