summaryrefstree
path: root/kernel/test/string-stream-test.c
blob: a22fe100d9ada95d835d51e180066d2d56e945dc (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
// SPDX-License-Identifier: GPL-2.0
/*
 * KUnit test for struct string_stream.
 *
 * Copyright (C) 2018, Google LLC.
 * Author: Brendan Higgins <brendanhiggins@google.com>
 */

#include <linux/slab.h>
#include <kunit/test.h>
#include <kunit/string-stream.h>

static void string_stream_test_get_string(struct test *test)
{
	struct string_stream *stream = new_string_stream();
	char *output;

	stream->add(stream, "Foo");
	stream->add(stream, " %s", "bar");

	output = stream->get_string(stream);
	ASSERT_STREQ(test, output, "Foo bar");
	kfree(output);
	destroy_string_stream(stream);
}

static void string_stream_test_add_and_clear(struct test *test)
{
	struct string_stream *stream = new_string_stream();
	char *output;
	int i;

	for (i = 0; i < 10; i++)
		stream->add(stream, "A");

	output = stream->get_string(stream);
	ASSERT_STREQ(test, output, "AAAAAAAAAA");
	ASSERT_EQ(test, stream->length, 10);
	ASSERT_FALSE(test, stream->is_empty(stream));
	kfree(output);

	stream->clear(stream);

	output = stream->get_string(stream);
	ASSERT_STREQ(test, output, "");
	ASSERT_TRUE(test, stream->is_empty(stream));
	destroy_string_stream(stream);
}

static struct test_case string_stream_test_cases[] = {
	TEST_CASE(string_stream_test_get_string),
	TEST_CASE(string_stream_test_add_and_clear),
	{}
};

static struct test_module string_stream_test_module = {
	.name = "string-stream-test",
	.test_cases = string_stream_test_cases
};
module_test(string_stream_test_module);