hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 4786fb916a404d672f1617988c8b57d16e8b28c8
parent 405e5dcb9592234cf0e987458de4128bc0a99c16
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date:   Sun,  7 Feb 2021 00:54:16 +0100

io: separate general purpose testing stream and copy test

Diffstat:
Aio/+test/copy.ha | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aio/+test/stream.ha | 34++++++++++++++++++++++++++++++++++
Dio/copy+test.ha | 78------------------------------------------------------------------------------
3 files changed, 104 insertions(+), 78 deletions(-)

diff --git a/io/+test/copy.ha b/io/+test/copy.ha @@ -0,0 +1,70 @@ +fn test_copier_read(s: *stream, buf: []u8) (size | EOF | error) = { + let stream = s: *test_stream; + if (stream.n == 0z) { + assert(len(buf) > 42z); + stream.n = 42z; + return 42z; + } else { + return EOF; + }; +}; + +fn test_copier_open() test_stream = { + let stream = test_stream_open(); + stream.stream.reader = &test_copier_read; + return stream; +}; + +fn test_copier_copy(a: *stream, b: *stream) (size | error) = { + assert(a != b); + assert(a.reader == &test_copier_read && b.reader == &test_copier_read); + let stream = a: *test_stream; + stream.n = 62893z; + return 1337z; +}; + +fn test_copy_unsupported(a: *stream, b: *stream) (size | error) = unsupported; + +@test fn copy() void = { + let a = test_copier_open(), b = test_copier_open(); + match (copy(&b.stream, &a.stream)) { + n: size => { + assert(n == 42z); + assert(a.n == 42z); + assert(b.n == 42z); + }, + error => abort(), + }; + close(&a: *stream); + close(&b: *stream); + + a = test_copier_open(); + b = test_copier_open(); + a.stream.copier = &test_copier_copy; + b.stream.copier = &test_copier_copy; + match (copy(&b.stream, &a.stream)) { + n: size => { + assert(n == 1337z); + assert(b.n == 62893z); + }, + error => abort(), + }; + close(&a: *stream); + close(&b: *stream); + + // Fallback + a = test_copier_open(); + b = test_copier_open(); + a.stream.copier = &test_copy_unsupported; + b.stream.copier = &test_copy_unsupported; + match (copy(&b.stream, &a.stream)) { + n: size => { + assert(n == 42z); + assert(a.n == 42z); + assert(b.n == 42z); + }, + error => abort(), + }; + close(&a: *stream); + close(&b: *stream); +}; diff --git a/io/+test/stream.ha b/io/+test/stream.ha @@ -0,0 +1,34 @@ +use strings; + +type test_stream = struct { + stream: stream, + n: size, +}; + +fn test_stream_read(s: *stream, buf: []u8) (size | EOF | error) = { + let stream = s: *test_stream; + stream.n = len(buf); + return len(buf); +}; + +fn test_stream_write(s: *stream, buf: const []u8) (size | error) = { + let stream = s: *test_stream; + stream.n = len(buf); + return len(buf); +}; + +fn test_stream_open() test_stream = test_stream { + stream = stream { + name = strings::dup("test_stream"), + reader = &test_stream_read, + writer = &test_stream_write, + closer = &test_stream_close, + ... + }, + n = 0z, +}; + +fn test_stream_close(s: *stream) void = { + let stream = s: *test_stream; + free(stream.stream.name); +}; diff --git a/io/copy+test.ha b/io/copy+test.ha @@ -1,78 +0,0 @@ -type test_copier = struct { - stream: stream, - n: size, -}; - -fn test_copier_read(s: *stream, buf: []u8) (size | EOF | error) = { - let stream = s: *test_copier; - if (stream.n == 0z) { - assert(len(buf) > 42z); - stream.n = 42z; - return 42z; - } else { - return EOF; - }; -}; - -fn test_copier_write(s: *stream, buf: const []u8) (size | error) = { - let stream = s: *test_copier; - stream.n = len(buf); - return len(buf); -}; - -fn test_copier_open() test_copier = test_copier { - stream = stream { - reader = &test_copier_read, - writer = &test_copier_write, - ... - }, - ... -}; - -fn test_copier_copy(a: *stream, b: *stream) (size | error) = { - assert(a != b); - assert(a.reader == &test_copier_read && b.reader == &test_copier_read); - let stream = a: *test_copier; - stream.n = 62893z; - return 1337z; -}; - -fn test_copy_unsupported(a: *stream, b: *stream) (size | error) = unsupported; - -@test fn copy() void = { - let a = test_copier_open(), b = test_copier_open(); - match (copy(&b.stream, &a.stream)) { - n: size => { - assert(n == 42z); - assert(a.n == 42z); - assert(b.n == 42z); - }, - error => abort(), - }; - - a = test_copier_open(); - b = test_copier_open(); - a.stream.copier = &test_copier_copy; - b.stream.copier = &test_copier_copy; - match (copy(&b.stream, &a.stream)) { - n: size => { - assert(n == 1337z); - assert(b.n == 62893z); - }, - error => abort(), - }; - - // Fallback - a = test_copier_open(); - b = test_copier_open(); - a.stream.copier = &test_copy_unsupported; - b.stream.copier = &test_copy_unsupported; - match (copy(&b.stream, &a.stream)) { - n: size => { - assert(n == 42z); - assert(a.n == 42z); - assert(b.n == 42z); - }, - error => abort(), - }; -};