• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 gRPC 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 #ifndef GRPC_TEST_CORE_TEST_UTIL_PROTO_BIT_GEN_H
16 #define GRPC_TEST_CORE_TEST_UTIL_PROTO_BIT_GEN_H
17 
18 #include <grpc/support/port_platform.h>
19 #include <stddef.h>
20 
21 #include <cstdint>
22 #include <limits>
23 #include <random>
24 #include <vector>
25 
26 namespace grpc_core {
27 
28 // Set of random numbers from a proto file (or other container) forming a bit
29 // source. Satisfies the requirements for a URNG.
30 class ProtoBitGen : public std::numeric_limits<uint64_t> {
31  public:
32   template <typename SourceContainer>
ProtoBitGen(const SourceContainer & c)33   explicit ProtoBitGen(const SourceContainer& c) {
34     for (auto r : c) {
35       results_.push_back(r);
36     }
37   }
38 
39   using result_type = uint64_t;
40 
operator()41   uint64_t operator()() {
42     if (current_ < results_.size()) {
43       return results_[current_++];
44     }
45     return generator_();
46   }
47 
48  private:
49   std::vector<uint64_t> results_;
50   size_t current_ = 0;
51   std::mt19937_64 generator_ = [this]() {
52     std::seed_seq seq(results_.begin(), results_.end());
53     return std::mt19937_64(seq);
54   }();
55 };
56 
57 }  // namespace grpc_core
58 
59 #endif  // GRPC_TEST_CORE_TEST_UTIL_PROTO_BIT_GEN_H
60