Skip to content

Commit daca151

Browse files
chrisseatonLillian Zhang
authored andcommitted
Tidy up detect_recursion specs
1 parent 4d6f79d commit daca151

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed
Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. This
1+
# Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
22
# code is released under a tri EPL/GPL/LGPL license. You can use it,
33
# redistribute it and/or modify it under the terms of the:
44
#
@@ -8,59 +8,61 @@
88

99
require_relative '../../ruby/spec_helper'
1010

11-
describe "Thread#detect_recursion" do
12-
13-
before :each do
14-
def check_recursion_to_depth(obj, depth)
15-
return false unless obj.class.method_defined? :each
16-
Thread.detect_recursion obj do
17-
if depth > 1
18-
obj.each do |el|
19-
if check_recursion_to_depth(el, depth-1)
20-
return true
21-
end
11+
module TruffleThreadDetectRecursionSpecFixtures
12+
def self.check_recursion_to_depth(obj, depth)
13+
# checks that obj recurses to a given depth
14+
return false unless obj.class.method_defined? :each
15+
Thread.detect_recursion(obj) do
16+
if depth > 1
17+
obj.each do |el|
18+
if check_recursion_to_depth(el, depth-1)
19+
return true
2220
end
2321
end
2422
end
2523
end
24+
end
2625

27-
def check_double_recursion_equality_to_depth(obj1, obj2, depth)
28-
# checks that obj1 and obj2 are both recursive and equal structurally
29-
# (because detect_recursion on two objects is only used during object comparison,
30-
# and aborts after inequality is discovered)
31-
return false unless obj1.class == obj2.class
32-
return false unless obj1.class.method_defined? :each
33-
return false unless obj1.size == obj2.size
34-
35-
Thread.detect_recursion obj1 obj2 do
36-
if depth > 1
37-
if obj1.class == Hash
38-
obj1.each do |key, val|
39-
return false unless obj2.has_key?(key)
40-
if check_double_recursion_equality_to_depth(val, obj2[key], depth-1)
41-
return true
42-
end
26+
def self.check_double_recursion_equality_to_depth(obj1, obj2, depth)
27+
# checks that obj1 and obj2 are both recursive and equal structurally
28+
# (because detect_recursion on two objects is only used during object comparison,
29+
# and aborts after inequality is discovered)
30+
return false unless obj1.class == obj2.class
31+
return false unless obj1.class.method_defined? :each
32+
return false unless obj1.size == obj2.size
33+
34+
Thread.detect_recursion(obj1, obj2) do
35+
if depth > 1
36+
if obj1.class == Hash
37+
obj1.each do |key, val|
38+
return false unless obj2.has_key?(key)
39+
if check_double_recursion_equality_to_depth(val, obj2[key], depth-1)
40+
return true
4341
end
44-
else
45-
obj1.size.times do |i|
46-
if check_double_recursion_equality_to_depth(obj1[i], obj2[i], depth-1)
47-
return true
48-
end
42+
end
43+
else
44+
obj1.size.times do |i|
45+
if check_double_recursion_equality_to_depth(obj1[i], obj2[i], depth-1)
46+
return true
4947
end
5048
end
5149
end
5250
end
5351
end
5452
end
53+
end
54+
55+
describe "Thread#detect_recursion" do
5556

5657
describe "for single arrays" do
5758
it "for non-recursive arrays returns false" do
5859
a = [1,[2,[3], 4],[[[5,6,7]]]]
5960

6061
10.times do |i|
61-
check_recursion_to_depth(a, i).should be_false
62+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_false
6263
end
6364
end
65+
6466
it "for recursive arrays returns true after sufficient depth to detect recursion" do
6567
a = []
6668
a << [[[a]]]
@@ -70,11 +72,11 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
7072

