File: test_files3.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (92 lines) | stat: -rw-r--r-- 2,271 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

import os
from microbit import display, Image

#We don't have space for Beowolf, so here's the next best thing...
text = """
’Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe:
All mimsy were the borogoves,
  And the mome raths outgrabe.

“Beware the Jabberwock, my son!
  The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
 The frumious Bandersnatch!”

He took his vorpal sword in hand;
  Long time the manxome foe he sought—
So rested he by the Tumtum tree
  And stood awhile in thought.

And, as in uffish thought he stood,
  The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
  And burbled as it came!

One, two! One, two! And through and through
  The vorpal blade went snicker-snack!
He left it dead, and with its head
  He went galumphing back.

“And hast thou slain the Jabberwock?
  Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!”
  He chortled in his joy.

’Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe:
All mimsy were the borogoves,
  And the mome raths outgrabe.
"""

def test_read_while_writing():
    j1 = open("jabbawocky.txt", "w")
    j1.write(text)
    j2 = open("jabbawocky.txt")
    short = j2.read()
    assert text.startswith(short)
    del j2
    j1.close()
    j2 = open("jabbawocky.txt")
    assert j2.read() == text
    j2.close()
    os.remove("jabbawocky.txt")

def test_removing_mid_read():
    with open("jabbawocky.txt", "w") as j:
        j.write(text)
    j = open("jabbawocky.txt")
    os.remove("jabbawocky.txt")
    try:
        j.read()
        assert False, "Shouldn't reach here"
    except OSError:
        pass

def test_removing_mid_write():
    j = open("jabbawocky.txt", "w")
    os.remove("jabbawocky.txt")
    try:
        j.write(text)
        assert False, "Shouldn't reach here"
    except OSError:
        pass

def test_repeated_write():
    for i in range(40):
        with open("jabbawocky.txt", "w") as j:
            j.write(text)

display.clear()
try:
    test_read_while_writing()
    test_removing_mid_read()
    test_removing_mid_write()
    test_repeated_write()
    print("File test: PASS")
    display.show(Image.HAPPY)
except Exception as ae:
    display.show(Image.SAD)
    raise