~sircmpwn/hare-compress

9b47d8a113b2262c7527e19f9403fd7cb6451253 — Sertonix 7 months ago 6c7fe22
compress::*: remove test dependencies from non tests
A compress/flate/+test.ha => compress/flate/+test.ha +72 -0
@@ 0,0 1,72 @@
// License: MPL-2.0
// (c) 2022 Alexey Yerin <yyp@disroot.org>
// (c) 2021 Bor Grošelj Simić <bor.groseljsimic@telemach.net>
// (c) 2021 Drew DeVault <sir@cmpwn.com>
// (c) 2021 Ember Sawady <ecs@d2evs.net>
use bytes;
use fmt;
use io;
use memio;

@init fn init_fixed() void = {
	// See section 3.2.6 of RFC 1951
	let lens: [288]u16 = [0...];
	let i = 0z;
	for (i < 144; i += 1) {
		lens[i] = 8;
	};
	for (i < 256; i += 1) {
		lens[i] = 9;
	};
	for (i < 280; i += 1) {
		lens[i] = 7;
	};
	for (i < 288; i += 1) {
		lens[i] = 8;
	};
	construct(&fixed_len, lens[..]) as void;

	let dists: [30]u16 = [5...];
	construct(&fixed_dist, dists[..]) as void;
};

// Further testing is done in compress::zlib
@test fn inflate() void = {
	const in: []u8 = [
		// Non-final block, uncompressed
		0b00000000,
		// 4 bytes long
		0b00000100,
		0b00000000,
		// ~LEN
		0b11111011,
		0b11111111,
		// Data
		0xDE, 0xAD, 0xBE, 0xEF,
		// Final block, uncompressed
		0b00000001,
		// 2 bytes long
		0b00000010,
		0b00000000,
		// ~LEN
		0b11111101,
		0b11111111,
		// Data
		0x42, 0x69,
	];
	let expected: []u8 = [0xDE, 0xAD, 0xBE, 0xEF, 0x42, 0x69];

	let ins = memio::fixed(in);
	let outs = memio::dynamic();
	let s = inflate(&ins);
	defer io::close(&s)!;
	match (io::copy(&outs, &s)) {
	case size => void;
	case let e: io::error =>
		fmt::errorln(io::strerror(e))!;
		abort();
	};
	let out = memio::buffer(&outs);
	defer free(out);
	assert(bytes::equal(expected, out));
};

M compress/flate/inflate.ha => compress/flate/inflate.ha +0 -66
@@ 3,12 3,9 @@
// (c) 2021 Bor Grošelj Simić <bor.groseljsimic@telemach.net>
// (c) 2021 Drew DeVault <sir@cmpwn.com>
// (c) 2021 Ember Sawady <ecs@d2evs.net>
use bytes;
use endian;
use errors;
use fmt;
use io;
use memio;

def BUFSIZE: size = 1 << 16;
def MAXBITS: size = 15;


@@ 67,28 64,6 @@ export type decompressor = struct {
	bufstart: []u8,
};

@init fn init_fixed() void = {
	// See section 3.2.6 of RFC 1951
	let lens: [288]u16 = [0...];
	let i = 0z;
	for (i < 144; i += 1) {
		lens[i] = 8;
	};
	for (i < 256; i += 1) {
		lens[i] = 9;
	};
	for (i < 280; i += 1) {
		lens[i] = 7;
	};
	for (i < 288; i += 1) {
		lens[i] = 8;
	};
	construct(&fixed_len, lens[..]) as void;

	let dists: [30]u16 = [5...];
	construct(&fixed_dist, dists[..]) as void;
};

