Skip to content

Commit edfb617

Browse files
committed
use seeds in fuzztest & some small changes
1 parent 94d1bba commit edfb617

7 files changed

Lines changed: 82 additions & 58 deletions

File tree

include/zeroerr/domains/arbitrary.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class Arbitrary<
8484
T, typename std::enable_if<detail::is_specialization<T, std::basic_string>::value>::type>
8585
: public Domain<T, std::vector<typename T::value_type>> {
8686
Arbitrary<std::vector<typename T::value_type>> impl;
87+
8788
public:
8889
using ValueType = T;
8990
using CorpusType = std::vector<typename T::value_type>;
@@ -93,9 +94,7 @@ class Arbitrary<
9394
return CorpusType(v.begin(), v.end());
9495
}
9596

96-
CorpusType GetRandomCorpus(Rng& rng) const override {
97-
return impl.GetRandomCorpus(rng);
98-
}
97+
CorpusType GetRandomCorpus(Rng& rng) const override { return impl.GetRandomCorpus(rng); }
9998

10099
void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
101100
impl.Mutate(rng, v, only_shrink);
@@ -126,11 +125,13 @@ class Arbitrary<
126125

127126
template <typename T, typename U>
128127
class Arbitrary<std::pair<T, U>>
129-
: public AggregateOf<std::pair<typename std::remove_const<T>::type, typename std::remove_const<U>::type>> {};
128+
: public AggregateOf<
129+
std::pair<typename std::remove_const<T>::type, typename std::remove_const<U>::type>> {};
130130

131131

132132
template <typename... T>
133-
class Arbitrary<std::tuple<T...>> : public AggregateOf<std::tuple<typename std::remove_const<T>::type...>> {};
133+
class Arbitrary<std::tuple<T...>>
134+
: public AggregateOf<std::tuple<typename std::remove_const<T>::type...>> {};
134135

135136
template <typename T>
136137
class Arbitrary<const T> : public Arbitrary<T> {};

