7
from test_all import verbose
9
# Check if we are using python 2.2
10
if sys.version_info[0] == 2 and sys.version_info[1] == 2:
15
def WriteRead(testTuple):
18
print "Running test for object %s" % \
21
# Create an instance of HDF5 Table
22
file = tempfile.mktemp(".h5")
23
fileh = openFile(file, mode = "w")
26
# Create the array under root and name 'somearray'
29
fileh.createArray(root, 'somearray', a, "Some array")
33
# re-raise ValueError exception
38
# Re-open the file in read-only mode
39
fileh = openFile(file, mode = "r")
42
# Read the saved array
43
b = root.somearray.read()
44
# Compare them. They should be equal.
45
if not a == b and verbose:
46
print "Write and read lists/tuples differ!"
47
print "Object written:", a
48
print "Object read:", b
51
# Check strictly the array equality
56
# Then, delete the file
60
class BasicTestCase(unittest.TestCase):
62
def test00_char(self):
63
"Data integrity during recovery (character types)"
69
def test01_types(self):
70
"Data integrity during recovery (numerical types)"
72
a = self.numericalList
76
class Basic0DOneTestCase(BasicTestCase):
78
title = "Rank-0 case 1"
82
class Basic0DTwoTestCase(BasicTestCase):
84
title = "Rank-0 case 2"
88
# This does not work anymore because I've splitted the chunked arrays to happen
89
# mainly in EArray objects
90
# class Basic1DZeroTestCase(BasicTestCase):
91
# title = "Rank-1 case 0"
95
class Basic1DOneTestCase(BasicTestCase):
97
title = "Rank-1 case 1"
101
class Basic1DTwoTestCase(BasicTestCase):
103
title = "Rank-1 case 2"
104
numericalList = [3.2, 4.2]
107
class Basic1DThreeTestCase(BasicTestCase):
109
# Detection for tuples (only works for 0-dimensional tuples)
110
# i.e. it won't work with ((23,2)) or [(23,2)]
111
title = "Rank-1 case 3"
112
numericalList = (3, 4, 5.2)
113
charList = ("aaa", "bbb")
115
class Basic2DTestCase(BasicTestCase):
117
title = "Rank-2 case 1"
118
numericalList = [[1,2]]*5
119
charList = [["qq","zz"]]*5
121
class Basic10DTestCase(BasicTestCase):
123
title = "Rank-10 case 1"
124
numericalList = [[[[[[[[[[1,2],[3,4]]]]]]]]]]*5
125
# Dimensions greather than 6 in strings gives some warnings
126
charList = [[[[[[[[[["a","b"],["qq","zz"]]]]]]]]]]*5
129
class ExceptionTestCase(unittest.TestCase):
131
def test00_char(self):
132
"Non suppported lists objects (character objects)"
135
print '\n', '-=' * 30
136
print "Running test for %s" % \
143
(type, value, traceback) = sys.exc_info()
144
print "\nGreat!, the next error was catched!"
145
print type, ":", value
147
self.fail("expected an ValueError")
152
def test01_types(self):
153
"Non supported lists object (numerical types)"
155
a = self.numericalList
161
# Then, delete the file
164
(type, value, traceback) = sys.exc_info()
165
print "\nGreat!, the next ValueError was catched!"
168
self.fail("expected an ValueError")
173
class Basic1DFourTestCase(ExceptionTestCase):
174
# numeric is still not able to detect that
175
# Activate when numarray support this kind of detection
176
title = "Rank-1 case 4 (non-regular list)"
177
numericalList = [3, [4, 5.2]]
178
charList = ["aaa", ["bbb", "ccc"]]
181
class GetItemTestCase(unittest.TestCase):
183
def test00_single(self):
184
"Single element access (character types)"
186
file = tempfile.mktemp(".h5")
187
fileh = openFile(file, mode = "w")
188
# Create the array under root and name 'somearray'
190
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
192
# Get and compare an element
194
print "Original first element:", a[0]
195
print "Read first element:", arr[0]
196
assert a[0] == arr[0]
200
# Then, delete the file
204
def test01_single(self):
205
"Single element access (numerical types)"
207
file = tempfile.mktemp(".h5")
208
fileh = openFile(file, mode = "w")
209
# Create the array under root and name 'somearray'
210
a = self.numericalList
211
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
213
# Get and compare an element
215
print "Original first element:", a[0]
216
print "Read first element:", arr[0]
217
assert a[0] == arr[0]
221
# Then, delete the file
225
def test02_range(self):
226
"Range element access (character types)"
228
file = tempfile.mktemp(".h5")
229
fileh = openFile(file, mode = "w")
230
# Create the array under root and name 'somearray'
232
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
234
# Get and compare an element
236
print "Original elements:", a[1:4]
237
print "Read elements:", arr[1:4]
238
assert a[1:4] == arr[1:4]
242
# Then, delete the file
246
def test03_range(self):
247
"Range element access (numerical types)"
249
file = tempfile.mktemp(".h5")
250
fileh = openFile(file, mode = "w")
251
# Create the array under root and name 'somearray'
252
a = self.numericalListME
253
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
255
# Get and compare an element
257
print "Original elements:", a[1:4]
258
print "Read elements:", arr[1:4]
259
assert a[1:4] == arr[1:4]
263
# Then, delete the file
267
def test04_range(self):
268
"Range element access, strided (character types)"
271
file = tempfile.mktemp(".h5")
272
fileh = openFile(file, mode = "w")
273
# Create the array under root and name 'somearray'
275
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
277
# Extended slicing not supported in Python 2.2
279
# Get and compare an element
281
print "Original elements:", a[1:4:2]
282
print "Read elements:", arr[1:4:2]
283
assert a[1:4:2] == arr[1:4:2]
287
# Then, delete the file
291
def test05_range(self):
292
"Range element access (numerical types)"
295
file = tempfile.mktemp(".h5")
296
fileh = openFile(file, mode = "w")
297
# Create the array under root and name 'somearray'
298
a = self.numericalListME
299
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
301
# Extended slicing not supported in Python 2.2
303
# Get and compare an element
305
print "Original elements:", a[1:4:2]
306
print "Read elements:", arr[1:4:2]
307
assert a[1:4:2] == arr[1:4:2]
311
# Then, delete the file
315
def test06_negativeIndex(self):
316
"Negative Index element access (character types)"
318
file = tempfile.mktemp(".h5")
319
fileh = openFile(file, mode = "w")
320
# Create the array under root and name 'somearray'
322
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
324
# Get and compare an element
326
print "Original last element:", a[-1]
327
print "Read last element:", arr[-1]
328
assert a[-1] == arr[-1]
332
# Then, delete the file
336
def test07_negativeIndex(self):
337
"Negative Index element access (numerical types)"
339
file = tempfile.mktemp(".h5")
340
fileh = openFile(file, mode = "w")
341
# Create the array under root and name 'somearray'
342
a = self.numericalListME
343
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
345
# Get and compare an element
347
print "Original before last element:", a[-2]
348
print "Read before last element:", arr[-2]
349
assert a[-2] == arr[-2]
353
# Then, delete the file
357
def test08_negativeRange(self):
358
"Negative range element access (character types)"
360
file = tempfile.mktemp(".h5")
361
fileh = openFile(file, mode = "w")
362
# Create the array under root and name 'somearray'
364
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
366
# Get and compare an element
368
print "Original last elements:", a[-4:-1]
369
print "Read last elements:", arr[-4:-1]
370
assert a[-4:-1] == arr[-4:-1]
374
# Then, delete the file
378
def test09_negativeRange(self):
379
"Negative range element access (numerical types)"
381
file = tempfile.mktemp(".h5")
382
fileh = openFile(file, mode = "w")
383
# Create the array under root and name 'somearray'
384
a = self.numericalListME
385
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
387
# Get and compare an element
389
print "Original last elements:", a[-4:-1]
390
print "Read last elements:", arr[-4:-1]
391
assert a[-4:-1] == arr[-4:-1]
395
# Then, delete the file
399
class GI1ListTestCase(GetItemTestCase):
400
title = "Rank-1 case 1 (lists)"
402
numericalListME = [3,2,1,0,4,5,6]
404
charListME = ["321","221","121","021","421","521","621"]
406
class GI1TupleTestCase(GetItemTestCase):
407
title = "Rank-1 case 1 (tuples)"
409
numericalListME = (3,2,1,0,4,5,6)
411
charListME = ("321","221","121","021","421","521","621")
413
class GI2ListTestCase(GetItemTestCase):
414
# A more complex example
415
title = "Rank-1,2 case 2 (lists)"
416
numericalList = [3,4]
417
numericalListME = [[3,2,1,0,4,5,6],
424
charListME = [["321","221","121","021","421","521","621"],
425
["21","21","11","02","42","21","61"],
426
["31","21","12","21","41","51","621"],
427
["321","221","121","021","421","521","621"],
428
["3241","2321","13216","0621","4421","5421","a621"],
429
["a321","s221","d121","g021","b421","5vvv21","6zxzxs21"]]
431
class GI2TupleTestCase(GetItemTestCase):
432
# A more complex example
433
title = "Rank-1,2 case 2 (tuples)"
434
numericalList = (3,4)
435
numericalListME = ((3,2,1,0,4,5,6),
442
charListME = (("321","221","121","021","421","521","621"),
443
("21","21","11","02","42","21","61"),
444
("31","21","12","21","41","51","621"),
445
("321","221","121","021","421","521","621"),
446
("3241","2321","13216","0621","4421","5421","a621"),
447
("a321","s221","d121","g021","b421","5vvv21","6zxzxs21"))
450
class GeneratorTestCase(unittest.TestCase):
452
def test00a_single(self):
453
"Testing generator access to Arrays, single elements (char)"
455
file = tempfile.mktemp(".h5")
456
fileh = openFile(file, mode = "w")
457
# Create the array under root and name 'somearray'
459
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
461
# Get and compare an element
463
garr = [i for i in arr]
465
print "Result of original iterator:", ga
466
print "Result of read generator:", garr
471
# Then, delete the file
475
def test00b_me(self):
476
"Testing generator access to Arrays, multiple elements (char)"
478
file = tempfile.mktemp(".h5")
479
fileh = openFile(file, mode = "w")
480
# Create the array under root and name 'somearray'
482
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
484
# Get and compare an element
486
garr = [i for i in arr]
488
print "Result of original iterator:", ga
489
print "Result of read generator:", garr
494
# Then, delete the file
498
def test01a_single(self):
499
"Testing generator access to Arrays, single elements (numeric)"
501
file = tempfile.mktemp(".h5")
502
fileh = openFile(file, mode = "w")
503
# Create the array under root and name 'somearray'
504
a = self.numericalList
505
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
507
# Get and compare an element
509
garr = [i for i in arr]
511
print "Result of original iterator:", ga
512
print "Result of read generator:", garr
517
# Then, delete the file
521
def test01b_me(self):
522
"Testing generator access to Arrays, multiple elements (numeric)"
524
file = tempfile.mktemp(".h5")
525
fileh = openFile(file, mode = "w")
526
# Create the array under root and name 'somearray'
527
a = self.numericalListME
528
arr=fileh.createArray(fileh.root, 'somearray', a, "Some array")
530
# Get and compare an element
532
garr = [i for i in arr]
534
print "Result of original iterator:", ga
535
print "Result of read generator:", garr
540
# Then, delete the file
544
class GE1ListTestCase(GeneratorTestCase):
546
title = "Rank-1 case 1 (lists)"
548
numericalListME = [3,2,1,0,4,5,6]
550
charListME = ["321","221","121","021","421","521","621"]
552
class GE1TupleTestCase(GeneratorTestCase):
554
title = "Rank-1 case 1 (tuples)"
556
numericalListME = (3,2,1,0,4,5,6)
558
charListME = ("321","221","121","021","421","521","621")
560
class GE2ListTestCase(GeneratorTestCase):
562
title = "Rank-1,2 case 2 (lists)"
563
numericalList = [3,4]
564
numericalListME = [[3,2,1,0,4,5,6],
571
charListME = [["321","221","121","021","421","521","621"],
572
["21","21","11","02","42","21","61"],
573
["31","21","12","21","41","51","621"],
574
["321","221","121","021","421","521","621"],
575
["3241","2321","13216","0621","4421","5421","a621"],
576
["a321","s221","d121","g021","b421","5vvv21","6zxzxs21"]]
578
class GE2TupleTestCase(GeneratorTestCase):
580
title = "Rank-1,2 case 2 (tuples)"
581
numericalList = (3,4)
582
numericalListME = ((3,2,1,0,4,5,6),
589
charListME = (("321","221","121","021","421","521","621"),
590
("21","21","11","02","42","21","61"),
591
("31","21","12","21","41","51","621"),
592
("321","221","121","021","421","521","621"),
593
("3241","2321","13216","0621","4421","5421","a621"),
594
("a321","s221","d121","g021","b421","5vvv21","6zxzxs21"))
598
theSuite = unittest.TestSuite()
601
#theSuite.addTest(unittest.makeSuite(Basic1DFourTestCase))
602
for i in range(niter):
603
# The scalar case test should be refined in order to work
604
theSuite.addTest(unittest.makeSuite(Basic0DOneTestCase))
605
theSuite.addTest(unittest.makeSuite(Basic0DTwoTestCase))
606
#theSuite.addTest(unittest.makeSuite(Basic1DZeroTestCase))
607
theSuite.addTest(unittest.makeSuite(Basic1DOneTestCase))
608
theSuite.addTest(unittest.makeSuite(Basic1DTwoTestCase))
609
theSuite.addTest(unittest.makeSuite(Basic1DThreeTestCase))
610
theSuite.addTest(unittest.makeSuite(Basic1DFourTestCase))
611
theSuite.addTest(unittest.makeSuite(Basic2DTestCase))
612
theSuite.addTest(unittest.makeSuite(Basic10DTestCase))
613
theSuite.addTest(unittest.makeSuite(GI1ListTestCase))
614
theSuite.addTest(unittest.makeSuite(GI1TupleTestCase))
615
theSuite.addTest(unittest.makeSuite(GI2ListTestCase))
616
theSuite.addTest(unittest.makeSuite(GI2TupleTestCase))
617
theSuite.addTest(unittest.makeSuite(GE1ListTestCase))
618
theSuite.addTest(unittest.makeSuite(GE1TupleTestCase))
619
theSuite.addTest(unittest.makeSuite(GE2ListTestCase))
620
theSuite.addTest(unittest.makeSuite(GE2TupleTestCase))
625
if __name__ == '__main__':
626
unittest.main( defaultTest='suite' )