|
11 | 11 |
|
12 | 12 | #include "string-stream.h"
|
13 | 13 |
|
14 |
| -static void string_stream_test_empty_on_creation(struct kunit *test) |
| 14 | +static char *get_concatenated_string(struct kunit *test, struct string_stream *stream) |
15 | 15 | {
|
16 |
| - struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); |
| 16 | + char *str = string_stream_get_string(stream); |
17 | 17 |
|
| 18 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str); |
| 19 | + |
| 20 | + return str; |
| 21 | +} |
| 22 | + |
| 23 | +/* string_stream object is initialized correctly. */ |
| 24 | +static void string_stream_init_test(struct kunit *test) |
| 25 | +{ |
| 26 | + struct string_stream *stream; |
| 27 | + |
| 28 | + stream = alloc_string_stream(test, GFP_KERNEL); |
| 29 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); |
| 30 | + |
| 31 | + KUNIT_EXPECT_EQ(test, stream->length, 0); |
| 32 | + KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments)); |
| 33 | + KUNIT_EXPECT_PTR_EQ(test, stream->test, test); |
| 34 | + KUNIT_EXPECT_TRUE(test, (stream->gfp == GFP_KERNEL)); |
18 | 35 | KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
|
19 | 36 | }
|
20 | 37 |
|
21 |
| -static void string_stream_test_not_empty_after_add(struct kunit *test) |
| 38 | +/* |
| 39 | + * Add a series of lines to a string_stream. Check that all lines |
| 40 | + * appear in the correct order and no characters are dropped. |
| 41 | + */ |
| 42 | +static void string_stream_line_add_test(struct kunit *test) |
| 43 | +{ |
| 44 | + struct string_stream *stream; |
| 45 | + char line[60]; |
| 46 | + char *concat_string, *pos, *string_end; |
| 47 | + size_t len, total_len; |
| 48 | + int num_lines, i; |
| 49 | + |
| 50 | + stream = alloc_string_stream(test, GFP_KERNEL); |
| 51 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); |
| 52 | + |
| 53 | + /* Add series of sequence numbered lines */ |
| 54 | + total_len = 0; |
| 55 | + for (i = 0; i < 100; ++i) { |
| 56 | + len = snprintf(line, sizeof(line), |
| 57 | + "The quick brown fox jumps over the lazy penguin %d\n", i); |
| 58 | + |
| 59 | + /* Sanity-check that our test string isn't truncated */ |
| 60 | + KUNIT_ASSERT_LT(test, len, sizeof(line)); |
| 61 | + |
| 62 | + string_stream_add(stream, line); |
| 63 | + total_len += len; |
| 64 | + } |
| 65 | + num_lines = i; |
| 66 | + |
| 67 | + concat_string = get_concatenated_string(test, stream); |
| 68 | + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string); |
| 69 | + KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len); |
| 70 | + |
| 71 | + /* |
| 72 | + * Split the concatenated string at the newlines and check that |
| 73 | + * all the original added strings are present. |
| 74 | + */ |
| 75 | + pos = concat_string; |
| 76 | + for (i = 0; i < num_lines; ++i) { |
| 77 | + string_end = strchr(pos, '\n'); |
| 78 | + KUNIT_EXPECT_NOT_NULL(test, string_end); |
| 79 | + |
| 80 | + /* Convert to NULL-terminated string */ |
| 81 | + *string_end = '\0'; |
| 82 | + |
| 83 | + snprintf(line, sizeof(line), |
| 84 | + "The quick brown fox jumps over the lazy penguin %d", i); |
| 85 | + KUNIT_EXPECT_STREQ(test, pos, line); |
| 86 | + |
| 87 | + pos = string_end + 1; |
| 88 | + } |
| 89 | + |
| 90 | + /* There shouldn't be any more data after this */ |
| 91 | + KUNIT_EXPECT_EQ(test, strlen(pos), 0); |
| 92 | +} |
| 93 | + |
| 94 | +/* Add a series of lines of variable length to a string_stream. */ |
| 95 | +static void string_stream_variable_length_line_test(struct kunit *test) |
| 96 | +{ |
| 97 | + static const char line[] = |
| 98 | + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 99 | + " 0123456789!$%^&*()_-+={}[]:;@'~#<>,.?/|"; |
| 100 | + struct string_stream *stream; |
| 101 | + struct rnd_state rnd; |
| 102 | + char *concat_string, *pos, *string_end; |
| 103 | + size_t offset, total_len; |
| 104 | + int num_lines, i; |
| 105 | + |
| 106 | + stream = alloc_string_stream(test, GFP_KERNEL); |
| 107 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); |
| 108 | + |
| 109 | + /* |
| 110 | + * Log many lines of varying lengths until we have created |
| 111 | + * many fragments. |
| 112 | + * The "randomness" must be repeatable. |
| 113 | + */ |
| 114 | + prandom_seed_state(&rnd, 3141592653589793238ULL); |
| 115 | + total_len = 0; |
| 116 | + for (i = 0; i < 100; ++i) { |
| 117 | + offset = prandom_u32_state(&rnd) % (sizeof(line) - 1); |
| 118 | + string_stream_add(stream, "%s\n", &line[offset]); |
| 119 | + total_len += sizeof(line) - offset; |
| 120 | + } |
| 121 | + num_lines = i; |
| 122 | + |
| 123 | + concat_string = get_concatenated_string(test, stream); |
| 124 | + KUNIT_EXPECT_NOT_ERR_OR_NULL(test, concat_string); |
| 125 | + KUNIT_EXPECT_EQ(test, strlen(concat_string), total_len); |
| 126 | + |
| 127 | + /* |
| 128 | + * Split the concatenated string at the newlines and check that |
| 129 | + * all the original added strings are present. |
| 130 | + */ |
| 131 | + prandom_seed_state(&rnd, 3141592653589793238ULL); |
| 132 | + pos = concat_string; |
| 133 | + for (i = 0; i < num_lines; ++i) { |
| 134 | + string_end = strchr(pos, '\n'); |
| 135 | + KUNIT_EXPECT_NOT_NULL(test, string_end); |
| 136 | + |
| 137 | + /* Convert to NULL-terminated string */ |
| 138 | + *string_end = '\0'; |
| 139 | + |
| 140 | + offset = prandom_u32_state(&rnd) % (sizeof(line) - 1); |
| 141 | + KUNIT_EXPECT_STREQ(test, pos, &line[offset]); |
| 142 | + |
| 143 | + pos = string_end + 1; |
| 144 | + } |
| 145 | + |
| 146 | + /* There shouldn't be any more data after this */ |
| 147 | + KUNIT_EXPECT_EQ(test, strlen(pos), 0); |
| 148 | +} |
| 149 | + |
| 150 | +/* Appending the content of one string stream to another. */ |
| 151 | +static void string_stream_append_test(struct kunit *test) |
22 | 152 | {
|
23 |
| - struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); |
| 153 | + static const char * const strings_1[] = { |
| 154 | + "one", "two", "three", "four", "five", "six", |
| 155 | + "seven", "eight", "nine", "ten", |
| 156 | + }; |
| 157 | + static const char * const strings_2[] = { |
| 158 | + "Apple", "Pear", "Orange", "Banana", "Grape", "Apricot", |
| 159 | + }; |
| 160 | + struct string_stream *stream_1, *stream_2; |
| 161 | + const char *stream1_content_before_append, *stream_2_content; |
| 162 | + char *combined_content; |
| 163 | + size_t combined_length; |
| 164 | + int i; |
| 165 | + |
| 166 | + stream_1 = alloc_string_stream(test, GFP_KERNEL); |
| 167 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1); |
| 168 | + |
| 169 | + stream_2 = alloc_string_stream(test, GFP_KERNEL); |
| 170 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_2); |
| 171 | + |
| 172 | + /* Append content of empty stream to empty stream */ |
| 173 | + string_stream_append(stream_1, stream_2); |
| 174 | + KUNIT_EXPECT_EQ(test, strlen(get_concatenated_string(test, stream_1)), 0); |
24 | 175 |
|
25 |
| - string_stream_add(stream, "Foo"); |
| 176 | + /* Add some data to stream_1 */ |
| 177 | + for (i = 0; i < ARRAY_SIZE(strings_1); ++i) |
| 178 | + string_stream_add(stream_1, "%s\n", strings_1[i]); |
26 | 179 |
|
27 |
| - KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream)); |
| 180 | + stream1_content_before_append = get_concatenated_string(test, stream_1); |
| 181 | + |
| 182 | + /* Append content of empty stream to non-empty stream */ |
| 183 | + string_stream_append(stream_1, stream_2); |
| 184 | + KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), |
| 185 | + stream1_content_before_append); |
| 186 | + |
| 187 | + /* Add some data to stream_2 */ |
| 188 | + for (i = 0; i < ARRAY_SIZE(strings_2); ++i) |
| 189 | + string_stream_add(stream_2, "%s\n", strings_2[i]); |
| 190 | + |
| 191 | + /* Append content of non-empty stream to non-empty stream */ |
| 192 | + string_stream_append(stream_1, stream_2); |
| 193 | + |
| 194 | + /* |
| 195 | + * End result should be the original content of stream_1 plus |
| 196 | + * the content of stream_2. |
| 197 | + */ |
| 198 | + stream_2_content = get_concatenated_string(test, stream_2); |
| 199 | + combined_length = strlen(stream1_content_before_append) + strlen(stream_2_content); |
| 200 | + combined_length++; /* for terminating \0 */ |
| 201 | + combined_content = kunit_kmalloc(test, combined_length, GFP_KERNEL); |
| 202 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, combined_content); |
| 203 | + snprintf(combined_content, combined_length, "%s%s", |
| 204 | + stream1_content_before_append, stream_2_content); |
| 205 | + |
| 206 | + KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), combined_content); |
| 207 | + |
| 208 | + /* Append content of non-empty stream to empty stream */ |
| 209 | + string_stream_destroy(stream_1); |
| 210 | + |
| 211 | + stream_1 = alloc_string_stream(test, GFP_KERNEL); |
| 212 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream_1); |
| 213 | + |
| 214 | + string_stream_append(stream_1, stream_2); |
| 215 | + KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream_1), stream_2_content); |
28 | 216 | }
|
29 | 217 |
|
30 |
| -static void string_stream_test_get_string(struct kunit *test) |
| 218 | +/* Adding an empty string should not create a fragment. */ |
| 219 | +static void string_stream_append_empty_string_test(struct kunit *test) |
31 | 220 | {
|
32 |
| - struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL); |
33 |
| - char *output; |
| 221 | + struct string_stream *stream; |
| 222 | + int original_frag_count; |
| 223 | + |
| 224 | + stream = alloc_string_stream(test, GFP_KERNEL); |
| 225 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, stream); |
| 226 | + |
| 227 | + /* Formatted empty string */ |
| 228 | + string_stream_add(stream, "%s", ""); |
| 229 | + KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream)); |
| 230 | + KUNIT_EXPECT_TRUE(test, list_empty(&stream->fragments)); |
34 | 231 |
|
35 |
| - string_stream_add(stream, "Foo"); |
36 |
| - string_stream_add(stream, " %s", "bar"); |
| 232 | + /* Adding an empty string to a non-empty stream */ |
| 233 | + string_stream_add(stream, "Add this line"); |
| 234 | + original_frag_count = list_count_nodes(&stream->fragments); |
37 | 235 |
|
38 |
| - output = string_stream_get_string(stream); |
39 |
| - KUNIT_ASSERT_STREQ(test, output, "Foo bar"); |
| 236 | + string_stream_add(stream, "%s", ""); |
| 237 | + KUNIT_EXPECT_EQ(test, list_count_nodes(&stream->fragments), original_frag_count); |
| 238 | + KUNIT_EXPECT_STREQ(test, get_concatenated_string(test, stream), "Add this line"); |
40 | 239 | }
|
41 | 240 |
|
42 | 241 | 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), |
| 242 | + KUNIT_CASE(string_stream_init_test), |
| 243 | + KUNIT_CASE(string_stream_line_add_test), |
| 244 | + KUNIT_CASE(string_stream_variable_length_line_test), |
| 245 | + KUNIT_CASE(string_stream_append_test), |
| 246 | + KUNIT_CASE(string_stream_append_empty_string_test), |
46 | 247 | {}
|
47 | 248 | };
|
48 | 249 |
|
|
0 commit comments