aboutsummaryrefslogtreecommitdiff
path: root/tests/create_compress_files.c
blob: 44367d8f18eca5fd28cf18fdd2df9b9938a2a6fc (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
///////////////////////////////////////////////////////////////////////////////
//
/// \file       create_compress_files.c
/// \brief      Creates bunch of test files to be compressed
///
/// Using a test file generator program saves space in the source code
/// package considerably.
//
//  Author:     Lasse Collin
//
//  This file has been put into the public domain.
//  You can do whatever you want with this file.
//
///////////////////////////////////////////////////////////////////////////////

#include "sysdefs.h"
#include <stdio.h>


// Avoid re-creating the test files every time the tests are run.
#define create_test(name) \
do { \
	if (!file_exists("compress_generated_" #name)) { \
		FILE *file = file_create("compress_generated_" #name); \
		write_ ## name(file); \
		file_finish(file, "compress_generated_" #name); \
	} \
} while (0)


static bool
file_exists(const char *filename)
{
	// Trying to be somewhat portable by avoiding stat().
	FILE *file = fopen(filename, "rb");
	bool ret;

	if (file != NULL) {
		fclose(file);
		ret = true;
	} else {
		ret = false;
	}

	return ret;
}


static FILE *
file_create(const char *filename)
{
	FILE *file = fopen(filename, "wb");

	if (file == NULL) {
		perror(filename);
		exit(1);
	}

	return file;
}


static void
file_finish(FILE *file, const char *filename)
{
	const bool ferror_fail = ferror(file);
	const bool fclose_fail = fclose(file);

	if (ferror_fail || fclose_fail) {
		perror(filename);
		exit(1);
	}
}


// File that repeats "abc\n" a few thousand times. This is targeted
// especially at Subblock filter's run-length encoder.
static void
write_abc(FILE *file)
{
	for (size_t i = 0; i < 12345; ++i)
		fwrite("abc\n", 4, 1, file);
}


// File that doesn't compress. We always use the same random seed to
// generate identical files on all systems.
static void
write_random(FILE *file)
{
	uint32_t n = 5;

	for (size_t i = 0; i < 123456; ++i) {
		n = 101771 * n + 71777;

		putc(n & 0xFF, file);
		putc((n >> 8) & 0xFF, file);
		putc((n >> 16) & 0xFF, file);
		putc(n >> 24, file);
	}
}


// Text file
static void
write_text(FILE *file)
{
	static const char *lorem[] = {
		"Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur",
		"adipisicing", "elit,", "sed", "do", "eiusmod", "tempor",
		"incididunt", "ut", "labore", "et", "dolore", "magna",
		"aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis",
		"nostrud", "exercitation", "ullamco", "laboris", "nisi",
		"ut", "aliquip", "ex", "ea", "commodo", "consequat.",
		"Duis", "aute", "irure", "dolor", "in", "reprehenderit",
		"in", "voluptate", "velit", "esse", "cillum", "dolore",
		"eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint",
		"occaecat", "cupidatat", "non", "proident,", "sunt", "in",
		"culpa", "qui", "officia", "deserunt", "mollit", "anim",
		"id", "est", "laborum."
	};

	// Let the first paragraph be the original text.
	for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
		fprintf(file, "%s ", lorem[w]);

		if (w % 7 == 6)
			fprintf(file, "\n");
	}

	// The rest shall be (hopefully) meaningless combinations of
	// the same words.
	uint32_t n = 29;

	for (size_t p = 0; p < 500; ++p) {
		fprintf(file, "\n\n");

		for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
			n = 101771 * n + 71777;

			fprintf(file, "%s ", lorem[n % ARRAY_SIZE(lorem)]);

			if (w % 7 == 6)
				fprintf(file, "\n");
		}
	}
}


int
main(void)
{
	create_test(abc);
	create_test(random);
	create_test(text);
	return 0;
}