/* Avoids a cast warning if kfree() is passed direct to kunit_add_action(). */
KUNIT_DEFINE_ACTION_WRAPPER(kfree_wrapper, kfree, constvoid *);
/* Avoids a cast warning if string_stream_destroy() is passed direct to kunit_add_action(). */
KUNIT_DEFINE_ACTION_WRAPPER(cleanup_raw_stream, string_stream_destroy, struct string_stream *);
/* The kunit could own string_streams other than the one we are testing. */ if (stream == priv->expected_free_stream) { if (priv->stream_was_freed)
priv->stream_free_again = true; else
priv->stream_was_freed = true;
}
/* * Calling string_stream_destroy() will only call this function again * because the redirection stub is still active. * Avoid calling deactivate_static_stub() or changing current->kunit_test * during cleanup.
*/
string_stream_clear(stream);
kfree(stream);
}
/* * Activate stub before creating string_stream so the * string_stream will be cleaned up first.
*/
priv->expected_free_stream = NULL;
priv->stream_was_freed = false;
priv->stream_free_again = false;
/* * Add a series of lines to a string_stream. Check that all lines * appear in the correct order and no characters are dropped.
*/ staticvoid string_stream_line_add_test(struct kunit *test)
{ struct string_stream *stream; char line[60]; char *concat_string, *pos, *string_end;
size_t len, total_len; int num_lines, i;
/* Add series of sequence numbered lines */
total_len = 0; for (i = 0; i < 100; ++i) {
len = snprintf(line, sizeof(line), "The quick brown fox jumps over the lazy penguin %d\n", i);
/* Sanity-check that our test string isn't truncated */
KUNIT_ASSERT_LT(test, len, sizeof(line));
/* * Split the concatenated string at the newlines and check that * all the original added strings are present.
*/
pos = concat_string; for (i = 0; i < num_lines; ++i) {
string_end = strchr(pos, '\n');
KUNIT_EXPECT_NOT_NULL(test, string_end);
/* Convert to NULL-terminated string */
*string_end = '\0';
snprintf(line, sizeof(line), "The quick brown fox jumps over the lazy penguin %d", i);
KUNIT_EXPECT_STREQ(test, pos, line);
pos = string_end + 1;
}
/* There shouldn't be any more data after this */
KUNIT_EXPECT_EQ(test, strlen(pos), 0);
}
/* Add a series of lines of variable length to a string_stream. */ staticvoid string_stream_variable_length_line_test(struct kunit *test)
{ staticconstchar line[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" " 0123456789!$%^&*()_-+={}[]:;@'~#<>,.?/|"; struct string_stream *stream; struct rnd_state rnd; char *concat_string, *pos, *string_end;
size_t offset, total_len; int num_lines, i;
/* * Log many lines of varying lengths until we have created * many fragments. * The "randomness" must be repeatable.
*/
prandom_seed_state(&rnd, 3141592653589793238ULL);
total_len = 0; for (i = 0; i < 100; ++i) {
offset = prandom_u32_state(&rnd) % (sizeof(line) - 1);
string_stream_add(stream, "%s\n", &line[offset]);
total_len += sizeof(line) - offset;
}
num_lines = i;
/* * Split the concatenated string at the newlines and check that * all the original added strings are present.
*/
prandom_seed_state(&rnd, 3141592653589793238ULL);
pos = concat_string; for (i = 0; i < num_lines; ++i) {
string_end = strchr(pos, '\n');
KUNIT_EXPECT_NOT_NULL(test, string_end);
/* Convert to NULL-terminated string */
*string_end = '\0';
/* Append content of empty stream to non-empty stream */
string_stream_append(stream_1, stream_2);
KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1),
stream1_content_before_append);
/* Add some data to stream_2 */ for (i = 0; i < ARRAY_SIZE(strings_2); ++i)
string_stream_add(stream_2, "%s\n", strings_2[i]);
/* Append content of non-empty stream to non-empty stream */
string_stream_append(stream_1, stream_2);
/* * End result should be the original content of stream_1 plus * the content of stream_2.
*/
stream_2_content = get_concatenated_string(test, stream_2);
combined_length = strlen(stream1_content_before_append) + strlen(stream_2_content);
combined_length++; /* for terminating \0 */
combined_content = kunit_kmalloc(test, combined_length, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, combined_content);
snprintf(combined_content, combined_length, "%s%s",
stream1_content_before_append, stream_2_content);
/* Appending the content of one string stream to one with auto-newlining. */ staticvoid string_stream_append_auto_newline_test(struct kunit *test)
{ struct string_stream *stream_1, *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. */ staticvoid string_stream_append_empty_string_test(struct kunit *test)
{ struct string_stream *stream; int original_frag_count;
/* Adding an empty string to a non-empty stream */
string_stream_add(stream, "Add this line");
original_frag_count = list_count_nodes(&stream->fragments);
/* * 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");
}
/* * 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");
}
/* * This doesn't actually "test" anything. It reports time taken * and memory used for logging a large number of lines.
*/ staticvoid string_stream_performance_test(struct kunit *test)
{ struct string_stream_fragment *frag_container; struct string_stream *stream; char test_line[101];
ktime_t start_time, end_time;
size_t len, bytes_requested, actual_bytes_used, total_string_length; int offset, i;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.