for (i = 0; i < strlen(test_string) + 1; i++) {
result = strchr(test_string, test_string[i]);
KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, "char:%c", 'a' + i);
}
result = strchr(empty_string, '\0');
KUNIT_ASSERT_PTR_EQ(test, result, empty_string);
result = strchr(empty_string, 'a');
KUNIT_ASSERT_NULL(test, result);
result = strchr(test_string, 'z');
KUNIT_ASSERT_NULL(test, result);
}
staticvoid string_test_strnchr(struct kunit *test)
{ constchar *test_string = "abcdefghijkl"; constchar *empty_string = ""; char *result; int i, j;
for (i = 0; i < strlen(test_string) + 1; i++) { for (j = 0; j < strlen(test_string) + 2; j++) {
result = strnchr(test_string, j, test_string[i]); if (j <= i) {
KUNIT_ASSERT_NULL_MSG(test, result, "char:%c i:%d j:%d", 'a' + i, i, j);
} else {
KUNIT_ASSERT_EQ_MSG(test, result - test_string, i, "char:%c i:%d j:%d", 'a' + i, i, j);
}
}
}
result = strnchr(empty_string, 0, '\0');
KUNIT_ASSERT_NULL(test, result);
result = strnchr(empty_string, 1, '\0');
KUNIT_ASSERT_PTR_EQ(test, result, empty_string);
result = strnchr(empty_string, 1, 'a');
KUNIT_ASSERT_NULL(test, result);
result = strnchr(NULL, 0, '\0');
KUNIT_ASSERT_NULL(test, result);
}
staticvoid string_test_strcmp(struct kunit *test)
{ /* Equal strings */
STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!"); /* First string is lexicographically less than the second */
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!"); /* First string is lexicographically larger than the second */
STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!"); /* Empty string is always lexicographically less than any non-empty string */
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string"); /* Two empty strings should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", ""); /* Compare two strings which have only one char difference */
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba"); /* Compare two strings which have the same prefix*/
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else");
}
staticvoid string_test_strncmp(struct kunit *test)
{ /* Equal strings */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13); /* First string is lexicographically less than the second */
STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13); /* Result is always 'equal' when count = 0 */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0); /* Strings with common prefix are equal if count = length of prefix */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3); /* Strings with common prefix are not equal when count = length of prefix + 1 */
STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4); /* If one string is a prefix of another, the shorter string is lexicographically smaller */
STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else",
strlen("Just a string and something else")); /* * If one string is a prefix of another, and we check first length * of prefix chars, the result is 'equal'
*/
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else",
strlen("Just a string"));
}
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN); /* the strings are equal up to STRCMP_CHANGE_POINT */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_CHANGE_POINT);
STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_CHANGE_POINT + 1);
}
staticvoid string_test_strcasecmp(struct kunit *test)
{ /* Same strings in different case should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!"); /* Empty strings should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", ""); /* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */
STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B");
STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a"); /* Special symbols and numbers should be processed correctly */
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^");
}
/** * strscpy_check() - Run a specific test case. * @test: KUnit test context pointer * @src: Source string, argument to strscpy_pad() * @count: Size of destination buffer, argument to strscpy_pad() * @expected: Expected return value from call to strscpy_pad() * @chars: Number of characters from the src string expected to be * written to the dst buffer. * @terminator: 1 if there should be a terminating null byte 0 otherwise. * @pad: Number of pad characters expected (in the tail of dst buffer). * (@pad does not include the null terminator byte.) * * Calls strscpy_pad() and verifies the return value and state of the * destination buffer after the call returns.
*/ staticvoid strscpy_check(struct kunit *test, char *src, int count, int expected, int chars, int terminator, int pad)
{ int nr_bytes_poison; int max_expected; int max_count; int written; char buf[6]; int index, i; constchar POISON = 'z';
KUNIT_ASSERT_TRUE_MSG(test, src != NULL, "null source string not supported");
memset(buf, POISON, sizeof(buf)); /* Future proofing test suite, validate args */
max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
max_expected = count - 1; /* Space for the null */
KUNIT_ASSERT_LE_MSG(test, count, max_count, "count (%d) is too big (%d) ... aborting", count, max_count);
KUNIT_EXPECT_LE_MSG(test, expected, max_expected, "expected (%d) is bigger than can possibly be returned (%d)",
expected, max_expected);
written = strscpy_pad(buf, src, count);
KUNIT_ASSERT_EQ(test, written, expected);
if (count && written == -E2BIG) {
KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1), "buffer state invalid for -E2BIG");
KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0', "too big string is not null terminated correctly");
}
for (i = 0; i < chars; i++)
KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i], "buf[i]==%c != src[i]==%c", buf[i], src[i]);
if (terminator)
KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0', "string is not null terminated correctly");
for (i = 0; i < pad; i++) {
index = chars + terminator + i;
KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0', "padding missing at index: %d", i);
}
nr_bytes_poison = sizeof(buf) - chars - terminator - pad; for (i = 0; i < nr_bytes_poison; i++) {
index = sizeof(buf) - 1 - i; /* Check from the end back */
KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON, "poison value missing at index: %d", i);
}
}
/* * strscpy_check() uses a destination buffer of size 6 and needs at * least 2 characters spare (one for null and one to check for * overflow). This means we should only call tc() with * strings up to a maximum of 4 characters long and 'count' * should not exceed 4. To test with longer strings increase * the buffer size in tc().
*/
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.