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
2
2
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3
3
# redistribute it and/or modify it under the terms of the:
4
4
#
8
8
9
9
require_relative '../../ruby/spec_helper'
10
10
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
22
20
end
23
21
end
24
22
end
25
23
end
24
+ end
26
25
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
43
41
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
49
47
end
50
48
end
51
49
end
52
50
end
53
51
end
54
52
end
53
+ end
54
+
55
+ describe "Thread#detect_recursion" do
55
56
56
57
describe "for single arrays" do
57
58
it "for non-recursive arrays returns false" do
58
59
a = [ 1 , [ 2 , [ 3 ] , 4 ] , [ [ [ 5 , 6 , 7 ] ] ] ]
59
60
60
61
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
62
63
end
63
64
end
65
+
64
66
it "for recursive arrays returns true after sufficient depth to detect recursion" do
65
67
a = [ ]
66
68
a << [ [ [ a ] ] ]
@@ -70,11 +72,11 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
70
72
71
73
10 . times do |i |
72
74
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
75
77
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
78
80
end
79
81
end
80
82
end
@@ -85,18 +87,19 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
85
87
a = { :q => { :w => "qwe" } , :t => { :q => { :w => "qwe" } , :t => { :q => { :w => "qwe" } } } }
86
88
87
89
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
89
91
end
90
92
end
93
+
91
94
it "for recursive hashes returns true after sufficient depth to detect recursion" do
92
95
a = { :q => { :w => "qwe" } }
93
96
a [ :t ] = a
94
97
95
98
10 . times do |i |
96
99
if i < 3
97
- check_recursion_to_depth ( a , i ) . should be_false
100
+ TruffleThreadDetectRecursionSpecFixtures . check_recursion_to_depth ( a , i ) . should be_false
98
101
else
99
- check_recursion_to_depth ( a , i ) . should be_true
102
+ TruffleThreadDetectRecursionSpecFixtures . check_recursion_to_depth ( a , i ) . should be_true
100
103
end
101
104
end
102
105
end
@@ -110,9 +113,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
110
113
111
114
10 . times do |i |
112
115
if i < 2
113
- check_recursion_to_depth ( a , i ) . should be_false
116
+ TruffleThreadDetectRecursionSpecFixtures . check_recursion_to_depth ( a , i ) . should be_false
114
117
else
115
- check_recursion_to_depth ( a , i ) . should be_true
118
+ TruffleThreadDetectRecursionSpecFixtures . check_recursion_to_depth ( a , i ) . should be_true
116
119
end
117
120
end
118
121
end
@@ -126,9 +129,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
126
129
127
130
10 . times do |i |
128
131
if i < 8
129
- check_recursion_to_depth ( a , i ) . should be_false
132
+ TruffleThreadDetectRecursionSpecFixtures . check_recursion_to_depth ( a , i ) . should be_false
130
133
else
131
- check_recursion_to_depth ( a , i ) . should be_true
134
+ TruffleThreadDetectRecursionSpecFixtures . check_recursion_to_depth ( a , i ) . should be_true
132
135
end
133
136
end
134
137
end
@@ -142,9 +145,10 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
142
145
c = [ [ [ [ [ [ [ [ [ a , 1 ] ] ] ] ] ] ] ] ]
143
146
144
147
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
146
149
end
147
150
end
151
+
148
152
it "returns true after sufficient depth to detect recursion and equivalent structure" do
149
153
a = [ ]
150
154
a << a
@@ -154,9 +158,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
154
158
155
159
10 . times do |i |
156
160
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
158
162
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
160
164
end
161
165
end
162
166
end
@@ -170,9 +174,10 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
170
174
b = { :q => { :w => "qwe" } , :t => { :t => { :w => "qwe" } , :q => a } }
171
175
172
176
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
174
178
end
175
179
end
180
+
176
181
it "returns true after sufficient depth to detect recursion and equivalent structure" do
177
182
a = { :q => { :w => "qwe" } }
178
183
a [ :t ] = a
@@ -181,9 +186,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
181
186
182
187
10 . times do |i |
183
188
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
185
190
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
187
192
end
188
193
end
189
194
end
@@ -198,9 +203,9 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
198
203
199
204
10 . times do |i |
200
205
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
202
207
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
204
209
end
205
210
end
206
211
end
@@ -215,11 +220,12 @@ def check_double_recursion_equality_to_depth(obj1, obj2, depth)
215
220
216
221
20 . times do |i |
217
222
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
219
224
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
221
226
end
222
227
end
223
228
end
224
229
end
230
+
225
231
end
0 commit comments