PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Tests/Compat/gen_ctrl_flow.rb

#
Ruby | 167 lines | 120 code | 26 blank | 21 comment | 18 complexity | 3f7071cb0959f27795bdd103b9d76d6d 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. require "pp"
  16. require "compat_common"
  17. def one_level_loop
  18. bool_seq = Bool_Sequence.new(64)
  19. cb = Combination.new([1, 3, 5,], ['next', 'break', 'retry', 'redo'])
  20. ['while', 'until'].each do |keyword|
  21. bool_seq.each do |bool_sequence|
  22. cb.generate do |number, flow|
  23. t = "bools ="
  24. PP.pp(bool_sequence + [keyword == 'while' ? false : true] * 2, t)
  25. t += "i = 0
  26. $g = \"\"
  27. #{keyword} bools[i] do
  28. i += 1
  29. $g += \"a1\"
  30. if i == #{number}
  31. $g += \"#{flow[0,1]}1\"
  32. #{flow}
  33. $g += \"#{flow[0,1]}2\"
  34. end
  35. $g += \"a2\"
  36. end
  37. puts $g
  38. "
  39. yield t
  40. end
  41. cb.generate2 do |number1, flow1, number2, flow2|
  42. t = "bools ="
  43. PP.pp(bool_sequence + [keyword == 'while' ? false : true] * 3, t)
  44. t += "i = 0
  45. $g = \"\"
  46. #{keyword} bools[i] do
  47. i += 1
  48. $g += \"a1\"
  49. if i == #{number1}
  50. $g += \"#{flow1[0,1]}1\"
  51. #{flow1}
  52. $g += \"#{flow1[0,1]}2\"
  53. end
  54. $g += \"a2\"
  55. if i == #{number2}
  56. $g += \"#{flow2[0,1]}1\"
  57. #{flow2}
  58. $g += \"#{flow2[0,1]}2\"
  59. end
  60. $g += \"a3\"
  61. end
  62. puts $g
  63. "
  64. yield t
  65. end
  66. end
  67. end
  68. end
  69. def three_level_loop
  70. bs2 = Bool_Sequence.new(8)
  71. bs3 = Bool_Sequence.new(8)
  72. cb1 = Combination.new([1, 2, 3,], ['next', 'break', 'retry', 'redo'])
  73. cb2 = Combination.new([1, 2, 3,], ['next', 'break', 'retry', 'redo'])
  74. [[true, true,], ].each do |bool_sequence1|
  75. bs2.each do |bool_sequence2|
  76. bs3.each do |bool_sequence3|
  77. cb1.generate do |number1, flow1|
  78. cb2.generate do |number2, flow2|
  79. t = "bools1 = "
  80. PP.pp(bool_sequence1 + [false] * 2, t)
  81. t += "bools2 = "
  82. PP.pp(bool_sequence2 + [true] * 2, t)
  83. t += "bools3 = "
  84. PP.pp(bool_sequence3 + [false] * 2, t)
  85. t += "
  86. $g = \"\"
  87. i = 0
  88. while bools1[i] do
  89. i += 1
  90. $g += \"a1\"
  91. j = 0
  92. until bools2[j] do
  93. j += 1
  94. $g += \"b1\"
  95. if j == #{number1}
  96. $g += \"#{flow1[0,1]}1\"
  97. #{flow1}
  98. $g += \"#{flow1[0,1]}2\"
  99. end
  100. k = 0
  101. while bools3[k]
  102. k += 1
  103. $g += \"c1\"
  104. if k == #{number2}
  105. $g += \"#{flow2[0,1]}1\"
  106. #{flow2}
  107. $g += \"#{flow2[0,1]}2\"
  108. end
  109. $g += \"c2\"
  110. end
  111. $g += \"b2\"
  112. end
  113. $g += \"a2\"
  114. end
  115. puts $g
  116. "
  117. yield t
  118. end
  119. end
  120. end
  121. end
  122. end
  123. end
  124. total = 0
  125. p = lambda do |t|
  126. total += 1
  127. results = GenFile.create_temporaily(t).run()
  128. if results[0] == 0
  129. GenFile.create_sequentially(t + "\nraise if $g != \"#{results[1]}\"\n")
  130. else
  131. tf = GenFile.create_sequentially(t + "\n#expected to fail\n")
  132. GenFile.add_negative_test(tf)
  133. end
  134. end
  135. GenFile.prepare("ctlf_one_")
  136. one_level_loop &p
  137. GenFile.finish
  138. GenFile.prepare("ctlf_three_")
  139. three_level_loop &p
  140. GenFile.finish
  141. printf("total generated: %s", total)