@@ -92,4 +92,140 @@ def range_like.exclude_end?
9292 result . should be_nil
9393 end
9494 end
95+
96+ describe "rb_arithmetic_sequence_extract" do
97+ it "returns begin, end, step, exclude end of an instance of an Enumerator::ArithmeticSequence" do
98+ enum = ( 10 ..20 ) . step ( 5 )
99+ enum . should . kind_of? ( Enumerator ::ArithmeticSequence )
100+
101+ @s . rb_arithmetic_sequence_extract ( enum ) . should == [ 1 , 10 , 20 , 5 , false ]
102+ end
103+
104+ it "returns begin, end, step, exclude end of an instance of a Range" do
105+ range = ( 10 ..20 )
106+ @s . rb_arithmetic_sequence_extract ( range ) . should == [ 1 , 10 , 20 , 1 , false ]
107+ end
108+
109+ it "returns begin, end, step, exclude end of a non-Range object with Range properties" do
110+ object = Object . new
111+ def object . begin
112+ 10
113+ end
114+ def object . end
115+ 20
116+ end
117+ def object . exclude_end?
118+ false
119+ end
120+
121+ @s . rb_arithmetic_sequence_extract ( object ) . should == [ 1 , 10 , 20 , 1 , false ]
122+ end
123+
124+ it "returns failed status if given object is not Enumerator::ArithmeticSequence or Range or Range-like object" do
125+ object = Object . new
126+ @s . rb_arithmetic_sequence_extract ( object ) . should == [ 0 ]
127+ end
128+ end
129+
130+ describe "rb_arithmetic_sequence_beg_len_step" do
131+ it "returns correct begin, length, step and result" do
132+ as = ( 2 ..5 ) . step ( 5 )
133+ error_code = 0
134+
135+ success , beg , len , step = @s . rb_arithmetic_sequence_beg_len_step ( as , 6 , error_code )
136+ success . should be_true
137+
138+ beg . should == 2
139+ len . should == 4
140+ step . should == 5
141+ end
142+
143+ it "takes into account excluded end boundary" do
144+ as = ( 2 ...5 ) . step ( 1 )
145+ error_code = 0
146+
147+ success , _ , len , _ = @s . rb_arithmetic_sequence_beg_len_step ( as , 6 , error_code )
148+ success . should be_true
149+ len . should == 3
150+ end
151+
152+ it "adds length to negative begin boundary" do
153+ as = ( -2 ..5 ) . step ( 1 )
154+ error_code = 0
155+
156+ success , beg , len , _ = @s . rb_arithmetic_sequence_beg_len_step ( as , 6 , error_code )
157+ success . should be_true
158+
159+ beg . should == 4
160+ len . should == 2
161+ end
162+
163+ it "adds length to negative end boundary" do
164+ as = ( 2 ..-1 ) . step ( 1 )
165+ error_code = 0
166+
167+ success , beg , len , _ = @s . rb_arithmetic_sequence_beg_len_step ( as , 6 , error_code )
168+ success . should be_true
169+
170+ beg . should == 2
171+ len . should == 4
172+ end
173+
174+ it "truncates arithmetic sequence length if end boundary greater than specified length value" do
175+ as = ( 2 ..10 ) . step ( 1 )
176+ error_code = 0
177+
178+ success , _ , len , _ = @s . rb_arithmetic_sequence_beg_len_step ( as , 6 , error_code )
179+ success . should be_true
180+ len . should == 4
181+ end
182+
183+ it "returns inverted begin and end boundaries when step is negative" do
184+ as = ( 2 ..5 ) . step ( -2 )
185+ error_code = 0
186+
187+ success , beg , len , step = @s . rb_arithmetic_sequence_beg_len_step ( as , 6 , error_code )
188+ success . should be_true
189+
190+ beg . should == 5
191+ len . should == 0
192+ step . should == -2
193+ end
194+
195+ it "returns nil when not in range and error code = 0" do
196+ as = ( 2 ..5 ) . step ( 1 )
197+ error_code = 0
198+
199+ success , = @s . rb_arithmetic_sequence_beg_len_step ( as , 1 , error_code )
200+ success . should be_nil
201+ end
202+
203+ it "returns nil when not in range, negative boundaries and error code = 0" do
204+ as = ( -5 ..-1 ) . step ( 1 )
205+ error_code = 0
206+
207+ success , = @s . rb_arithmetic_sequence_beg_len_step ( as , 1 , 0 )
208+ success . should be_nil
209+ end
210+
211+ it "returns begin, length and step and doesn't raise a RangeError when not in range and error code = 1" do
212+ as = ( 2 ..5 ) . step ( 1 )
213+ error_code = 1
214+
215+ success , beg , len , step = @s . rb_arithmetic_sequence_beg_len_step ( as , 1 , error_code )
216+ success . should be_true
217+
218+ beg . should == 2
219+ len . should == 4
220+ step . should == 1
221+ end
222+
223+ it "returns nil and doesn't raise a RangeError when not in range, negative boundaries and error code = 1" do
224+ as = ( -5 ..-1 ) . step ( 1 )
225+ error_code = 1
226+
227+ success , = @s . rb_arithmetic_sequence_beg_len_step ( as , 1 , error_code )
228+ success . should be_nil
229+ end
230+ end
95231end
0 commit comments