From 9433a1ad65b4d79f6aa61909ce4d02f5e233ff2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sun, 22 Jan 2017 03:06:52 +0100 Subject: [PATCH] Add basic file operation test. --- tests/0-file.d | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/0-file.d diff --git a/tests/0-file.d b/tests/0-file.d new file mode 100644 index 0000000..b533d8e --- /dev/null +++ b/tests/0-file.d @@ -0,0 +1,63 @@ +/++ dub.sdl: + name "test" + dependency "eventcore" path=".." + debugVersions "EventCoreLogFiles" ++/ +module test; + +import eventcore.core; +import eventcore.socket; +import std.file : remove; +import std.socket : InternetAddress; +import core.time : Duration, msecs; + +bool s_done; + +void main() +{ + auto f = eventDriver.files.open("test.txt", FileOpenMode.createTrunc); + assert(eventDriver.files.getSize(f) == 0); + auto data = cast(const(ubyte)[])"Hello, World!"; + auto tm = eventDriver.timers.create(); + eventDriver.timers.set(tm, 500.msecs, 0.msecs); + eventDriver.timers.wait(tm, (tm) { + assert(false, "File operation stalled."); + }); + + eventDriver.files.write(f, 0, data[0 .. 7], IOMode.all, (f, status, nbytes) { + assert(status == IOStatus.ok); + assert(nbytes == 7); + assert(eventDriver.files.getSize(f) == 7); + eventDriver.files.write(f, 5, data[5 .. $], IOMode.all, (f, status, nbytes) { + assert(status == IOStatus.ok); + assert(nbytes == data.length - 5); + assert(eventDriver.files.getSize(f) == data.length); + auto dst = new ubyte[data.length]; + eventDriver.files.read(f, 0, dst[0 .. 7], IOMode.all, (f, status, nbytes) { + assert(status == IOStatus.ok); + assert(nbytes == 7); + assert(dst[0 .. 7] == data[0 .. 7]); + eventDriver.files.read(f, 5, dst[5 .. $], IOMode.all, (f, status, nbytes) { + assert(status == IOStatus.ok); + assert(nbytes == data.length - 5); + assert(dst == data); + eventDriver.files.close(f); + () @trusted { + scope (failure) assert(false); + remove("test.txt"); + } (); + eventDriver.files.releaseRef(f); + s_done = true; + eventDriver.core.exit(); + }); + }); + }); + }); + + ExitReason er; + do er = eventDriver.core.processEvents(Duration.max); + while (er == ExitReason.idle); + //assert(er == ExitReason.outOfWaiters); // FIXME: see above + assert(s_done); + s_done = false; +}