PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/test-suite/ruby/stl_new_runme.rb

#
Ruby | 117 lines | 90 code | 13 blank | 14 comment | 22 complexity | 9df1c07013da1e55b8c5ce1d7051d605 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. #!/usr/bin/env ruby
  2. #
  3. # This is a test of STL containers, iterators and using proc
  4. # objects to change the sorting function used in them. Same as a
  5. # std::binary_predicate in C++.
  6. #
  7. #
  8. #
  9. #
  10. #
  11. require 'swig_assert'
  12. require 'stl_new'
  13. def _sequence(container)
  14. swig_assert_each_line(<<'EOF', binding)
  15. cont = container.new([9,1,8,2,7,3,6,4,5])
  16. cont.to_a == [9,1,8,2,7,3,6,4,5]
  17. cont.size == 9
  18. i = cont.begin
  19. i.class == Stl_new::Iterator
  20. cont.end - cont.begin == cont.size
  21. cont.begin.value == 9
  22. (cont.end-1).value == 5
  23. cont[0],cont[1] = cont[1],cont[0]
  24. cont.to_a == [1,9,8,2,7,3,6,4,5]
  25. i0 = cont.begin
  26. i1 = i0+1
  27. tmp = i0.value # tmp = 1
  28. tmp == 1
  29. i0.value = i1.value # elem[0] = 9
  30. i1.value = tmp # elem[1] = 1
  31. cont.to_a == [9,1,8,2,7,3,6,4,5]
  32. i0 += 8
  33. prev = i0.value
  34. i0 -= 8
  35. cur = i0.value
  36. i0.value = prev
  37. prev = cur
  38. i0 += 8
  39. cur = i0.value
  40. i0.value = prev
  41. cont.to_a == [5,1,8,2,7,3,6,4,9]
  42. i0 == cont.end-1
  43. i0 != cont.end
  44. EOF
  45. end
  46. def _random_iterator(container)
  47. swig_assert_each_line(<<EOF, binding)
  48. cont = #{container}.new([9,1,8,2,7,3,6,4,5])
  49. Stl_new.nth_element(cont.begin,cont.begin+cont.size/2,cont.end)
  50. cont.to_a == [3, 1, 2, 4, 5, 6, 7, 8, 9]
  51. Stl_new.nth_element(cont.begin,cont.begin+1,cont.end, proc { |a,b| b<a } )
  52. cont.to_a == [9, 8, 7, 6, 5, 4, 2, 1, 3]
  53. EOF
  54. end
  55. def _set(container)
  56. swig_assert_each_line(<<EOF, binding)
  57. cont = #{container}.new
  58. [9,1,8,2,7,3,6,4,5].each { |x| cont.insert(x) }
  59. i0 = cont.begin()
  60. cont.to_a == [1,2,3,4,5,6,7,8,9]
  61. cont = #{container}.new( proc { |a,b| b < a } )
  62. [9,1,8,2,7,3,6,4,5].each { |x| cont.insert(x) }
  63. cont.to_a == [9, 8, 7, 6, 5, 4, 3, 2, 1]
  64. cont = #{container}.new( proc { |a,b| b > a } )
  65. [9,1,8,2,7,3,6,4,5].each { |x| cont.insert(x) }
  66. cont.to_a == [1, 2, 3, 4, 5, 6, 7, 8, 9]
  67. cont = #{container}.new(proc { |a,b| b < a } )
  68. cont.insert(1)
  69. cont.to_a == [1]
  70. i0 = cont.begin()
  71. cont.erase(i0) # don't use i0 anymore, it is invalid now
  72. cont.to_a == []
  73. EOF
  74. end
  75. def _map(container)
  76. swig_assert_each_line(<<EOF, binding)
  77. cont = #{container}.new
  78. cont['z'] = 9
  79. cont['y'] = 1
  80. cont['x'] = 8
  81. cont['w'] = 2
  82. cont.to_a == [['w',2],['x',8],['y',1],['z',9]]
  83. cont = #{container}.new(proc { |a,b| b < a } )
  84. cont['z'] = 9
  85. cont['y'] = 1
  86. cont['x'] = 8
  87. cont['w'] = 2
  88. cont.to_a == [['z',9],['y',1],['x',8],['w',2]]
  89. #cont.iterator.to_a == [['w',2],['x',8],['y',1],['z',9]]
  90. EOF
  91. end
  92. def test
  93. for container in [Stl_new::Vector, Stl_new::Deque, Stl_new::List]
  94. yield method(:_sequence), container
  95. end
  96. yield method(:_set), Stl_new::Set
  97. yield method(:_map), Stl_new::Map
  98. # for container in [Stl_new::Vector, Stl_new::Deque]
  99. # yield method(:_random_iterator), container
  100. # end
  101. end
  102. test do |proc, container|
  103. proc.call(container)
  104. end