coreutils / tests /fmt /tests_for_set_prefix.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#include "../../unity/unity.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* setUp and tearDown required by Unity */
void setUp(void) {
/* Nothing to set up */
}
void tearDown(void) {
/* Nothing to tear down */
}
/* Tests */
void test_set_prefix_no_spaces(void) {
char buf[] = "abc";
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);
TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
TEST_ASSERT_EQUAL_INT(3, prefix_full_length); /* strlen("abc") */
TEST_ASSERT_EQUAL_INT(3, prefix_length);
TEST_ASSERT_EQUAL_STRING("abc", prefix);
}
void test_set_prefix_only_spaces(void) {
char buf[] = " "; /* four spaces */
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(4, prefix_lead_space);
/* prefix should point at the terminating NUL within buf */
TEST_ASSERT_EQUAL_PTR(&buf[4], prefix);
TEST_ASSERT_EQUAL_INT(0, prefix_full_length);
TEST_ASSERT_EQUAL_INT(0, prefix_length);
TEST_ASSERT_EQUAL_STRING("", prefix);
}
void test_set_prefix_leading_spaces_only(void) {
char buf[] = " abc";
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(3, prefix_lead_space);
TEST_ASSERT_EQUAL_PTR(&buf[3], prefix);
TEST_ASSERT_EQUAL_INT(3, prefix_full_length); /* "abc" + no trailing spaces yet considered */
TEST_ASSERT_EQUAL_INT(3, prefix_length);
TEST_ASSERT_EQUAL_STRING("abc", prefix);
}
void test_set_prefix_trailing_spaces_only(void) {
char buf[] = "abc "; /* three trailing spaces */
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);
TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
TEST_ASSERT_EQUAL_INT(6, prefix_full_length); /* strlen("abc ") */
TEST_ASSERT_EQUAL_INT(3, prefix_length);
/* Ensure trailing spaces were nulled just after 'c' */
TEST_ASSERT_EQUAL_CHAR('a', buf[0]);
TEST_ASSERT_EQUAL_CHAR('b', buf[1]);
TEST_ASSERT_EQUAL_CHAR('c', buf[2]);
TEST_ASSERT_EQUAL_INT('\0', buf[3]);
/* The bytes after the new NUL remain whatever they were; commonly spaces */
TEST_ASSERT_EQUAL_STRING("abc", prefix);
}
void test_set_prefix_leading_and_trailing_spaces(void) {
char buf[] = " abc ";
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(2, prefix_lead_space);
TEST_ASSERT_EQUAL_PTR(&buf[2], prefix);
TEST_ASSERT_EQUAL_INT(5, prefix_full_length); /* strlen("abc ") */
TEST_ASSERT_EQUAL_INT(3, prefix_length);
/* After trimming trailing spaces, NUL should be placed at buf[5] */
TEST_ASSERT_EQUAL_CHAR('a', buf[2]);
TEST_ASSERT_EQUAL_CHAR('b', buf[3]);
TEST_ASSERT_EQUAL_CHAR('c', buf[4]);
TEST_ASSERT_EQUAL_INT('\0', buf[5]);
TEST_ASSERT_EQUAL_STRING("abc", prefix);
}
void test_set_prefix_empty_string(void) {
char buf[] = "";
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(0, prefix_lead_space);
TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
TEST_ASSERT_EQUAL_INT(0, prefix_full_length);
TEST_ASSERT_EQUAL_INT(0, prefix_length);
TEST_ASSERT_EQUAL_STRING("", prefix);
}
void test_set_prefix_tabs_not_trimmed(void) {
/* Leading tab and an internal tab; only the trailing space should be trimmed */
char buf[] = "\tabc\t ";
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(0, prefix_lead_space); /* tabs are not counted as spaces */
TEST_ASSERT_EQUAL_PTR(&buf[0], prefix);
TEST_ASSERT_EQUAL_INT(6, prefix_full_length); /* strlen("\tabc\t ") */
TEST_ASSERT_EQUAL_INT(5, prefix_length); /* after trimming the final ' ' only */
/* After trimming, the string should be "\tabc\t" */
TEST_ASSERT_EQUAL_STRING("\tabc\t", prefix);
/* Ensure the NUL was written over the final space */
TEST_ASSERT_EQUAL_INT('\0', buf[5]);
}
void test_set_prefix_internal_spaces_preserved(void) {
char buf[] = " ab c ";
set_prefix(buf);
TEST_ASSERT_EQUAL_INT(2, prefix_lead_space);
TEST_ASSERT_EQUAL_PTR(&buf[2], prefix);
TEST_ASSERT_EQUAL_INT(6, prefix_full_length); /* strlen("ab c ") */
TEST_ASSERT_EQUAL_INT(4, prefix_length); /* "ab c" */
TEST_ASSERT_EQUAL_STRING("ab c", prefix);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_set_prefix_no_spaces);
RUN_TEST(test_set_prefix_only_spaces);
RUN_TEST(test_set_prefix_leading_spaces_only);
RUN_TEST(test_set_prefix_trailing_spaces_only);
RUN_TEST(test_set_prefix_leading_and_trailing_spaces);
RUN_TEST(test_set_prefix_empty_string);
RUN_TEST(test_set_prefix_tabs_not_trimmed);
RUN_TEST(test_set_prefix_internal_spaces_preserved);
return UNITY_END();
}