9
9
end
10
10
11
11
specify :test_value do
12
- atomic = described_class . new ( 0 )
12
+ atomic = described_class . new ( 0 )
13
13
atomic . value = 1
14
14
15
15
expect ( atomic . value ) . to eq 1
18
18
specify :test_update do
19
19
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
20
20
atomic = described_class . new ( 1000 )
21
- res = atomic . update { |v | v + 1 }
21
+ res = atomic . update { |v | v + 1 }
22
22
23
23
expect ( atomic . value ) . to eq 1001
24
24
expect ( res ) . to eq 1001
27
27
specify :test_try_update do
28
28
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
29
29
atomic = described_class . new ( 1000 )
30
- res = atomic . try_update { |v | v + 1 }
30
+ res = atomic . try_update { |v | v + 1 }
31
31
32
32
expect ( atomic . value ) . to eq 1001
33
33
expect ( res ) . to eq 1001
36
36
specify :test_try_update_bang do
37
37
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
38
38
atomic = described_class . new ( 1000 )
39
- res = atomic . try_update! { |v | v + 1 }
39
+ res = atomic . try_update! { |v | v + 1 }
40
40
41
41
expect ( atomic . value ) . to eq 1001
42
42
expect ( res ) . to eq 1001
43
43
end
44
44
45
45
specify :test_swap do
46
46
atomic = described_class . new ( 1000 )
47
- res = atomic . swap ( 1001 )
47
+ res = atomic . swap ( 1001 )
48
48
49
49
expect ( atomic . value ) . to eq 1001
50
50
expect ( res ) . to eq 1000
54
54
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
55
55
atomic = described_class . new ( 1000 )
56
56
expect (
57
- # assigning within block exploits implementation detail for test
58
- atomic . try_update { |v | atomic . value = 1001 ; v + 1 }
57
+ # assigning within block exploits implementation detail for test
58
+ atomic . try_update { |v | atomic . value = 1001 ; v + 1 }
59
59
) . to be_falsey
60
60
end
61
61
64
64
atomic = described_class . new ( 1000 )
65
65
expect {
66
66
# assigning within block exploits implementation detail for test
67
- atomic . try_update! { |v | atomic . value = 1001 ; v + 1 }
67
+ atomic . try_update! { |v | atomic . value = 1001 ; v + 1 }
68
68
} . to raise_error Concurrent ::ConcurrentUpdateError
69
69
end
70
70
73
73
# use a number outside JRuby's fixnum cache range, to ensure identity is preserved
74
74
atomic = described_class . new ( 1000 )
75
75
# assigning within block exploits implementation detail for test
76
- atomic . update { |v | tries += 1 ; atomic . value = 1001 ; v + 1 }
76
+ atomic . update { |v | tries += 1 ; atomic . value = 1001 ; v + 1 }
77
77
78
78
expect ( tries ) . to eq 2
79
79
end
82
82
atomic = described_class . new ( 0 )
83
83
84
84
# 9-bit idempotent Fixnum (JRuby)
85
- max_8 = 2 ** 256 - 1
86
- min_8 = -( 2 ** 256 )
85
+ max_8 = 2 ** 256 - 1
86
+ min_8 = -( 2 ** 256 )
87
87
88
88
atomic . set ( max_8 )
89
89
max_8 . upto ( max_8 + 2 ) do |i |
90
- expect ( atomic . compare_and_swap ( i , i + 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i + 1 } "
90
+ expect ( atomic . compare_and_swap ( i , i + 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i + 1 } "
91
91
end
92
92
93
93
atomic . set ( min_8 )
94
94
min_8 . downto ( min_8 - 2 ) do |i |
95
- expect ( atomic . compare_and_swap ( i , i - 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i - 1 } "
95
+ expect ( atomic . compare_and_swap ( i , i - 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i - 1 } "
96
96
end
97
97
98
98
# 64-bit idempotent Fixnum (MRI, Rubinius)
99
- max_64 = 2 ** 62 - 1
100
- min_64 = -( 2 ** 62 )
99
+ max_64 = 2 ** 62 - 1
100
+ min_64 = -( 2 ** 62 )
101
101
102
102
atomic . set ( max_64 )
103
103
max_64 . upto ( max_64 + 2 ) do |i |
104
- expect ( atomic . compare_and_swap ( i , i + 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i + 1 } "
104
+ expect ( atomic . compare_and_swap ( i , i + 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i + 1 } "
105
105
end
106
106
107
107
atomic . set ( min_64 )
108
108
min_64 . downto ( min_64 - 2 ) do |i |
109
- expect ( atomic . compare_and_swap ( i , i - 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i - 1 } "
109
+ expect ( atomic . compare_and_swap ( i , i - 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i - 1 } "
110
110
end
111
111
112
112
## 64-bit overflow into Bignum (JRuby)
113
- max_64 = 2 ** 63 - 1
114
- min_64 = ( -2 ** 63 )
113
+ max_64 = 2 ** 63 - 1
114
+ min_64 = ( -2 ** 63 )
115
115
116
116
atomic . set ( max_64 )
117
117
max_64 . upto ( max_64 + 2 ) do |i |
118
- expect ( atomic . compare_and_swap ( i , i + 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i + 1 } "
118
+ expect ( atomic . compare_and_swap ( i , i + 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i + 1 } "
119
119
end
120
120
121
121
atomic . set ( min_64 )
122
122
min_64 . downto ( min_64 - 2 ) do |i |
123
- expect ( atomic . compare_and_swap ( i , i - 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i - 1 } "
123
+ expect ( atomic . compare_and_swap ( i , i - 1 ) ) . to be_truthy , "CAS failed for numeric #{ i } => #{ i - 1 } "
124
124
end
125
125
126
126
# non-idempotent Float (JRuby, Rubinius, MRI < 2.0.0 or 32-bit)
127
127
atomic . set ( 1.0 + 0.1 )
128
128
expect ( atomic . compare_and_set ( 1.0 + 0.1 , 1.2 ) ) . to be_truthy , "CAS failed for #{ 1.0 + 0.1 } => 1.2"
129
129
130
130
# Bignum
131
- atomic . set ( 2 ** 100 )
132
- expect ( atomic . compare_and_set ( 2 ** 100 , 0 ) ) . to be_truthy , "CAS failed for #{ 2 ** 100 } => 0"
131
+ atomic . set ( 2 ** 100 )
132
+ expect ( atomic . compare_and_set ( 2 ** 100 , 0 ) ) . to be_truthy , "CAS failed for #{ 2 ** 100 } => 0"
133
133
134
134
# Rational
135
135
require 'rational' unless '' . respond_to? :to_r
136
- atomic . set ( Rational ( 1 , 3 ) )
137
- expect ( atomic . compare_and_set ( Rational ( 1 , 3 ) , 0 ) ) . to be_truthy , "CAS failed for #{ Rational ( 1 , 3 ) } => 0"
136
+ atomic . set ( Rational ( 1 , 3 ) )
137
+ expect ( atomic . compare_and_set ( Rational ( 1 , 3 ) , 0 ) ) . to be_truthy , "CAS failed for #{ Rational ( 1 , 3 ) } => 0"
138
138
139
139
# Complex
140
140
require 'complex' unless '' . respond_to? :to_c
141
- atomic . set ( Complex ( 1 , 2 ) )
142
- expect ( atomic . compare_and_set ( Complex ( 1 , 2 ) , 0 ) ) . to be_truthy , "CAS failed for #{ Complex ( 1 , 2 ) } => 0"
141
+ atomic . set ( Complex ( 1 , 2 ) )
142
+ expect ( atomic . compare_and_set ( Complex ( 1 , 2 ) , 0 ) ) . to be_truthy , "CAS failed for #{ Complex ( 1 , 2 ) } => 0"
143
143
end
144
144
end
145
145
@@ -162,53 +162,22 @@ module Concurrent
162
162
end
163
163
164
164
if defined? Concurrent ::CAtomicReference
165
- RSpec . describe CAtomicReference , ext : true do
165
+ RSpec . describe CAtomicReference do
166
166
it_should_behave_like :atomic_reference
167
167
end
168
- elsif defined? Concurrent ::JavaAtomicReference
168
+ end
169
+ if defined? Concurrent ::JavaAtomicReference
169
170
RSpec . describe JavaAtomicReference do
170
171
it_should_behave_like :atomic_reference
171
172
end
172
- elsif defined? Concurrent ::RbxAtomicReference
173
+ end
174
+ if defined? Concurrent ::RbxAtomicReference
173
175
RSpec . describe RbxAtomicReference do
174
176
it_should_behave_like :atomic_reference
175
177
end
176
178
end
177
-
178
- RSpec . describe AtomicReference do
179
- if Concurrent . on_jruby?
180
- it 'inherits from JavaAtomicReference' do
181
- expect ( AtomicReference . ancestors ) . to include ( Concurrent ::JavaAtomicReference )
182
- end
183
- elsif Concurrent . allow_c_extensions?
184
- it 'inherits from CAtomicReference' do
185
- expect ( AtomicReference . ancestors ) . to include ( Concurrent ::CAtomicReference )
186
- end
187
- elsif Concurrent . on_rbx?
188
- it 'inherits from RbxAtomicReference' do
189
- expect ( AtomicReference . ancestors ) . to include ( Concurrent ::RbxAtomicReference )
190
- end
191
- else
192
- it 'inherits from MutexAtomicReference' do
193
- expect ( AtomicReference . ancestors ) . to include ( Concurrent ::MutexAtomicReference )
194
- end
195
- end
196
- end
197
-
198
- RSpec . describe MutexAtomicReference do
199
- it_should_behave_like :atomic_reference
200
- end
201
-
202
- if defined? Concurrent ::CAtomicReference
203
- RSpec . describe CAtomicReference , ext : true do
204
- it_should_behave_like :atomic_reference
205
- end
206
- elsif defined? Concurrent ::JavaAtomicReference
207
- RSpec . describe JavaAtomicReference do
208
- it_should_behave_like :atomic_reference
209
- end
210
- elsif defined? Concurrent ::RbxAtomicReference
211
- RSpec . describe RbxAtomicReference do
179
+ if defined? Concurrent ::TruffleRubyAtomicReference
180
+ RSpec . describe TruffleRubyAtomicReference do
212
181
it_should_behave_like :atomic_reference
213
182
end
214
183
end
@@ -219,13 +188,17 @@ module Concurrent
219
188
expect ( described_class . ancestors ) . to include ( Concurrent ::JavaAtomicReference )
220
189
end
221
190
elsif Concurrent . allow_c_extensions?
222
- it 'inherits from CAtomicReference' , ext : true do
191
+ it 'inherits from CAtomicReference' do
223
192
expect ( described_class . ancestors ) . to include ( Concurrent ::CAtomicReference )
224
193
end
225
194
elsif Concurrent . on_rbx?
226
195
it 'inherits from RbxAtomicReference' do
227
196
expect ( described_class . ancestors ) . to include ( Concurrent ::RbxAtomicReference )
228
197
end
198
+ elsif Concurrent . on_truffleruby?
199
+ it 'inherits from TruffleRubyAtomicReference' do
200
+ expect ( described_class . ancestors ) . to include ( Concurrent ::TruffleRubyAtomicReference )
201
+ end
229
202
else
230
203
it 'inherits from MutexAtomicReference' do
231
204
expect ( described_class . ancestors ) . to include ( Concurrent ::MutexAtomicReference )
0 commit comments