PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. # page 344
  16. require '../../util/assert.rb'
  17. # executes body zero or more times as long as bool-expression is true (for while), false (for until)
  18. def test_zero_times
  19. g = 1
  20. x = while false
  21. g += 10
  22. end
  23. assert_equal(g, 1)
  24. assert_nil(x)
  25. y = until true
  26. g += 100
  27. end
  28. assert_equal(g, 1)
  29. assert_nil(y)
  30. end
  31. def test_more_times
  32. x = true
  33. g = 0
  34. a = while x
  35. g += 1
  36. if g > 2
  37. x = false
  38. end
  39. end
  40. assert_equal(g, 3)
  41. assert_nil(a)
  42. x = false
  43. g = 0
  44. b = until x
  45. g += 1
  46. if g > 5
  47. x = true
  48. end
  49. end
  50. assert_equal(g, 6)
  51. assert_nil(b)
  52. end
  53. # while and until modifier
  54. def test_modifer
  55. # bad thing won't happen
  56. divide_by_zero while false
  57. divide_by_zero until true
  58. #
  59. x = 0
  60. x += 1 while x > 5
  61. assert_equal(x, 0)
  62. x = 0
  63. x += 1 while x < 5
  64. assert_equal(x, 5)
  65. x = 0
  66. x += 1 until x > 5
  67. assert_equal(x, 6)
  68. x = 0
  69. x += 1 until x < 5
  70. assert_equal(x, 0)
  71. end
  72. def test_evaluate_condition
  73. g = c = 1
  74. x = true
  75. while (g+=10; x)
  76. c += 1
  77. x = c < 3
  78. end
  79. assert_equal(g, 31)
  80. g = c = 1
  81. x = false
  82. until (g+=10; x)
  83. c += 1
  84. x = c > 3
  85. end
  86. assert_equal(g, 41)
  87. end
  88. # TODO
  89. def test_expr_result
  90. x = while true; return 3; end
  91. assert_equal(x, 3)
  92. x = while true; break 4; end
  93. assert_equal(x, 4)
  94. end
  95. test_zero_times
  96. test_more_times
  97. test_modifer
  98. test_evaluate_condition
  99. test_expr_result