// Creates a stream which decompresses Deflate (RFC 1951) data.
export fn inflate(src: io::handle) decompressor = decompressor {
	vtable = &decompressor_vtable,


@@ 431,44 406,3 @@ fn close(s: *io::stream) (void | io::error) = {
	free(s.bufstart);
	free(s.hist);
};

// Further testing is done in compress::zlib
@test fn inflate() void = {
	const in: []u8 = [
		// Non-final block, uncompressed
		0b00000000,
		// 4 bytes long
		0b00000100,
		0b00000000,
		// ~LEN
		0b11111011,
		0b11111111,
		// Data
		0xDE, 0xAD, 0xBE, 0xEF,
		// Final block, uncompressed
		0b00000001,
		// 2 bytes long
		0b00000010,
		0b00000000,
		// ~LEN
		0b11111101,
		0b11111111,
		// Data
		0x42, 0x69,
	];
	let expected: []u8 = [0xDE, 0xAD, 0xBE, 0xEF, 0x42, 0x69];

	let ins = memio::fixed(in);
	let outs = memio::dynamic();
	let s = inflate(&ins);
	defer io::close(&s)!;
	match (io::copy(&outs, &s)) {
	case size => void;
	case let e: io::error =>
		fmt::errorln(io::strerror(e))!;
		abort();
	};
	let out = memio::buffer(&outs);
	defer free(out);
	assert(bytes::equal(expected, out));
};

A compress/gzip/+test.ha => compress/gzip/+test.ha +46 -0
@@ 0,0 1,46 @@
use bytes;
use fmt;
use hash;
use hash::crc32;
use io;
use memio;

@test fn readstr() void = {
	const vectors: []([]u8, str) = [
		([0], ""),
		(['h', 'i', 0], "hi"),
		(['h', 'é', 0], "hé"),
	];
	for (let i = 0z; i < len(vectors); i += 1) {
		const vector = vectors[i];
		const st = memio::fixed(vector.0);
		const h = crc32::crc32(&hash::crc32::ieee_table);
		const s = readstr(&st, &h) as str;
		assert(s == vector.1);
		free(s);
	};
};

@test fn decompress() void = {
	for (let i = 0z; i < len(vectors); i += 1) {
		let in = memio::fixed(*vectors[i].2);
		let out = memio::dynamic();
		let d = match (decompress(&in)) {
		case let res: (header, reader) =>
			yield res.1;
		case let e: error =>
			fmt::errorln(strerror(e))!;
			abort();
		};
		match (io::copy(&out, &d)) {
		case let e: io::error =>
			fmt::errorfln("vector {}: {}", i, io::strerror(e))!;
			abort();
		case size =>
			yield;
		};
		let s = memio::buffer(&out);
		assert(bytes::equal(s, *vectors[i].1));
		assert(d.header.name == vectors[i].0);
	};
};

M compress/gzip/reader.ha => compress/gzip/reader.ha +0 -44
@@ 3,14 3,10 @@ use bytes;
use compress::flate;
use endian;
use errors;
use fmt;
use fs;
use hash::crc32;
use hash;
use io;
use memio;
use os;
use strings;
use time;

def GZIPID1: u8     = 0x1f;


@@ 157,22 153,6 @@ fn readstr(s: io::handle, h: *hash::hash) (str | io::error) = {
	return memio::string(&out)!;
};

@test fn readstr() void = {
	const vectors: []([]u8, str) = [
		([0], ""),
		(['h', 'i', 0], "hi"),
		(['h', 'é', 0], "hé"),
	];
	for (let i = 0z; i < len(vectors); i += 1) {
		const vector = vectors[i];
		const st = memio::fixed(vector.0);
		const h = crc32::crc32(&hash::crc32::ieee_table);
		const s = readstr(&st, &h) as str;
		assert(s == vector.1);
		free(s);
	};
};

fn read(s: *io::stream, buf: []u8) (size | io::EOF | io::error) = {
	let s = s: *reader;
	match (io::read(&s.flate, buf)?) {


@@ 204,27 184,3 @@ fn close(s: *io::stream) (void | io::error) = {
	free(s.header.comment);
	free(s.header.extra);
};

@test fn decompress() void = {
	for (let i = 0z; i < len(vectors); i += 1) {
		let in = memio::fixed(*vectors[i].2);
		let out = memio::dynamic();
		let d = match (decompress(&in)) {
		case let res: (header, reader) =>
			yield res.1;
		case let e: error =>
			fmt::errorln(strerror(e))!;
			abort();
		};
		match (io::copy(&out, &d)) {
		case let e: io::error =>
			fmt::errorfln("vector {}: {}", i, io::strerror(e))!;
			abort();
		case size =>
			yield;
		};
		let s = memio::buffer(&out);
		assert(bytes::equal(s, *vectors[i].1));
		assert(d.header.name == vectors[i].0);
	};
};

M compress/zlib/reader.ha => compress/zlib/reader.ha +0 -3
@@ 4,12 4,9 @@
// (c) 2021 Drew DeVault <sir@cmpwn.com>
// (c) 2021 Ember Sawady <ecs@d2evs.net>
// (c) 2021 Kiëd Llaentenn <kiedtl@tilde.team>
use bufio;
use bytes;
use compress::flate;
use endian;
use errors;
use fmt;
use hash::adler32;
use hash;
use io;