forked from google/differential-privacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
density_estimation_test.cc
106 lines (92 loc) · 3.32 KB
/
density_estimation_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "testing/density_estimation.h"
#include <limits>
#include "base/logging.h"
#include "base/testing/status_matchers.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "base/status.h"
namespace differential_privacy {
namespace testing {
namespace {
template <typename T>
class HistogramTest : public ::testing::Test {};
typedef ::testing::Types<int64_t, double> NumericTypes;
TYPED_TEST_SUITE(HistogramTest, NumericTypes);
TYPED_TEST(HistogramTest, EmptyHistogram) {
Histogram<TypeParam> hist(-2, .5, 8);
for (int i = 0; i < hist.NumBins(); ++i) {
EXPECT_EQ(0, hist.BinCountOrDie(i));
}
EXPECT_EQ(hist.Total(), 0);
EXPECT_EQ(hist.MaxBinCount(), 0);
}
TYPED_TEST(HistogramTest, AddOutOfBounds) {
Histogram<TypeParam> hist(-2, .5, 8);
EXPECT_EQ(hist.Add(-3).code(), absl::StatusCode::kInvalidArgument);
}
TYPED_TEST(HistogramTest, BinCountOutOfBounds) {
const int num_bins = 8;
Histogram<TypeParam> hist(-2, .5, num_bins);
EXPECT_EQ(hist.BinCount(-1).status().code(),
absl::StatusCode::kInvalidArgument);
EXPECT_EQ(hist.BinCount(num_bins + 1).status().code(),
absl::StatusCode::kInvalidArgument);
}
TYPED_TEST(HistogramTest, BinBoundaries) {
Histogram<TypeParam> hist(-2, .5, 8);
EXPECT_EQ(hist.BinBoundary(0), -2);
EXPECT_EQ(hist.BinBoundary(4), 0);
EXPECT_EQ(hist.BinBoundary(8), std::numeric_limits<TypeParam>::max());
}
TYPED_TEST(HistogramTest, SmallInputSet) {
Histogram<TypeParam> hist(-2, 2, 3);
EXPECT_OK(hist.Add(-1));
EXPECT_OK(hist.Add(0));
EXPECT_OK(hist.Add(0));
EXPECT_OK(hist.Add(100));
EXPECT_EQ(hist.BinCountOrDie(0), 1);
EXPECT_EQ(hist.BinCountOrDie(1), 2);
EXPECT_EQ(hist.BinCountOrDie(2), 1);
EXPECT_EQ(hist.Total(), 4);
EXPECT_EQ(hist.MaxBinCount(), 2);
}
TEST(HistogramTest, ToStringEmpty) {
Histogram<double> hist(0, 1.0 / 3.0, 2);
const std::string expected = absl::StrCat(
"\n", "-------------------------------------------------------------\n",
"Empty histogram.\n",
"-------------------------------------------------------------");
EXPECT_EQ(hist.ToString(), expected);
LOG(INFO) << hist.ToString();
}
TEST(HistogramTest, ToString) {
Histogram<double> hist(0, 1.0 / 3.0, 2);
ASSERT_OK(hist.Add(.25));
ASSERT_OK(hist.Add(.4));
ASSERT_OK(hist.Add(.4));
const std::string expected = absl::StrCat(
"\n", "-------------------------------------------------------------\n",
"[ 0, 0.333333) 1 33.333% ######\n",
"[ 0.333333, inf) 2 66.667% ############\n",
"-------------------------------------------------------------");
EXPECT_EQ(hist.ToString(), expected);
LOG(INFO) << hist.ToString();
}
} // namespace
} // namespace testing
} // namespace differential_privacy