~sircmpwn/hare

3b095c26ab8f8437e9013b08de27535b8e9ea9da — Sebastian 2 months ago fd269b1
bytes: improve tests style and add test

Signed-off-by: Sebastian <sebastian@sebsite.pw>
2 files changed, 13 insertions(+), 33 deletions(-)

M bytes/contains.ha
M bytes/trim.ha
M bytes/contains.ha => bytes/contains.ha +12 -32
@@ 23,22 23,12 @@ export fn hasprefix(in: []u8, prefix: []u8) bool = {
};

@test fn hasprefix() void = {
	const cases: []([]u8, []u8, bool) = [
		([], [], true),
		([0], [], true),
		([], [0], false),
		([1, 2, 3], [1, 2], true),
		([1, 2, 3], [1, 1], false),
		([1, 2, 3], [1, 2, 3, 4], false),
	];

	for (let i = 0z; i < len(cases); i += 1) {
		let s = cases[i].0;
		let prefix = cases[i].1;
		let expected = cases[i].2;
		let actual = hasprefix(s, prefix);
		assert(actual == expected);
	};
	assert(hasprefix([], []));
	assert(hasprefix([0], []));
	assert(!hasprefix([], [0]));
	assert(hasprefix([1, 2, 3], [1, 2]));
	assert(!hasprefix([1, 2, 3], [1, 1]));
	assert(!hasprefix([1, 2, 3], [1, 2, 3, 4]));
};

// Returns true if "in" has the given suffix, false otherwise


@@ 48,20 38,10 @@ export fn hassuffix(in: []u8, suffix: []u8) bool = {
};

@test fn hassuffix() void = {
	const cases: []([]u8, []u8, bool) = [
		([], [], true),
		([0], [], true),
		([], [0], false),
		([1, 2, 3], [2, 3], true),
		([1, 2, 3], [2, 2], false),
		([1, 2, 3, 4], [2, 3, 4], true),
	];

	for (let i = 0z; i < len(cases); i += 1) {
		let s = cases[i].0;
		let prefix = cases[i].1;
		let expected = cases[i].2;
		let actual = hassuffix(s, prefix);
		assert(actual == expected);
	};
	assert(hassuffix([], []));
	assert(hassuffix([0], []));
	assert(!hassuffix([], [0]));
	assert(hassuffix([1, 2, 3], [2, 3]));
	assert(!hassuffix([1, 2, 3], [2, 2]));
	assert(hassuffix([1, 2, 3, 4], [2, 3, 4]));
};

M bytes/trim.ha => bytes/trim.ha +1 -1
@@ 26,5 26,5 @@ export fn trim(in: []u8, trim: u8...) []u8 = ltrim(rtrim(in, trim...), trim...);
	assert(equal(trim([1, 2, 3, 5], 0), [1, 2, 3, 5]));
	assert(equal(trim([0, 0, 0], 0), []));
	assert(equal(trim([0, 5, 0], 5), [0, 5, 0]));
	assert(equal(trim([], 0), []));
};