7173
10.times do |i|
7274
if i < 5
73-
check_recursion_to_depth(a, i).should be_false
74-
check_recursion_to_depth(b, i).should be_false
75+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_false
76+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(b, i).should be_false
7577
else
76-
check_recursion_to_depth(a, i).should be_true
77-
check_recursion_to_depth(b, i).should be_true
78+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_true
79+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(b, i).should be_true
7880
end
7981
end
8082
end
@@ -85,18 +87,19 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
8587
a = {:q => {:w => "qwe" }, :t => {:q => {:w => "qwe" }, :t => {:q => {:w => "qwe" }}}}
8688

8789
10.times do |i|
88-
check_recursion_to_depth(a, i).should be_false
90+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_false
8991
end
9092
end
93+
9194
it "for recursive hashes returns true after sufficient depth to detect recursion" do
9295
a = {:q => {:w => "qwe" }}
9396
a[:t] = a
9497

9598
10.times do |i|
9699
if i < 3
97-
check_recursion_to_depth(a, i).should be_false
100+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_false
98101
else
99-
check_recursion_to_depth(a, i).should be_true
102+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_true
100103
end
101104
end
102105
end
@@ -110,9 +113,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
110113

111114
10.times do |i|
112115
if i < 2
113-
check_recursion_to_depth(a, i).should be_false
116+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_false
114117
else
115-
check_recursion_to_depth(a, i).should be_true
118+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_true
116119
end
117120
end
118121
end
@@ -126,9 +129,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
126129

127130
10.times do |i|
128131
if i < 8
129-
check_recursion_to_depth(a, i).should be_false
132+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_false
130133
else
131-
check_recursion_to_depth(a, i).should be_true
134+
TruffleThreadDetectRecursionSpecFixtures.check_recursion_to_depth(a, i).should be_true
132135
end
133136
end
134137
end
@@ -142,9 +145,10 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
142145
c = [[[[[[[[[a,1]]]]]]]]]
143146

144147
10.times do |i|
145-
check_double_recursion_equality_to_depth(a, c, i).should be_false
148+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, c, i).should be_false
146149
end
147150
end
151+
148152
it "returns true after sufficient depth to detect recursion and equivalent structure" do
149153
a = []
150154
a << a
@@ -154,9 +158,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
154158

155159
10.times do |i|
156160
if i < 8
157-
check_double_recursion_equality_to_depth(a, b, i).should be_false
161+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_false
158162
else
159-
check_double_recursion_equality_to_depth(a, b, i).should be_true
163+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_true
160164
end
161165
end
162166
end
@@ -170,9 +174,10 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
170174
b = {:q => {:w => "qwe" }, :t => {:t => {:w => "qwe" }, :q => a}}
171175

172176
10.times do |i|
173-
check_double_recursion_equality_to_depth(a, b, i).should be_false
177+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_false
174178
end
175179
end
180+
176181
it "returns true after sufficient depth to detect recursion and equivalent structure" do
177182
a = {:q => {:w => "qwe" }}
178183
a[:t] = a
@@ -181,9 +186,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
181186

182187
10.times do |i|
183188
if i < 4
184-
check_double_recursion_equality_to_depth(a, b, i).should be_false
189+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_false
185190
else
186-
check_double_recursion_equality_to_depth(a, b, i).should be_true
191+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_true
187192
end
188193
end
189194
end
@@ -198,9 +203,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
198203

199204
10.times do |i|
200205
if i < 3
201-
check_double_recursion_equality_to_depth(a, b, i).should be_false
206+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_false
202207
else
203-
check_double_recursion_equality_to_depth(a, b, i).should be_true
208+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_true
204209
end
205210
end
206211
end
@@ -215,11 +220,12 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
215220

216221
20.times do |i|
217222
if i < 11
218-
check_double_recursion_equality_to_depth(a, b, i).should be_false
223+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_false
219224
else
220-
check_double_recursion_equality_to_depth(a, b, i).should be_true
225+
TruffleThreadDetectRecursionSpecFixtures.check_double_recursion_equality_to_depth(a, b, i).should be_true
221226
end
222227
end
223228
end
224229
end
230+
225231
end

0 commit comments

Comments
 (0)