PageRenderTime 61ms CodeModel.GetById 35ms app.highlight 23ms RepoModel.GetById 1ms 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
  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
 12require 'swig_assert'
 13require 'stl_new'
 14
 15
 16def _sequence(container)
 17  swig_assert_each_line(<<'EOF', binding)
 18cont = container.new([9,1,8,2,7,3,6,4,5])
 19cont.to_a == [9,1,8,2,7,3,6,4,5]
 20cont.size == 9
 21i = cont.begin
 22i.class == Stl_new::Iterator
 23cont.end - cont.begin == cont.size
 24cont.begin.value == 9
 25(cont.end-1).value == 5
 26cont[0],cont[1] = cont[1],cont[0]
 27cont.to_a == [1,9,8,2,7,3,6,4,5]
 28i0 = cont.begin
 29i1 = i0+1
 30tmp = i0.value   # tmp = 1
 31tmp == 1
 32i0.value = i1.value # elem[0] = 9
 33i1.value = tmp      # elem[1] = 1
 34cont.to_a == [9,1,8,2,7,3,6,4,5]
 35i0 += 8
 36prev = i0.value
 37i0 -= 8
 38cur = i0.value
 39i0.value = prev
 40prev = cur
 41i0 += 8
 42cur = i0.value
 43i0.value = prev
 44cont.to_a == [5,1,8,2,7,3,6,4,9]
 45i0 == cont.end-1
 46i0 != cont.end
 47EOF
 48end
 49
 50def _random_iterator(container)
 51  swig_assert_each_line(<<EOF, binding)
 52  cont = #{container}.new([9,1,8,2,7,3,6,4,5])
 53  Stl_new.nth_element(cont.begin,cont.begin+cont.size/2,cont.end)
 54  cont.to_a == [3, 1, 2, 4, 5, 6, 7, 8, 9]
 55  Stl_new.nth_element(cont.begin,cont.begin+1,cont.end, proc { |a,b| b<a } )
 56  cont.to_a == [9, 8, 7, 6, 5, 4, 2, 1, 3]
 57EOF
 58end
 59
 60def _set(container)
 61  swig_assert_each_line(<<EOF, binding)
 62    cont = #{container}.new
 63    [9,1,8,2,7,3,6,4,5].each { |x| cont.insert(x) }
 64    i0 = cont.begin()
 65    cont.to_a == [1,2,3,4,5,6,7,8,9]
 66    cont = #{container}.new( proc { |a,b| b < a } )
 67    [9,1,8,2,7,3,6,4,5].each { |x| cont.insert(x) }
 68    cont.to_a == [9, 8, 7, 6, 5, 4, 3, 2, 1]
 69    cont = #{container}.new( proc { |a,b| b > a } )
 70    [9,1,8,2,7,3,6,4,5].each { |x| cont.insert(x) }
 71    cont.to_a == [1, 2, 3, 4, 5, 6, 7, 8, 9]
 72    cont = #{container}.new(proc { |a,b| b < a } )
 73    cont.insert(1)
 74    cont.to_a == [1]
 75    i0 = cont.begin()
 76    cont.erase(i0) # don't use i0 anymore, it is invalid now
 77    cont.to_a == []
 78EOF
 79end
 80    
 81def _map(container)
 82  swig_assert_each_line(<<EOF, binding)
 83    cont = #{container}.new
 84    cont['z'] = 9
 85    cont['y'] = 1
 86    cont['x'] = 8
 87    cont['w'] = 2
 88    cont.to_a == [['w',2],['x',8],['y',1],['z',9]]
 89
 90    cont = #{container}.new(proc { |a,b| b < a } )
 91    cont['z'] = 9
 92    cont['y'] = 1
 93    cont['x'] = 8
 94    cont['w'] = 2
 95    cont.to_a == [['z',9],['y',1],['x',8],['w',2]]
 96
 97    #cont.iterator.to_a == [['w',2],['x',8],['y',1],['z',9]]
 98EOF
 99end
100
101def test
102  for container in [Stl_new::Vector, Stl_new::Deque, Stl_new::List]
103    yield method(:_sequence), container
104  end
105  yield method(:_set), Stl_new::Set
106  yield method(:_map), Stl_new::Map
107#   for container in [Stl_new::Vector, Stl_new::Deque]
108#     yield method(:_random_iterator), container
109#   end
110end
111
112
113test do |proc, container|
114  proc.call(container)
115end
116
117