annotate test_lookup_list.py @ 275:323909110d1c

added test_lookup_list
author James Bergstra <bergstrj@iro.umontreal.ca>
date Thu, 05 Jun 2008 14:14:51 -0400
parents
children
rev   line source
275
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
1 from lookup_list import *
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
2 def have_raised(to_eval, **var):
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
3 have_thrown = False
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
4 try:
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
5 eval(to_eval)
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
6 except :
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
7 have_thrown = True
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
8 return have_thrown
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
9
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
10 def have_raised2(f, *args, **kwargs):
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
11 have_thrown = False
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
12 try:
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
13 f(*args, **kwargs)
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
14 except :
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
15 have_thrown = True
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
16 return have_thrown
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
17
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
18
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
19 def test_LookupList():
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
20 #test only the example in the doc???
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
21 print "test_LookupList"
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
22 example = LookupList(['x','y','z'],[1,2,3])
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
23 example['x'] = [1, 2, 3] # set or change a field
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
24 x, y, z = example
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
25 x = example[0]
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
26 x = example["x"]
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
27 assert example.keys()==['x','y','z']
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
28 assert example.values()==[[1,2,3],2,3]
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
29 assert example.items()==[('x',[1,2,3]),('y',2),('z',3)]
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
30 example.append_keyval('u',0) # adds item with name 'u' and value 0
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
31 assert len(example)==4 # number of items = 4 here
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
32 example2 = LookupList(['v','w'], ['a','b'])
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
33 example3 = LookupList(['x','y','z','u','v','w'], [[1, 2, 3],2,3,0,'a','b'])
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
34 assert example+example2==example3
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
35 assert have_raised("var['x']+var['x']",x=example)
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
36
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
37 del example, example2, example3, x, y ,z
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
38
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
39 if __name__=='__main__':
323909110d1c added test_lookup_list
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
40 test_LookupList()