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++.
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]
22
i.class == Stl_new::Iterator
23
cont.end - cont.begin == cont.size
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]
30
tmp = i0.value # 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]
44
cont.to_a == [5,1,8,2,7,3,6,4,9]
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]
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) }
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 } )
76
cont.erase(i0) # don't use i0 anymore, it is invalid now
82
swig_assert_each_line(<<EOF, binding)
83
cont = #{container}.new
88
cont.to_a == [['w',2],['x',8],['y',1],['z',9]]
90
cont = #{container}.new(proc { |a,b| b < a } )
95
cont.to_a == [['z',9],['y',1],['x',8],['w',2]]
97
#cont.iterator.to_a == [['w',2],['x',8],['y',1],['z',9]]
102
for container in [Stl_new::Vector, Stl_new::Deque, Stl_new::List]
103
yield method(:_sequence), container
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
113
test do |proc, container|