File size: 5,110 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#include "../../unity/unity.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
/* base2msbf_encode is defined in the program source and included before this file. */
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Helper: manually encode a single byte to MSB-first bit string */
static void byte_to_bits_msb(unsigned char c, char out[9]) {
for (int i = 0; i < 8; i++) {
out[i] = (c & 0x80) ? '1' : '0';
c <<= 1;
}
out[8] = '\0';
}
/* Helper: encode a buffer using the reference helper above */
static char *bytes_to_bits_msb(const unsigned char *in, size_t inlen) {
size_t outlen = inlen * 8;
char *res = (char *)malloc(outlen + 1);
TEST_ASSERT_NOT_NULL(res);
char *p = res;
for (size_t i = 0; i < inlen; i++) {
char tmp[9];
byte_to_bits_msb(in[i], tmp);
memcpy(p, tmp, 8);
p += 8;
}
res[outlen] = '\0';
return res;
}
void test_base2msbf_encode_empty_input_no_output(void) {
char outbuf[16];
/* prefill with sentinel */
for (size_t i = 0; i < sizeof(outbuf); i++) outbuf[i] = 'X';
const char inbuf[] = ""; /* inlen = 0 */
base2msbf_encode(inbuf, (idx_t)0, outbuf, (idx_t)8);
/* No output should be written; buffer remains unchanged */
for (size_t i = 0; i < sizeof(outbuf); i++) {
TEST_ASSERT_EQUAL_CHAR('X', outbuf[i]);
}
}
void test_base2msbf_encode_single_byte_zero(void) {
unsigned char inbuf[1] = {0x00};
char outbuf[8 + 1];
memset(outbuf, 0, sizeof(outbuf));
base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8);
outbuf[8] = '\0';
TEST_ASSERT_EQUAL_STRING("00000000", outbuf);
}
void test_base2msbf_encode_single_byte_ff(void) {
unsigned char inbuf[1] = {0xFF};
char outbuf[8 + 1];
memset(outbuf, 0, sizeof(outbuf));
base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8);
outbuf[8] = '\0';
TEST_ASSERT_EQUAL_STRING("11111111", outbuf);
}
void test_base2msbf_encode_single_byte_a5(void) {
unsigned char inbuf[1] = {0xA5}; /* 10100101 */
char outbuf[8 + 1];
memset(outbuf, 0, sizeof(outbuf));
base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8);
outbuf[8] = '\0';
TEST_ASSERT_EQUAL_STRING("10100101", outbuf);
}
void test_base2msbf_encode_single_byte_80(void) {
unsigned char inbuf[1] = {0x80}; /* 10000000 */
char outbuf[8 + 1];
memset(outbuf, 0, sizeof(outbuf));
base2msbf_encode((const char *)inbuf, (idx_t)1, outbuf, (idx_t)8);
outbuf[8] = '\0';
TEST_ASSERT_EQUAL_STRING("10000000", outbuf);
}
void test_base2msbf_encode_two_bytes_Hi(void) {
const unsigned char inbuf[2] = {'H', 'i'}; /* 0x48, 0x69 */
/* Expected: 0x48 -> 01001000, 0x69 -> 01101001 */
const char *expected = "0100100001101001";
char outbuf[16 + 1];
memset(outbuf, 0, sizeof(outbuf));
base2msbf_encode((const char *)inbuf, (idx_t)2, outbuf, (idx_t)16);
outbuf[16] = '\0';
TEST_ASSERT_EQUAL_STRING(expected, outbuf);
}
void test_base2msbf_encode_truncate_outlen_multiple_of_8(void) {
const unsigned char inbuf[3] = {'A', 'B', 'C'}; /* 0x41, 0x42, 0x43 */
/* Expect only first two bytes encoded because outlen==16 */
const char *expected_first2 = "0100000101000010"; /* 'A' then 'B' */
char outbuf[16 + 2];
memset(outbuf, 0, sizeof(outbuf));
outbuf[16] = 'Z'; /* sentinel */
outbuf[17] = '\0';
base2msbf_encode((const char *)inbuf, (idx_t)3, outbuf, (idx_t)16);
outbuf[16] = '\0';
TEST_ASSERT_EQUAL_STRING(expected_first2, outbuf);
/* Ensure sentinel not overwritten beyond outlen */
TEST_ASSERT_EQUAL_CHAR('Z', outbuf[16]);
}
void test_base2msbf_encode_outlen_zero_no_write(void) {
const unsigned char inbuf[2] = {0x01, 0xFF};
char outbuf[4];
for (size_t i = 0; i < sizeof(outbuf); i++) outbuf[i] = 'Q';
base2msbf_encode((const char *)inbuf, (idx_t)2, outbuf, (idx_t)0);
for (size_t i = 0; i < sizeof(outbuf); i++) {
TEST_ASSERT_EQUAL_CHAR('Q', outbuf[i]);
}
}
void test_base2msbf_encode_reference_compare_randomish_pattern(void) {
const unsigned char inbuf[] = {0x00, 0xFF, 0x12, 0xAB, 0x7E, 0x80, 0x01, 0xC3};
size_t inlen = sizeof(inbuf);
size_t outlen = inlen * 8;
char *expected = bytes_to_bits_msb(inbuf, inlen);
char *outbuf = (char *)malloc(outlen + 1);
TEST_ASSERT_NOT_NULL(outbuf);
memset(outbuf, 0, outlen + 1);
base2msbf_encode((const char *)inbuf, (idx_t)inlen, outbuf, (idx_t)outlen);
outbuf[outlen] = '\0';
TEST_ASSERT_EQUAL_STRING(expected, outbuf);
free(expected);
free(outbuf);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_base2msbf_encode_empty_input_no_output);
RUN_TEST(test_base2msbf_encode_single_byte_zero);
RUN_TEST(test_base2msbf_encode_single_byte_ff);
RUN_TEST(test_base2msbf_encode_single_byte_a5);
RUN_TEST(test_base2msbf_encode_single_byte_80);
RUN_TEST(test_base2msbf_encode_two_bytes_Hi);
RUN_TEST(test_base2msbf_encode_truncate_outlen_multiple_of_8);
RUN_TEST(test_base2msbf_encode_outlen_zero_no_write);
RUN_TEST(test_base2msbf_encode_reference_compare_randomish_pattern);
return UNITY_END();
} |