add relational overloads to python

and tests
This commit is contained in:
John Cupitt 2014-09-17 09:39:57 +01:00
parent de594151a5
commit 8b9dc20684
3 changed files with 96 additions and 8 deletions

10
TODO
View File

@ -1,6 +1,16 @@
- python:
- try:
$ python test_arithmetic.py -v TestArithmetic.test_moreeq
test_moreeq (__main__.TestArithmetic) ... **
VIPS:ERROR:cache.c:606:vips_cache_insert: assertion failed:
(!g_hash_table_lookup( vips_cache_table, operation ))
Aborted (core dumped)
same for more; less, lesseq, equal and noteq are fine
- could import like this:
from gi.repository import Vips

View File

@ -56,8 +56,8 @@ def run_fn2(fn, x, y):
class TestArithmetic(unittest.TestCase):
# test a pair of things which can be lists for approx. equality
def assertAlmostEqualObjects(self, a, b, msg = ''):
#print 'assertAlmostEqualObjects %s = %s' % (a, b)
for x, y in zip_expand(a, b):
#print 'assertAlmostEqual %s = %s' % (x, y)
self.assertAlmostEqual(x, y, places = 4, msg = msg)
# run a function on an image and on a single pixel, the results
@ -195,6 +195,84 @@ class TestArithmetic(unittest.TestCase):
self.run_arith_const(my_xor, fmt = noncomplex_formats)
self.run_arith(my_xor, fmt = noncomplex_formats)
def test_more(self):
def more(x, y):
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
return x > y
else:
if x > y:
return 255
else:
return 0
self.run_arith_const(more)
self.run_arith(more)
def test_moreeq(self):
def moreeq(x, y):
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
return x >= y
else:
if x >= y:
return 255
else:
return 0
self.run_arith_const(moreeq)
self.run_arith(moreeq)
def test_less(self):
def less(x, y):
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
return x < y
else:
if x < y:
return 255
else:
return 0
self.run_arith_const(less)
self.run_arith(less)
def test_lesseq(self):
def lesseq(x, y):
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
return x <= y
else:
if x <= y:
return 255
else:
return 0
self.run_arith_const(lesseq)
self.run_arith(lesseq)
def test_equal(self):
def equal(x, y):
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
return x == y
else:
if x == y:
return 255
else:
return 0
self.run_arith_const(equal)
self.run_arith(equal)
def test_noteq(self):
def noteq(x, y):
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
return x != y
else:
if x != y:
return 255
else:
return 0
self.run_arith_const(noteq)
self.run_arith(noteq)
# run a function on an image,
# 50,50 and 10,10 should have different values on the test image
def run_testunary(self, message, im, fn):

View File

@ -422,37 +422,37 @@ def vips_more(self, other):
if isinstance(other, Vips.Image):
return self.relational(other, Vips.OperationRelational.MORE)
else:
return self.relational_const(Vips.OperationRelational.MORE, other)
return self.relational_const(other, Vips.OperationRelational.MORE)
def vips_moreeq(self, other):
if isinstance(other, Vips.Image):
return self.relational(other, Vips.OperationRelational.MOREEQ)
else:
return self.relational_const(Vips.OperationRelational.MOREEQ, other)
return self.relational_const(other, Vips.OperationRelational.MOREEQ)
def vips_less(self, other):
if isinstance(other, Vips.Image):
return self.relational(other, Vips.OperationRelational.MORE)
return self.relational(other, Vips.OperationRelational.LESS)
else:
return self.relational_const(Vips.OperationRelational.MORE, other)
return self.relational_const(other, Vips.OperationRelational.LESS)
def vips_lesseq(self, other):
if isinstance(other, Vips.Image):
return self.relational(other, Vips.OperationRelational.LESSEQ)
else:
return self.relational_const(Vips.OperationRelational.LESSEQ, other)
return self.relational_const(other, Vips.OperationRelational.LESSEQ)
def vips_equal(self, other):
if isinstance(other, Vips.Image):
return self.relational(other, Vips.OperationRelational.EQUAL)
else:
return self.relational_const(Vips.OperationRelational.EQUAL, other)
return self.relational_const(other, Vips.OperationRelational.EQUAL)
def vips_notequal(self, other):
if isinstance(other, Vips.Image):
return self.relational(other, Vips.OperationRelational.NOTEQ)
else:
return self.relational_const(Vips.OperationRelational.NOTEQ, other)
return self.relational_const(other, Vips.OperationRelational.NOTEQ)
def vips_neg(self):
return -1 * self