changeset 84:aa9e786ee849

added function have_raised that evaluate the string in parameter and return true if the function have raised an exception code cleanup and renaming added a few test
author Frederic Bastien <bastienf@iro.umontreal.ca>
date Mon, 05 May 2008 11:49:40 -0400
parents c0f211213a58
children 7b1f3ad3d60b c4726e19b8ec
files test_dataset.py
diffstat 1 files changed, 62 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/test_dataset.py	Mon May 05 11:14:28 2008 -0400
+++ b/test_dataset.py	Mon May 05 11:49:40 2008 -0400
@@ -3,7 +3,16 @@
 from math import *
 import numpy
 
+def have_raised(to_eval):
+    have_thrown = False
+    try:
+        eval(to_eval)
+    except :
+        have_thrown = True
+    return have_thrown
+
 def test1():
+    print "test1"
     global a,ds
     a = numpy.random.rand(10,4)
     print a
@@ -29,8 +38,8 @@
     #test with y too
     #test missing value
 
+    print "test_ArrayDataSet"
     a = numpy.random.rand(10,4)
-    print a
     ds = ArrayDataSet(a,{'x':slice(3),'y':3,'z':[0,2]})
     assert len(ds)==10
     #assert ds==a? should this work?
@@ -38,7 +47,6 @@
         assert ds[i]['x'].all()==a[i][:2].all()
         assert ds[i]['y']==a[i][3]
         assert ds[i]['z'].all()==a[i][0:3:2].all()
-    print "x=",ds["x"]
     i=0
     for x in ds('x','y'):
         assert numpy.append(x['x'],x['y']).all()==a[i].all()
@@ -61,19 +69,8 @@
 #    for var in minibatch:
 #        print "var=",var
 #    print "take a slice and look at field y",ds[1:6:2]["y"]
-    have_thrown = False
-    try:
-        ds['h'] # h is not defined...
-    except :
-        have_thrown = True
-    assert have_thrown == True
-
-    have_thrown = False
-    try:
-        ds[['h']] # h is not defined...
-    except :
-        have_thrown = True
-    assert have_thrown == True
+    assert have_raised("ds['h']")  # h is not defined...
+    assert have_raised("ds[['h']]")  # h is not defined...
 
     assert len(ds.fields())==3
     for field in ds.fields():
@@ -93,20 +90,33 @@
     #ds[:n] returns a dataset with the n first examples.
     assert len(ds[:3])==3
     i=0
+    for x,z,y in ds[:3]('x','z','y'):
+        assert ds[i]['x'].all()==a[i][:3].all()
+        assert ds[i]['x'].all()==x.all()
+        assert ds[i]['y']==a[i][3]
+        assert ds[i]['y']==y
+        assert ds[i]['z'].all()==a[i][0:3:2].all()
+        assert ds[i]['z'].all()==z.all()
+        i+=1
+    i=0
     for x,z in ds[:3]('x','z'):
         assert ds[i]['z'].all()==a[i][0:3:2].all()
         i+=1
 
     #ds[i1:i2:s]# returns a ds with the examples i1,i1+s,...i2-s.
-        ds[1:7:2][1] #fail???
+    ds[1:7:2][1]
     assert len(ds[1:7:2])==3 # should be number example 1,3 and 5
     i=0
-    for x,z in ds[1:7:2]('x','z'):
-        assert ds[i]['z'].all()==a[i][0:3:2].all()
+    index=[1,3,5]
+    for z,y,x in ds[1:7:2]('z','y','x'):
+        assert ds[index[i]]['x'].all()==a[index[i]][:3].all()
+        assert ds[index[i]]['x'].all()==x.all()
+        assert ds[index[i]]['y']==a[index[i]][3]
+        assert ds[index[i]]['y']==y
+        assert ds[index[i]]['z'].all()==a[index[i]][0:3:2].all()
+        assert ds[index[i]]['z'].all()==z.all()
         i+=1
-    ds2=ds[1:7:2]
-    for i in range(len(ds2)):
-        print ds2[i]
+
     #ds[[i1,i2,...in]]# returns a ds with examples i1,i2,...in.
     i=0
     for x in ds[[1,2]]:
@@ -129,44 +139,45 @@
 
 def test_LookupList():
     #test only the example in the doc???
-       example = LookupList(['x','y','z'],[1,2,3])
-       example['x'] = [1, 2, 3] # set or change a field
-       x, y, z = example
-       x = example[0]
-       x = example["x"]
-       assert example.keys()==['x','y','z']
-       assert example.values()==[[1,2,3],2,3]
-       assert example.items()==[('x',[1,2,3]),('y',2),('z',3)]
-       example.append_keyval('u',0) # adds item with name 'u' and value 0
-       assert len(example)==4 # number of items = 4 here
-       example2 = LookupList(['v','w'], ['a','b'])
-       example3 = LookupList(['x','y','z','u','v','w'], [[1, 2, 3],2,3,0,'a','b'])
-       print example3
-       print example+example2
-       print example+example2
-       assert example+example2==example3
-       have_throw=False
-       try:
-           example+example
-       except:
-           have_throw=True
-       assert have_throw
+    print "test_LookupList"
+    example = LookupList(['x','y','z'],[1,2,3])
+    example['x'] = [1, 2, 3] # set or change a field
+    x, y, z = example
+    x = example[0]
+    x = example["x"]
+    assert example.keys()==['x','y','z']
+    assert example.values()==[[1,2,3],2,3]
+    assert example.items()==[('x',[1,2,3]),('y',2),('z',3)]
+    example.append_keyval('u',0) # adds item with name 'u' and value 0
+    assert len(example)==4 # number of items = 4 here
+    example2 = LookupList(['v','w'], ['a','b'])
+    example3 = LookupList(['x','y','z','u','v','w'], [[1, 2, 3],2,3,0,'a','b'])
+    assert example+example2==example3
+    assert have_raised("example+example")
 
-def ApplyFunctionDataSet():
+def test_ApplyFunctionDataSet():
+    print "test_ApplyFunctionDataSet"
     raise NotImplementedError()
-def CacheDataSet():
+def test_CacheDataSet():
+    print "test_CacheDataSet"
     raise NotImplementedError()
-def FieldsSubsetDataSet():
+def test_FieldsSubsetDataSet():
+    print "test_FieldsSubsetDataSet"
     raise NotImplementedError()
-def DataSetFields():
+def test_DataSetFields():
+    print "test_DataSetFields"
     raise NotImplementedError()
-def MinibatchDataSet():
+def test_MinibatchDataSet():
+    print "test_MinibatchDataSet"
     raise NotImplementedError()
-def HStackedDataSet():
+def test_HStackedDataSet():
+    print "test_HStackedDataSet"
     raise NotImplementedError()
-def VStackedDataSet():
+def test_VStackedDataSet():
+    print "test_VStackedDataSet"
     raise NotImplementedError()
-def ArrayFieldsDataSet():
+def test_ArrayFieldsDataSet():
+    print "test_ArrayFieldsDataSet"
     raise NotImplementedError()
 
 test1()