aboutsummaryrefslogtreecommitdiff
path: root/lib/io/test/istream_mem.c
blob: ca5146cdf8ace7d60e10c72117b077c6ecd15b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * istream_mem.c
 *
 * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "util/test.h"
#include "io/mem.h"

static const size_t end0 = 449;	/* region 1: filled with 'A' */
static const size_t end1 = 521;	/* region 2: filled with 'B' */
static const size_t end2 = 941;	/* region 3: filled with 'C' */

static unsigned char data[941];

static unsigned char byte_at_offset(size_t off)
{
	if (off < end0)
		return 'A';
	if (off < end1)
		return 'B';
	return 'C';
}

static void init_buffer(void)
{
	for (size_t i = 0; i < end2; ++i)
		data[i] = byte_at_offset(i);
}

int main(int argc, char **argv)
{
	size_t i, diff, size;
	bool eat_all = true;
	const sqfs_u8 *ptr;
	istream_t *in;
	int ret;
	(void)argc; (void)argv;

	init_buffer();

	in = istream_memory_create("memstream.txt", 61, data, sizeof(data));
	TEST_NOT_NULL(in);
	TEST_EQUAL_UI(((sqfs_object_t *)in)->refcount, 1);

	TEST_STR_EQUAL(in->get_filename(in), "memstream.txt");

	for (i = 0; i < end2; i += diff) {
		ret = in->get_buffered_data(in, &ptr, &size, 61);
		TEST_EQUAL_I(ret, 0);

		if ((end2 - i) >= 61) {
			TEST_NOT_NULL(ptr);
			TEST_EQUAL_UI(size, 61);
		} else {
			TEST_NOT_NULL(ptr);
			TEST_EQUAL_UI(size, (end2 - i));
		}

		for (size_t j = 0; j < size; ++j) {
			TEST_EQUAL_UI(ptr[j], byte_at_offset(i + j));
		}

		diff = eat_all ? size : (size / 2);
		eat_all = !eat_all;
		in->advance_buffer(in, diff);
	}

	sqfs_drop(in);
	return EXIT_SUCCESS;
}