File: test_file.py

package info (click to toggle)
gensio 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,364 kB
  • sloc: ansic: 90,328; python: 5,220; sh: 4,935; cpp: 3,258; makefile: 1,418
file content (88 lines) | stat: -rw-r--r-- 1,992 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
#
#  gensio - A library for abstracting stream I/O
#  Copyright (C) 2018  Corey Minyard <minyard@acm.org>
#
#  SPDX-License-Identifier: GPL-2.0-only
#

import utils
import gensio
import os

test1 = "asdfasdf"
test2 = "jkl;jkl;"
test3 = "01234;"

print("Test file")

testfile = "asdffile"

try:
    os.remove(testfile)
except:
    pass

print(" Testing no create")
g = gensio.gensio(utils.o, "file(outfile=" + testfile + ")", None)
g.set_sync()
failed = False
try:
    g.open_s()
except Exception as e:
    if not str(e).endswith("Value or file not found"):
        raise Exception("Unknown exception opening file: " + str(e))
    failed = True
if not failed:
    raise Exception("Open didn't fail")
del g

def writefile(f, data, options = ""):
    g = gensio.gensio(utils.o, "file(outfile=%s%s)" % (f, options), None)
    g.set_sync()
    g.open_s()
    g.write_s(data, 1000)
    g.close_s()
    del g

def readfile(f, options = ""):
    g = gensio.gensio(utils.o, "file(infile=%s%s)" % (f, options), None)
    g.set_sync()
    g.open_s()
    s = g.read_s(100, 1000)
    g.close_s()
    del g
    s = s[0].decode('utf-8')
    return s
    
print(" Testing file data")
writefile(testfile, test1, ",create")
s = readfile(testfile)
if s != test1:
    raise Exception("file data didn't match, expected %s, got %s" % (
        test1, s))

print(" Testing append")
writefile(testfile, test2, ",append")
s = readfile(testfile)
if s != test1 + test2:
    raise Exception("file data didn't match, expected %s, got %s" % (
        test1 + test2, s))

print(" Testing overwrite")
writefile(testfile, test2)
s = readfile(testfile)
if s != test2 + test2:
    raise Exception("file data didn't match, expected %s, got %s" % (
        test2 + test2, s))

print(" Testing trunc")
writefile(testfile, test3, ",trunc")
s = readfile(testfile)
if s != test3:
    raise Exception("file data didn't match, expected %s, got %s" % (
        test3, s))

os.remove(testfile)

utils.test_shutdown()
print("  Success!")