File indexing completed on 2024-04-14 03:43:52

0001 #  SPDX-FileCopyrightText: 2009 Cies Breijs
0002 #  SPDX-FileCopyrightText: 2009 Niels Slot
0003 #
0004 #  SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 require File.dirname(__FILE__) + '/spec_helper.rb'
0007 $i = Interpreter.instance
0008 
0009 describe "for loop" do
0010   it "should work as expected for ascending positive values" do
0011     $i.should_run_clean <<-EOS
0012       $y = 1
0013 
0014       for $x = 1 to 10 {
0015         assert $x == $y
0016         $y = $y + 1
0017       }
0018       assert $y == 11
0019     EOS
0020   end
0021 
0022   it "should work as expected for ascending negative values" do
0023     $i.should_run_clean <<-EOS
0024       $y = -10
0025 
0026       for $x = -10 to -1 {
0027         assert $x == $y
0028         $y = $y + 1
0029       }
0030       assert $y == 0
0031     EOS
0032   end
0033 
0034   it "should work as expected for descending positive values" do
0035     $i.should_run_clean <<-EOS
0036       $y = 10
0037 
0038       for $x = 10 to 1 step -1 {
0039         assert $x == $y
0040         $y = $y - 1
0041       }
0042       assert $y == 0
0043     EOS
0044   end
0045 
0046   it "should work as expected for descending negative values" do
0047     $i.should_run_clean <<-EOS
0048       $y = -1
0049 
0050       for $x = -1 to -10 step -1 {
0051         assert $x == $y
0052         $y = $y - 1
0053       }
0054       assert $y == -11
0055     EOS
0056   end
0057 
0058   it "should work as expected with step > 1" do
0059     $i.should_run_clean <<-EOS
0060       $y = 0
0061 
0062       for $x = 0 to 10 step 2 {
0063         assert $x == $y
0064         $y = $y + 2
0065       }
0066       assert $y == 12
0067     EOS
0068   end
0069 
0070   it "should work as expected with 0 > step > 1" do
0071     $i.should_run_clean <<-EOS
0072       $y = 1
0073 
0074       for $x = 1 to 10 step 0.5 {
0075         assert $x == $y
0076         $y = $y + 0.5
0077       }
0078       assert $y == 10.5
0079     EOS
0080   end
0081 
0082   it "should work as expected with -1 > step > 0" do
0083     $i.should_run_clean <<-EOS
0084       $y = 10
0085 
0086       for $x = 10 to 1 step -0.5 {
0087         assert $x == $y
0088         $y = $y - 0.5
0089       }
0090       assert $y == 0.5
0091     EOS
0092   end
0093 
0094   it "should work as expected with step < -1" do
0095     $i.should_run_clean <<-EOS
0096       $y = 10
0097 
0098       for $x = 10 to 0 step -2 {
0099         assert $x == $y
0100         $y = $y - 2
0101       }
0102       assert $y == -2
0103     EOS
0104   end
0105 
0106   it "should work as expected when no execution required" do
0107     $i.should_run_clean <<-EOS
0108       for $x = 2 to 1 {
0109         assert false
0110       }
0111     EOS
0112   end
0113 end