From 9a3a49b9177006e66d9b845e517efbeedcb28141 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Mon, 3 Jan 2011 13:47:47 +0900 Subject: [PATCH] added some tests --- test/helper.rb | 32 +++++++- test/test_cvmat.rb | 159 ++++++++++++++++++++++++++++++++++++++- test/test_preliminary.rb | 48 +++++++++--- 3 files changed, 225 insertions(+), 14 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index db4b9c3..88e605b 100755 --- a/test/helper.rb +++ b/test/helper.rb @@ -25,7 +25,6 @@ class OpenCVTestCase < Test::Unit::TestCase end alias original_assert_in_delta assert_in_delta - # alias original_assert_equal assert_equal def assert_cvscalar_equal(expected, actual, message = nil) assert_equal(CvScalar, actual.class, message) @@ -66,5 +65,36 @@ class OpenCVTestCase < Test::Unit::TestCase } m end + + def assert_each_cvscalar(actual, delta, &block) + raise unless block_given? + count = 0 + actual.height.times { |j| + actual.width.times { |i| + expected = block.call(j, i, count) + if delta == 0 + expected = expected.to_ary if expected.is_a? CvScalar + assert_array_equal(expected, actual[i, j].to_ary) + else + assert_in_delta(expected, actual[i, j], delta) + end + count += 1 + } + } + end + + def print_cvmat(mat) + s = [] + mat.height.times { |j| + a = [] + mat.width.times { |i| + tmp = mat[i, j].to_ary.map {|m| m.to_i }.join(',') + #tmp = mat[i, j].to_ary.map {|m| m.to_f.round(2) }.join(',') + a << "[#{tmp}]" + } + s << a.join(' ') + } + puts s.join("\n") + end end diff --git a/test/test_cvmat.rb b/test/test_cvmat.rb index 2e2d262..f29c923 100755 --- a/test/test_cvmat.rb +++ b/test/test_cvmat.rb @@ -692,7 +692,7 @@ class TestCvMat < OpenCVTestCase end def test_mix_channels - flunk('CvMat.mix_channels is not implemented yet.') + flunk('FIXME: CvMat.mix_channels is not implemented yet.') end def test_rand_shuffle @@ -968,7 +968,162 @@ class TestCvMat < OpenCVTestCase } } end - + + def test_mul + m1 = create_cvmat(3, 3, :cv32f) + s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4) + m2 = create_cvmat(3, 3, :cv32f) { s1 } + + # CvMat * CvMat + m3 = m1.mul(m2) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = c + 1 + CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4) + } + + # CvMat * CvMat * scale + scale = 2.5 + m3 = m1.mul(m2, scale) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = (c + 1) * scale + CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4) + } + + # CvMat * CvScalar + scale = 2.5 + m3 = m1.mul(s1) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = c + 1 + CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4) + } + + # CvMat * CvScalar * scale + m3 = m1.mul(s1, scale) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = (c + 1) * scale + CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4) + } + end + + def test_mat_mul + flunk('FIXME: CvMat#mat_mul is not implemented yet.') + end + + def test_div + m1 = create_cvmat(3, 3, :cv32f) + s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4) + m2 = create_cvmat(3, 3, :cv32f) { s1 } + + # CvMat / CvMat + m3 = m1.div(m2) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = c + 1 + CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4) + } + + # scale * CvMat / CvMat + scale = 2.5 + m3 = m1.div(m2, scale) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = (c + 1) * scale + CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4) + } + + # CvMat / CvScalar + scale = 2.5 + m3 = m1.div(s1) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = c + 1 + CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4) + } + + # scale * CvMat / CvScalar + m3 = m1.div(s1, scale) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = (c + 1) * scale + CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4) + } + + # Alias + m3 = m1 / m2 + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = c + 1 + CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4) + } + end + + def test_and + m1 = create_cvmat(6, 4) + s1 = CvScalar.new(1, 2, 3, 4) + m2 = create_cvmat(6, 4) { s1 } + mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c| + s = (i < 3 and j < 2) ? 1 : 0 + CvScalar.new(s) + } + + # CvMat & CvMat + m3 = m1.and(m2) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0) { |j, i, c| + n = c + 1 + CvScalar.new(n & 1, n & 2, n & 3, n & 4) + } + + # CvMat & CvMat with mask + m3 = m1.and(m2, mask) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0) { |j, i, c| + n = c + 1 + if i < 3 and j < 2 + CvScalar.new(n & 1, n & 2, n & 3, n & 4) + else + CvScalar.new(n, n, n, n) + end + } + + # CvMat & CvScalar + m3 = m1.and(s1) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0.001) { |j, i, c| + n = c + 1 + CvScalar.new(n & 1, n & 2, n & 3, n & 4) + } + + # CvMat & CvScalar with mask + m3 = m1.and(s1, mask) + assert_equal(m1.height, m3.height) + assert_equal(m1.width, m3.width) + assert_each_cvscalar(m3, 0) { |j, i, c| + n = c + 1 + if i < 3 and j < 2 + CvScalar.new(n & 1, n & 2, n & 3, n & 4) + else + CvScalar.new(n, n, n, n) + end + } + end + # def test_avg_sdv # m = CvMat.new(1, 8, CV_32F) # [2, 4, 4, 4, 5, 5, 7, 9].each_with_index { |v, i| diff --git a/test/test_preliminary.rb b/test/test_preliminary.rb index 72cd4d7..521a528 100755 --- a/test/test_preliminary.rb +++ b/test/test_preliminary.rb @@ -40,16 +40,41 @@ class TestPreliminary < OpenCVTestCase # assert_in_delta([1, 2, 3, 4], [1.01, 2.01, 3.01, 4.01], 0.001) end + def test_assert_each_cvscalar + mat = CvMat.new(5, 5, :cv32f, 4) + c = 0 + mat.height.times { |j| + mat.width.times { |i| + mat[i, j] = CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4) + c += 1 + } + } + + assert_each_cvscalar(mat, 0.001) { |j, i, n| + CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4) + } + + # Uncomment the following lines to check the fail cases + # assert_each_cvscalar(mat, 0.001) { |j, i, n| + # CvScalar.new(n * 0.1, n * 0.2, n * 0.3, 0) + # } + # assert_each_cvscalar(mat, 0.001) { |j, i, n| + # CvScalar.new(1, 2, 3, 4) + # } + end + + def test_create_cvmat mat = create_cvmat(3, 4) assert_equal(3, mat.height) assert_equal(4, mat.width) assert_equal(:cv8u, mat.depth) assert_equal(4, mat.channel) - c = 1 - mat.height { |j| - mat.width { |i| - assert_cvscalar_equal(CvScalar.new(c, c, c, c), mat[i, j]) + c = 0 + mat.height.times { |j| + mat.width.times { |i| + s = CvScalar.new(c + 1, c + 1, c + 1, c + 1) + assert_cvscalar_equal(s, mat[i, j]) c += 1 } } @@ -59,10 +84,11 @@ class TestPreliminary < OpenCVTestCase assert_equal(3, mat.width) assert_equal(:cv16s, mat.depth) assert_equal(2, mat.channel) - c = 1 - mat.height { |j| - mat.width { |i| - assert_cvscalar_equal(CvScalar.new(c, c, 0, 0), mat[i, j]) + c = 0 + mat.height.times { |j| + mat.width.times { |i| + s = CvScalar.new(c + 1, c + 1, 0, 0) + assert_cvscalar_equal(s, mat[i, j]) c += 1 } } @@ -75,9 +101,9 @@ class TestPreliminary < OpenCVTestCase assert_equal(3, mat.width) assert_equal(:cv16u, mat.depth) assert_equal(3, mat.channel) - c = 1 - mat.height { |j| - mat.width { |i| + c = 0 + mat.height.times { |j| + mat.width.times { |i| n = j + i + c assert_cvscalar_equal(CvScalar.new(n, n, n, 0), mat[i, j]) c += 1