include/zeroerr/domains/element_of.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ class ElementOf : public Domain<T, uint64_t> {
2323

2424
CorpusType FromValue(const ValueType& v) const override {
2525
for (size_t i = 0; i < elements.size(); i++) {
26-
if (elements[i] == v) {
27-
return i;
28-
}
26+
if (elements[i] == v) return i;
2927
}
3028
return 0;
3129
}

include/zeroerr/fuzztest.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,32 @@ extern void RunFuzzTest(IFuzzTest& fuzz_test, int seed = 0, int runs = 1000, int
134134

135135
template <typename TargetFunction, typename FuncType, typename Domain, typename Base>
136136
struct FuzzTestWithDomain : public Base {
137-
FuzzTestWithDomain(const Base& ft, const Domain& domain) : Base(ft), domain(domain) {}
137+
FuzzTestWithDomain(const Base& ft, const Domain& domain) : Base(ft), m_domain(domain) {}
138138

139139
virtual void Run(int _count = 1000, int _seed = 0) override {
140140
Base::count = 1;
141141
Base::max_count = _count;
142-
rng = new Rng(_seed);
142+
m_rng = new Rng(_seed);
143143
RunFuzzTest(*this, _seed, _count, 500, 1200, 1);
144-
delete rng;
145-
rng = nullptr;
144+
delete m_rng;
145+
m_rng = nullptr;
146+
}
147+
148+
typename Domain::CorpusType GetRandomCorpus() {
149+
Rng& rng = *this->m_rng;
150+
if (Base::seeds.size() > 0 && rng.bounded(2) == 0) {
151+
return m_domain.FromValue(Base::seeds[rng() % Base::seeds.size()]);
152+
}
153+
return m_domain.GetRandomCorpus(rng);
146154
}
147155

148156
typename Domain::CorpusType TryParse(const std::string& input) {
149157
try {
150158
IRObject obj = IRObject::FromString(input);
151-
if (obj.type == IRObject::Type::Undefined) return domain.GetRandomCorpus(*rng);
152-
return domain.ParseCorpus(obj);
159+
if (obj.type == IRObject::Type::Undefined) return GetRandomCorpus();
160+
return m_domain.ParseCorpus(obj);
153161
} catch (...) {
154-
return domain.GetRandomCorpus(*rng);
162+
return GetRandomCorpus();
155163
}
156164
}
157165

@@ -164,22 +172,22 @@ struct FuzzTestWithDomain : public Base {
164172
Base::count++;
165173
std::string input = std::string((const char*)data, size);
166174
typename Domain::CorpusType corpus = TryParse(input);
167-
typename Domain::ValueType value = domain.GetValue(corpus);
175+
typename Domain::ValueType value = m_domain.GetValue(corpus);
168176
apply(value, detail::gen_seq<std::tuple_size<typename Domain::ValueType>::value>{});
169177
}
170178

171179
virtual std::string MutateData(const uint8_t* data, size_t size, size_t max_size,
172180
unsigned int seed) override {
173-
Rng mrng(seed);
181+
Rng rng(seed);
174182
std::string input = std::string((const char*)data, size);
175183
typename Domain::CorpusType corpus = TryParse(input);
176-
domain.Mutate(mrng, corpus, false);
177-
IRObject mutated_obj = domain.SerializeCorpus(corpus);
184+
m_domain.Mutate(rng, corpus, false);
185+
IRObject mutated_obj = m_domain.SerializeCorpus(corpus);
178186
return IRObject::ToString(mutated_obj);
179187
}
180188

181-
Domain domain;
182-
Rng* rng;
189+
Domain m_domain;
190+
Rng* m_rng;
183191
};
184192

185193

include/zeroerr/internal/config.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
#define ZEROERR_VERSION_MAJOR 0
44
#define ZEROERR_VERSION_MINOR 2
5-
#define ZEROERR_VERSION_PATCH 0
5+
#define ZEROERR_VERSION_PATCH 1
66
#define ZEROERR_VERSION \
77
(ZEROERR_VERSION_MAJOR * 10000 + ZEROERR_VERSION_MINOR * 100 + ZEROERR_VERSION_PATCH)
8-
#define ZEROERR_VERSION_STR "0.2.0"
8+
9+
#define ZEROERR_STR(x) #x
10+
11+
#define ZEROERR_VERSION_STR_BUILDER(a, b, c) ZEROERR_STR(a) "." ZEROERR_STR(b) "." ZEROERR_STR(c)
12+
#define ZEROERR_VERSION_STR \
13+
ZEROERR_VERSION_STR_BUILDER(ZEROERR_VERSION_MAJOR, ZEROERR_VERSION_MINOR, ZEROERR_VERSION_PATCH)
914

1015
// If you just wish to use the color without dynamic
1116
// enable or disable it, you can uncomment the following line

include/zeroerr/internal/typetraits.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#pragma once
22
#include "zeroerr/internal/config.h"
33

4-
#include <type_traits>
54
#include <ostream>
65
#include <sstream>
76
#include <string>
87
#include <tuple> // this should be removed
8+
#include <type_traits>
99

1010
ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
1111

@@ -223,7 +223,7 @@ void visit2_at(std::tuple<Ts...>& tup1, std::tuple<T2s...> const& tup2, size_t i
223223

224224
} // namespace detail
225225

226-
} // namespace zeroerr
226+
} // namespace zeroerr
227227

228228

229229
ZEROERR_SUPPRESS_COMMON_WARNINGS_POP

