JSONTest.cpp
6.19 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//===-- JSON Tests --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "JSON.h"
#include "LibcBenchmark.h"
#include "LibcMemoryBenchmark.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::AllOf;
using testing::ExplainMatchResult;
using testing::Field;
using testing::Pointwise;
namespace llvm {
namespace libc_benchmarks {
namespace {
Study getStudy() {
return Study{
HostState{
"CpuName", 123, {CacheInfo{"A", 1, 2, 3}, CacheInfo{"B", 4, 5, 6}}},
BenchmarkOptions{std::chrono::seconds(1), std::chrono::seconds(2), 10,
100, 6, 100, 0.1, 2, BenchmarkLog::Full},
StudyConfiguration{2, 3, SizeRange{4, 5, 6}, Align(8), 9, 10},
{FunctionMeasurements{"A",
{Measurement{3, std::chrono::seconds(3)},
Measurement{3, std::chrono::seconds(4)}}},
FunctionMeasurements{"B", {}}}};
}
static std::string SerializeToString(const Study &S) {
std::string Buffer;
raw_string_ostream RSO(Buffer);
json::OStream JOS(RSO);
SerializeToJson(S, JOS);
return Buffer;
}
MATCHER(EqualsCacheInfo, "") {
const CacheInfo &A = ::testing::get<0>(arg);
const CacheInfo &B = ::testing::get<1>(arg);
return ExplainMatchResult(AllOf(Field(&CacheInfo::Type, B.Type),
Field(&CacheInfo::Level, B.Level),
Field(&CacheInfo::Size, B.Size),
Field(&CacheInfo::NumSharing, B.NumSharing)),
A, result_listener);
}
auto Equals(const HostState &H) -> auto {
return AllOf(
Field(&HostState::CpuName, H.CpuName),
Field(&HostState::CpuFrequency, H.CpuFrequency),
Field(&HostState::Caches, Pointwise(EqualsCacheInfo(), H.Caches)));
}
auto Equals(const BenchmarkOptions &BO) -> auto {
return AllOf(
Field(&BenchmarkOptions::MinDuration, BO.MinDuration),
Field(&BenchmarkOptions::MaxDuration, BO.MaxDuration),
Field(&BenchmarkOptions::InitialIterations, BO.InitialIterations),
Field(&BenchmarkOptions::MaxIterations, BO.MaxIterations),
Field(&BenchmarkOptions::MinSamples, BO.MinSamples),
Field(&BenchmarkOptions::MaxSamples, BO.MaxSamples),
Field(&BenchmarkOptions::Epsilon, BO.Epsilon),
Field(&BenchmarkOptions::ScalingFactor, BO.ScalingFactor),
Field(&BenchmarkOptions::Log, BO.Log));
}
auto Equals(const SizeRange &SR) -> auto {
return AllOf(Field(&SizeRange::From, SR.From), Field(&SizeRange::To, SR.To),
Field(&SizeRange::Step, SR.Step));
}
auto Equals(const StudyConfiguration &SC) -> auto {
return AllOf(
Field(&StudyConfiguration::Runs, SC.Runs),
Field(&StudyConfiguration::BufferSize, SC.BufferSize),
Field(&StudyConfiguration::Size, Equals(SC.Size)),
Field(&StudyConfiguration::AddressAlignment, SC.AddressAlignment),
Field(&StudyConfiguration::MemsetValue, SC.MemsetValue),
Field(&StudyConfiguration::MemcmpMismatchAt, SC.MemcmpMismatchAt));
}
MATCHER(EqualsMeasurement, "") {
const Measurement &A = ::testing::get<0>(arg);
const Measurement &B = ::testing::get<1>(arg);
return ExplainMatchResult(AllOf(Field(&Measurement::Size, B.Size),
Field(&Measurement::Runtime, B.Runtime)),
A, result_listener);
}
MATCHER(EqualsFunctions, "") {
const FunctionMeasurements &A = ::testing::get<0>(arg);
const FunctionMeasurements &B = ::testing::get<1>(arg);
return ExplainMatchResult(
AllOf(Field(&FunctionMeasurements::Name, B.Name),
Field(&FunctionMeasurements::Measurements,
Pointwise(EqualsMeasurement(), B.Measurements))),
A, result_listener);
}
auto Equals(const Study &S) -> auto {
return AllOf(
Field(&Study::Host, Equals(S.Host)),
Field(&Study::Options, Equals(S.Options)),
Field(&Study::Configuration, Equals(S.Configuration)),
Field(&Study::Functions, Pointwise(EqualsFunctions(), S.Functions)));
}
TEST(JsonTest, RoundTrip) {
const Study S = getStudy();
auto StudyOrError = ParseJsonStudy(SerializeToString(S));
if (auto Err = StudyOrError.takeError())
EXPECT_FALSE(Err) << "Unexpected error";
const Study &Parsed = *StudyOrError;
EXPECT_THAT(Parsed, Equals(S));
}
TEST(JsonTest, SupplementaryField) {
auto Failure = ParseJsonStudy(R"({
"UnknownField": 10
}
)");
EXPECT_EQ(toString(Failure.takeError()), "Unknown field: UnknownField");
}
TEST(JsonTest, InvalidType) {
auto Failure = ParseJsonStudy(R"({
"Options": 1
}
)");
EXPECT_EQ(toString(Failure.takeError()), "Expected JSON Object");
}
TEST(JsonTest, InvalidDuration) {
auto Failure = ParseJsonStudy(R"({
"Options": {
"MinDuration": "Duration should be a Number"
}
}
)");
EXPECT_EQ(toString(Failure.takeError()), "Can't parse Duration");
}
TEST(JsonTest, InvalidAlignType) {
auto Failure = ParseJsonStudy(R"({
"Configuration":{
"AddressAlignment": "Align should be an Integer"
}
}
)");
EXPECT_EQ(toString(Failure.takeError()), "Can't parse Align, not an Integer");
}
TEST(JsonTest, InvalidAlign) {
auto Failure = ParseJsonStudy(R"({
"Configuration":{
"AddressAlignment":3
}
}
)");
EXPECT_EQ(toString(Failure.takeError()),
"Can't parse Align, not a power of two");
}
TEST(JsonTest, InvalidBenchmarkLogType) {
auto Failure = ParseJsonStudy(R"({
"Options":{
"Log": 3
}
}
)");
EXPECT_EQ(toString(Failure.takeError()),
"Can't parse BenchmarkLog, not a String");
}
TEST(JsonTest, InvalidBenchmarkLog) {
auto Failure = ParseJsonStudy(R"({
"Options":{
"Log": "Unknown"
}
}
)");
EXPECT_EQ(toString(Failure.takeError()),
"Can't parse BenchmarkLog, invalid value 'Unknown'");
}
} // namespace
} // namespace libc_benchmarks
} // namespace llvm