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

fix Size and add tests

This commit is contained in:
ser1zw 2016-04-03 07:15:25 +09:00
parent c235d72f00
commit cb14a132b7
2 changed files with 80 additions and 0 deletions

View file

@ -1,4 +1,5 @@
// -*- mode: c++; coding: utf-8 -*- // -*- mode: c++; coding: utf-8 -*-
#include <sstream>
#include "ruby.h" #include "ruby.h"
#include "opencv2/core.hpp" #include "opencv2/core.hpp"
@ -99,11 +100,34 @@ namespace rubyopencv {
selfptr->width = NUM2INT(v1); selfptr->width = NUM2INT(v1);
selfptr->height = NUM2INT(v2); selfptr->height = NUM2INT(v2);
break; break;
default:
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 2)", argc);
break;
} }
return self; return self;
} }
/*
* Return size as an +String+.
*
* @overload to_s
* @return [String] String representation of the size
*/
VALUE rb_to_s(VALUE self) {
std::stringstream s;
cv::Size* selfptr = obj2size(self);
s << *selfptr;
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);
}
void init() { void init() {
VALUE opencv = rb_define_module("OpenCV"); VALUE opencv = rb_define_module("OpenCV");
@ -115,6 +139,8 @@ namespace rubyopencv {
rb_define_method(rb_klass, "width=", RUBY_METHOD_FUNC(rb_set_width), 1); 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_height), 0);
rb_define_method(rb_klass, "height=", RUBY_METHOD_FUNC(rb_set_height), 1); 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);
} }
} }
} }

54
test/test_size.rb Executable file
View file

@ -0,0 +1,54 @@
#!/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::Size
class TestSize < OpenCVTestCase
def test_width
size = Size.new
size.width = 100
assert_equal(100, size.width)
size.width = 200
assert_equal(200, size.width)
end
def test_height
size = Size.new
size.height = 100
assert_equal(100, size.height)
size.height = 200
assert_equal(200, size.height)
end
def test_initialize
size = Size.new
assert_equal(0, size.width)
assert_equal(0, size.height)
size = Size.new(10, 20)
assert_equal(10, size.width)
assert_equal(20, size.height)
assert_raise(TypeError) {
Size.new(DUMMY_OBJ, 1)
}
assert_raise(TypeError) {
Size.new(1, DUMMY_OBJ)
}
assert_raise(ArgumentError) {
Size.new(1)
}
assert_raise(ArgumentError) {
Size.new(1, 2, 3)
}
end
def test_to_s
size = Size.new(10, 20)
assert_equal('#<OpenCV::Size:[10 x 20]>', size.to_s)
end
end