backports 1.16.4 → 1.16.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,4 @@
2
2
  :major: 1
3
3
  :build:
4
4
  :minor: 16
5
- :patch: 4
5
+ :patch: 5
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backports}
8
- s.version = "1.16.4"
8
+ s.version = "1.16.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marc-Andr\303\251 Lafortune"]
12
- s.date = %q{2010-04-11}
12
+ s.date = %q{2010-04-13}
13
13
  s.description = %q{ Essential backports that enable some of the really nice features of ruby 1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
14
14
  }
15
15
  s.email = %q{github@marc-andre.ca}
@@ -108,6 +108,7 @@ Gem::Specification.new do |s|
108
108
  "test/method_test.rb",
109
109
  "test/module_test.rb",
110
110
  "test/object_test.rb",
111
+ "test/random_marshal_test.rb",
111
112
  "test/regexp_test.rb",
112
113
  "test/string_test.rb",
113
114
  "test/symbol_test.rb",
@@ -131,6 +132,7 @@ Gem::Specification.new do |s|
131
132
  "test/method_test.rb",
132
133
  "test/module_test.rb",
133
134
  "test/object_test.rb",
135
+ "test/random_marshal_test.rb",
134
136
  "test/regexp_test.rb",
135
137
  "test/string_test.rb",
136
138
  "test/symbol_test.rb",
@@ -5,8 +5,6 @@ class Random
5
5
  LAST_STATE = STATE_SIZE - 1
6
6
  PAD_32_BITS = 0xffffffff
7
7
 
8
- attr_reader :state, :last_read
9
-
10
8
  # See seed=
11
9
  def initialize(seed)
12
10
  self.seed = seed
@@ -35,14 +35,32 @@ class Random
35
35
  random.pack("L" * nb_32_bits)[0, nb]
36
36
  end
37
37
 
38
- def to_bignum
38
+ def state_as_bignum
39
39
  b = 0
40
- state.each_with_index do |val, i|
40
+ @state.each_with_index do |val, i|
41
41
  b |= val << (32 * i)
42
42
  end
43
43
  b
44
44
  end
45
45
 
46
+ def left # It's actually the number of words left + 1, as per MRI...
47
+ MT19937::STATE_SIZE - @last_read
48
+ end
49
+
50
+ def marshal_dump
51
+ [state_as_bignum, left]
52
+ end
53
+
54
+ def marshal_load(ary)
55
+ b, left = ary
56
+ @last_read = MT19937::STATE_SIZE - left
57
+ @state = Array.new(STATE_SIZE)
58
+ STATE_SIZE.times do |i|
59
+ @state[i] = b & PAD_32_BITS
60
+ b >>= 32
61
+ end
62
+ end
63
+
46
64
  # Convert an Integer seed of arbitrary size to either a single 32 bit integer, or an Array of 32 bit integers
47
65
  def self.convert_seed(seed)
48
66
  seed = seed.abs
@@ -46,13 +46,23 @@ class Random
46
46
  state == other.send(:state)
47
47
  end
48
48
 
49
+ def marshal_dump
50
+ @mt.marshal_dump << @seed
51
+ end
52
+
53
+ def marshal_load(ary)
54
+ @seed = ary.pop
55
+ @mt = MT19937.allocate
56
+ @mt.marshal_load(ary)
57
+ end
58
+
49
59
  private
50
60
  def state
51
- @mt.to_bignum
61
+ @mt.state_as_bignum
52
62
  end
53
63
 
54
64
  def left
55
- MT19937::STATE_SIZE - @mt.last_read
65
+ @mt.left
56
66
  end
57
67
 
58
68
  def _rand_range(limit)
@@ -0,0 +1,33 @@
1
+ # Run independently and with Ruby 1.9
2
+
3
+ def get_randomizer
4
+ r = Random.new(42)
5
+ 1000.times{r.rand}
6
+ r
7
+ end
8
+
9
+ def get_info(r)
10
+ Hash[
11
+ [:state, :left, :seed].map{|info| [info, r.send(info)]}
12
+ ]
13
+ end
14
+
15
+ MRIs = get_randomizer
16
+ dump = Marshal.dump(MRIs)
17
+ Object.send :remove_const, :Random # Kill original definition
18
+
19
+ require_relative "../lib/backports/1.9.2"
20
+
21
+ ours = get_randomizer
22
+
23
+ puts "Info ok" if get_info(MRIs) == get_info(ours)
24
+ if dump == (our_dump = Marshal.dump(get_randomizer))
25
+ puts "dump identical"
26
+ else
27
+ puts "Oups"
28
+ end
29
+ if get_info(MRIs) == get_info(Marshal.load(dump))
30
+ puts "load identical"
31
+ else
32
+ puts "Oups"
33
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 16
8
- - 4
9
- version: 1.16.4
8
+ - 5
9
+ version: 1.16.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Marc-Andr\xC3\xA9 Lafortune"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-11 00:00:00 -04:00
17
+ date: 2010-04-13 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -118,6 +118,7 @@ files:
118
118
  - test/method_test.rb
119
119
  - test/module_test.rb
120
120
  - test/object_test.rb
121
+ - test/random_marshal_test.rb
121
122
  - test/regexp_test.rb
122
123
  - test/string_test.rb
123
124
  - test/symbol_test.rb
@@ -170,6 +171,7 @@ test_files:
170
171
  - test/method_test.rb
171
172
  - test/module_test.rb
172
173
  - test/object_test.rb
174
+ - test/random_marshal_test.rb
173
175
  - test/regexp_test.rb
174
176
  - test/string_test.rb
175
177
  - test/symbol_test.rb