~azzar1/unity/add-show-desktop-key

270 by dilshan_a
Initial check in for test framework, and example problems
1
def print_file(name, mode='r'):
2
    f = file(name, mode)
3
    print f.read()
4
    f.close()
5
    print
6
7
# read inital file
8
print "initial_file.txt: read in r mode"
9
print_file("initial_file.txt", "r")
10
11
print "initial_file.txt: read in r mode again"
12
print_file("initial_file.txt", "r")
13
14
print "initial_file.txt: read in r+ mode"
15
print_file("initial_file.txt", "r+")
16
17
print "initial_file.txt: read in a+ mode"
18
print_file("initial_file.txt", "a+")
19
20
21
print "initial_file.txt: read in w+ mode"
22
print_file("initial_file.txt", "w+")
23
                               
24
# simple write tests
25
f = file("file_1.txt", "w")
26
f.write("Write a new file, mode = 'w'\n")
27
f.close()
28
29
print "file_1.txt: write test"
30
print_file("file_1.txt")
31
32
f = file("file_2.txt", "w")
33
f.write("Write to file multiple times\n")
34
f.write("mode = 'w'\n")
35
f.close()
36
37
# read tests
38
print "file_2.txt: read in r mode"
39
print_file("file_2.txt", "r")
40
41
print "file_2.txt: read in r+ mode"
42
print_file("file_2.txt", "r+")
43
44
print "file_2.txt: read in a+ mode"
45
print_file("file_2.txt", "a+")
46
47
print "file_2.txt: read in w+ mode"
48
print_file("file_2.txt", "w+")
49
50
# append tests
51
f = file("file_3.txt", "w")
52
f.write("Text in w mode\n")
53
f.close()
54
f = file("file_3.txt", "a")
55
f.write("Text appended in\n")
56
f.write("a mode\n")
57
f.close()
58
59
print "file_3.txt: append test"
60
print_file("file_3.txt")
61
62
# rwa + modes tests here
63
64
65
# error tests
66
f = file("file_4.txt", 'w')
67
f.write("File for testing errors\n")
68
f.close()
69
70
print "Writing to read only files"
278 by dilshan_a
Added README files to explain the test framework and the examples.
71
f = file("file_4.txt")
270 by dilshan_a
Initial check in for test framework, and example problems
72
try:
278 by dilshan_a
Added README files to explain the test framework and the examples.
73
    f.write("a")
270 by dilshan_a
Initial check in for test framework, and example problems
74
    print "Test failed"
75
except Exception, e:
76
    print e
278 by dilshan_a
Added README files to explain the test framework and the examples.
77
f.close()
270 by dilshan_a
Initial check in for test framework, and example problems
78
79
print
80
print "Reading non-existant files"
81
try:
82
    file("non_existant.txt").read()
83
    print "Test failed"
84
except Exception, e:
85
    print e
86
87
print
88
print "Reading form write only files"
278 by dilshan_a
Added README files to explain the test framework and the examples.
89
f = file("file_4.txt",'w')
270 by dilshan_a
Initial check in for test framework, and example problems
90
try:
278 by dilshan_a
Added README files to explain the test framework and the examples.
91
    f.read()
270 by dilshan_a
Initial check in for test framework, and example problems
92
    print "Test failed"
93
except Exception, e:
94
    print e
278 by dilshan_a
Added README files to explain the test framework and the examples.
95
f.close()
270 by dilshan_a
Initial check in for test framework, and example problems
96
97
print
98
print "Reading form append only files"
278 by dilshan_a
Added README files to explain the test framework and the examples.
99
f = file("file_4.txt",'a')
270 by dilshan_a
Initial check in for test framework, and example problems
100
try:
278 by dilshan_a
Added README files to explain the test framework and the examples.
101
    f.read()
270 by dilshan_a
Initial check in for test framework, and example problems
102
    print "Test failed"
103
except Exception, e:
104
    print e
278 by dilshan_a
Added README files to explain the test framework and the examples.
105
f.close()