src/unittest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,10 @@ class XmlReporter : public IReporter {
676676

677677
struct TestCaseData {
678678
struct TestCase {
679-
std::string filename, name;
680-
unsigned line;
681-
double time;
682-
TestContext context;
679+
std::string filename, name;
680+
unsigned line;
681+
double time;
682+
TestContext context;
683683
};
684684

685685
std::vector<TestCase> testcases;

zeroerr.hpp

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55

66
#define ZEROERR_VERSION_MAJOR 0
77
#define ZEROERR_VERSION_MINOR 2
8-
#define ZEROERR_VERSION_PATCH 0
8+
#define ZEROERR_VERSION_PATCH 1
99
#define ZEROERR_VERSION \
1010
(ZEROERR_VERSION_MAJOR * 10000 + ZEROERR_VERSION_MINOR * 100 + ZEROERR_VERSION_PATCH)
11-
#define ZEROERR_VERSION_STR "0.2.0"
11+
12+
#define ZEROERR_STR(x) #x
13+
14+
#define ZEROERR_VERSION_STR_BUILDER(a, b, c) ZEROERR_STR(a) "." ZEROERR_STR(b) "." ZEROERR_STR(c)
15+
#define ZEROERR_VERSION_STR \
16+
ZEROERR_VERSION_STR_BUILDER(ZEROERR_VERSION_MAJOR, ZEROERR_VERSION_MINOR, ZEROERR_VERSION_PATCH)
1217

1318
// If you just wish to use the color without dynamic
1419
// enable or disable it, you can uncomment the following line
@@ -686,11 +691,11 @@ __attribute__((always_inline)) __inline__ static bool isDebuggerActive() { retur
686691
#pragma once
687692

688693

689-
#include <type_traits>
690694
#include <ostream>
691695
#include <sstream>
692696
#include <string>
693697
#include <tuple> // this should be removed
698+
#include <type_traits>
694699

695700
ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
696701

@@ -908,7 +913,7 @@ void visit2_at(std::tuple<Ts...>& tup1, std::tuple<T2s...> const& tup2, size_t i
908913

909914
} // namespace detail
910915

911-
} // namespace zeroerr
916+
} // namespace zeroerr
912917

913918

914919
ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
@@ -2052,9 +2057,7 @@ class ElementOf : public Domain<T, uint64_t> {
20522057

20532058
CorpusType FromValue(const ValueType& v) const override {
20542059
for (size_t i = 0; i < elements.size(); i++) {
2055-
if (elements[i] == v) {
2056-
return i;
2057-
}
2060+
if (elements[i] == v) return i;
20582061
}
20592062
return 0;
20602063
}
@@ -2410,6 +2413,7 @@ class Arbitrary<
24102413
T, typename std::enable_if<detail::is_specialization<T, std::basic_string>::value>::type>
24112414
: public Domain<T, std::vector<typename T::value_type>> {
24122415
Arbitrary<std::vector<typename T::value_type>> impl;
2416+
24132417
public:
24142418
using ValueType = T;
24152419
using CorpusType = std::vector<typename T::value_type>;
@@ -2419,9 +2423,7 @@ class Arbitrary<
24192423
return CorpusType(v.begin(), v.end());
24202424
}
24212425

2422-
CorpusType GetRandomCorpus(Rng& rng) const override {
2423-
return impl.GetRandomCorpus(rng);
2424-
}
2426+
CorpusType GetRandomCorpus(Rng& rng) const override { return impl.GetRandomCorpus(rng); }
24252427

24262428
void Mutate(Rng& rng, CorpusType& v, bool only_shrink) const override {
24272429
impl.Mutate(rng, v, only_shrink);
@@ -2452,11 +2454,13 @@ class Arbitrary<
24522454

24532455
template <typename T, typename U>
24542456
class Arbitrary<std::pair<T, U>>
2455-
: public AggregateOf<std::pair<typename std::remove_const<T>::type, typename std::remove_const<U>::type>> {};
2457+
: public AggregateOf<
2458+
std::pair<typename std::remove_const<T>::type, typename std::remove_const<U>::type>> {};
24562459

24572460

24582461
template <typename... T>
2459-
class Arbitrary<std::tuple<T...>> : public AggregateOf<std::tuple<typename std::remove_const<T>::type...>> {};
2462+
class Arbitrary<std::tuple<T...>>
2463+
: public AggregateOf<std::tuple<typename std::remove_const<T>::type...>> {};
24602464

24612465
template <typename T>
24622466
class Arbitrary<const T> : public Arbitrary<T> {};
@@ -3926,24 +3930,32 @@ extern void RunFuzzTest(IFuzzTest& fuzz_test, int seed = 0, int runs = 1000, int
39263930

39273931
template <typename TargetFunction, typename FuncType, typename Domain, typename Base>
39283932
struct FuzzTestWithDomain : public Base {
3929-
FuzzTestWithDomain(const Base& ft, const Domain& domain) : Base(ft), domain(domain) {}
3933+
FuzzTestWithDomain(const Base& ft, const Domain& domain) : Base(ft), m_domain(domain) {}
39303934

39313935
virtual void Run(int _count = 1000, int _seed = 0) override {
39323936
Base::count = 1;
39333937
Base::max_count = _count;
3934-
rng = new Rng(_seed);
3938+
m_rng = new Rng(_seed);
39353939
RunFuzzTest(*this, _seed, _count, 500, 1200, 1);
3936-
delete rng;
3937-
rng = nullptr;
3940+
delete m_rng;
3941+
m_rng = nullptr;
3942+
}
3943+
3944+
typename Domain::CorpusType GetRandomCorpus() {
3945+
Rng& rng = *this->m_rng;
3946+
if (Base::seeds.size() > 0 && rng.bounded(2) == 0) {
3947+
return m_domain.FromValue(Base::seeds[rng() % Base::seeds.size()]);
3948+
}
3949+
return m_domain.GetRandomCorpus(rng);
39383950
}
39393951

39403952
typename Domain::CorpusType TryParse(const std::string& input) {
39413953
try {
39423954
IRObject obj = IRObject::FromString(input);
3943-
if (obj.type == IRObject::Type::Undefined) return domain.GetRandomCorpus(*rng);
3944-
return domain.ParseCorpus(obj);
3955+
if (obj.type == IRObject::Type::Undefined) return GetRandomCorpus();
3956+
return m_domain.ParseCorpus(obj);
39453957
} catch (...) {
3946-
return domain.GetRandomCorpus(*rng);
3958+
return GetRandomCorpus();
39473959
}
39483960
}
39493961

@@ -3956,22 +3968,22 @@ struct FuzzTestWithDomain : public Base {
39563968
Base::count++;
39573969
std::string input = std::string((const char*)data, size);
39583970
typename Domain::CorpusType corpus = TryParse(input);
3959-
typename Domain::ValueType value = domain.GetValue(corpus);
3971+
typename Domain::ValueType value = m_domain.GetValue(corpus);
39603972
apply(value, detail::gen_seq<std::tuple_size<typename Domain::ValueType>::value>{});
39613973
}
39623974

39633975
virtual std::string MutateData(const uint8_t* data, size_t size, size_t max_size,
39643976
unsigned int seed) override {
3965-
Rng mrng(seed);
3977+
Rng rng(seed);
39663978
std::string input = std::string((const char*)data, size);
39673979
typename Domain::CorpusType corpus = TryParse(input);
3968-
domain.Mutate(mrng, corpus, false);
3969-
IRObject mutated_obj = domain.SerializeCorpus(corpus);
3980+
m_domain.Mutate(rng, corpus, false);
3981+
IRObject mutated_obj = m_domain.SerializeCorpus(corpus);
39703982
return IRObject::ToString(mutated_obj);
39713983
}
39723984

3973-
Domain domain;
3974-
Rng* rng;
3985+
Domain m_domain;
3986+
Rng* m_rng;
39753987
};
39763988

39773989

@@ -5528,10 +5540,10 @@ class XmlReporter : public IReporter {
55285540

55295541
struct TestCaseData {
55305542
struct TestCase {
5531-
std::string filename, name;
5532-
unsigned line;
5533-
double time;
5534-
TestContext context;
5543+
std::string filename, name;
5544+
unsigned line;
5545+
double time;
5546+
TestContext context;
55355547
};
55365548

55375549
std::vector<TestCase> testcases;

0 commit comments

Comments
 (0)