hare

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

stream_test+test.ha (3272B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use io;
      6 use memio;
      7 use strings;
      8 
      9 @test fn read() void = {
     10 	let sourcebuf: []u8 = [1, 3, 3, 7];
     11 	let source = memio::fixed(sourcebuf);
     12 	defer io::close(&source)!;
     13 
     14 	let rbuf: [1024]u8 = [0...];
     15 	let f = init(&source, rbuf, []);
     16 	defer io::close(&f)!;
     17 
     18 	let buf: [1024]u8 = [0...];
     19 	assert(io::read(&f, buf[..2]) as size == 2);
     20 	assert(source.pos == len(source.buf), "fixed stream was not fully consumed");
     21 	assert(bytes::equal(buf[..2], [1, 3]));
     22 
     23 	assert(io::read(&f, buf[2..]) as size == 2);
     24 	assert(bytes::equal(buf[..4], [1, 3, 3, 7]));
     25 	assert(io::read(&f, buf) is io::EOF);
     26 
     27 	let sourcebuf: [32]u8 = [1, 3, 3, 7, 0...];
     28 	let source = memio::fixed(sourcebuf);
     29 
     30 	let rbuf: [16]u8 = [0...];
     31 	let f = init(&source, rbuf, []);
     32 	defer io::close(&f)!;
     33 
     34 	let buf: [32]u8 = [0...];
     35 	assert(io::read(&f, buf) as size == 16);
     36 	assert(source.pos == 16);
     37 
     38 	assert(io::read(&f, buf[16..]) as size == 16);
     39 	assert(bytes::equal(buf, sourcebuf));
     40 	assert(io::read(&f, buf) is io::EOF);
     41 	assert(source.pos == len(source.buf));
     42 };
     43 
     44 @test fn write() void = {
     45 	// Normal case
     46 	let sink = memio::dynamic();
     47 	defer io::close(&sink)!;
     48 
     49 	let wbuf: [1024]u8 = [0...];
     50 	let f = init(&sink, [], wbuf);
     51 	defer io::close(&f)!;
     52 
     53 	assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4);
     54 	assert(len(memio::buffer(&sink)) == 0);
     55 	assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4);
     56 	assert(flush(&f) is void);
     57 	assert(bytes::equal(memio::buffer(&sink), [1, 3, 3, 7, 1, 3, 3, 7]));
     58 
     59 	// Test flushing via buffer exhaustion
     60 	let sink = memio::dynamic();
     61 	defer io::close(&sink)!;
     62 
     63 	let wbuf: [4]u8 = [0...];
     64 	let f = init(&sink, [], wbuf);
     65 
     66 	assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4);
     67 	assert(len(memio::buffer(&sink)) == 0);
     68 	assert(io::writeall(&f, [1, 3, 3, 7]) as size == 4);
     69 	assert(bytes::equal(memio::buffer(&sink), [1, 3, 3, 7]));
     70 	io::close(&f)!; // Should flush
     71 	assert(bytes::equal(memio::buffer(&sink), [1, 3, 3, 7, 1, 3, 3, 7]));
     72 
     73 	// Test flushing via flush characters
     74 	let sink = memio::dynamic();
     75 	defer io::close(&sink)!;
     76 
     77 	let wbuf: [1024]u8 = [0...];
     78 	let f = init(&sink, [], wbuf);
     79 
     80 	assert(io::writeall(&f, strings::toutf8("hello")) as size == 5);
     81 	assert(len(memio::buffer(&sink)) == 0);
     82 	assert(io::writeall(&f, strings::toutf8(" world!\n")) as size == 8);
     83 	assert(bytes::equal(memio::buffer(&sink), strings::toutf8("hello world!\n")));
     84 };
     85 
     86 @test fn unread() void = {
     87 	let rbuf: [8]u8 = [0...];
     88 	let f = init(io::zero, rbuf, []);
     89 
     90 	let buf: [16]u8 = [42...];
     91 	assert(io::read(&f, buf[..4]) as size == 4);
     92 	assert(buf[0] == 0);
     93 	assert(buf[1] == 0);
     94 	assert(buf[2] == 0);
     95 	assert(buf[3] == 0);
     96 	unread(&f, [1, 2, 3, 4]);
     97 
     98 	assert(io::read(&f, buf[..8]) as size == 8);
     99 	assert(buf[0] == 1);
    100 	assert(buf[1] == 2);
    101 	assert(buf[2] == 3);
    102 	assert(buf[3] == 4);
    103 	assert(buf[4] == 0);
    104 	assert(buf[5] == 0);
    105 	assert(buf[6] == 0);
    106 	assert(buf[7] == 0);
    107 
    108 	assert(io::read(&f, buf) as size == 8);
    109 	for (let i = 0z; i < 8; i += 1) {
    110 		assert(buf[i] == 0);
    111 	};
    112 
    113 	let input: []u8 = [1, 2, 3, 4];
    114 	let f = init(&memio::fixed(input), rbuf, []);
    115 
    116 	assert(io::read(&f, buf) as size == 4);
    117 	unread(&f, [1, 2, 3, 4]);
    118 	assert(io::read(&f, buf) as size == 4);
    119 	assert(io::read(&f, buf) is io::EOF);
    120 };