|
|
#include "../../unity/unity.h" |
|
|
#include <stdio.h> |
|
|
#include <string.h> |
|
|
#include <stdlib.h> |
|
|
#include <limits.h> |
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
static void test_max_out_mutates_u_to_d_and_length_correct(void) { |
|
|
char fmt_mutable[] = "%u"; |
|
|
|
|
|
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); |
|
|
} |
|
|
|
|
|
static void test_max_out_with_width_and_zero_flag(void) { |
|
|
char fmt[] = "%020d"; |
|
|
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); |
|
|
} |
|
|
|
|
|
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(); |
|
|
} |