File size: 2,933 Bytes
78d2150 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#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();
} |