Web Wispers 1.2.2 C++ Unit Test Coverage |
|
|
|
|
Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2009 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : /**
18 : : * @file
19 : : * This header defines the Random class.
20 : : */
21 : :
22 : : #ifndef __SSRC_WSPR_UTILITY_RANDOM_H
23 : : #define __SSRC_WSPR_UTILITY_RANDOM_H
24 : :
25 : : #include <ssrc/wispers-packages.h>
26 : :
27 : : #include <fcntl.h>
28 : : #include <unistd.h>
29 : :
30 : : #include <cerrno>
31 : : #include <cstring>
32 : : #include <stdexcept>
33 : :
34 : : #include <boost/integer_traits.hpp>
35 : : #include <boost/nondet_random.hpp>
36 : : #include <boost/random/uniform_int.hpp>
37 : : #include <boost/random/variate_generator.hpp>
38 : :
39 : : __BEGIN_NS_SSRC_WSPR_UTILITY
40 : :
41 : : class random_device {
42 : : int _fd;
43 : :
44 : : public:
45 : 15 : explicit random_device(const std::string & device_file = "/dev/urandom")
46 : : SSRC_DECL_THROW(std::runtime_error) :
47 : 15 : _fd(::open(device_file.c_str(), O_RDONLY))
48 : : {
49 [ - + ]: 15 : if(_fd < 0)
50 [ # # # # : 0 : throw std::runtime_error(std::strerror(errno));
# # # # ]
51 : 15 : }
52 : :
53 : 15 : ~random_device() {
54 : 15 : ::close(_fd);
55 : 15 : }
56 : :
57 : : template<typename result_type>
58 : 248 : void read(result_type & result) const SSRC_DECL_THROW(std::runtime_error) {
59 [ - + ]: 248 : if(::read(_fd, &result, sizeof(result_type)) != sizeof(result_type))
60 [ # # # # : 0 : throw std::runtime_error(std::strerror(errno));
# # # # ]
61 : 248 : }
62 : : };
63 : :
64 : : template<typename int_type>
65 : 15 : class random_engine {
66 : : random_device _dev_random;
67 : :
68 : : public:
69 : : typedef int_type result_type;
70 : : static const bool has_fixed_range = true;
71 : : static const result_type min_value =
72 : : boost::integer_traits<result_type>::const_min;
73 : : static const result_type max_value =
74 : : boost::integer_traits<result_type>::const_max;
75 : :
76 : 15 : explicit random_engine(const std::string & device_file = "/dev/urandom")
77 : : SSRC_DECL_THROW(std::runtime_error) :
78 : 15 : _dev_random(device_file)
79 : 15 : { }
80 : :
81 : 400 : result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const {
82 : 400 : return min_value;
83 : : }
84 : :
85 : 200 : result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const {
86 : 200 : return max_value;
87 : : }
88 : :
89 : : double entropy() const {
90 : : return 10;
91 : : }
92 : :
93 : 248 : result_type operator()() const SSRC_DECL_THROW(std::runtime_error) {
94 : : result_type result;
95 : 248 : _dev_random.read(result);
96 : 248 : return result;
97 : : }
98 : : };
99 : :
100 : : template<typename int_type,
101 : : template <typename U> class distribution = boost::uniform_int,
102 : : class engine = random_engine<unsigned int> >
103 : 10 : class Random {
104 : : public:
105 : : typedef int_type result_type;
106 : : typedef boost::integer_traits<result_type> result_type_traits;
107 : : typedef engine random_engine;
108 : : typedef distribution<result_type> random_distribution;
109 : :
110 : : private:
111 : : random_engine _engine;
112 : : boost::variate_generator<random_engine &, random_distribution> _random;
113 : :
114 : : public:
115 : :
116 : 10 : explicit Random(result_type min = result_type_traits::const_min,
117 : : result_type max = result_type_traits::const_max) :
118 [ + - + - : 10 : _engine(), _random(_engine, random_distribution(min, max))
+ - + - ]
119 : 10 : { }
120 : :
121 : 100 : result_type operator()() {
122 : 100 : return _random();
123 : : }
124 : : };
125 : :
126 : :
127 : : __END_NS_SSRC_WSPR_UTILITY
128 : :
129 : : #endif
|
Copyright © 2017 Savarese Software Research Corporation. All rights reserved