mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
added error handling to some CvMat method
This commit is contained in:
parent
d983d80fa6
commit
8b48490a43
3 changed files with 581 additions and 263 deletions
File diff suppressed because it is too large
Load diff
|
@ -16,32 +16,32 @@ class OpenCVTestCase < Test::Unit::TestCase
|
|||
|
||||
CvMat.class_eval do
|
||||
# Range check for debug
|
||||
alias original_aref []
|
||||
alias original_aset []=;
|
||||
# alias original_aref []
|
||||
# alias original_aset []=;
|
||||
|
||||
def [](*idx)
|
||||
if idx.size == 1 and idx[0].is_a? Numeric
|
||||
n = idx[0]
|
||||
throw ArgumentError.new("index #{n} is out of range") if n >= rows * cols
|
||||
elsif idx.all? { |elem| elem.is_a? Numeric }
|
||||
j, i = *(idx.map { |x| x.to_i })
|
||||
throw ArgumentError.new("index for row #{j} is out of range") if j >= rows
|
||||
throw ArgumentError.new("index for column #{i} is out of range") if i >= cols
|
||||
end
|
||||
original_aref(*idx)
|
||||
end
|
||||
# def [](*idx)
|
||||
# if idx.size == 1 and idx[0].is_a? Numeric
|
||||
# n = idx[0]
|
||||
# throw ArgumentError.new("index #{n} is out of range") if n >= rows * cols
|
||||
# elsif idx.all? { |elem| elem.is_a? Numeric }
|
||||
# j, i = *(idx.map { |x| x.to_i })
|
||||
# throw ArgumentError.new("index for row #{j} is out of range") if j >= rows
|
||||
# throw ArgumentError.new("index for column #{i} is out of range") if i >= cols
|
||||
# end
|
||||
# original_aref(*idx)
|
||||
# end
|
||||
|
||||
def []=(*args)
|
||||
if args.size == 2 and args[0].is_a? Numeric
|
||||
n = args[0] # index
|
||||
throw ArgumentError.new("index #{n} is out of range") if n >= rows * cols
|
||||
elsif args[0..1].all? { |elem| elem.is_a? Numeric }
|
||||
j, i = *args
|
||||
throw ArgumentError.new("index for row #{j} is out of range") if j.to_i >= rows
|
||||
throw ArgumentError.new("index for column #{i} is out of range") if i.to_i >= cols
|
||||
end
|
||||
original_aset(*args)
|
||||
end
|
||||
# def []=(*args)
|
||||
# if args.size == 2 and args[0].is_a? Numeric
|
||||
# n = args[0] # index
|
||||
# throw ArgumentError.new("index #{n} is out of range") if n >= rows * cols
|
||||
# elsif args[0..1].all? { |elem| elem.is_a? Numeric }
|
||||
# j, i = *args
|
||||
# throw ArgumentError.new("index for row #{j} is out of range") if j.to_i >= rows
|
||||
# throw ArgumentError.new("index for column #{i} is out of range") if i.to_i >= cols
|
||||
# end
|
||||
# original_aset(*args)
|
||||
# end
|
||||
end
|
||||
|
||||
def snap(*images)
|
||||
|
|
|
@ -443,12 +443,11 @@ class TestCvMat < OpenCVTestCase
|
|||
assert_cvscalar_equal(s, d[i])
|
||||
}
|
||||
|
||||
flunk('FIXME: Not handle out of range error yet')
|
||||
# [m.rows, m.cols, -m.rows, -m.cols].each { |d|
|
||||
# assert_raise(ArgumentError) {
|
||||
# m.diag(d)
|
||||
# }
|
||||
# }
|
||||
[m.rows, m.cols, -m.rows, -m.cols].each { |d|
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m.diag(d)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_size
|
||||
|
@ -487,6 +486,25 @@ class TestCvMat < OpenCVTestCase
|
|||
assert_raise(TypeError) {
|
||||
m[DUMMY_OBJ]
|
||||
}
|
||||
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[-1]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[6]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[2, 2]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[1, 3]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[2, 2, 1]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[1, 3, 1]
|
||||
}
|
||||
end
|
||||
|
||||
def test_aset
|
||||
|
@ -506,6 +524,25 @@ class TestCvMat < OpenCVTestCase
|
|||
assert_raise(TypeError) {
|
||||
m[0] = DUMMY_OBJ
|
||||
}
|
||||
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[-1]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[6]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[2, 2]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[1, 3]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[2, 2, 1]
|
||||
}
|
||||
assert_raise(CvStsOutOfRange) {
|
||||
m[1, 3, 1]
|
||||
}
|
||||
end
|
||||
|
||||
def test_set_data
|
||||
|
@ -837,14 +874,14 @@ class TestCvMat < OpenCVTestCase
|
|||
assert_raise(ArgumentError) {
|
||||
CvMat.merge(m1, m2, m3, m4, m5)
|
||||
}
|
||||
assert_raise(StandardError) {
|
||||
assert_raise(ArgumentError) {
|
||||
CvMat.merge(CvMat.new(1, 2, :cv8u, 2))
|
||||
}
|
||||
assert_raise(StandardError) {
|
||||
assert_raise(ArgumentError) {
|
||||
CvMat.merge(CvMat.new(1, 2, :cv8u, 1),
|
||||
CvMat.new(2, 2, :cv8u, 1))
|
||||
}
|
||||
assert_raise(StandardError) {
|
||||
assert_raise(ArgumentError) {
|
||||
CvMat.merge(CvMat.new(1, 2, :cv8u, 1),
|
||||
CvMat.new(1, 2, :cv32f, 1))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue