18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * KUnit test for struct string_stream.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC.
68c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <kunit/test.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "string-stream.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic void string_stream_test_empty_on_creation(struct kunit *test)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic void string_stream_test_not_empty_after_add(struct kunit *test)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	string_stream_add(stream, "Foo");
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream));
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic void string_stream_test_get_string(struct kunit *test)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
338c2ecf20Sopenharmony_ci	char *output;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	string_stream_add(stream, "Foo");
368c2ecf20Sopenharmony_ci	string_stream_add(stream, " %s", "bar");
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	output = string_stream_get_string(stream);
398c2ecf20Sopenharmony_ci	KUNIT_ASSERT_STREQ(test, output, "Foo bar");
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic struct kunit_case string_stream_test_cases[] = {
438c2ecf20Sopenharmony_ci	KUNIT_CASE(string_stream_test_empty_on_creation),
448c2ecf20Sopenharmony_ci	KUNIT_CASE(string_stream_test_not_empty_after_add),
458c2ecf20Sopenharmony_ci	KUNIT_CASE(string_stream_test_get_string),
468c2ecf20Sopenharmony_ci	{}
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic struct kunit_suite string_stream_test_suite = {
508c2ecf20Sopenharmony_ci	.name = "string-stream-test",
518c2ecf20Sopenharmony_ci	.test_cases = string_stream_test_cases
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_cikunit_test_suites(&string_stream_test_suite);
54