PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Ruby | 122 lines | 91 code | 22 blank | 9 comment | 10 complexity | 88562f3d72929829f953040e2f5b92ee MD5 | raw file
  1#!/usr/bin/env ruby
  2#
  3# Put description here
  4#
  5# 
  6# 
  7# 
  8#
  9
 10require 'swig_assert'
 11
 12require 'li_std_vector'
 13
 14include Li_std_vector
 15
 16iv = IntVector.new(4)
 17
 18swig_assert( "iv.respond_to? :each", binding )
 19
 20begin
 21  iv.each
 22  swig_assert( false, nil, "iv.each worked with no block!")
 23rescue ArgumentError
 24end
 25
 26swig_assert_each_line(<<'EOF', binding)
 27iv.respond_to?(:each) == true
 28iv.respond_to?(:each_with_index) == true
 29EOF
 30
 31iv.each_with_index { |e,i| 
 32  swig_assert("#{e} == 0", binding, "for iv[#{i}] == 0") 
 33}
 34
 350.upto(3) { |i| iv[i] = i }
 36
 37{ "iv[-1]" => 3,
 38  "iv.slice(0,2).to_s" => "01", 
 39  "iv.slice(1,2).to_s" => "12", 
 40  "iv[0,-2]" => nil,
 41  "iv[0,3].to_s" => "012",
 42  "iv[0,10].to_s" => "012",
 43  "iv[1..2].to_s" => '12',
 44  "iv[1..3].to_s" => '123',
 45  "iv[1..4].to_s" => '123',
 46  "iv[1..-2].to_s" => '12',
 47  "iv[2..-3]" => nil,
 48}.each do |k,v|
 49  swig_assert( "#{k} == #{v.inspect}", binding )
 50end
 51
 52swig_assert_each_line(<<'EOF', binding)
 53iv << 5
 54iv.push 5
 55iv.pop == 5
 56iv.unshift(7)
 57iv.shift == 7
 58iv.unshift(7, 3)
 59iv.insert(1,5)
 60iv.insert(0, 3)
 61iv.unshift(2,3,4)
 62x = average(iv)
 63y = average([1, 2, 3, 4])
 64half([10, 10.5, 11, 11.5])
 65EOF
 66
 67dv = DoubleVector.new(10)
 68
 69swig_assert( "dv.respond_to? :each_with_index", binding )
 70
 71dv.each_with_index { |e,i| swig_assert_equal("dv[#{i}]", 0.0, binding) }
 72
 730.upto(9) { |i| dv[i] = i/2.0 }
 74
 75{ "dv[-1]" => 4.5,
 76  "dv.slice(0,2).to_s" => "0.00.5",
 77  "dv[0,-2]" => nil,
 78  "dv[0,3].to_s" => "0.00.51.0",
 79  "dv[3,3].to_s" => "1.52.02.5",
 80}.each do |k,v|
 81  swig_assert_equal( k, v.inspect, binding )
 82end
 83
 84swig_assert_each_line(<<'EOF', binding)
 85dv.delete_at(2)
 86dv.delete_if { |x| x == 2.0 }
 87dv.include? 3.0
 88dv.find {|x| x == 3.0 }
 89dv.kind_of? DoubleVector
 90halved = []
 91halved = dv.map { |x| x / 2 }
 92halve_in_place(dv)
 93halved.to_a == dv.to_a
 94sv = StructVector.new
 95sv << Li_std_vector::Struct.new
 96sv[0].class == Li_std_vector::Struct
 97sv[1] = Li_std_vector::Struct.new
 98
 99EOF
100
101bv = BoolVector.new(2)
102[true, false, true, true].each { |i| bv.push(i) }
1030.upto(bv.size-1) { |i| bv[i] = !bv[i] }
104bv_check = [true, true, false, true, false, false]
105for i in 0..bv.size-1 do
106  swig_assert(bv_check[i] == bv[i], binding, "bv[#{i}]")
107end
108
109swig_assert_each_line(<<'EOF', binding)
110lv = LanguageVector.new
111lv << 1
112lv << [1,2]
113lv << 'asd'
114lv[0], lv[1] = lv[1], lv[0]
115EOF
116
117
118# this should assert
119begin
120  lv = LanguageVector.new('crapola')
121rescue
122end