changeset 32:def8098ddb9e task

Improved test suite
author David Grundberg <individ@acc.umu.se>
date Tue, 05 May 2009 20:57:24 +0200
parents d3c4fa6c6272
children 1d7bab3bc745
files test/test.py testfile.m
diffstat 2 files changed, 174 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/test/test.py	Tue May 05 20:55:13 2009 +0200
+++ b/test/test.py	Tue May 05 20:57:24 2009 +0200
@@ -1,89 +1,203 @@
 #!/usr/bin/python
+# -*- coding:utf-8 -*-
 
 import pytave
 import Numeric
 
-pytave.feval(0, "addpath", ".",);
+print "No messages indicates test pass."
 
 arr0_0 = Numeric.zeros((0,0));
 arr0_1 = Numeric.zeros((0,1));
 arr1_0 = Numeric.zeros((1,0));
-arr1f = Numeric.array([1, 2, 3, 4], Numeric.Float32)
-arr1b = Numeric.array([1, 2, 3, 256], Numeric.Int8)
-arr1i = Numeric.array([1, 2, 3, 4], Numeric.Int)
-arr1i32 = Numeric.array([1, 2, 3, 4], Numeric.Int32)
-arr1a = Numeric.array([1, 2, 3, 4])
-arr2f = Numeric.array([[1, 2, 3, 4],[5,6,7,8]], Numeric.Float32)
-arr2d = Numeric.array([[1, 2, 3, 4],[5,6,7,8]], Numeric.Float)
-arr3f = Numeric.array([[[1, 2, 3, 4],[5,6,7,8]],[[9, 10, 11, 12],[13,14,15,16]]], Numeric.Float32)
+number = Numeric.array([1.32], Numeric.Float32)
+arr1fT = Numeric.array([[1.32], [2], [3], [4]], Numeric.Float32)
+arr1fT2 = Numeric.array([[1.32, 2, 3, 4]], Numeric.Float32)
+arr1f = Numeric.array([[1.32, 2, 3, 4]], Numeric.Float32)
+arr1b = Numeric.array([[8, 2, 3, 256]], Numeric.Int8)
+arr1i = Numeric.array([[17, 2, 3, 4]], Numeric.Int)
+arr1i32 = Numeric.array([[32, 2, 3, 4]], Numeric.Int32)
+arr1a = Numeric.array([[1, 2, 3, 4]])
+arr2f = Numeric.array([[1.32, 2, 3, 4],[5,6,7,8]], Numeric.Float32)
+arr2d = Numeric.array([[1.17, 2, 3, 4],[5,6,7,8]], Numeric.Float)
+arr3f = Numeric.array([[[1.32, 2, 3, 4],[5,6,7,8]],[[9, 10, 11, 12],[13,14,15,16]]], Numeric.Float32)
 
-alimit_int32 = Numeric.array([-2147483648, 2147483647], Numeric.Int32);
-alimit_int16 = Numeric.array([-32768, 32767, -32769, 32768], Numeric.Int16);
-alimit_int8 = Numeric.array([-128, 127, -129, 128], Numeric.Int8);
-alimit_uint8 = Numeric.array([0, 255, -1, 256], Numeric.UnsignedInt8);
+alimit_int32 = Numeric.array([[-2147483648, 2147483647]], Numeric.Int32);
+alimit_int16 = Numeric.array([[-32768, 32767, -32769, 32768]], Numeric.Int16);
+alimit_int8 = Numeric.array([[-128, 127, -129, 128]], Numeric.Int8);
+alimit_uint8 = Numeric.array([[0, 255, -1, 256]], Numeric.UnsignedInt8);
 
 
-b = pytave.feval(1, "testfile", 1)
-print "first exec ok:", b
+# This eval call is not to be seen as a encouragement to use Pytave
+# like this. Create a separate .m-file with your complex Octave code.
+pytave.feval(1, "eval", "function [result] = test_return(arg) "
+"result = arg; endfunction")
+
+pytave.feval(1, "test_return", 1)
 
 def testequal(value):
 	try:
-		print "------------ test ", value
-		nvalue = pytave.feval(1, "testfile", *value)
+		nvalue, = pytave.feval(1, "test_return", value)
 		if nvalue != value:
-			print "Equal ", value, " == ", nvalue, ", ", (nvalue == value), " Equal"
+			print "FAIL as ", value, " != ", nvalue
+	except TypeError, e:
+		print "FAIL: ", value,":", e
+
+def testexpect(value, expected):
+	try:
+		nvalue, = pytave.feval(1, "test_return", value)
+		if nvalue != expected:
+			print "FAIL as ", nvalue, " != ", expected, ","
+			print "        sent in", value
+	except TypeError, e:
+		print "FAIL: ", value,":", e
+
+
+def testmatrix(value):
+	try:
+		nvalue, = pytave.feval(1, "test_return", value)
+#		print "test", (value,)
+#		print "returned ", (nvalue,)
+		class1 = pytave.feval(1, "class", value)
+		class2 = pytave.feval(1, "class", nvalue)
+		if nvalue != value:
+			print "FAIL as ", value, " != ", nvalue
+		if value.shape != nvalue.shape:
+			print "Size check failed for: ", (value,) ,". Got ",value.shape, "and later", nvalue.shape, " =++ ", (nvalue,)
+		if class1 != class2:
+			print "Type check failed for: ", (value,) ,". Got ",class1, "and later", class2
 	except TypeError, e:
 		print "Execute failed: ", value,":", e
 
-def testint(value):
+def testobjecterror(value):
+	try:
+		print pytave.feval(1, "test_return", value);
+		print "FAIL:", (value,)
+	except pytave.ObjectConvertError:
+		pass
+	except Exception, e:
+		print "FAIL", (value,), e
+
+def testvalueerror(*value):
+	try:
+		print pytave.feval(1, *value);
+		print "FAIL:", (value,)
+	except pytave.ValueConvertError:
+		pass
+	except Exception, e:
+		print "FAIL", (value,), e
+
+def testvalueok(*value):
 	try:
