random_data.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 #include <inttypes.h>
21 #include <algorithm>
22 #include <bitset>
23 #include <climits>
24 #include <cstddef>
25 #include <functional>
26 #include <random>
28 
30 {
31 private:
32  using random_bytes_engine = std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>;
33  uint8_t* buf_;
35  size_t n_;
36 
37 public:
38  RandomDataTest(size_t n) : buf_(new uint8_t[n]), bp_(buf_, n), n_(n)
39  {
41  std::generate(buf_, buf_ + n, std::ref(rbe));
42  }
43 
45  {
46  delete buf_;
47  }
48 
49  BinParser getParser(bool skip = false)
50  {
51  return BinParser(buf_, n_ - (skip ? sizeof(int32_t) : 0));
52  }
53 
54  template <typename T>
55  T getNext()
56  {
57  T actual;
58  bp_.parse(actual);
59  return actual;
60  }
61 
62  template <typename T, size_t N>
63  std::bitset<N> getNext()
64  {
65  T actual;
66  bp_.parse(actual);
67  return std::bitset<N>(actual);
68  }
69 
70  template <typename T>
71  void set(T data, size_t pos)
72  {
73  std::memcpy(&data, buf_ + pos, sizeof(T));
74  }
75 
76  void skip(size_t n)
77  {
78  bp_.consume(n);
79  }
80 };
BinParser bp_
Definition: random_data.h:34
BinParser getParser(bool skip=false)
Definition: random_data.h:49
RandomDataTest(size_t n)
Definition: random_data.h:38
std::bitset< N > getNext()
Definition: random_data.h:63
uint8_t * buf_
Definition: random_data.h:33
void parse(T &val)
Definition: bin_parser.h:96
void skip(size_t n)
Definition: random_data.h:76
std::independent_bits_engine< std::default_random_engine, CHAR_BIT, uint8_t > random_bytes_engine
Definition: random_data.h:32
void consume()
Definition: bin_parser.h:175


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00