zlib / tests /tests_gzclose_gzclose.c
AryaWu's picture
Upload folder using huggingface_hub
e996a55 verified
#include "unity/unity.h"
#include "zlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
/* Unity hooks */
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Helper: generate a temporary gzip file path */
static void gen_temp_gz_path(char *out, size_t siz) {
if (out == NULL || siz == 0) return;
/* Try tmpnam into a buffer, then append .gz */
char base[L_tmpnam];
out[0] = '\0';
if (tmpnam(base) != NULL) {
/* Append .gz to the generated name */
(void)snprintf(out, siz, "%s.gz", base);
return;
}
/* Fallback: generate a name in current directory */
long t = (long)time(NULL);
unsigned r = (unsigned)rand();
(void)snprintf(out, siz, "gzclose_test_%ld_%u.gz", t, r);
}
/* Test: NULL pointer should return Z_STREAM_ERROR */
void test_gzclose_null_pointer_returns_stream_error(void) {
int ret = gzclose((gzFile)NULL);
TEST_ASSERT_EQUAL_INT(Z_STREAM_ERROR, ret);
}
/* Test: write mode path — close after writing should return Z_OK and create a non-empty file */
void test_gzclose_write_mode_returns_ok_and_creates_file(void) {
char path[L_tmpnam + 32];
gen_temp_gz_path(path, sizeof(path));
gzFile f = gzopen(path, "wb");
if (f == NULL) {
TEST_IGNORE_MESSAGE("gzopen(\"wb\") not supported in this build (NO_GZCOMPRESS?)");
return;
}
const char *msg = "Hello gzclose write-mode test";
int w = gzwrite(f, msg, (unsigned)strlen(msg));
TEST_ASSERT_TRUE_MESSAGE(w > 0, "gzwrite failed");
int ret = gzclose(f);
TEST_ASSERT_EQUAL_INT(Z_OK, ret);
/* Verify file exists and is non-empty */
FILE *fp = fopen(path, "rb");
TEST_ASSERT_NOT_NULL_MESSAGE(fp, "Closed gzip file not found on disk");
if (fp) {
if (fseek(fp, 0, SEEK_END) == 0) {
long sz = ftell(fp);
TEST_ASSERT_TRUE_MESSAGE(sz > 0, "Gzip file size should be > 0 after write and close");
} else {
TEST_FAIL_MESSAGE("fseek failed when verifying file size");
}
fclose(fp);
}
remove(path);
}
/* Test: read mode path — open an existing gzip file for reading and close should return Z_OK */
void test_gzclose_read_mode_returns_ok(void) {
char path[L_tmpnam + 32];
gen_temp_gz_path(path, sizeof(path));
/* Create a gzip file to read */
gzFile fw = gzopen(path, "wb");
if (fw == NULL) {
TEST_IGNORE_MESSAGE("gzopen(\"wb\") not supported; cannot set up file for read-mode test");
return;
}
const char *msg = "Data for read-mode gzclose test";
int w = gzwrite(fw, msg, (unsigned)strlen(msg));
TEST_ASSERT_TRUE_MESSAGE(w > 0, "gzwrite failed in setup for read-mode test");
TEST_ASSERT_EQUAL_INT(Z_OK, gzclose(fw));
/* Open for reading and close via gzclose (should dispatch to gzclose_r) */
gzFile fr = gzopen(path, "rb");
TEST_ASSERT_NOT_NULL_MESSAGE(fr, "gzopen(\"rb\") failed on created gzip file");
int ret = gzclose(fr);
TEST_ASSERT_EQUAL_INT(Z_OK, ret);
remove(path);
}
/* Test: write mode close without writing any data should still return Z_OK */
void test_gzclose_write_mode_without_writing_returns_ok(void) {
char path[L_tmpnam + 32];
gen_temp_gz_path(path, sizeof(path));
gzFile f = gzopen(path, "wb");
if (f == NULL) {
TEST_IGNORE_MESSAGE("gzopen(\"wb\") not supported in this build (NO_GZCOMPRESS?)");
return;
}
/* Close immediately without writing */
int ret = gzclose(f);
TEST_ASSERT_EQUAL_INT(Z_OK, ret);
/* File may or may not be present, but if present it should be at least a valid gzip stream header */
FILE *fp = fopen(path, "rb");
if (fp) {
/* Not asserting size here since an empty stream is acceptable; just clean up */
fclose(fp);
remove(path);
} else {
/* If not present, that's acceptable; nothing to remove */
}
}
int main(void) {
srand((unsigned)time(NULL));
UNITY_BEGIN();
RUN_TEST(test_gzclose_null_pointer_returns_stream_error);
RUN_TEST(test_gzclose_write_mode_returns_ok_and_creates_file);
RUN_TEST(test_gzclose_read_mode_returns_ok);
RUN_TEST(test_gzclose_write_mode_without_writing_returns_ok);
return UNITY_END();
}