-		print "------------ int test ", value
-		nvalue = pytave.feval(1, "testfile", *value)
-		class1 = pytave.feval(1, "class", *value)
-		class2 = pytave.feval(1, "class", *nvalue)
-		if class1 != class2:
-			print "Integer check failed, got ",class1, "and later", class2
-	except TypeError, e:
-		print "Execute failed: ", value,":", e
-	
-print "------------"
+		pytave.feval(1, *value);
+	except Exception, e:
+		print "FAIL", (value,), e
+
+def testcellinvariant(value):
+	pass
+
+testequal('a')
+
+
+testmatrix(alimit_int32)
+testmatrix(alimit_int16)
+testmatrix(alimit_int8)
+
+# Strings
+# Multi-row character matrix cannot be returned
+testvalueerror("eval", "['foo'; 'bar']")
+testequal('a')
 
-testint((alimit_int32, ))
-testint((alimit_int16, ))
-testint((alimit_int8, ))
+testequal("mystring")
+testequal('mystring')
+testequal("mystringåäöÅÄÖ")
+
+testequal(1)
+testequal(1L)
+testequal(1.2)
+testequal(1.2)
 
-testequal((alimit_int32, ))
-testequal((alimit_int16, ))
-testequal((alimit_int8, ))
+# Vector arrays
+testmatrix(arr1a)
+testmatrix(arr1f)
+testmatrix(arr1fT)
+testmatrix(arr1fT2)
+testmatrix(arr1i)
+testmatrix(arr1b)
+testmatrix(arr1i32)
 
-testequal(("mystring", ))
+# 2d arrays
+testmatrix(arr2f)
+testmatrix(arr2d)
+
+# 3d arrays
+testmatrix(arr3f)
 
-testequal((1, ))
-testequal((1L, ))
-testequal((1.2, ))
-testequal((1.2, ))
-testequal((arr1a, ))
+# Note, both arr0_0 == arr0_0 and arr0_0 != arr0_0 are false!
+if (arr0_0 != arr0_0) or (arr0_0 == arr0_0):
+	print "FAIL: Zero test", 
+
+testmatrix(arr0_0)
+testmatrix(arr1_0)
+testmatrix(arr0_1)
+
+# Lists
+testequal([1, 2])
+testequal([[1, 2], [3, 4]])
+testequal([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
+testequal([])
 
-testequal((arr1f, ))
-testequal((arr1i, ))
-testequal((arr1b, ))
-testequal((arr1i32, ))
-testequal((arr2f, ))
-testequal((arr2d, ))
+# Return cells with OK dimensions
+testvalueok("cell", 1, 3);
+testvalueok("cell", 1, 0)
+
+# Return cells with incompatible dimensions
+testvalueerror("cell", 3, 1)
+testvalueerror("cell", 0, 0)
+testvalueerror("cell", 0, 1)
 
-testequal((arr3f, ))
+# Dictionaries
+
+# Simple dictionary tests
+testequal({"foo": [1], "bar": [2]})
+testequal({"x": [1, 3], "y": [2, 4]})
+testequal({"x": [1, "baz"], "y": [2, "foobar"]})
+testequal({"x": [arr1f], "y": [arr1i]})
+testequal({})
 
-print("Equality for these three tests fails")
-testequal((arr0_0, ))
-testequal((arr1_0, ))
-testequal((arr0_1, ))
+# Try some odd dictionaries
+# The implicit conversion makes Pytave return cell-wrapped results.
+testexpect({"foo": number,   "bar": 2},
+	   {"foo": [number], "bar": [2]})
+testexpect({"foo": arr1f,    "bar": arr2f},
+	   {"foo": [arr1f],  "bar": [arr2f]})
+testexpect({"foo": 1,        "bar": 2},
+	   {"foo": [1],      "bar": [2]})
+testexpect({"foo": 1,        "bar": [2]},
+	   {"foo": [1],      "bar": [2]})
+testexpect({"foo": 1,        "bar": [2, 3]},
+	   {"foo": [1, 1],   "bar": [2, 3]})
+testexpect({"foo": [1],      "bar": [2, 4]},
+	   {"foo": [1, 1],   "bar": [2, 4]})
+testexpect({"bar": 1,        "foo": [2, 3]},
+	   {"bar": [1, 1],   "foo": [2, 3]})
+testexpect({"bar": [1],      "foo": [2, 4]},
+	   {"bar": [1, 1],   "foo": [2, 4]})
 
-# these should fail.
-print("These tests should fail.")
-testequal(([1, 2],))
-testequal((None,))
-testequal(((1,),))
-testequal(([],))
-testequal(((),))
-testequal((1, 8.9, 3, 4, "testfile", [], arr1f))
+# Try some invalid keys
+testobjecterror({"this is not an Octave identifier": 1})
+testobjecterror({1.22: 1})
+
+# These should fail: No object conversion defined.
+testobjecterror(None)
+testobjecterror((1, ))
+testobjecterror(())
 
+result, = pytave.feval(1, "eval", "[1, 1, 1]")
+if result.shape != (1, 3):
+	print "FAIL: expected 1x3 matrix"
+
+result, = pytave.feval(1, "eval", "[1; 2; 3]");
+if result.shape != (3, 1):
+	print "FAIL: expected 3x1 matrix"
+
+
--- a/testfile.m	Tue May 05 20:55:13 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-function [testresult] = testfile(arg)
-	 disp(arg)
-	 disp(["Type info: " typeinfo(arg)])
-	 testresult = arg;#int8([1]);
-end