1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

fix Scalar and add tests

This commit is contained in:
ser1zw 2016-04-03 07:37:28 +09:00
parent efaee2d677
commit 078e136ddb
3 changed files with 100 additions and 10 deletions

View file

@ -1,4 +1,5 @@
// -*- mode: c++; coding: utf-8 -*-
#include <sstream>
#include "ruby.h"
#include "opencv2/core.hpp"
@ -43,6 +44,16 @@ namespace rubyopencv {
return TypedData_Make_Struct(klass, cv::Scalar, &opencv_scalar_type, ptr);
}
/*
* Creates a new Scalar.
*
* @overload new(v0 = 0, v1 = 0, v2 = 0, v3 = 0)
* @param v0 [Number] Value 0
* @param v1 [Number] Value 1
* @param v2 [Number] Value 2
* @param v3 [Number] Value 3
* @return [Scalar] +self+
*/
VALUE rb_initialize(int argc, VALUE *argv, VALUE self) {
const int SIZE = 4;
VALUE values[SIZE];
@ -82,16 +93,17 @@ namespace rubyopencv {
* @return [String] String representation of the scalar
*/
VALUE rb_to_s(VALUE self) {
const int i = 6;
VALUE str[i];
str[0] = rb_str_new2("<%s: [%g,%g,%g,%g]>");
str[1] = rb_str_new2(rb_class2name(CLASS_OF(self)));
str[2] = rb_aref(self, INT2FIX(0));
str[3] = rb_aref(self, INT2FIX(1));
str[4] = rb_aref(self, INT2FIX(2));
str[5] = rb_aref(self, INT2FIX(3));
std::stringstream s;
cv::Scalar* selfptr = obj2scalar(self);
s << *selfptr;
return rb_f_sprintf(i, str);
VALUE param[3];
param[0] = rb_str_new2("#<%s:%s>");
param[1] = rb_str_new2(rb_class2name(CLASS_OF(self)));
param[2] = rb_str_new2(s.str().c_str());
int n = sizeof(param) / sizeof(param[0]);
return rb_f_sprintf(n, param);
}
/*

View file

@ -16,6 +16,8 @@ class OpenCVTestCase < Test::Unit::TestCase
DUMMY_OBJ = Digest::MD5.new # dummy object for argument type check test
alias original_assert_in_delta assert_in_delta
def snap(*images)
n = -1
images.map! { |val|
@ -66,7 +68,7 @@ class OpenCVTestCase < Test::Unit::TestCase
0.upto(expected.cols - 1) { |c|
0.upto(expected.channels - 1) { |i|
msg = "Failed at #{actual.class.to_s}<depth=#{actual.depth},channels=#{actual.channels}>(#{r}, #{c})[#{i}]"
assert_in_delta(expected[r, c][i], actual[r, c][i], delta, msg)
original_assert_in_delta(expected[r, c][i], actual[r, c][i], delta, msg)
}
}
}
@ -84,5 +86,20 @@ class OpenCVTestCase < Test::Unit::TestCase
}
m
end
def assert_in_delta(expected, actual, delta, msg = nil)
if expected.is_a? Scalar or actual.is_a? Scalar
expected = expected.to_a if expected.is_a? Scalar
actual = actual.to_a if actual.is_a? Scalar
assert_in_delta(expected, actual, delta, msg)
elsif expected.is_a? Array and actual.is_a? Array
assert_equal(expected.size, actual.size)
expected.zip(actual) { |e, a|
original_assert_in_delta(e, a, delta, msg)
}
else
original_assert_in_delta(expected, actual, delta, msg)
end
end
end

61
test/test_scalar.rb Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8 -*-
require 'test/unit'
require 'opencv'
require File.expand_path(File.dirname(__FILE__)) + '/helper'
include OpenCV
# Tests for OpenCV::Scalar
class TestScalar < OpenCVTestCase
def test_initialize
s = Scalar.new
assert_in_delta([0, 0, 0, 0], s, 0.01)
s = Scalar.new(1.1)
assert_in_delta([1.1, 0, 0, 0], s, 0.01)
s = Scalar.new(1.1, 2.2)
assert_in_delta([1.1, 2.2, 0, 0], s, 0.01)
s = Scalar.new(1.1, 2.2, 3.3)
assert_in_delta([1.1, 2.2, 3.3, 0], s, 0.01)
s = Scalar.new(1.1, 2.2, 3.3, 4.4)
assert_in_delta([1.1, 2.2, 3.3, 4.4], s, 0.01)
end
def test_aref
assert_in_delta([0, 0, 0, 0], Scalar.new, 0.01)
assert_in_delta([10, 20, 30, 40], Scalar.new(10, 20, 30, 40), 0.01)
assert_in_delta([0.1, 0.2, 0.3, 0.4], Scalar.new(0.1, 0.2, 0.3, 0.4), 0.01)
end
def test_aset
s = Scalar.new
[10, 20, 30, 40].each_with_index { |x, i|
s[i] = x
}
assert_in_delta([10, 20, 30, 40], s, 0.01)
s = Scalar.new
[0.1, 0.2, 0.3, 0.4].each_with_index { |x, i|
s[i] = x
}
assert_in_delta([0.1, 0.2, 0.3, 0.4], s, 0.01)
end
def test_to_s
assert_equal('#<OpenCV::Scalar:[10, 20, 30, 40]>', Scalar.new(10, 20, 30, 40).to_s)
assert_equal('#<OpenCV::Scalar:[0.1, 0.2, 0.3, 0.4]>', Scalar.new(0.1, 0.2, 0.3, 0.4).to_s)
end
def test_to_ary
[[10, 20, 30, 40], [0.1, 0.2, 0.3, 0.4]].each { |a|
s = Scalar.new(*a)
x = s.to_a # Alias
assert_equal(Array, x.class)
assert_in_delta(a, x, 0.01)
}
end
end