<commit_before>#include "test.h"
#include "../src/alloc.h"
#include "../src/fzp.h"

static const char *file="utest_fzp";
static const char *content="0123456789abcdefg";

static void tear_down(void)
{
	alloc_check();
	unlink(file);
}

static void setup_for_read(
	struct fzp *(*open_func)(const char *, const char *),
	const char *to_write)
{
	struct fzp *fzp;
	size_t len=strlen(to_write);
	unlink(file);
	fail_unless((fzp=open_func(file, "wb"))!=NULL);
	fail_unless(fzp_write(fzp, to_write, len)==len);
	fail_unless(!fzp_flush(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);
}

struct rdata
{
	size_t want;
	size_t got;
	const char *expected_str;
};

static struct rdata rd[] = {
	{ 0,   0, "" },
	{ 3,   3, "012" },
	{ 16, 16, "0123456789abcdef" },
	{ 20, 17, "0123456789abcdefg" },
};

static void read_checks(
	struct fzp *(*open_func)(const char *, const char *),
	struct rdata *d)
{
	char buf[32]="";
	struct fzp *fzp;

	fail_unless((fzp=open_func(file, "rb"))!=NULL);
	fail_unless(fzp_read(fzp, buf, d->want)==(int)d->got);
	if(d->want > d->got)
		fail_unless(fzp_eof(fzp));
	else
		fail_unless(!fzp_eof(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);

	ck_assert_str_eq(d->expected_str, buf);
}

static void do_read_tests(
	struct fzp *(*open_func)(const char *, const char *))
{
	setup_for_read(open_func, content);
	FOREACH(rd) read_checks(open_func, &rd[i]);
	tear_down();
};

struct sdata
{
	off_t pos;
};

static struct sdata sd[] = {
	{ 0 },
	{ 5 },
	{ 10 },
	{ 16 },
	{ 20 } // It is OK to seek beyond the end of a file.
};

static void seek_checks(
	struct fzp *(*open_func)(const char *, const char *),
	struct sdata *d)
{
	struct fzp *fzp;

	fail_unless((fzp=open_func(file, "rb"))!=NULL);
	fail_unless(!fzp_seek(fzp, d->pos, SEEK_SET));
	fail_unless(fzp_tell(fzp)==d->pos);
	fail_unless(!fzp_eof(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);
}

static void do_seek_tests(
	struct fzp *(*open_func)(const char *, const char *))
{
	setup_for_read(open_func, content);
	FOREACH(sd) seek_checks(open_func, &sd[i]);
	tear_down();
};

struct tdata
{
	off_t pos;
	const char *expected_str;
};

static struct tdata td[] = {
	{  0, "" },
	{  5, "01234" },
	{ 17, "0123456789abcdefg" },
	{ 20, "0123456789abcdefg" }
};

static void truncate_checks(
	struct fzp *(*open_func)(const char *, const char *),
	enum fzp_type type,
	struct tdata *d)
{
	char buf[32]="";
	struct fzp *fzp;

	fail_unless(!fzp_truncate(file, type, d->pos, 9 /* compression */));
	fail_unless((fzp=open_func(file, "rb"))!=NULL);
	fzp_read(fzp, buf, sizeof(buf));
	fail_unless(fzp_eof(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);

	ck_assert_str_eq(d->expected_str, buf);
}

static void do_truncate_tests(
	struct fzp *(*open_func)(const char *, const char *),
	enum fzp_type type)
{
	FOREACH(td)
	{
		setup_for_read(open_func, content);
		truncate_checks(open_func, type, &td[i]);
		tear_down();
	}
}

START_TEST(test_fzp_read)
{
	do_read_tests(fzp_open);
}
END_TEST

START_TEST(test_fzp_gzread)
{
	do_read_tests(fzp_gzopen);
}
END_TEST

START_TEST(test_fzp_seek)
{
	do_seek_tests(fzp_open);
}
END_TEST

START_TEST(test_fzp_gzseek)
{
	do_seek_tests(fzp_gzopen);
}
END_TEST

START_TEST(test_fzp_truncate)
{
	do_truncate_tests(fzp_open, FZP_FILE);
}
END_TEST

START_TEST(test_fzp_gztruncate)
{
	do_truncate_tests(fzp_gzopen, FZP_COMPRESSED);
}
END_TEST
<commit_msg>Fix test_fzp for older zlib versions.<commit_after>#include "test.h"
#include "../src/alloc.h"
#include "../src/fzp.h"
#include "../src/handy.h"

static const char *file="utest_fzp";
static const char *content="0123456789abcdefg";

static void tear_down(void)
{
	alloc_check();
	unlink(file);
}

static void setup_for_read(
	struct fzp *(*open_func)(const char *, const char *),
	const char *to_write)
{
	struct fzp *fzp;
	size_t len=strlen(to_write);
	unlink(file);
	fail_unless((fzp=open_func(file, "wb"))!=NULL);
	fail_unless(fzp_write(fzp, to_write, len)==len);
	fail_unless(!fzp_flush(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);
}

struct rdata
{
	size_t want;
	size_t got;
	const char *expected_str;
};

static struct rdata rd[] = {
	{ 0,   0, "" },
	{ 3,   3, "012" },
	{ 16, 16, "0123456789abcdef" },
	{ 20, 17, "0123456789abcdefg" },
};

static void read_checks(
	struct fzp *(*open_func)(const char *, const char *),
	struct rdata *d)
{
	char buf[32]="";
	struct fzp *fzp;

	fail_unless((fzp=open_func(file, "rb"))!=NULL);
	fail_unless(fzp_read(fzp, buf, d->want)==(int)d->got);
	if(d->want > d->got)
		fail_unless(fzp_eof(fzp));
	else
		fail_unless(!fzp_eof(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);

	ck_assert_str_eq(d->expected_str, buf);
}

static void do_read_tests(
	struct fzp *(*open_func)(const char *, const char *))
{
	setup_for_read(open_func, content);
	FOREACH(rd) read_checks(open_func, &rd[i]);
	tear_down();
};

struct sdata
{
	off_t pos;
};

static struct sdata sd[] = {
	{ 0 },
	{ 5 },
	{ 10 },
	{ 16 },
	{ 20 } // It is OK to seek beyond the end of a file.
};

static int fzp_gzopen_old_zlib_seek_hack=0;

static void seek_checks(
	struct fzp *(*open_func)(const char *, const char *),
	struct sdata *d)
{
	struct fzp *fzp;
	int expected=0;

	fail_unless((fzp=open_func(file, "rb"))!=NULL);
	if(fzp_gzopen_old_zlib_seek_hack && d->pos > (off_t)strlen(content))
		expected=-1;
	fail_unless(fzp_seek(fzp, d->pos, SEEK_SET)==expected);
	fail_unless(fzp_tell(fzp)==d->pos);
	fail_unless(!fzp_eof(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);
}

static void do_seek_tests(
	struct fzp *(*open_func)(const char *, const char *))
{
	setup_for_read(open_func, content);
	FOREACH(sd) seek_checks(open_func, &sd[i]);
	tear_down();
};

struct tdata
{
	off_t pos;
	const char *expected_str;
};

static struct tdata td[] = {
	{  0, "" },
	{  5, "01234" },
	{ 17, "0123456789abcdefg" },
	{ 20, "0123456789abcdefg" }
};

static void truncate_checks(
	struct fzp *(*open_func)(const char *, const char *),
	enum fzp_type type,
	struct tdata *d)
{
	char buf[32]="";
	struct fzp *fzp;

	fail_unless(!fzp_truncate(file, type, d->pos, 9 /* compression */));
	fail_unless((fzp=open_func(file, "rb"))!=NULL);
	fzp_read(fzp, buf, sizeof(buf));
	fail_unless(fzp_eof(fzp));
	fail_unless(!fzp_close(&fzp));
	fail_unless(fzp==NULL);

	ck_assert_str_eq(d->expected_str, buf);
}

static void do_truncate_tests(
	struct fzp *(*open_func)(const char *, const char *),
	enum fzp_type type)
{
	FOREACH(td)
	{
		setup_for_read(open_func, content);
		truncate_checks(open_func, type, &td[i]);
		tear_down();
	}
}

START_TEST(test_fzp_read)
{
	do_read_tests(fzp_open);
}
END_TEST

START_TEST(test_fzp_gzread)
{
	do_read_tests(fzp_gzopen);
}
END_TEST

START_TEST(test_fzp_seek)
{
	do_seek_tests(fzp_open);
}
END_TEST

START_TEST(test_fzp_gzseek)
{
	if(version_to_long(ZLIB_VERSION) <= version_to_long("1.2.3"))
		fzp_gzopen_old_zlib_seek_hack=1;
	do_seek_tests(fzp_gzopen);
}
END_TEST

START_TEST(test_fzp_truncate)
{
	do_truncate_tests(fzp_open, FZP_FILE);
}
END_TEST

START_TEST(test_fzp_gztruncate)
{
	do_truncate_tests(fzp_gzopen, FZP_COMPRESSED);
}
END_TEST

{'fraction_non_alphanumeric': 0.16942496493688639, 'fraction_numerical': 0.025946704067321177, 'mean_word_length': 5.721017907634307, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3169845', 'n_tokens_mistral': 3486, 'n_tokens_neox': 3260, 'n_words': 559}