~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Examples/test-suite/ruby/stl_new_runme.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-06 10:27:08 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206102708-t37t62i45n595w0e
Tags: 1.3.33-2ubuntu1
* Merge with Debian; remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - Clean Runtime/ as well.
  - Force a few environment variables.
* debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
12
require 'swig_assert'
 
13
require 'stl_new'
 
14
 
 
15
 
 
16
def _sequence(container)
 
17
  swig_assert_each_line(<<'EOF', binding)
 
18
cont = container.new([9,1,8,2,7,3,6,4,5])
 
19
cont.to_a == [9,1,8,2,7,3,6,4,5]
 
20
cont.size == 9
 
21
i = cont.begin
 
22
i.class == Stl_new::Iterator
 
23
cont.end - cont.begin == cont.size
 
24
cont.begin.value == 9
 
25
(cont.end-1).value == 5
 
26
cont[0],cont[1] = cont[1],cont[0]
 
27
cont.to_a == [1,9,8,2,7,3,6,4,5]
 
28
i0 = cont.begin
 
29
i1 = i0+1
 
30
tmp = i0.value   # tmp = 1
 
31
tmp == 1
 
32
i0.value = i1.value # elem[0] = 9
 
33
i1.value = tmp      # elem[1] = 1
 
34
cont.to_a == [9,1,8,2,7,3,6,4,5]
 
35
i0 += 8
 
36
prev = i0.value
 
37
i0 -= 8
 
38
cur = i0.value
 
39
i0.value = prev
 
40
prev = cur
 
41
i0 += 8
 
42
cur = i0.value
 
43
i0.value = prev
 
44
cont.to_a == [5,1,8,2,7,3,6,4,9]
 
45
i0 == cont.end-1
 
46
i0 != cont.end
 
47
EOF
 
48
end
 
49
 
 
50
def _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]
 
57
EOF
 
58
end
 
59
 
 
60
def _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 == []
 
78
EOF
 
79
end
 
80
    
 
81
def _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]]
 
98
EOF
 
99
end
 
100
 
 
101
def 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
 
110
end
 
111
 
 
112
 
 
113
test do |proc, container|
 
114
  proc.call(container)
 
115
end
 
116
 
 
117