zlib / tests /tests_gzlib_gz_intmax.c
AryaWu's picture
Upload folder using huggingface_hub
e996a55 verified
#include "unity/unity.h"
#include "zlib.h"
#include <limits.h>
#include <stdint.h>
/* Forward declaration of the internal function under test. */
unsigned gz_intmax(void);
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Helper to compute expected INT_MAX using bit width (portable across int models). */
static unsigned expected_intmax_by_bits(void) {
/* Compute 2^(sizeof(int)*CHAR_BIT - 1) - 1 in a wide type, then cast. */
unsigned bits = (unsigned)(sizeof(int) * CHAR_BIT);
/* bits is at least 16 by the C standard */
unsigned long long one = 1ULL;
unsigned long long max_val = (one << (bits - 1)) - 1ULL;
return (unsigned)max_val;
}
void test_gz_intmax_equals_INT_MAX(void) {
unsigned got = gz_intmax();
TEST_ASSERT_EQUAL_UINT((unsigned)INT_MAX, got);
}
void test_gz_intmax_matches_bit_calculation(void) {
unsigned got = gz_intmax();
unsigned expected = expected_intmax_by_bits();
TEST_ASSERT_EQUAL_UINT(expected, got);
}
void test_gz_intmax_is_consistent_across_calls(void) {
unsigned a = gz_intmax();
unsigned b = gz_intmax();
unsigned c = gz_intmax();
TEST_ASSERT_EQUAL_UINT(a, b);
TEST_ASSERT_EQUAL_UINT(b, c);
}
void test_gz_intmax_basic_properties(void) {
unsigned v = gz_intmax();
/* It should be positive and not exceed UINT_MAX. */
TEST_ASSERT_TRUE(v > 0);
TEST_ASSERT_TRUE(v <= (unsigned)UINT_MAX);
/* It should not be equal to UINT_MAX (since INT_MAX < UINT_MAX on all sane platforms). */
TEST_ASSERT_TRUE(v != (unsigned)UINT_MAX);
/* Adding 1 should increase the value without wrapping. */
unsigned v_plus_1 = v + 1u;
TEST_ASSERT_TRUE(v_plus_1 > v);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_gz_intmax_equals_INT_MAX);
RUN_TEST(test_gz_intmax_matches_bit_calculation);
RUN_TEST(test_gz_intmax_is_consistent_across_calls);
RUN_TEST(test_gz_intmax_basic_properties);
return UNITY_END();
}