From 171310a30152aab151c970801f2925f4363a7ad9 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 9 Jan 2011 19:01:43 +0900 Subject: [PATCH] fixed CvPoint2D32f, CvPoint3D32f, and added some tests for them --- ext/cvpoint2d32f.cpp | 2 +- ext/cvpoint3d32f.cpp | 6 ++- test/test_cvpoint2d32f.rb | 70 ++++++++++++++++++++++++++++++++ test/test_cvpoint3d32f.rb | 84 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 3 deletions(-) create mode 100755 test/test_cvpoint2d32f.rb create mode 100755 test/test_cvpoint3d32f.rb diff --git a/ext/cvpoint2d32f.cpp b/ext/cvpoint2d32f.cpp index 05b5b0e..86e6a71 100644 --- a/ext/cvpoint2d32f.cpp +++ b/ext/cvpoint2d32f.cpp @@ -182,7 +182,7 @@ rb_to_s(VALUE self) { const int i = 4; VALUE str[i]; - str[0] = rb_str_new2("<%s:(%f,%f)>"); + str[0] = rb_str_new2("<%s:(%g,%g)>"); str[1] = rb_str_new2(rb_class2name(CLASS_OF(self))); str[2] = rb_x(self); str[3] = rb_y(self); diff --git a/ext/cvpoint3d32f.cpp b/ext/cvpoint3d32f.cpp index dd2eacc..3bdc9e5 100644 --- a/ext/cvpoint3d32f.cpp +++ b/ext/cvpoint3d32f.cpp @@ -85,7 +85,9 @@ define_ruby_class() VALUE rb_compatible_q(VALUE klass, VALUE object) { - return (rb_respond_to(object, rb_intern("x")) && rb_respond_to(object, rb_intern("y"))) ? Qtrue : Qfalse; + return (rb_respond_to(object, rb_intern("x")) && + rb_respond_to(object, rb_intern("y")) && + rb_respond_to(object, rb_intern("z"))) ? Qtrue : Qfalse; } VALUE @@ -213,7 +215,7 @@ rb_to_s(VALUE self) { const int i = 5; VALUE str[i]; - str[0] = rb_str_new2("<%s:(%f,%f,%f)>"); + str[0] = rb_str_new2("<%s:(%g,%g,%g)>"); str[1] = rb_str_new2(rb_class2name(CLASS_OF(self))); str[2] = rb_x(self); str[3] = rb_y(self); diff --git a/test/test_cvpoint2d32f.rb b/test/test_cvpoint2d32f.rb new file mode 100755 index 0000000..cd196c3 --- /dev/null +++ b/test/test_cvpoint2d32f.rb @@ -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::CvPoint2D32f +class TestCvPoint2D32f < OpenCVTestCase + class MyPoint; end + def test_x + point = CvPoint2D32f.new + point.x = 1.1 + assert_in_delta(1.1, point.x, 0.001) + point.x = 2.2 + assert_in_delta(2.2, point.x, 0.001) + end + + def test_y + point = CvPoint2D32f.new + point.y = 1.1 + assert_in_delta(1.1, point.y, 0.001) + point.y = 2.2 + assert_in_delta(2.2, point.y, 0.001) + end + + def test_compatible + assert(!(CvPoint2D32f.compatible? MyPoint.new)) + MyPoint.class_eval { def x; end } + assert(!(CvPoint2D32f.compatible? MyPoint.new)) + MyPoint.class_eval { def y; end } + assert(CvPoint2D32f.compatible? MyPoint.new) + assert(CvPoint2D32f.compatible? CvPoint2D32f.new) + end + + def test_initialize + point = CvPoint2D32f.new + assert_in_delta(0, point.x, 0.001) + assert_in_delta(0, point.y, 0.001) + + point = CvPoint2D32f.new(1.1, 2.2) + assert_in_delta(1.1, point.x, 0.001) + assert_in_delta(2.2, point.y, 0.001) + + point = CvPoint2D32f.new(CvPoint2D32f.new(1.1, 2.2)) + assert_in_delta(1.1, point.x, 0.001) + assert_in_delta(2.2, point.y, 0.001) + + assert_raise(ArgumentError) { + CvPoint2D32f.new('string') + } + assert_raise(ArgumentError) { + CvPoint2D32f.new(1, 2, 3) + } + end + + def test_to_s + point = CvPoint2D32f.new(1.1, 2.2) + assert_equal('', point.to_s) + end + + def test_to_ary + a = CvPoint2D32f.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 + + diff --git a/test/test_cvpoint3d32f.rb b/test/test_cvpoint3d32f.rb new file mode 100755 index 0000000..8150d06 --- /dev/null +++ b/test/test_cvpoint3d32f.rb @@ -0,0 +1,84 @@ +#!/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::CvPoint3D32f +class TestCvPoint3D32f < OpenCVTestCase + class MyPoint; end + def test_x + point = CvPoint3D32f.new + point.x = 1.1 + assert_in_delta(1.1, point.x, 0.001) + point.x = 2.2 + assert_in_delta(2.2, point.x, 0.001) + end + + def test_y + point = CvPoint3D32f.new + point.y = 1.1 + assert_in_delta(1.1, point.y, 0.001) + point.y = 2.2 + assert_in_delta(2.2, point.y, 0.001) + end + + def test_z + point = CvPoint3D32f.new + point.z = 1.1 + assert_in_delta(1.1, point.z, 0.001) + point.z = 2.2 + assert_in_delta(2.2, point.z, 0.001) + end + + def test_compatible + assert(!(CvPoint3D32f.compatible? MyPoint.new)) + MyPoint.class_eval { def x; end } + assert(!(CvPoint3D32f.compatible? MyPoint.new)) + MyPoint.class_eval { def y; end } + assert(!(CvPoint3D32f.compatible? MyPoint.new)) + MyPoint.class_eval { def z; end } + assert(CvPoint3D32f.compatible? MyPoint.new) + assert(CvPoint3D32f.compatible? CvPoint3D32f.new) + end + + def test_initialize + point = CvPoint3D32f.new + assert_in_delta(0, point.x, 0.001) + assert_in_delta(0, point.y, 0.001) + assert_in_delta(0, point.z, 0.001) + + point = CvPoint3D32f.new(1.1, 2.2, 3.3) + assert_in_delta(1.1, point.x, 0.001) + assert_in_delta(2.2, point.y, 0.001) + assert_in_delta(3.3, point.z, 0.001) + + point = CvPoint3D32f.new(CvPoint3D32f.new(1.1, 2.2, 3.3)) + assert_in_delta(1.1, point.x, 0.001) + assert_in_delta(2.2, point.y, 0.001) + assert_in_delta(3.3, point.z, 0.001) + + assert_raise(ArgumentError) { + CvPoint3D32f.new('string') + } + assert_raise(ArgumentError) { + CvPoint3D32f.new(1, 2) + } + end + + def test_to_s + point = CvPoint3D32f.new(1.1, 2.2, 3.3) + assert_equal('', point.to_s) + end + + def test_to_ary + a = CvPoint3D32f.new(1.1, 2.2, 3.3).to_ary + assert_in_delta(1.1, a[0], 0.001) + assert_in_delta(2.2, a[1], 0.001) + assert_in_delta(3.3, a[2], 0.001) + end +end + +