From: Richard Fitzgerald <rf@opensource.cirrus.com>
To: <brendan.higgins@linux.dev>, <davidgow@google.com>, <rmoar@google.com>
Cc: <linux-kselftest@vger.kernel.org>, <kunit-dev@googlegroups.com>,
<linux-kernel@vger.kernel.org>, <patches@opensource.cirrus.com>,
"Richard Fitzgerald" <rf@opensource.cirrus.com>
Subject: [PATCH v5 04/10] kunit: string-stream: Add cases for string_stream newline appending
Date: Thu, 24 Aug 2023 15:31:23 +0100 [thread overview]
Message-ID: <20230824143129.1957914-5-rf@opensource.cirrus.com> (raw)
In-Reply-To: <20230824143129.1957914-1-rf@opensource.cirrus.com>
Add test cases for testing the string_stream feature that appends a
newline to strings that do not already end with a newline.
string_stream_no_auto_newline_test() tests with this feature disabled.
Newlines should not be added or dropped.
string_stream_auto_newline_test() tests with this feature enabled.
Newlines should be added to lines that do not end with a newline.
string_stream_append_auto_newline_test() tests appending the
content of one stream to another stream when the target stream
has newline appending enabled.
Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
Changes since V4:
- string_stream_append_auto_newline_test() doesn't clear the destination
stream_1 between the newline and no-newline case. This is just a
simplification of the code.
- string_stream_no_auto_newline_test() uses the same set of test strings
as string_stream_auto_newline_test().
---
lib/kunit/string-stream-test.c | 93 ++++++++++++++++++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/lib/kunit/string-stream-test.c b/lib/kunit/string-stream-test.c
index 2b761ba01835..2a9936db1b9f 100644
--- a/lib/kunit/string-stream-test.c
+++ b/lib/kunit/string-stream-test.c
@@ -32,6 +32,7 @@ static void string_stream_init_test(struct kunit *test)
KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments));
KUNIT_EXPECT_PTR_EQ(test, stream->test, test);
KUNIT_EXPECT_EQ(test, stream->gfp, GFP_KERNEL);
+ KUNIT_EXPECT_FALSE(test, stream->append_newlines);
KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
}
@@ -214,6 +215,45 @@ static void string_stream_append_test(struct kunit *test)
KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), stream_2_content);
}
+/* Appending the content of one string stream to one with auto-newlining. */
+static void string_stream_append_auto_newline_test(struct kunit *test)
+{
+ struct string_stream *stream_1, *stream_2;
+
+ /* Stream 1 has newline appending enabled */
+ stream_1 = alloc_string_stream(test, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1);
+ string_stream_set_append_newlines(stream_1, true);
+ KUNIT_EXPECT_TRUE(test, stream_1->append_newlines);
+
+ /* Stream 2 does not append newlines */
+ stream_2 = alloc_string_stream(test, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
+
+ /* Appending a stream with a newline should not add another newline */
+ string_stream_add(stream_1, "Original string\n");
+ string_stream_add(stream_2, "Appended content\n");
+ string_stream_add(stream_2, "More stuff\n");
+ string_stream_append(stream_1, stream_2);
+ KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
+ "Original string\nAppended content\nMore stuff\n");
+
+ string_stream_destroy(stream_2);
+ stream_2 = alloc_string_stream(test, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2);
+
+ /*
+ * Appending a stream without newline should add a final newline.
+ * The appended string_stream is treated as a single string so newlines
+ * should not be inserted between fragments.
+ */
+ string_stream_add(stream_2, "Another");
+ string_stream_add(stream_2, "And again");
+ string_stream_append(stream_1, stream_2);
+ KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
+ "Original string\nAppended content\nMore stuff\nAnotherAnd again\n");
+}
+
/* Adding an empty string should not create a fragment. */
static void string_stream_append_empty_string_test(struct kunit *test)
{
@@ -237,12 +277,65 @@ static void string_stream_append_empty_string_test(struct kunit *test)
KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream), "Add this line");
}
+/* Adding strings without automatic newline appending */
+static void string_stream_no_auto_newline_test(struct kunit *test)
+{
+ struct string_stream *stream;
+
+ stream = alloc_string_stream(test, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
+
+ /*
+ * Add some strings with and without newlines. All formatted newlines
+ * should be preserved. It should not add any extra newlines.
+ */
+ string_stream_add(stream, "One");
+ string_stream_add(stream, "Two\n");
+ string_stream_add(stream, "%s\n", "Three");
+ string_stream_add(stream, "%s", "Four\n");
+ string_stream_add(stream, "Five\n%s", "Six");
+ string_stream_add(stream, "Seven\n\n");
+ string_stream_add(stream, "Eight");
+ KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream),
+ "OneTwo\nThree\nFour\nFive\nSixSeven\n\nEight");
+}
+
+/* Adding strings with automatic newline appending */
+static void string_stream_auto_newline_test(struct kunit *test)
+{
+ struct string_stream *stream;
+
+ stream = alloc_string_stream(test, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream);
+
+ string_stream_set_append_newlines(stream, true);
+ KUNIT_EXPECT_TRUE(test, stream->append_newlines);
+
+ /*
+ * Add some strings with and without newlines. Newlines should
+ * be appended to lines that do not end with \n, but newlines
+ * resulting from the formatting should not be changed.
+ */
+ string_stream_add(stream, "One");
+ string_stream_add(stream, "Two\n");
+ string_stream_add(stream, "%s\n", "Three");
+ string_stream_add(stream, "%s", "Four\n");
+ string_stream_add(stream, "Five\n%s", "Six");
+ string_stream_add(stream, "Seven\n\n");
+ string_stream_add(stream, "Eight");
+ KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream),
+ "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n");
+}
+
static struct kunit_case string_stream_test_cases[] = {
KUNIT_CASE(string_stream_init_test),
KUNIT_CASE(string_stream_line_add_test),
KUNIT_CASE(string_stream_variable_length_line_test),
KUNIT_CASE(string_stream_append_test),
+ KUNIT_CASE(string_stream_append_auto_newline_test),
KUNIT_CASE(string_stream_append_empty_string_test),
+ KUNIT_CASE(string_stream_no_auto_newline_test),
+ KUNIT_CASE(string_stream_auto_newline_test),
{}
};
--
2.30.2
next prev parent reply other threads:[~2023-08-24 14:33 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-24 14:31 [PATCH v5 00/10] kunit: Add dynamically-extending log Richard Fitzgerald
2023-08-24 14:31 ` [PATCH v5 01/10] kunit: string-stream: Don't create a fragment for empty strings Richard Fitzgerald
2023-08-24 22:42 ` Rae Moar
2023-08-25 6:49 ` David Gow
2023-08-24 14:31 ` [PATCH v5 02/10] kunit: string-stream: Improve testing of string_stream Richard Fitzgerald
2023-08-24 22:42 ` Rae Moar
2023-08-25 14:58 ` Richard Fitzgerald
2023-08-25 6:49 ` David Gow
2023-08-25 9:32 ` Richard Fitzgerald
2023-08-24 14:31 ` [PATCH v5 03/10] kunit: string-stream: Add option to make all lines end with newline Richard Fitzgerald
2023-08-24 23:13 ` Rae Moar
2023-08-25 6:49 ` David Gow
2023-08-24 14:31 ` Richard Fitzgerald [this message]
2023-08-24 23:22 ` [PATCH v5 04/10] kunit: string-stream: Add cases for string_stream newline appending Rae Moar
2023-08-25 6:49 ` David Gow
2023-08-24 14:31 ` [PATCH v5 05/10] kunit: Don't use a managed alloc in is_literal() Richard Fitzgerald
2023-08-25 6:49 ` David Gow
2023-08-24 14:31 ` [PATCH v5 06/10] kunit: string-stream: Add kunit_alloc_string_stream() Richard Fitzgerald
2023-08-25 6:49 ` David Gow
2023-08-24 14:31 ` [PATCH v5 07/10] kunit: string-stream: Decouple string_stream from kunit Richard Fitzgerald
2023-08-25 6:17 ` kernel test robot
2023-08-25 6:49 ` David Gow
2023-08-24 14:31 ` [PATCH v5 08/10] kunit: string-stream: Add tests for freeing resource-managed string_stream Richard Fitzgerald
2023-08-25 6:49 ` David Gow
2023-08-25 9:41 ` Richard Fitzgerald
2023-08-24 14:31 ` [PATCH v5 09/10] kunit: Use string_stream for test log Richard Fitzgerald
2023-08-25 6:17 ` kernel test robot
2023-08-25 6:53 ` David Gow
2023-08-25 7:30 ` kernel test robot
2023-08-24 14:31 ` [PATCH v5 10/10] kunit: string-stream: Test performance of string_stream Richard Fitzgerald
2023-08-25 6:50 ` David Gow
2023-08-25 6:58 ` [PATCH v5 00/10] kunit: Add dynamically-extending log David Gow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230824143129.1957914-5-rf@opensource.cirrus.com \
--to=rf@opensource.cirrus.com \
--cc=brendan.higgins@linux.dev \
--cc=davidgow@google.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=patches@opensource.cirrus.com \
--cc=rmoar@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox