mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
implemented CvSize2D32f#to_s, CvSize2D32f#to_ary, and added some tests for CvSize and CvSize2D32f
This commit is contained in:
parent
171310a301
commit
04bb08414b
4 changed files with 175 additions and 0 deletions
|
@ -51,6 +51,8 @@ define_ruby_class()
|
|||
rb_define_method(rb_klass, "width=", RUBY_METHOD_FUNC(rb_set_width), 1);
|
||||
rb_define_method(rb_klass, "height", RUBY_METHOD_FUNC(rb_height), 0);
|
||||
rb_define_method(rb_klass, "height=", RUBY_METHOD_FUNC(rb_set_height), 1);
|
||||
rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0);
|
||||
rb_define_method(rb_klass, "to_ary", RUBY_METHOD_FUNC(rb_to_ary), 0);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -168,6 +170,36 @@ rb_set_height(VALUE self, VALUE y)
|
|||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* to_s -> "<OpenCV::CvSize2D32f:widthxheight>"
|
||||
*
|
||||
* Return width and height by String.
|
||||
*/
|
||||
VALUE
|
||||
rb_to_s(VALUE self)
|
||||
{
|
||||
const int i = 4;
|
||||
VALUE str[i];
|
||||
str[0] = rb_str_new2("<%s:%gx%g>");
|
||||
str[1] = rb_str_new2(rb_class2name(CLASS_OF(self)));
|
||||
str[2] = rb_width(self);
|
||||
str[3] = rb_height(self);
|
||||
return rb_f_sprintf(i, str);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* to_ary -> [width, height]
|
||||
*
|
||||
* Return width and height by Array.
|
||||
*/
|
||||
VALUE
|
||||
rb_to_ary(VALUE self)
|
||||
{
|
||||
return rb_ary_new3(2, rb_width(self), rb_height(self));
|
||||
}
|
||||
|
||||
VALUE
|
||||
new_object(CvSize2D32f size)
|
||||
{
|
||||
|
|
|
@ -31,6 +31,9 @@ VALUE rb_set_width(VALUE self, VALUE width);
|
|||
VALUE rb_height(VALUE self);
|
||||
VALUE rb_set_height(VALUE self, VALUE height);
|
||||
|
||||
VALUE rb_to_s(VALUE self);
|
||||
VALUE rb_to_ary(VALUE self);
|
||||
|
||||
VALUE new_object(CvSize2D32f size);
|
||||
|
||||
__NAMESPACE_END_CVSIZE2D32F
|
||||
|
|
70
test/test_cvsize.rb
Executable file
70
test/test_cvsize.rb
Executable file
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env ruby
|
||||
# -*- mode: ruby; coding: utf-8-unix -*-
|
||||
require 'test/unit'
|
||||
require 'opencv'
|
||||
require File.expand_path(File.dirname(__FILE__)) + '/helper'
|
||||
|
||||
include OpenCV
|
||||
|
||||
# Tests for OpenCV::CvSize
|
||||
class TestCvSize < OpenCVTestCase
|
||||
class MySize; end
|
||||
|
||||
def test_width
|
||||
size = CvSize.new
|
||||
size.width = 100
|
||||
assert_equal(100, size.width)
|
||||
size.width = 200
|
||||
assert_equal(200, size.width)
|
||||
end
|
||||
|
||||
def test_height
|
||||
size = CvSize.new
|
||||
size.height = 100
|
||||
assert_equal(100, size.height)
|
||||
size.height = 200
|
||||
assert_equal(200, size.height)
|
||||
end
|
||||
|
||||
def test_compatible
|
||||
assert(!(CvSize.compatible? MySize.new))
|
||||
MySize.class_eval { def width; end }
|
||||
assert(!(CvSize.compatible? MySize.new))
|
||||
MySize.class_eval { def height; end }
|
||||
assert(CvSize.compatible? MySize.new)
|
||||
assert(CvSize.compatible? CvSize.new)
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
size = CvSize.new
|
||||
assert_equal(0, size.width)
|
||||
assert_equal(0, size.height)
|
||||
|
||||
size = CvSize.new(10, 20)
|
||||
assert_equal(10, size.width)
|
||||
assert_equal(20, size.height)
|
||||
|
||||
size = CvSize.new(CvSize.new(10, 20))
|
||||
assert_equal(10, size.width)
|
||||
assert_equal(20, size.height)
|
||||
|
||||
assert_raise(ArgumentError) {
|
||||
CvSize.new('string')
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
CvSize.new(1, 2, 3)
|
||||
}
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
size = CvSize.new(10, 20)
|
||||
assert_equal('<OpenCV::CvSize:10x20>', size.to_s)
|
||||
end
|
||||
|
||||
def test_to_ary
|
||||
a = CvSize.new(10, 20).to_ary
|
||||
assert_equal(10, a[0])
|
||||
assert_equal(20, a[1])
|
||||
end
|
||||
end
|
||||
|
70
test/test_cvsize2d32f.rb
Executable file
70
test/test_cvsize2d32f.rb
Executable file
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env ruby
|
||||
# -*- mode: ruby; coding: utf-8-unix -*-
|
||||
require 'test/unit'
|
||||
require 'opencv'
|
||||
require File.expand_path(File.dirname(__FILE__)) + '/helper'
|
||||
|
||||
include OpenCV
|
||||
|
||||
# Tests for OpenCV::CvSize2D32f
|
||||
class TestCvSize2D32f < OpenCVTestCase
|
||||
class MySize; end
|
||||
|
||||
def test_width
|
||||
size = CvSize2D32f.new
|
||||
size.width = 1.1
|
||||
assert_in_delta(1.1, size.width, 0.001)
|
||||
size.width = 2.2
|
||||
assert_in_delta(2.2, size.width, 0.001)
|
||||
end
|
||||
|
||||
def test_height
|
||||
size = CvSize2D32f.new
|
||||
size.height = 1.1
|
||||
assert_in_delta(1.1, size.height, 0.001)
|
||||
size.height = 2.2
|
||||
assert_in_delta(2.2, size.height, 0.001)
|
||||
end
|
||||
|
||||
def test_compatible
|
||||
assert(!(CvSize2D32f.compatible? MySize.new))
|
||||
MySize.class_eval { def width; end }
|
||||
assert(!(CvSize2D32f.compatible? MySize.new))
|
||||
MySize.class_eval { def height; end }
|
||||
assert(CvSize2D32f.compatible? MySize.new)
|
||||
assert(CvSize2D32f.compatible? CvSize2D32f.new)
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
size = CvSize2D32f.new
|
||||
assert_in_delta(0, size.width, 0.001)
|
||||
assert_in_delta(0, size.height, 0.001)
|
||||
|
||||
size = CvSize2D32f.new(1.1, 2.2)
|
||||
assert_in_delta(1.1, size.width, 0.001)
|
||||
assert_in_delta(2.2, size.height, 0.001)
|
||||
|
||||
size = CvSize2D32f.new(CvSize2D32f.new(1.1, 2.2))
|
||||
assert_in_delta(1.1, size.width, 0.001)
|
||||
assert_in_delta(2.2, size.height, 0.001)
|
||||
|
||||
assert_raise(ArgumentError) {
|
||||
CvSize2D32f.new('string')
|
||||
}
|
||||
assert_raise(ArgumentError) {
|
||||
CvSize2D32f.new(1, 2, 3)
|
||||
}
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
size = CvSize2D32f.new(1.1, 2.2)
|
||||
assert_equal('<OpenCV::CvSize2D32f:1.1x2.2>', size.to_s)
|
||||
end
|
||||
|
||||
def test_to_ary
|
||||
a = CvSize2D32f.new(1.1, 2.2).to_ary
|
||||
assert_in_delta(1.1, a[0], 0.001)
|
||||
assert_in_delta(2.2, a[1], 0.001)
|
||||
end
|
||||
end
|
||||
|
Loading…
Add table
Reference in a new issue