commit 5e628befce69407234939459bc14b1fe3c26d12d
parent ee3d3f9016d14d7ccf4e42415919636cc54389f6
Author: Drew DeVault <sir@cmpwn.com>
Date: Thu, 4 Feb 2021 17:06:23 -0500
io::copy: add test
Diffstat:
A | io/copy+test.ha | | | 78 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 78 insertions(+), 0 deletions(-)
diff --git a/io/copy+test.ha b/io/copy+test.ha
@@ -0,0 +1,78 @@
+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(),
+ };
+};