blob: f760c6ddb7919208cf9474379e50d4e72210efb6 [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_get_string(struct kunit *test)
{
struct string_stream *stream = new_string_stream();
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);
destroy_string_stream(stream);
}
static void string_stream_test_add_and_clear(struct kunit *test)
{
struct string_stream *stream = new_string_stream();
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, 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));
destroy_string_stream(stream);
}
static struct kunit_case string_stream_test_cases[] = {
KUNIT_CASE(string_stream_test_get_string),
KUNIT_CASE(string_stream_test_add_and_clear),
{}
};
static struct kunit_module string_stream_test_module = {
.name = "string-stream-test",
.test_cases = string_stream_test_cases
};
module_test(string_stream_test_module);