added file operations and additional tests

This commit is contained in:
Add00
2023-08-12 16:02:54 -04:00
parent 38ba2273dd
commit 0c005d0af2
2 changed files with 215 additions and 45 deletions

View File

@@ -1,5 +1,7 @@
import fileio;
var PATH: string const = "scripts:/lib/file/fileio.txt";
// test global constants
{
assert MAX_FILENAME_SIZE > 0, "MAX_FILENAME_SIZE failed";
@@ -9,7 +11,7 @@ import fileio;
// test open and close
{
var file = open("scripts:/lib/file/fileio.txt", "r");
var file = open(PATH, "r");
assert file != null, "open failed";
var wrong = open("scripts:/doesNotExist", "r");
@@ -18,38 +20,54 @@ import fileio;
assert file.close() == true, "close failed";
}
// test accessors
// test append
{
var file = open("scripts:/lib/file/fileio.txt", "r");
print file.error();
print file.completed();
print file.position();
print file.size();
print file.mode();
var file = open(PATH, "a");
assert file.write("appended text") == true, "append failed";
file.close();
}
// test output
// test accessors
{
// assert output.write(8), "write int failed";
// assert output.write("\n"), "write string failed";
// assert output.write(12.5), "write float failed";
var file = open(PATH, "r");
assert file.error() == false, "error failed";
assert file.completed() == false, "completed failed";
assert file.position() == 0, "position failed";
assert file.size() >= 13, "size failed";
assert file.mode() == "r", "mode failed";
file.read(string);
assert file.error() == false, "error failed";
assert file.completed() == true, "completed failed";
assert file.position() >= 13, "position failed";
assert file.size() >= 13, "size failed";
// assert file.mode() == "r", "mode failed";
file.close();
}
// test input
// test write
{
// // enter 8
// assert input.read(int) == 8, "read int failed";
// // enter 12.5
// assert input.read(float) == 12.5, "read float failed";
// // enter test
// assert input.read(string) == "test\n", "read string failed";
// // invaild types
// assert input.read(type) == null, "type failed";
// assert input.read(any) == null, "any failed";
assert output.write(8), "write int failed";
assert output.write("\n"), "write string failed";
assert output.write(12.5), "write float failed";
}
// test read
{
// enter 8
assert input.read(int) == 8, "read int failed";
// enter 12.5
assert input.read(float) == 12.5, "read float failed";
// enter test
assert input.read(string) == "test\n", "read string failed";
// invaild types
assert input.read(type) == null, "type failed";
assert input.read(any) == null, "any failed";
}