coreutils / tests /nl /tests_for_check_section.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#include "../../unity/unity.h"
#include <string.h>
#include <stddef.h>
/* Unity hooks */
void setUp(void) {
/* No global setup needed; each test fully initializes the globals it uses. */
}
void tearDown(void) {
/* No teardown needed. */
}
/* Helper to configure default delimiters: section_del = "\:", and
header/body/footer delimiters as "\:h", "\:b", "\:f". */
static void configure_default_delims(void)
{
/* Use static storage so pointers remain valid during the test function. */
static char h[] = "\\:h";
static char b[] = "\\:b";
static char f[] = "\\:f";
/* section_del defaults to "\\:" defined in the program; use that object
rather than a literal to avoid constness issues. */
section_del = DEFAULT_SECTION_DELIMITERS;
header_del = h;
header_del_len = strlen(h);
body_del = b;
body_del_len = strlen(b);
footer_del = f;
footer_del_len = strlen(f);
}
/* Helper to set the global line buffer to a given C string s whose
contents include a trailing '\n'. The length is set to strlen(s),
consistent with the program's usage (length includes the newline). */
static void set_line_buf_str(const char *s)
{
line_buf.buffer = (char *) s; /* We will not modify s. */
line_buf.length = strlen(s);
}
/* Tests */
static void test_check_section_returns_Text_when_line_too_short(void)
{
configure_default_delims();
/* Only one character before newline => len = 1, should be Text. */
const char *line = "A\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Text, sec);
}
static void test_check_section_returns_Text_when_prefix_mismatch(void)
{
configure_default_delims();
/* Does not start with the section_del ("\\:"), so must be Text. */
const char *line = "xyh\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Text, sec);
}
static void test_check_section_detects_Header(void)
{
configure_default_delims();
/* Matches "\\:h" exactly (before newline). */
const char *line = "\\:h\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Header, sec);
}
static void test_check_section_detects_Body(void)
{
configure_default_delims();
/* Matches "\\:b" exactly (before newline). */
const char *line = "\\:b\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Body, sec);
}
static void test_check_section_detects_Footer(void)
{
configure_default_delims();
/* Matches "\\:f" exactly (before newline). */
const char *line = "\\:f\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Footer, sec);
}
static void test_check_section_only_prefix_without_type_is_Text(void)
{
configure_default_delims();
/* Line equals just the two-character prefix before newline; should be Text
since len (2) != any of the *_del_len (3). */
const char *line = "\\:\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Text, sec);
}
static void test_check_section_disabled_by_empty_delimiter_returns_Text(void)
{
configure_default_delims();
/* Disable section matching by setting footer_del_len < 2, as per code path. */
footer_del_len = 0;
/* Even though the line would otherwise be a Header marker, it must be Text. */
const char *line = "\\:h\n";
set_line_buf_str(line);
enum section sec = check_section();
TEST_ASSERT_EQUAL_INT(Text, sec);
}
/* Unity main */
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_check_section_returns_Text_when_line_too_short);
RUN_TEST(test_check_section_returns_Text_when_prefix_mismatch);
RUN_TEST(test_check_section_detects_Header);
RUN_TEST(test_check_section_detects_Body);
RUN_TEST(test_check_section_detects_Footer);
RUN_TEST(test_check_section_only_prefix_without_type_is_Text);
RUN_TEST(test_check_section_disabled_by_empty_delimiter_returns_Text);
return UNITY_END();
}