blob: 5f27d576d2daf4ec06e5fb3108d4f7f9ee91a9cb [file] [log] [blame]
// SPDX-License-Identifier: GPL-2.0
/*
* KUnit test for struct string_stream.
*
* Copyright (C) 2019, 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_empty_on_creation(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test);
KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
}
static void string_stream_test_not_empty_after_add(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test);
string_stream_add(stream, "Foo");
KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream));
}
static void string_stream_test_get_string(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test);
char *output;
string_stream_add(stream, "Foo");
string_stream_add(stream, " %s", "bar");
output = string_stream_get_string(stream);
KUNIT_ASSERT_STREQ(test, output, "Foo bar");
kfree(output);
}
static void string_stream_test_add_and_clear(struct kunit *test)
{
struct string_stream *stream = alloc_string_stream(test);
char *output;
int i;
for (i = 0; i < 10; i++)
string_stream_add(stream, "A");
output = string_stream_get_string(stream);
KUNIT_ASSERT_STREQ(test, output, "AAAAAAAAAA");
KUNIT_ASSERT_EQ(test, stream->length, (size_t)10);
KUNIT_ASSERT_FALSE(test, string_stream_is_empty(stream));
kfree(output);
string_stream_clear(stream);
output = string_stream_get_string(stream);
KUNIT_ASSERT_STREQ(test, output, "");
KUNIT_ASSERT_TRUE(test, string_stream_is_empty(stream));
}
static struct kunit_case string_stream_test_cases[] = {
KUNIT_CASE(string_stream_test_empty_on_creation),
KUNIT_CASE(string_stream_test_not_empty_after_add),
KUNIT_CASE(string_stream_test_get_string),
KUNIT_CASE(string_stream_test_add_and_clear),
{}
};
static struct kunit_suite string_stream_test_suite = {
.name = "string-stream-test",
.test_cases = string_stream_test_cases
};
kunit_test_suite(string_stream_test_suite);