PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/test/ruby/test_iterator.rb

http://github.com/ruby/ruby
Ruby | 507 lines | 421 code | 80 blank | 6 comment | 13 complexity | da3bf05b967a51e71b50ca3698014d26 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. class Array
  4. def iter_test1
  5. collect{|e| [e, yield(e)]}.sort{|a,b|a[1]<=>b[1]}
  6. end
  7. def iter_test2
  8. ary = collect{|e| [e, yield(e)]}
  9. ary.sort{|a,b|a[1]<=>b[1]}
  10. end
  11. end
  12. class TestIterator < Test::Unit::TestCase
  13. def test_yield_at_toplevel
  14. assert_separately([],"#{<<~"begin;"}\n#{<<~'end;'}")
  15. begin;
  16. assert(!block_given?)
  17. assert(!defined?(yield))
  18. end;
  19. end
  20. def test_array
  21. x = [1, 2, 3, 4]
  22. y = []
  23. # iterator over array
  24. for i in x
  25. y.push i
  26. end
  27. assert_equal(x, y)
  28. end
  29. def tt
  30. 1.upto(10) {|i|
  31. yield i
  32. }
  33. end
  34. def tt2(dummy)
  35. yield 1
  36. end
  37. def tt3(&block)
  38. tt2(raise(ArgumentError,""),&block)
  39. end
  40. def test_nested_iterator
  41. i = 0
  42. tt{|j| break if j == 5}
  43. assert_equal(0, i)
  44. assert_raise(ArgumentError) do
  45. tt3{}
  46. end
  47. end
  48. def tt4 &block
  49. tt2(raise(ArgumentError,""),&block)
  50. end
  51. def test_block_argument_without_paren
  52. assert_raise(ArgumentError) do
  53. tt4{}
  54. end
  55. end
  56. # iterator break/redo/next
  57. def test_break
  58. done = true
  59. loop{
  60. break if true
  61. done = false # should not reach here
  62. }
  63. assert(done)
  64. done = false
  65. bad = false
  66. loop {
  67. break if done
  68. done = true
  69. next if true
  70. bad = true # should not reach here
  71. }
  72. assert(!bad)
  73. done = false
  74. bad = false
  75. loop {
  76. break if done
  77. done = true
  78. redo if true
  79. bad = true # should not reach here
  80. }
  81. assert(!bad)
  82. x = []
  83. for i in 1 .. 7
  84. x.push i
  85. end
  86. assert_equal(7, x.size)
  87. assert_equal([1, 2, 3, 4, 5, 6, 7], x)
  88. end
  89. def test_array_for_masgn
  90. a = [Struct.new(:to_ary).new([1,2])]
  91. x = []
  92. a.each {|i,j|x << [i,j]}
  93. assert_equal([[1,2]], x)
  94. x = []
  95. for i,j in a; x << [i,j]; end
  96. assert_equal([[1,2]], x)
  97. end
  98. def test_append_method_to_built_in_class
  99. x = [[1,2],[3,4],[5,6]]
  100. assert_equal(x.iter_test1{|e|e}, x.iter_test2{|e|e})
  101. end
  102. class IterTest
  103. def initialize(e); @body = e; end
  104. def each0(&block); @body.each(&block); end
  105. def each1(&block); @body.each {|*x| block.call(*x) } end
  106. def each2(&block); @body.each {|*x| block.call(x) } end
  107. def each3(&block); @body.each {|x| block.call(*x) } end
  108. def each4(&block); @body.each {|x| block.call(x) } end
  109. def each5; @body.each {|*x| yield(*x) } end
  110. def each6; @body.each {|*x| yield(x) } end
  111. def each7; @body.each {|x| yield(*x) } end
  112. def each8; @body.each {|x| yield(x) } end
  113. def f(a)
  114. a
  115. end
  116. end
  117. def test_itertest
  118. assert_equal([1], IterTest.new(nil).method(:f).to_proc.call([1]))
  119. m = /\w+/.match("abc")
  120. assert_equal([m], IterTest.new(nil).method(:f).to_proc.call([m]))
  121. IterTest.new([0]).each0 {|x| assert_equal(0, x)}
  122. IterTest.new([1]).each1 {|x| assert_equal(1, x)}
  123. IterTest.new([2]).each2 {|x| assert_equal([2], x)}
  124. IterTest.new([4]).each4 {|x| assert_equal(4, x)}
  125. IterTest.new([5]).each5 {|x| assert_equal(5, x)}
  126. IterTest.new([6]).each6 {|x| assert_equal([6], x)}
  127. IterTest.new([8]).each8 {|x| assert_equal(8, x)}
  128. IterTest.new([[0]]).each0 {|x| assert_equal([0], x)}
  129. IterTest.new([[1]]).each1 {|x| assert_equal([1], x)}
  130. IterTest.new([[2]]).each2 {|x| assert_equal([[2]], x)}
  131. IterTest.new([[3]]).each3 {|x| assert_equal(3, x)}
  132. IterTest.new([[4]]).each4 {|x| assert_equal([4], x)}
  133. IterTest.new([[5]]).each5 {|x| assert_equal([5], x)}
  134. IterTest.new([[6]]).each6 {|x| assert_equal([[6]], x)}
  135. IterTest.new([[7]]).each7 {|x| assert_equal(7, x)}
  136. IterTest.new([[8]]).each8 {|x| assert_equal([8], x)}
  137. IterTest.new([[0,0]]).each0 {|*x| assert_equal([[0,0]], x)}
  138. IterTest.new([[8,8]]).each8 {|*x| assert_equal([[8,8]], x)}
  139. end
  140. def m(var)
  141. var
  142. end
  143. def m1
  144. m(block_given?)
  145. end
  146. def m2
  147. m(block_given?,&proc{})
  148. end
  149. def test_block_given
  150. assert(m1{p 'test'})
  151. assert(m2{p 'test'})
  152. assert(!m1())
  153. assert(!m2())
  154. end
  155. def m3(var, &block)
  156. m(yield(var), &block)
  157. end
  158. def m4(&block)
  159. m(m1(), &block)
  160. end
  161. def test_block_passing
  162. assert(!m4())
  163. assert(!m4 {})
  164. assert_equal(100, m3(10) {|x|x*x})
  165. end
  166. class C
  167. include Enumerable
  168. def initialize
  169. @a = [1,2,3]
  170. end
  171. def each(&block)
  172. @a.each(&block)
  173. end
  174. end
  175. def test_collect
  176. assert_equal([1,2,3], C.new.collect{|n| n})
  177. end
  178. def test_proc
  179. assert_instance_of(Proc, lambda{})
  180. assert_instance_of(Proc, Proc.new{})
  181. lambda{|a|assert_equal(a, 1)}.call(1)
  182. end
  183. def test_block
  184. assert_instance_of(NilClass, get_block)
  185. assert_instance_of(Proc, get_block{})
  186. end
  187. def test_argument
  188. assert_nothing_raised {lambda{||}.call}
  189. assert_raise(ArgumentError) {lambda{||}.call(1)}
  190. assert_nothing_raised {lambda{|a,|}.call(1)}
  191. assert_raise(ArgumentError) {lambda{|a,|}.call()}
  192. assert_raise(ArgumentError) {lambda{|a,|}.call(1,2)}
  193. end
  194. def get_block(&block)
  195. block
  196. end
  197. def test_get_block
  198. assert_instance_of(Proc, get_block{})
  199. assert_nothing_raised {get_block{||}.call()}
  200. assert_nothing_raised {get_block{||}.call(1)}
  201. assert_nothing_raised {get_block{|a,|}.call(1)}
  202. assert_nothing_raised {get_block{|a,|}.call()}
  203. assert_nothing_raised {get_block{|a,|}.call(1,2)}
  204. assert_nothing_raised {get_block(&lambda{||}).call()}
  205. assert_raise(ArgumentError) {get_block(&lambda{||}).call(1)}
  206. assert_nothing_raised {get_block(&lambda{|a,|}).call(1)}
  207. assert_raise(ArgumentError) {get_block(&lambda{|a,|}).call(1,2)}
  208. block = get_block{11}
  209. assert_instance_of(Proc, block)
  210. assert_instance_of(Proc, block.to_proc)
  211. assert_equal(block.clone.call, 11)
  212. assert_instance_of(Proc, get_block(&block))
  213. lmd = lambda{44}
  214. assert_instance_of(Proc, lmd)
  215. assert_instance_of(Proc, lmd.to_proc)
  216. assert_equal(lmd.clone.call, 44)
  217. assert_instance_of(Proc, get_block(&lmd))
  218. assert_equal(1, Proc.new{|a,| a}.call(1,2,3))
  219. assert_nothing_raised {Proc.new{|a,|}.call(1,2)}
  220. end
  221. def return1_test
  222. Proc.new {
  223. return 55
  224. }.call + 5
  225. end
  226. def test_return1
  227. assert_equal(55, return1_test())
  228. end
  229. def return2_test
  230. lambda {
  231. return 55
  232. }.call + 5
  233. end
  234. def test_return2
  235. assert_equal(60, return2_test())
  236. end
  237. def proc_call(&b)
  238. b.call
  239. end
  240. def proc_call2(b)
  241. b.call
  242. end
  243. def proc_yield()
  244. yield
  245. end
  246. def proc_return1
  247. proc_call{return 42}+1
  248. end
  249. def test_proc_return1
  250. assert_equal(42, proc_return1())
  251. end
  252. def proc_return2
  253. proc_yield{return 42}+1
  254. end
  255. def test_proc_return2
  256. assert_equal(42, proc_return2())
  257. end
  258. def test_ljump
  259. assert_raise(LocalJumpError) {get_block{break}.call}
  260. assert_raise(LocalJumpError) {proc_call2(get_block{break}){}}
  261. # cannot use assert_nothing_raised due to passing block.
  262. begin
  263. val = lambda{break 11}.call
  264. rescue LocalJumpError
  265. assert(false, "LocalJumpError occurred from break in lambda")
  266. else
  267. assert_equal(11, val)
  268. end
  269. block = get_block{11}
  270. lmd = lambda{44}
  271. assert_equal(0, block.arity)
  272. assert_equal(0, lmd.arity)
  273. assert_equal(0, lambda{||}.arity)
  274. assert_equal(1, lambda{|a|}.arity)
  275. assert_equal(1, lambda{|a,|}.arity)
  276. assert_equal(2, lambda{|a,b|}.arity)
  277. end
  278. def marity_test(m)
  279. mobj = method(m)
  280. assert_equal(mobj.arity, mobj.to_proc.arity)
  281. end
  282. def test_marity
  283. marity_test(:assert)
  284. marity_test(:marity_test)
  285. marity_test(:p)
  286. lambda(&method(:assert)).call(true)
  287. lambda(&get_block{|a,n| assert(a,n)}).call(true, "marity")
  288. end
  289. def foo
  290. yield(:key, :value)
  291. end
  292. def bar(&blk)
  293. blk.call(:key, :value)
  294. end
  295. def test_yield_vs_call
  296. foo{|k,v| assert_equal([:key, :value], [k,v])}
  297. bar{|k,v| assert_equal([:key, :value], [k,v])}
  298. end
  299. class H
  300. def each
  301. yield [:key, :value]
  302. end
  303. alias each_pair each
  304. end
  305. def test_assoc_yield
  306. [{:key=>:value}, H.new].each {|h|
  307. h.each{|a| assert_equal([:key, :value], a)}
  308. h.each{|a,| assert_equal(:key, a)}
  309. h.each{|*a| assert_equal([[:key, :value]], a)}
  310. h.each{|k,v| assert_equal([:key, :value], [k,v])}
  311. h.each_pair{|a| assert_equal([:key, :value], a)}
  312. h.each_pair{|a,| assert_equal(:key, a)}
  313. h.each_pair{|*a| assert_equal([[:key, :value]], a)}
  314. h.each_pair{|k,v| assert_equal([:key, :value], [k,v])}
  315. }
  316. end
  317. class ITER_TEST1
  318. def a
  319. block_given?
  320. end
  321. end
  322. class ITER_TEST2 < ITER_TEST1
  323. include Test::Unit::Assertions
  324. def a
  325. assert(super)
  326. super
  327. end
  328. end
  329. def test_iter_test2
  330. assert(ITER_TEST2.new.a {})
  331. end
  332. class ITER_TEST3
  333. def foo x
  334. return yield if block_given?
  335. x
  336. end
  337. end
  338. class ITER_TEST4 < ITER_TEST3
  339. include Test::Unit::Assertions
  340. def foo x
  341. assert_equal(super, yield)
  342. assert_equal(x, super(x, &nil))
  343. end
  344. end
  345. def test_iter4
  346. ITER_TEST4.new.foo(44){55}
  347. end
  348. def test_break__nested_loop1
  349. _test_break__nested_loop1 do
  350. break
  351. end
  352. end
  353. def _test_break__nested_loop1
  354. while true
  355. yield
  356. end
  357. assert(false, "must not reach here")
  358. end
  359. def test_break__nested_loop2
  360. _test_break__nested_loop2 do
  361. break
  362. end
  363. end
  364. def _test_break__nested_loop2
  365. until false
  366. yield
  367. end
  368. assert(false, "must not reach here")
  369. end
  370. def test_break__nested_loop3
  371. _test_break__nested_loop3 do
  372. break
  373. end
  374. end
  375. def _test_break__nested_loop3
  376. loop do
  377. yield
  378. end
  379. assert(false, "must not reach here")
  380. end
  381. def test_break_from_enum
  382. result = ["a"].inject("ng") {|x,y| break "ok"}
  383. assert_equal("ok", result)
  384. end
  385. def _test_return_trace_func(x)
  386. set_trace_func(proc {})
  387. [].fetch(2) {return x}
  388. ensure
  389. set_trace_func(nil)
  390. end
  391. def test_return_trace_func
  392. ok = "returned gracefully"
  393. result = "skipped"
  394. result = _test_return_trace_func(ok)
  395. ensure
  396. assert_equal(ok, result)
  397. return
  398. end
  399. class IterString < ::String
  400. def ===(other)
  401. super if !block_given?
  402. end
  403. end
  404. # Check that the block passed to an iterator
  405. # does not get propagated inappropriately
  406. def test_block_given_within_iterator
  407. assert_equal(["b"], ["a", "b", "c"].grep(IterString.new("b")) {|s| s})
  408. end
  409. def test_enumerator
  410. [1,2,3].each.with_index {|x,i|
  411. assert_equal(x, i+1)
  412. }
  413. e = [1,2,3].each
  414. assert_equal(1, e.next)
  415. assert_equal(2, e.next)
  416. assert_equal(3, e.next)
  417. assert_raise(StopIteration){e.next}
  418. e.rewind
  419. assert_equal(1, e.next)
  420. e.rewind
  421. a = []
  422. loop{a.push e.next}
  423. assert_equal([1,2,3], a)
  424. assert_equal([[8, 1, 10], [6, 2, 11], [4, 3, 12]],
  425. [8,6,4].zip((1..10),(10..100)).to_a)
  426. end
  427. end