1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file create_compress_files.c
4 /// \brief Creates bunch of test files to be compressed
6 /// Using a test file generator program saves space in the source code
7 /// package considerably.
9 // Author: Lasse Collin
11 // This file has been put into the public domain.
12 // You can do whatever you want with this file.
14 ///////////////////////////////////////////////////////////////////////////////
20 // If a command-line argument was given, only create the file if its
21 // name was specified on the command line. If no args were given then
22 // all files are created.
24 // Avoid re-creating the test files every time the tests are run.
25 #define maybe_create_test(argc, argv, name) \
27 if ((argc < 2 || strcmp(argv[1], "compress_generated_" #name) == 0) \
28 && !file_exists("compress_generated_" #name)) { \
29 FILE *file = file_create("compress_generated_" #name); \
30 write_ ## name(file); \
31 file_finish(file, "compress_generated_" #name); \
37 file_exists(const char *filename
)
39 // Trying to be somewhat portable by avoiding stat().
40 FILE *file
= fopen(filename
, "rb");
55 file_create(const char *filename
)
57 FILE *file
= fopen(filename
, "wb");
69 file_finish(FILE *file
, const char *filename
)
71 const bool ferror_fail
= ferror(file
);
72 const bool fclose_fail
= fclose(file
);
74 if (ferror_fail
|| fclose_fail
) {
81 // File that repeats "abc\n" a few thousand times. This is targeted
82 // especially at Subblock filter's run-length encoder.
86 for (size_t i
= 0; i
< 12345; ++i
)
87 if (fwrite("abc\n", 4, 1, file
) != 1)
92 // File that doesn't compress. We always use the same random seed to
93 // generate identical files on all systems.
95 write_random(FILE *file
)
99 for (size_t i
= 0; i
< 123456; ++i
) {
100 n
= 101771 * n
+ 71777;
102 putc((uint8_t)(n
), file
);
103 putc((uint8_t)(n
>> 8), file
);
104 putc((uint8_t)(n
>> 16), file
);
105 putc((uint8_t)(n
>> 24), file
);
112 write_text(FILE *file
)
114 static const char *lorem
[] = {
115 "Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur",
116 "adipisicing", "elit,", "sed", "do", "eiusmod", "tempor",
117 "incididunt", "ut", "labore", "et", "dolore", "magna",
118 "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis",
119 "nostrud", "exercitation", "ullamco", "laboris", "nisi",
120 "ut", "aliquip", "ex", "ea", "commodo", "consequat.",
121 "Duis", "aute", "irure", "dolor", "in", "reprehenderit",
122 "in", "voluptate", "velit", "esse", "cillum", "dolore",
123 "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint",
124 "occaecat", "cupidatat", "non", "proident,", "sunt", "in",
125 "culpa", "qui", "officia", "deserunt", "mollit", "anim",
126 "id", "est", "laborum."
129 // Let the first paragraph be the original text.
130 for (size_t w
= 0; w
< ARRAY_SIZE(lorem
); ++w
) {
131 fprintf(file
, "%s ", lorem
[w
]);
137 // The rest shall be (hopefully) meaningless combinations of
141 for (size_t p
= 0; p
< 500; ++p
) {
142 fprintf(file
, "\n\n");
144 for (size_t w
= 0; w
< ARRAY_SIZE(lorem
); ++w
) {
145 n
= 101771 * n
+ 71777;
147 fprintf(file
, "%s ", lorem
[n
% ARRAY_SIZE(lorem
)]);
157 main(int argc
, char **argv
)
159 maybe_create_test(argc
, argv
, abc
);
160 maybe_create_test(argc
, argv
, random
);
161 maybe_create_test(argc
, argv
, text
);