coreutils / tests /dd /tests_for_parse_symbols.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
/* Unity required functions */
void setUp(void) {
/* No setup needed for these tests */
}
void tearDown(void) {
/* No teardown needed for these tests */
}
/*
Target under test (declared static in dd.c). Since this test file is
#included into dd.c, we can call it directly.
static int parse_symbols (char const *str, struct symbol_value const *table,
bool exclusive, char const *error_msgid);
We also use the static symbol tables and constants from dd.c:
- conversions[] with C_* constants
- statuses[] with STATUS_* constants
- flags[] with O_* constants
*/
/* Basic: single symbol from conversions table */
void test_parse_symbols_single_swab(void) {
int val = parse_symbols("swab", conversions, false, "invalid conversion");
/* swab has C_SWAB | C_TWOBUFS */
TEST_ASSERT_EQUAL_INT(C_SWAB | C_TWOBUFS, val);
}
/* Multiple symbols OR-combination from conversions table */
void test_parse_symbols_multiple_or(void) {
int val = parse_symbols("swab,sync", conversions, false, "invalid conversion");
int expected = (C_SWAB | C_TWOBUFS) | C_SYNC;
TEST_ASSERT_EQUAL_INT(expected, val);
}
/* Duplicate tokens should not change the result beyond the bit once */
void test_parse_symbols_duplicate_tokens(void) {
int val = parse_symbols("ucase,ucase", conversions, false, "invalid conversion");
int expected = C_UCASE | C_TWOBUFS;
TEST_ASSERT_EQUAL_INT(expected, val);
}
/* Exclusive mode: last value wins (statuses table) */
void test_parse_symbols_exclusive_last_wins_progress(void) {
int val = parse_symbols("none,progress", statuses, true, "invalid status");
TEST_ASSERT_EQUAL_INT(STATUS_PROGRESS, val);
}
void test_parse_symbols_exclusive_last_wins_none(void) {
int val = parse_symbols("progress,none", statuses, true, "invalid status");
TEST_ASSERT_EQUAL_INT(STATUS_NONE, val);
}
/* Mixed conversions: ensure expected OR of values including C_TWOBUFS
for entries that specify it and not for those that don't. */
void test_parse_symbols_ascii_ucase_sparse(void) {
int val = parse_symbols("ascii,ucase,sparse", conversions, false, "invalid conversion");
int expected = 0;
expected |= C_ASCII; /* from ascii */
expected |= C_UNBLOCK; /* from ascii */
expected |= C_TWOBUFS; /* ascii has C_TWOBUFS */
expected |= C_UCASE; /* ucase */
expected |= C_TWOBUFS; /* ucase also has C_TWOBUFS (already set) */
expected |= C_SPARSE; /* sparse has no C_TWOBUFS */
TEST_ASSERT_EQUAL_INT(expected, val);
}
/* Use the flags table to ensure O_* flags combine as expected */
void test_parse_symbols_flags_combination(void) {
int val = parse_symbols("skip_bytes,seek_bytes", flags, false, "invalid flag");
int expected = O_SKIP_BYTES | O_SEEK_BYTES;
TEST_ASSERT_EQUAL_INT(expected, val);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_parse_symbols_single_swab);
RUN_TEST(test_parse_symbols_multiple_or);
RUN_TEST(test_parse_symbols_duplicate_tokens);
RUN_TEST(test_parse_symbols_exclusive_last_wins_progress);
RUN_TEST(test_parse_symbols_exclusive_last_wins_none);
RUN_TEST(test_parse_symbols_ascii_ucase_sparse);
RUN_TEST(test_parse_symbols_flags_combination);
return UNITY_END();
}