Brendan Higgins | d8e2a76 | 2019-09-23 02:02:37 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * KUnit test for struct string_stream. |
| 4 | * |
| 5 | * Copyright (C) 2019, Google LLC. |
| 6 | * Author: Brendan Higgins <brendanhiggins@google.com> |
| 7 | */ |
| 8 | |
Brendan Higgins | d8e2a76 | 2019-09-23 02:02:37 -0700 | [diff] [blame] | 9 | #include <kunit/test.h> |
| 10 | #include <linux/slab.h> |
| 11 | |
Alan Maguire | 109fb06 | 2020-01-06 22:28:18 +0000 | [diff] [blame] | 12 | #include "string-stream.h" |
| 13 | |
Brendan Higgins | d8e2a76 | 2019-09-23 02:02:37 -0700 | [diff] [blame] | 14 | static void string_stream_test_empty_on_creation(struct kunit *test) |
| 15 | { |
| 16 | struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); |
| 17 | |
| 18 | KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); |
| 19 | } |
| 20 | |
| 21 | static void string_stream_test_not_empty_after_add(struct kunit *test) |
| 22 | { |
| 23 | struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); |
| 24 | |
| 25 | string_stream_add(stream, "Foo"); |
| 26 | |
| 27 | KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream)); |
| 28 | } |
| 29 | |
| 30 | static void string_stream_test_get_string(struct kunit *test) |
| 31 | { |
| 32 | struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); |
| 33 | char *output; |
| 34 | |
| 35 | string_stream_add(stream, "Foo"); |
| 36 | string_stream_add(stream, " %s", "bar"); |
| 37 | |
| 38 | output = string_stream_get_string(stream); |
Brendan Higgins | e4aea8f | 2019-09-23 02:02:41 -0700 | [diff] [blame] | 39 | KUNIT_ASSERT_STREQ(test, output, "Foo bar"); |
Brendan Higgins | d8e2a76 | 2019-09-23 02:02:37 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static struct kunit_case string_stream_test_cases[] = { |
| 43 | KUNIT_CASE(string_stream_test_empty_on_creation), |
| 44 | KUNIT_CASE(string_stream_test_not_empty_after_add), |
| 45 | KUNIT_CASE(string_stream_test_get_string), |
| 46 | {} |
| 47 | }; |
| 48 | |
| 49 | static struct kunit_suite string_stream_test_suite = { |
| 50 | .name = "string-stream-test", |
| 51 | .test_cases = string_stream_test_cases |
| 52 | }; |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 53 | kunit_test_suites(&string_stream_test_suite); |