PageRenderTime 41ms CodeModel.GetById 30ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Tests/Runtime/Expression/test_while_until.rb

#
Ruby | 115 lines | 79 code | 16 blank | 20 comment | 10 complexity | 705acdb8cb756939835b127d693c1e83 MD5 | raw file
  1# ****************************************************************************
  2#
  3# Copyright (c) Microsoft Corporation. 
  4#
  5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6# copy of the license can be found in the License.html file at the root of this distribution. If 
  7# you cannot locate the  Apache License, Version 2.0, please send an email to 
  8# ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9# by the terms of the Apache License, Version 2.0.
 10#
 11# You must not remove this notice, or any other, from this software.
 12#
 13#
 14# ****************************************************************************
 15
 16# page 344
 17
 18require '../../util/assert.rb'
 19
 20# executes body zero or more times as long as bool-expression is true (for while), false (for until)
 21
 22def test_zero_times
 23    g = 1
 24    x = while false
 25        g += 10
 26    end
 27    assert_equal(g, 1)
 28    assert_nil(x)
 29    
 30    y = until true 
 31        g += 100
 32    end 
 33    assert_equal(g, 1)
 34    assert_nil(y)
 35end 
 36
 37def test_more_times
 38    x = true
 39    g = 0
 40    a = while x
 41        g += 1
 42        if g > 2
 43            x = false
 44        end
 45    end 
 46    assert_equal(g, 3)
 47    assert_nil(a)
 48    
 49    x = false
 50    g = 0
 51    b = until x
 52        g += 1
 53        if g > 5
 54            x = true
 55        end
 56    end 
 57    assert_equal(g, 6)
 58    assert_nil(b)
 59end
 60
 61# while and until modifier
 62def test_modifer
 63    # bad thing won't happen
 64    divide_by_zero while false
 65    divide_by_zero until true
 66    
 67    #
 68    x = 0 
 69    x += 1 while x > 5
 70    assert_equal(x, 0)
 71    x = 0
 72    x += 1 while x < 5
 73    assert_equal(x, 5)
 74    
 75    x = 0
 76    x += 1 until x > 5
 77    assert_equal(x, 6)
 78    x = 0
 79    x += 1 until x < 5
 80    assert_equal(x, 0)
 81end
 82
 83def test_evaluate_condition
 84    g = c = 1 
 85    x = true
 86    while (g+=10; x)
 87        c += 1
 88        x = c < 3
 89    end
 90    assert_equal(g, 31)
 91    
 92    g = c = 1 
 93    x = false
 94    until (g+=10; x)
 95        c += 1
 96        x = c > 3
 97    end
 98    assert_equal(g, 41)    
 99end 
100
101# TODO
102def test_expr_result
103    x = while true; return 3; end 
104    assert_equal(x, 3)
105    x = while true; break 4; end
106    assert_equal(x, 4)
107    
108    
109end 
110
111test_zero_times
112test_more_times
113test_modifer
114test_evaluate_condition
115test_expr_result