Yet even better swig assert.
Added more tests to std::vector for map. Added dup() function to containers. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9732 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
293750eaea
commit
aa3c28e230
4 changed files with 68 additions and 16 deletions
|
|
@ -64,7 +64,7 @@ dv = DoubleVector.new(10)
|
||||||
|
|
||||||
swig_assert( "dv.respond_to? :each_with_index", binding )
|
swig_assert( "dv.respond_to? :each_with_index", binding )
|
||||||
|
|
||||||
dv.each_with_index { |e,i| swig_assert("dv[#{i}] == 0.0", binding) }
|
dv.each_with_index { |e,i| swig_assert_equal("dv[#{i}]", 0.0, binding) }
|
||||||
|
|
||||||
0.upto(9) { |i| dv[i] = i/2.0 }
|
0.upto(9) { |i| dv[i] = i/2.0 }
|
||||||
|
|
||||||
|
|
@ -74,16 +74,19 @@ dv.each_with_index { |e,i| swig_assert("dv[#{i}] == 0.0", binding) }
|
||||||
"dv[0,3].to_s" => "0.00.51.0",
|
"dv[0,3].to_s" => "0.00.51.0",
|
||||||
"dv[3,3].to_s" => "1.52.02.5",
|
"dv[3,3].to_s" => "1.52.02.5",
|
||||||
}.each do |k,v|
|
}.each do |k,v|
|
||||||
swig_assert( "#{k} == #{v.inspect}", binding )
|
swig_assert_equal( k, v.inspect, binding )
|
||||||
end
|
end
|
||||||
|
|
||||||
swig_assert_each_line(<<'EOF', binding)
|
swig_assert_each_line(<<'EOF', binding)
|
||||||
dv.delete_at(2)
|
dv.delete_at(2)
|
||||||
dv.delete_if() { |x| x == 2.0 }
|
dv.delete_if { |x| x == 2.0 }
|
||||||
dv.include? 3.0
|
dv.include? 3.0
|
||||||
dv.find {|x| x==3.0 }
|
dv.find {|x| x == 3.0 }
|
||||||
halve_in_place(dv) == nil
|
dv.kind_of? DoubleVector
|
||||||
dv = [0.0,0.25,0.75,1.25,1.5,1.75,2.0,2.25]
|
halved = []
|
||||||
|
halved = dv.map { |x| x / 2 }
|
||||||
|
halve_in_place(dv)
|
||||||
|
halved == dv.to_a
|
||||||
sv = StructVector.new
|
sv = StructVector.new
|
||||||
sv << Li_std_vector::Struct.new
|
sv << Li_std_vector::Struct.new
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
|
|
@ -28,18 +28,23 @@ def swig_assert_equal( a, b, scope = nil, msg = nil )
|
||||||
ok = eval(check.to_s, scope)
|
ok = eval(check.to_s, scope)
|
||||||
else
|
else
|
||||||
ok = eval(check.to_s)
|
ok = eval(check.to_s)
|
||||||
msg = scope if !msg
|
if !msg
|
||||||
|
msg = scope
|
||||||
|
scope = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
unless ok
|
unless ok
|
||||||
raise SwigRubyError.new("FAILED CHECK: #{check} was #{eval(a)} #{msg}")
|
valA = eval(a, scope)
|
||||||
|
valB = eval(b, scope)
|
||||||
|
raise SwigRubyError.new("FAILED EQUALITY: #{check} was #{valA} not #{valB}")
|
||||||
end
|
end
|
||||||
|
|
||||||
if $VERBOSE
|
if $VERBOSE
|
||||||
$stdout.puts "\tPASSED #{check} #{msg}"
|
$stdout.puts "\tPASSED EQUALITY #{check} #{msg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
return ok
|
return ok
|
||||||
|
|
@ -54,12 +59,44 @@ end
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Asserts whether an expression runs properly
|
# Asserts whether an expression runs properly and is true
|
||||||
#
|
#
|
||||||
# scope - optional Binding where to run the code
|
# scope - optional Binding where to run the code
|
||||||
# msg - optional additional message to print
|
# msg - optional additional message to print
|
||||||
#
|
#
|
||||||
def swig_assert( expr, scope = nil, msg = nil )
|
def swig_assert( expr, scope = nil, msg = nil )
|
||||||
|
begin
|
||||||
|
if scope.kind_of? Binding
|
||||||
|
ok = eval(expr.to_s, scope)
|
||||||
|
else
|
||||||
|
ok = eval(expr.to_s)
|
||||||
|
msg = scope if !msg
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
|
||||||
|
raise SwigRubyError.new("FAILED: #{expr.to_s} - #{e}") unless ok
|
||||||
|
|
||||||
|
if $VERBOSE
|
||||||
|
$stdout.puts "\tPASSED #{expr} #{msg}"
|
||||||
|
end
|
||||||
|
rescue => e
|
||||||
|
trace = e.backtrace[1..-1]
|
||||||
|
$stderr.puts "#{trace[0,1]}: #{e}"
|
||||||
|
if trace.size > 1
|
||||||
|
$stderr.puts "\tfrom #{trace[1..-1].join("\n\t ")}"
|
||||||
|
end
|
||||||
|
exit(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# Asserts whether an expression runs properly
|
||||||
|
#
|
||||||
|
# scope - optional Binding where to run the code
|
||||||
|
# msg - optional additional message to print
|
||||||
|
#
|
||||||
|
def swig_eval( expr, scope = nil, msg = nil )
|
||||||
begin
|
begin
|
||||||
if scope.kind_of? Binding
|
if scope.kind_of? Binding
|
||||||
eval(expr.to_s, scope)
|
eval(expr.to_s, scope)
|
||||||
|
|
@ -87,7 +124,7 @@ end
|
||||||
# Given a set of lines as text, runs each of them, asserting them.
|
# Given a set of lines as text, runs each of them, asserting them.
|
||||||
# Lines that are of the form:
|
# Lines that are of the form:
|
||||||
# a == b are run with swig_assert_equal
|
# a == b are run with swig_assert_equal
|
||||||
# others are run with swig_assert.
|
# others are run with swig_eval.
|
||||||
#
|
#
|
||||||
# scope - optional Binding where to run the code
|
# scope - optional Binding where to run the code
|
||||||
# msg - optional additional message to print
|
# msg - optional additional message to print
|
||||||
|
|
@ -95,10 +132,10 @@ end
|
||||||
def swig_assert_each_line( lines, scope = nil, msg = nil )
|
def swig_assert_each_line( lines, scope = nil, msg = nil )
|
||||||
lines.split("\n").each do |line|
|
lines.split("\n").each do |line|
|
||||||
next if line.empty? or line =~ /^\s*#.*/
|
next if line.empty? or line =~ /^\s*#.*/
|
||||||
if line =~ /(.*)\s*==\s*(.*)/
|
if line =~ /^\s*([^\s]*)\s*==\s*(.*)\s*$/
|
||||||
swig_assert_equal($1, $2, scope, msg)
|
swig_assert_equal($1, $2, scope, msg)
|
||||||
else
|
else
|
||||||
swig_assert(line, scope, msg)
|
swig_eval(line, scope, msg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -534,7 +534,7 @@ namespace swig
|
||||||
for ( ; i != e; ++i, comma = true )
|
for ( ; i != e; ++i, comma = true )
|
||||||
{
|
{
|
||||||
if (comma) str = rb_str_cat2( str, "," );
|
if (comma) str = rb_str_cat2( str, "," );
|
||||||
tmp = swig::from( *i );
|
tmp = swig::from<Container::value_type>( *i );
|
||||||
tmp = rb_obj_as_string( tmp );
|
tmp = rb_obj_as_string( tmp );
|
||||||
str = rb_str_buf_append( str, tmp );
|
str = rb_str_buf_append( str, tmp );
|
||||||
}
|
}
|
||||||
|
|
@ -550,7 +550,7 @@ namespace swig
|
||||||
VALUE tmp;
|
VALUE tmp;
|
||||||
for ( ; i != e; ++i )
|
for ( ; i != e; ++i )
|
||||||
{
|
{
|
||||||
tmp = swig::from( *i );
|
tmp = swig::from<Container::value_type>( *i );
|
||||||
rb_ary_push( ary, tmp );
|
rb_ary_push( ary, tmp );
|
||||||
}
|
}
|
||||||
return ary;
|
return ary;
|
||||||
|
|
@ -564,7 +564,7 @@ namespace swig
|
||||||
VALUE tmp;
|
VALUE tmp;
|
||||||
for ( ; i != e; ++i )
|
for ( ; i != e; ++i )
|
||||||
{
|
{
|
||||||
tmp = swig::from( *i );
|
tmp = swig::from<Container::value_type>( *i );
|
||||||
tmp = rb_obj_as_string( tmp );
|
tmp = rb_obj_as_string( tmp );
|
||||||
str = rb_str_buf_append( str, tmp );
|
str = rb_str_buf_append( str, tmp );
|
||||||
}
|
}
|
||||||
|
|
@ -575,6 +575,16 @@ namespace swig
|
||||||
|
|
||||||
%define %swig_container_methods(Container...)
|
%define %swig_container_methods(Container...)
|
||||||
|
|
||||||
|
%extend {
|
||||||
|
|
||||||
|
%newobject dup;
|
||||||
|
Container* dup()
|
||||||
|
{
|
||||||
|
return new Container(*$self);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
%define %swig_sequence_methods_common(Sequence...)
|
%define %swig_sequence_methods_common(Sequence...)
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,13 @@
|
||||||
%define %swig_vector_methods(Type...)
|
%define %swig_vector_methods(Type...)
|
||||||
%swig_sequence_methods(Type)
|
%swig_sequence_methods(Type)
|
||||||
%swig_sequence_front_inserters(Type);
|
%swig_sequence_front_inserters(Type);
|
||||||
|
%swig_container_printing_methods(Type);
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
%define %swig_vector_methods_val(Type...)
|
%define %swig_vector_methods_val(Type...)
|
||||||
%swig_sequence_methods_val(Type);
|
%swig_sequence_methods_val(Type);
|
||||||
%swig_sequence_front_inserters(Type);
|
%swig_sequence_front_inserters(Type);
|
||||||
|
%swig_container_printing_methods(Type);
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue