summaryrefslogtreecommitdiff
path: root/Examples/test-suite/ruby/cpp11_std_array_runme.rb
blob: 13c5efb84f182c961f63b2aa61a9d13713392b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env ruby
#
# Put description here
#
# 
# 
# 
#

require 'swig_assert'

require 'cpp11_std_array'

include Cpp11_std_array


def failed(a, b, msg)
    raise RuntimeError, "#{msg} #{a} #{b}"
end

def compare_sequences(a, b)
    if a.size != b.size
        failed(a, b, "different sizes")
    end
    for i in 0..a.size-1
      failed(a, b, "elements are different:") if a[i] != b[i]
    end
end

def compare_containers(rubyarray, swigarray)
    compare_sequences(rubyarray, swigarray)
end

def setslice_exception(swigarray, newval)
    begin
        swigarray[0..swigarray.size] = newval
        raise RuntimeError, "swigarray[] = #{newval} missed set exception for swigarray: #{swigarray}"
    rescue ArgumentError => e
#        print "exception: #{e}"
    end
end


# Check std::array has similar behaviour to a Ruby array
# except it is not resizable

ps = [0, 1, 2, 3, 4, 5]

ai = ArrayInt6.new(ps)

compare_containers(ps, ai)

# slices
compare_containers(ps[0..5], ai[0..5])
compare_containers(ps[-6..-1], ai[-6..-1])
compare_containers(ps[0..10], ai[0..10])

# Reverse (.reverse is not provided)
rev = []
ai.reverse_each { |i| rev.push i }
compare_containers(ps.reverse, rev)

# Modify content
for i in 0..ps.size-1
    ps[i] = ps[i] * 10
    ai[i] = ai[i] * 10
end
compare_containers(ps, ai)

# Empty
ai = ArrayInt6.new()
compare_containers([0, 0, 0, 0, 0, 0], ai)

# Set slice
newvals = [10, 20, 30, 40, 50, 60]
ai[0, 6] = newvals
compare_containers(ai, newvals)

ai[-6, 6] = newvals
compare_containers(ai, newvals)

setslice_exception(ai, [1, 2, 3, 4, 5, 6, 7])
setslice_exception(ai, [1, 2, 3, 4, 5])
setslice_exception(ai, [1, 2, 3, 4])
setslice_exception(ai, [1, 2, 3])
setslice_exception(ai, [1, 2])
setslice_exception(ai, [1])
setslice_exception(ai, [])

# Check return
compare_containers(arrayOutVal(), [-2, -1, 0, 0, 1, 2])
compare_containers(arrayOutConstRef(), [-2, -1, 0, 0, 1, 2])
compare_containers(arrayOutRef(), [-2, -1, 0, 0, 1, 2])
compare_containers(arrayOutPtr(), [-2, -1, 0, 0, 1, 2])

# Check passing arguments
ai = arrayInVal([9, 8, 7, 6, 5, 4])
compare_containers(ai, [90, 80, 70, 60, 50, 40])

ai = arrayInConstRef([9, 8, 7, 6, 5, 4])
compare_containers(ai, [90, 80, 70, 60, 50, 40])

ai = ArrayInt6.new([9, 8, 7, 6, 5, 4])
arrayInRef(ai)
compare_containers(ai, [90, 80, 70, 60, 50, 40])

ai = ArrayInt6.new([9, 8, 7, 6, 5, 4])
arrayInPtr(ai)
compare_containers(ai, [90, 80, 70, 60, 50, 40])

# indexing
ai = ArrayInt6.new([9, 8, 7, 6, 5, 4])
swig_assert_equal(ai[0], 9, binding)
swig_assert_equal(ai[5], 4, binding)
swig_assert_equal(ai[6], nil, binding)
swig_assert_equal(ai[-7], nil, binding)

# fill
ai.fill(111)
compare_containers(ai, [111, 111, 111, 111, 111, 111])

# various
ai = ArrayInt6.new([9, 8, 7, 6, 5, 4])
swig_assert(ai.include? 9)
swig_assert(!ai.include?(99))
swig_assert(ai.kind_of? ArrayInt6)
swig_assert(ai.find {|x| x == 6 } == 6)
swig_assert(ai.find {|x| x == 66 } == nil)
swig_assert(ai.respond_to?(:each))
swig_assert(ai.respond_to?(:each_with_index))

ai = [0, 10, 20, 30, 40, 50]
ai.each_with_index { |e,i| swig_assert(e/10 == i) }