File size: 2,898 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 |
#include "../../unity/unity.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
/* Unity hooks */
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* The target function 'max_out' is defined earlier in the same translation unit
and available here due to source inclusion order. We do not redeclare it. */
static void test_max_out_simple_decimal(void) {
char fmt[] = "%d";
int expected = snprintf(NULL, 0, fmt, INT_MAX);
idx_t got = max_out(fmt);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("%d", fmt); /* no mutation expected */
}
static void test_max_out_mutates_u_to_d_and_length_correct(void) {
char fmt_mutable[] = "%u";
/* Expected uses %d, since max_out should mutate %u to %d */
const char expected_fmt[] = "%d";
int expected = snprintf(NULL, 0, expected_fmt, INT_MAX);
idx_t got = max_out(fmt_mutable);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("%d", fmt_mutable); /* verify mutation */
}
static void test_max_out_with_width_and_zero_flag(void) {
char fmt[] = "%020d"; /* zero-padded width 20 */
int expected = snprintf(NULL, 0, fmt, INT_MAX);
idx_t got = max_out(fmt);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("%020d", fmt); /* format should remain unchanged */
}
static void test_max_out_with_precision_and_text(void) {
char fmt[] = "abc%.12dXYZ";
int expected = snprintf(NULL, 0, fmt, INT_MAX);
idx_t got = max_out(fmt);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("abc%.12dXYZ", fmt);
}
static void test_max_out_with_literal_percent_and_padding(void) {
char fmt[] = "%%--%08d--%%";
int expected = snprintf(NULL, 0, fmt, INT_MAX);
idx_t got = max_out(fmt);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("%%--%08d--%%", fmt);
}
static void test_max_out_i_specifier_behaves_like_d(void) {
char fmt[] = "%i";
int expected = snprintf(NULL, 0, fmt, INT_MAX);
idx_t got = max_out(fmt);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("%i", fmt);
}
static void test_max_out_with_left_align_and_text_suffix(void) {
char fmt[] = "%-10d_end";
int expected = snprintf(NULL, 0, fmt, INT_MAX);
idx_t got = max_out(fmt);
TEST_ASSERT_EQUAL_INT(expected, (int)got);
TEST_ASSERT_EQUAL_STRING("%-10d_end", fmt);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_max_out_simple_decimal);
RUN_TEST(test_max_out_mutates_u_to_d_and_length_correct);
RUN_TEST(test_max_out_with_width_and_zero_flag);
RUN_TEST(test_max_out_with_precision_and_text);
RUN_TEST(test_max_out_with_literal_percent_and_padding);
RUN_TEST(test_max_out_i_specifier_behaves_like_d);
RUN_TEST(test_max_out_with_left_align_and_text_suffix);
return UNITY_END();
} |