Move random number generator inside of Mutator.
diff --git a/src/mutator.cc b/src/mutator.cc
index 43b4750..abd5b25 100644
--- a/src/mutator.cc
+++ b/src/mutator.cc
@@ -448,13 +448,13 @@
} // namespace
-Mutator::Mutator(RandomEngine* random) : random_(random) {}
+void Mutator::Seed(uint32_t value) { random_.seed(value); }
void Mutator::Mutate(Message* message, size_t size_increase_hint) {
bool repeat;
do {
repeat = false;
- MutationSampler mutation(keep_initialized_, random_, message);
+ MutationSampler mutation(keep_initialized_, &random_, message);
switch (mutation.mutation()) {
case Mutation::None:
break;
@@ -470,7 +470,7 @@
DeleteField()(mutation.field());
break;
case Mutation::Copy: {
- DataSourceSampler source(mutation.field(), random_, message);
+ DataSourceSampler source(mutation.field(), &random_, message);
if (source.IsEmpty()) {
repeat = true;
break;
@@ -530,20 +530,20 @@
// Shuffle
for (int j = 0; j < field_size2; ++j) {
- if (int k = GetRandomIndex(random_, field_size2 - j)) {
+ if (int k = GetRandomIndex(&random_, field_size2 - j)) {
reflection->SwapElements(message2, field, j, j + k);
}
}
- int keep = GetRandomIndex(random_, field_size2 + 1);
+ int keep = GetRandomIndex(&random_, field_size2 + 1);
if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
int remove = field_size2 - keep;
// Cross some message to keep with messages to remove.
- int cross = GetRandomIndex(random_, std::min(keep, remove) + 1);
+ int cross = GetRandomIndex(&random_, std::min(keep, remove) + 1);
for (int j = 0; j < cross; ++j) {
- int k = GetRandomIndex(random_, keep);
- int r = keep + GetRandomIndex(random_, remove);
+ int k = GetRandomIndex(&random_, keep);
+ int r = keep + GetRandomIndex(&random_, remove);
assert(k != r);
CrossOverImpl(reflection->GetRepeatedMessage(*message2, field, r),
reflection->MutableRepeatedMessage(message2, field, k));
@@ -556,10 +556,10 @@
} else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
if (!reflection->HasField(message1, field)) {
- if (GetRandomBool(random_))
+ if (GetRandomBool(&random_))
DeleteField()(FieldInstance(message2, field));
} else if (!reflection->HasField(*message2, field)) {
- if (GetRandomBool(random_)) {
+ if (GetRandomBool(&random_)) {
ConstFieldInstance source(&message1, field);
CopyField()(source, FieldInstance(message2, field));
}
@@ -568,7 +568,7 @@
reflection->MutableMessage(message2, field));
}
} else {
- if (GetRandomBool(random_)) {
+ if (GetRandomBool(&random_)) {
if (reflection->HasField(message1, field)) {
ConstFieldInstance source(&message1, field);
CopyField()(source, FieldInstance(message2, field));
@@ -623,58 +623,58 @@
}
}
-int32_t Mutator::MutateInt32(int32_t value) { return FlipBit(value, random_); }
+int32_t Mutator::MutateInt32(int32_t value) { return FlipBit(value, &random_); }
-int64_t Mutator::MutateInt64(int64_t value) { return FlipBit(value, random_); }
+int64_t Mutator::MutateInt64(int64_t value) { return FlipBit(value, &random_); }
uint32_t Mutator::MutateUInt32(uint32_t value) {
- return FlipBit(value, random_);
+ return FlipBit(value, &random_);
}
uint64_t Mutator::MutateUInt64(uint64_t value) {
- return FlipBit(value, random_);
+ return FlipBit(value, &random_);
}
-float Mutator::MutateFloat(float value) { return FlipBit(value, random_); }
+float Mutator::MutateFloat(float value) { return FlipBit(value, &random_); }
-double Mutator::MutateDouble(double value) { return FlipBit(value, random_); }
+double Mutator::MutateDouble(double value) { return FlipBit(value, &random_); }
bool Mutator::MutateBool(bool value) { return !value; }
size_t Mutator::MutateEnum(size_t index, size_t item_count) {
if (item_count <= 1) return 0;
- return (index + 1 + GetRandomIndex(random_, item_count - 1)) % item_count;
+ return (index + 1 + GetRandomIndex(&random_, item_count - 1)) % item_count;
}
std::string Mutator::MutateString(const std::string& value,
size_t size_increase_hint) {
std::string result = value;
- while (!result.empty() && GetRandomBool(random_)) {
- result.erase(GetRandomIndex(random_, result.size()), 1);
+ while (!result.empty() && GetRandomBool(&random_)) {
+ result.erase(GetRandomIndex(&random_, result.size()), 1);
}
- while (result.size() < size_increase_hint && GetRandomBool(random_)) {
- size_t index = GetRandomIndex(random_, result.size() + 1);
- result.insert(result.begin() + index, GetRandomIndex(random_, 1 << 8));
+ while (result.size() < size_increase_hint && GetRandomBool(&random_)) {
+ size_t index = GetRandomIndex(&random_, result.size() + 1);
+ result.insert(result.begin() + index, GetRandomIndex(&random_, 1 << 8));
}
if (result != value) return result;
if (result.empty()) {
- result.push_back(GetRandomIndex(random_, 1 << 8));
+ result.push_back(GetRandomIndex(&random_, 1 << 8));
return result;
}
if (!result.empty())
- FlipBit(result.size(), reinterpret_cast<uint8_t*>(&result[0]), random_);
+ FlipBit(result.size(), reinterpret_cast<uint8_t*>(&result[0]), &random_);
return result;
}
std::string Mutator::MutateUtf8String(const std::string& value,
size_t size_increase_hint) {
std::string str = MutateString(value, size_increase_hint);
- FixUtf8String(&str, random_);
+ FixUtf8String(&str, &random_);
return str;
}
@@ -686,12 +686,11 @@
// Randomly select one of the registered mutators. The default behavior is
// performed for index 0.
- size_t field_index = GetRandomIndex(random_, itr->second.size() + 1);
+ size_t field_index = GetRandomIndex(&random_, itr->second.size() + 1);
if (field_index == itr->second.size())
return false;
- if (GetRandomBool(random_, 100))
- itr->second[field_index](message);
+ if (GetRandomBool(&random_, 100)) itr->second[field_index](message);
return true;
}