test_bench-random 2.0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/test_bench/random/controls/seed.rb +21 -0
- data/lib/test_bench/random/controls.rb +1 -0
- data/lib/test_bench/random/random.rb +120 -0
- data/lib/test_bench/random.rb +1 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 452590bb0d9ac75c4e0ce685ac90a6c505af3df8a04251eb05e6e1a34f25c669
|
4
|
+
data.tar.gz: 96b7bf176cd7a7f766e6c28907ccc1131c01e1d72f3df6e933be31507f3c5a58
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2bad3af4d7cfd54342874d60e70bb8faf1205478a4c46d1b90336a9b41be4d2e4ba24260071158017f4c96b87eeb3a9fa604e3fdaccc5717007679493b7e21b3
|
7
|
+
data.tar.gz: e7d13adb57e70e29a5efa8569b4e82174175f634120f1a11122ef6d85259704a47ec6d0a8ad1d00de05c509cf3a1e0e94961419f74302cb37620fd6d45e11822
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TestBench
|
2
|
+
class Random
|
3
|
+
module Controls
|
4
|
+
module Seed
|
5
|
+
def self.example
|
6
|
+
1
|
7
|
+
end
|
8
|
+
|
9
|
+
module Namespace
|
10
|
+
def self.example
|
11
|
+
'some-seed-namespace'
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.other_example
|
15
|
+
'some-other-seed-namespace'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'test_bench/random/controls/seed'
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module TestBench
|
2
|
+
class Random
|
3
|
+
def generator
|
4
|
+
@generator ||= self.class.generator(seed, default_namespace)
|
5
|
+
end
|
6
|
+
attr_writer :generator
|
7
|
+
|
8
|
+
attr_reader :seed
|
9
|
+
attr_reader :default_namespace
|
10
|
+
|
11
|
+
def initialize(seed, default_namespace)
|
12
|
+
@seed = seed
|
13
|
+
@default_namespace = default_namespace
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.build(seed=nil, namespace: nil)
|
17
|
+
seed ||= Defaults.seed
|
18
|
+
namespace ||= Defaults.namespace
|
19
|
+
|
20
|
+
new(seed, namespace)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.instance
|
24
|
+
@instance ||= build
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.string = instance.string
|
28
|
+
def self.integer = instance.integer
|
29
|
+
def self.fraction = instance.fraction
|
30
|
+
def self.boolean = instance.boolean
|
31
|
+
def self.reset(...) = instance.reset(...)
|
32
|
+
|
33
|
+
def string
|
34
|
+
integer.to_s(36)
|
35
|
+
end
|
36
|
+
|
37
|
+
def integer
|
38
|
+
bytes = generator.bytes(8).bytes
|
39
|
+
|
40
|
+
number = 0
|
41
|
+
|
42
|
+
bytes.each_with_index do |byte, index|
|
43
|
+
number += byte ** index
|
44
|
+
end
|
45
|
+
|
46
|
+
number
|
47
|
+
end
|
48
|
+
|
49
|
+
def fraction
|
50
|
+
generator.rand
|
51
|
+
end
|
52
|
+
|
53
|
+
def boolean
|
54
|
+
byte = generator.bytes(1).bytes.first
|
55
|
+
|
56
|
+
if byte % 2 == 1
|
57
|
+
true
|
58
|
+
else
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def reset(namespace=nil)
|
64
|
+
namespace ||= self.default_namespace
|
65
|
+
|
66
|
+
generator = self.class.generator(seed, namespace)
|
67
|
+
|
68
|
+
self.generator = generator
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.generator(seed, namespace)
|
72
|
+
namespace_digest = namespace_digest(namespace)
|
73
|
+
|
74
|
+
seed ^= namespace_digest
|
75
|
+
|
76
|
+
::Random.new(seed)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.namespace_digest(namespace)
|
80
|
+
byte_width = 8
|
81
|
+
bit_width = byte_width * 8
|
82
|
+
|
83
|
+
limit = 2 ** bit_width
|
84
|
+
|
85
|
+
sequence = namespace.bytes.each_slice(byte_width).map do |bytes|
|
86
|
+
value = 0
|
87
|
+
|
88
|
+
bytes.each_with_index do |byte, index|
|
89
|
+
value <<= 8 if not index.zero?
|
90
|
+
|
91
|
+
value += byte
|
92
|
+
end
|
93
|
+
|
94
|
+
value
|
95
|
+
end
|
96
|
+
|
97
|
+
digest_salt = (0x100 ** byte_width) - 1
|
98
|
+
sequence.push(digest_salt)
|
99
|
+
|
100
|
+
sequence.reduce do |checksum, value|
|
101
|
+
checksum ^ value
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
module Defaults
|
106
|
+
def self.seed
|
107
|
+
seed = ENV['TEST_BENCH_SEED']
|
108
|
+
if not seed.nil?
|
109
|
+
seed.to_i
|
110
|
+
else
|
111
|
+
::Random.new_seed
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.namespace
|
116
|
+
$PROGRAM_NAME
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'test_bench/random/random'
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_bench-random
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Ladd
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test_bench-bootstrap
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email: nathanladd+github@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/test_bench/random.rb
|
34
|
+
- lib/test_bench/random/controls.rb
|
35
|
+
- lib/test_bench/random/controls/seed.rb
|
36
|
+
- lib/test_bench/random/random.rb
|
37
|
+
homepage: https://github.com/test-bench/test-bench-random
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.3.23
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Random value controls for TestBench
|
60
|
+
test_files: []
|