1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file create_compress_files.c
6 /// \brief Creates bunch of test files to be compressed
8 /// Using a test file generator program saves space in the source code
9 /// package considerably.
11 // Author: Lasse Collin
13 ///////////////////////////////////////////////////////////////////////////////
19 // If a command-line argument was given, only create the file if its
20 // name was specified on the command line. If no args were given then
21 // all files are created.
23 // Avoid re-creating the test files every time the tests are run.
24 #define maybe_create_test(argc, argv, name) \
26 if ((argc < 2 || strcmp(argv[1], "compress_generated_" #name) == 0) \
27 && !file_exists("compress_generated_" #name)) { \
28 FILE *file = file_create("compress_generated_" #name); \
29 write_ ## name(file); \
30 file_finish(file, "compress_generated_" #name); \
36 file_exists(const char *filename
)
38 // Trying to be somewhat portable by avoiding stat().
39 FILE *file
= fopen(filename
, "rb");
54 file_create(const char *filename
)
56 FILE *file
= fopen(filename
, "wb");
68 file_finish(FILE *file
, const char *filename
)
70 const bool ferror_fail
= ferror(file
);
71 const bool fclose_fail
= fclose(file
);
73 if (ferror_fail
|| fclose_fail
) {
80 // File that repeats "abc\n" a few thousand times. This is targeted
81 // especially at Subblock filter's run-length encoder.
85 for (size_t i
= 0; i
< 12345; ++i
)
86 if (fwrite("abc\n", 4, 1, file
) != 1)
91 // File that doesn't compress. We always use the same random seed to
92 // generate identical files on all systems.
94 write_random(FILE *file
)
98 for (size_t i
= 0; i
< 123456; ++i
) {
99 n
= 101771 * n
+ 71777;
101 putc((uint8_t)(n
), file
);
102 putc((uint8_t)(n
>> 8), file
);
103 putc((uint8_t)(n
>> 16), file
);
104 putc((uint8_t)(n
>> 24), file
);
111 write_text(FILE *file
)
113 static const char *lorem
[] = {
114 "Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur",
115 "adipisicing", "elit,", "sed", "do", "eiusmod", "tempor",
116 "incididunt", "ut", "labore", "et", "dolore", "magna",
117 "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis",
118 "nostrud", "exercitation", "ullamco", "laboris", "nisi",
119 "ut", "aliquip", "ex", "ea", "commodo", "consequat.",
120 "Duis", "aute", "irure", "dolor", "in", "reprehenderit",
121 "in", "voluptate", "velit", "esse", "cillum", "dolore",
122 "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint",
123 "occaecat", "cupidatat", "non", "proident,", "sunt", "in",
124 "culpa", "qui", "officia", "deserunt", "mollit", "anim",
125 "id", "est", "laborum."
128 // Let the first paragraph be the original text.
129 for (size_t w
= 0; w
< ARRAY_SIZE(lorem
); ++w
) {
130 fprintf(file
, "%s ", lorem
[w
]);
136 // The rest shall be (hopefully) meaningless combinations of
140 for (size_t p
= 0; p
< 500; ++p
) {
141 fprintf(file
, "\n\n");
143 for (size_t w
= 0; w
< ARRAY_SIZE(lorem
); ++w
) {
144 n
= 101771 * n
+ 71777;
146 fprintf(file
, "%s ", lorem
[n
% ARRAY_SIZE(lorem
)]);
156 main(int argc
, char **argv
)
158 maybe_create_test(argc
, argv
, abc
);
159 maybe_create_test(argc
, argv
, random
);
160 maybe_create_test(argc
, argv
, text
);