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