• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "fuzzers/tint_ast_fuzzer/probability_context.h"
16 
17 #include <cassert>
18 
19 namespace tint {
20 namespace fuzzers {
21 namespace ast_fuzzer {
22 namespace {
23 
24 const std::pair<uint32_t, uint32_t> kChanceOfReplacingIdentifiers = {30, 70};
25 
26 }  // namespace
27 
ProbabilityContext(RandomGenerator * generator)28 ProbabilityContext::ProbabilityContext(RandomGenerator* generator)
29     : generator_(generator),
30       chance_of_replacing_identifiers_(
31           RandomFromRange(kChanceOfReplacingIdentifiers)) {
32   assert(generator != nullptr && "generator must not be nullptr");
33 }
34 
RandomFromRange(std::pair<uint32_t,uint32_t> range)35 uint32_t ProbabilityContext::RandomFromRange(
36     std::pair<uint32_t, uint32_t> range) {
37   assert(range.first <= range.second && "Range must be non-decreasing");
38   return generator_->GetUInt32(
39       range.first, range.second + 1);  // + 1 need since range is inclusive.
40 }
41 
42 }  // namespace ast_fuzzer
43 }  // namespace fuzzers
44 }  // namespace tint
45