coreutils / tests /echo /tests_for_hextobin.c
AryaWu's picture
Upload folder using huggingface_hub
78d2150 verified
#include "../../unity/unity.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
/* Unity required hooks */
void setUp(void) {
/* No setup needed */
}
void tearDown(void) {
/* No teardown needed */
}
/* Tests for static int hextobin(unsigned char c) */
void test_hextobin_digits_0_through_9(void) {
for (int ic = '0'; ic <= '9'; ic++) {
unsigned char c = (unsigned char) ic;
int got = hextobin(c);
int expected = ic - '0';
TEST_ASSERT_EQUAL_INT(expected, got);
}
}
void test_hextobin_lowercase_a_through_f(void) {
for (int ic = 'a'; ic <= 'f'; ic++) {
unsigned char c = (unsigned char) ic;
int got = hextobin(c);
int expected = 10 + (ic - 'a');
TEST_ASSERT_EQUAL_INT(expected, got);
}
}
void test_hextobin_uppercase_A_through_F(void) {
for (int ic = 'A'; ic <= 'F'; ic++) {
unsigned char c = (unsigned char) ic;
int got = hextobin(c);
int expected = 10 + (ic - 'A');
TEST_ASSERT_EQUAL_INT(expected, got);
}
}
void test_hextobin_non_hex_characters_return_c_minus_zero(void) {
/* Characters that are not hex digits should fall through to default: c - '0' */
const unsigned char samples[] = {
(unsigned char) '/', /* 47 -> 47 - '0' (likely -1 in ASCII) */
(unsigned char) 'g', /* -> 'g' - '0' */
(unsigned char) 'G', /* -> 'G' - '0' */
(unsigned char) ' ', /* space -> ' ' - '0' (likely negative) */
(unsigned char) ':', /* 58 -> ':' - '0' = 10 in ASCII (just above '9') */
(unsigned char) '\n' /* newline */
};
size_t n = sizeof(samples)/sizeof(samples[0]);
for (size_t i = 0; i < n; i++) {
unsigned char c = samples[i];
int got = hextobin(c);
int expected = (int)c - '0';
TEST_ASSERT_EQUAL_INT(expected, got);
}
}
void test_hextobin_boundary_like_values(void) {
/* NUL character */
{
unsigned char c = (unsigned char) '\0';
int got = hextobin(c);
int expected = (int)c - '0';
TEST_ASSERT_EQUAL_INT(expected, got);
}
/* 0xFF (255) as unsigned char */
{
unsigned char c = (unsigned char) 0xFF;
int got = hextobin(c);
int expected = (int)c - '0';
TEST_ASSERT_EQUAL_INT(expected, got);
}
/* Check exact edges for hex letters */
{
unsigned char c1 = (unsigned char) 'a';
unsigned char c2 = (unsigned char) 'f';
unsigned char c3 = (unsigned char) 'A';
unsigned char c4 = (unsigned char) 'F';
TEST_ASSERT_EQUAL_INT(10, hextobin(c1));
TEST_ASSERT_EQUAL_INT(15, hextobin(c2));
TEST_ASSERT_EQUAL_INT(10, hextobin(c3));
TEST_ASSERT_EQUAL_INT(15, hextobin(c4));
}
}
/* Unity test runner */
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_hextobin_digits_0_through_9);
RUN_TEST(test_hextobin_lowercase_a_through_f);
RUN_TEST(test_hextobin_uppercase_A_through_F);
RUN_TEST(test_hextobin_non_hex_characters_return_c_minus_zero);
RUN_TEST(test_hextobin_boundary_like_values);
return UNITY_END();
}