greenbar 0.2.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/greenbar/RandSetup.rb +21 -21
- data/rakefile.rb +1 -1
- data/test/RandSetupTest.rb +6 -4
- metadata +2 -2
data/lib/greenbar/RandSetup.rb
CHANGED
@@ -9,7 +9,6 @@
|
|
9
9
|
# http://rubyforge.org/projects/greenbar/
|
10
10
|
#
|
11
11
|
require 'greenbar/TestSetup'
|
12
|
-
#TODO - use ClassMethodSetup?
|
13
12
|
|
14
13
|
#:startdoc:#
|
15
14
|
#===Purpose
|
@@ -34,49 +33,50 @@ require 'greenbar/TestSetup'
|
|
34
33
|
#
|
35
34
|
# :include: doc/examples/RandSetup.rb
|
36
35
|
#
|
37
|
-
#===Additional definitions
|
38
|
-
# Kernel#seed_rand_sequence=(max, sequence)
|
39
36
|
#
|
40
|
-
# Initializes a re-usable sequence for calls to Kernel#rand with max.
|
41
|
-
# The first element of the array is returned on the first call to rand(max),
|
42
|
-
# and the sequence is rotated, appending this return to the end.
|
43
|
-
#
|
44
37
|
|
45
38
|
module Greenbar::RandSetup
|
46
39
|
|
47
40
|
#:stopdoc:
|
48
41
|
include Greenbar::TestSetup
|
49
42
|
|
43
|
+
@@rand_sequences = {}
|
44
|
+
|
50
45
|
def setup_mixin
|
46
|
+
Kernel.instance_eval {
|
47
|
+
rand_sequences = Greenbar::RandSetup.instance_eval { class_variable_get :@@rand_sequences }
|
48
|
+
class_variable_set :@@rand_sequences, rand_sequences
|
49
|
+
}
|
50
|
+
|
51
51
|
Kernel.class_eval do
|
52
52
|
|
53
53
|
alias :rand_saved_by_RandSetup :rand
|
54
54
|
|
55
55
|
def rand(max=0)
|
56
|
-
@@rand_sequences ||= {}
|
57
56
|
raise "Must use seed_rand_sequence(#{max}) before using rand(#{max})." unless @@rand_sequences.has_key? max
|
58
57
|
@@rand_sequences[max] << @@rand_sequences[max].shift
|
59
58
|
@@rand_sequences[max][-1]
|
60
59
|
end
|
61
|
-
|
62
|
-
def seed_rand_sequence(max, sequence)
|
63
|
-
array = sequence.to_a
|
64
|
-
raise "Must provide a non-empty sequence." if array.empty?
|
65
|
-
@@rand_sequences ||= {}
|
66
|
-
@@rand_sequences[max] = array
|
67
|
-
end
|
68
|
-
|
69
60
|
end
|
70
|
-
|
71
61
|
end
|
72
|
-
|
62
|
+
|
73
63
|
def teardown_mixin
|
74
64
|
Kernel.class_eval do
|
75
65
|
alias :rand :rand_saved_by_RandSetup
|
76
66
|
remove_method :rand_saved_by_RandSetup
|
77
|
-
|
78
|
-
@@rand_sequences = nil
|
67
|
+
Kernel.instance_eval { remove_class_variable :@@rand_sequences}
|
79
68
|
end
|
80
69
|
end
|
81
|
-
|
70
|
+
#:startdoc:#
|
71
|
+
#
|
72
|
+
# Initializes a re-usable sequence for calls to Kernel#rand with max.
|
73
|
+
# The first element of the array is returned on the first call to rand(max),
|
74
|
+
# and the sequence is rotated, appending this return to the end.
|
75
|
+
#
|
76
|
+
def seed_rand_sequence(max, sequence)
|
77
|
+
array = sequence.to_a
|
78
|
+
raise "Must provide a non-empty sequence." if array.empty?
|
79
|
+
@@rand_sequences[max] = array
|
80
|
+
end
|
81
|
+
|
82
82
|
end
|
data/rakefile.rb
CHANGED
data/test/RandSetupTest.rb
CHANGED
@@ -28,12 +28,12 @@ class RandSetupTest < Test::Unit::TestCase
|
|
28
28
|
newSequence = [5, 7, 4, 2, 6]
|
29
29
|
|
30
30
|
test.setup
|
31
|
-
seed_rand_sequence 8, sequence_for_8
|
32
|
-
seed_rand_sequence 10, sequence_for_10
|
31
|
+
test.seed_rand_sequence 8, sequence_for_8
|
32
|
+
test.seed_rand_sequence 10, sequence_for_10
|
33
33
|
assert_equal sequence_for_8 * 3, test.random_sequence(sequence_for_8.length * 3, 8)
|
34
34
|
assert_equal sequence_for_10 * 3, test.random_sequence(sequence_for_10.length * 3, 10)
|
35
35
|
|
36
|
-
seed_rand_sequence 10, newSequence
|
36
|
+
test.seed_rand_sequence 10, newSequence
|
37
37
|
assert_equal newSequence * 3, test.random_sequence(newSequence.length * 3, 10)
|
38
38
|
|
39
39
|
test.teardown
|
@@ -57,10 +57,12 @@ class RandSetupTest < Test::Unit::TestCase
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
-
seed_rand_sequence 5, duck
|
60
|
+
test.seed_rand_sequence 5, duck
|
61
61
|
assert_equal [0, 1, 2, 3, 4], test.random_sequence(5, 5)
|
62
62
|
|
63
63
|
test.teardown
|
64
|
+
|
65
|
+
assert ! Kernel.class_variables.include?('@@rand_sequences'), "Kernel@@rand_sequences should have been removed"
|
64
66
|
end
|
65
67
|
|
66
68
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: greenbar
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2006-
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2006-05-01 00:00:00 -04:00
|
8
8
|
summary: Test::Unit tools.
|
9
9
|
require_paths:
|
10
10
|
- lib
|