1#!/usr/bin/env python
2import os
3os.environ['PATH'] = ':'.join(['.', os.environ['PATH']])
4
5def CompareFiles(a, b):
6    'Compare files using Universal Newline Support for portability'
7    return open(a, 'rU').readlines() != open(b, 'rU').readlines()
8
9tests = ['wtest',
10         ('rtest', 'rtest.dat', 'rtestok.dat'),
11         ('ftest', 'ftest.dat', 'ftestok.dat'),
12         'halftest']
13
14failed = 0
15for test in tests:
16    if type(test) is tuple:
17        cmd, output, ref = test
18        cmd = cmd + ' > ' + output
19    else:
20        cmd = test
21        output = ref = None
22    print 'Running:', cmd
23    status = os.system(cmd)
24    if status == 0 and output and ref:
25        print 'Comparing:', output, ref
26        status = CompareFiles(output, ref)
27    if status != 0:
28        print 'FAILED'
29        failed += 1
30    else:
31        print 'Passed'
32    print
33
34print 'Finished', len(tests), 'tests,'
35if failed == 0:
36    print 'All tests passed'
37else:
38    print failed, 'tests FAILED'
39    exit(1)
40