@@ -32,6 +32,7 @@ static void string_stream_init_test(struct kunit *test)
32
32
KUNIT_EXPECT_TRUE (test , list_empty (& stream -> fragments ));
33
33
KUNIT_EXPECT_PTR_EQ (test , stream -> test , test );
34
34
KUNIT_EXPECT_TRUE (test , (stream -> gfp == GFP_KERNEL ));
35
+ KUNIT_EXPECT_FALSE (test , stream -> append_newlines );
35
36
KUNIT_EXPECT_TRUE (test , string_stream_is_empty (stream ));
36
37
}
37
38
@@ -215,6 +216,45 @@ static void string_stream_append_test(struct kunit *test)
215
216
KUNIT_EXPECT_STREQ (test , get_concatenated_string (test , stream_1 ), stream_2_content );
216
217
}
217
218
219
+ /* Appending the content of one string stream to one with auto-newlining. */
220
+ static void string_stream_append_auto_newline_test (struct kunit * test )
221
+ {
222
+ struct string_stream * stream_1 , * stream_2 ;
223
+
224
+ /* Stream 1 has newline appending enabled */
225
+ stream_1 = alloc_string_stream (test , GFP_KERNEL );
226
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , stream_1 );
227
+ string_stream_set_append_newlines (stream_1 , true);
228
+ KUNIT_EXPECT_TRUE (test , stream_1 -> append_newlines );
229
+
230
+ /* Stream 2 does not append newlines */
231
+ stream_2 = alloc_string_stream (test , GFP_KERNEL );
232
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , stream_2 );
233
+
234
+ /* Appending a stream with a newline should not add another newline */
235
+ string_stream_add (stream_1 , "Original string\n" );
236
+ string_stream_add (stream_2 , "Appended content\n" );
237
+ string_stream_add (stream_2 , "More stuff\n" );
238
+ string_stream_append (stream_1 , stream_2 );
239
+ KUNIT_EXPECT_STREQ (test , get_concatenated_string (test , stream_1 ),
240
+ "Original string\nAppended content\nMore stuff\n" );
241
+
242
+ string_stream_destroy (stream_2 );
243
+ stream_2 = alloc_string_stream (test , GFP_KERNEL );
244
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , stream_2 );
245
+
246
+ /*
247
+ * Appending a stream without newline should add a final newline.
248
+ * The appended string_stream is treated as a single string so newlines
249
+ * should not be inserted between fragments.
250
+ */
251
+ string_stream_add (stream_2 , "Another" );
252
+ string_stream_add (stream_2 , "And again" );
253
+ string_stream_append (stream_1 , stream_2 );
254
+ KUNIT_EXPECT_STREQ (test , get_concatenated_string (test , stream_1 ),
255
+ "Original string\nAppended content\nMore stuff\nAnotherAnd again\n" );
256
+ }
257
+
218
258
/* Adding an empty string should not create a fragment. */
219
259
static void string_stream_append_empty_string_test (struct kunit * test )
220
260
{
@@ -238,12 +278,65 @@ static void string_stream_append_empty_string_test(struct kunit *test)
238
278
KUNIT_EXPECT_STREQ (test , get_concatenated_string (test , stream ), "Add this line" );
239
279
}
240
280
281
+ /* Adding strings without automatic newline appending */
282
+ static void string_stream_no_auto_newline_test (struct kunit * test )
283
+ {
284
+ struct string_stream * stream ;
285
+
286
+ stream = alloc_string_stream (test , GFP_KERNEL );
287
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , stream );
288
+
289
+ /*
290
+ * Add some strings with and without newlines. All formatted newlines
291
+ * should be preserved. It should not add any extra newlines.
292
+ */
293
+ string_stream_add (stream , "One" );
294
+ string_stream_add (stream , "Two\n" );
295
+ string_stream_add (stream , "%s\n" , "Three" );
296
+ string_stream_add (stream , "%s" , "Four\n" );
297
+ string_stream_add (stream , "Five\n%s" , "Six" );
298
+ string_stream_add (stream , "Seven\n\n" );
299
+ string_stream_add (stream , "Eight" );
300
+ KUNIT_EXPECT_STREQ (test , get_concatenated_string (test , stream ),
301
+ "OneTwo\nThree\nFour\nFive\nSixSeven\n\nEight" );
302
+ }
303
+
304
+ /* Adding strings with automatic newline appending */
305
+ static void string_stream_auto_newline_test (struct kunit * test )
306
+ {
307
+ struct string_stream * stream ;
308
+
309
+ stream = alloc_string_stream (test , GFP_KERNEL );
310
+ KUNIT_ASSERT_NOT_ERR_OR_NULL (test , stream );
311
+
312
+ string_stream_set_append_newlines (stream , true);
313
+ KUNIT_EXPECT_TRUE (test , stream -> append_newlines );
314
+
315
+ /*
316
+ * Add some strings with and without newlines. Newlines should
317
+ * be appended to lines that do not end with \n, but newlines
318
+ * resulting from the formatting should not be changed.
319
+ */
320
+ string_stream_add (stream , "One" );
321
+ string_stream_add (stream , "Two\n" );
322
+ string_stream_add (stream , "%s\n" , "Three" );
323
+ string_stream_add (stream , "%s" , "Four\n" );
324
+ string_stream_add (stream , "Five\n%s" , "Six" );
325
+ string_stream_add (stream , "Seven\n\n" );
326
+ string_stream_add (stream , "Eight" );
327
+ KUNIT_EXPECT_STREQ (test , get_concatenated_string (test , stream ),
328
+ "One\nTwo\nThree\nFour\nFive\nSix\nSeven\n\nEight\n" );
329
+ }
330
+
241
331
static struct kunit_case string_stream_test_cases [] = {
242
332
KUNIT_CASE (string_stream_init_test ),
243
333
KUNIT_CASE (string_stream_line_add_test ),
244
334
KUNIT_CASE (string_stream_variable_length_line_test ),
245
335
KUNIT_CASE (string_stream_append_test ),
336
+ KUNIT_CASE (string_stream_append_auto_newline_test ),
246
337
KUNIT_CASE (string_stream_append_empty_string_test ),
338
+ KUNIT_CASE (string_stream_no_auto_newline_test ),
339
+ KUNIT_CASE (string_stream_auto_newline_test ),
247
340
{}
248
341
};
249
342
0 commit comments