Update THANKS
[xz/debian.git] / tests / create_compress_files.c
blob2106ac8130246b7d4722cef13d8c50d9d67289d0
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file create_compress_files.c
6 /// \brief Creates bunch of test files to be compressed
7 ///
8 /// Using a test file generator program saves space in the source code
9 /// package considerably.
11 // Author: Lasse Collin
13 ///////////////////////////////////////////////////////////////////////////////
15 #include "sysdefs.h"
16 #include <stdio.h>
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) \
25 do { \
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); \
31 } \
32 } while (0)
35 static bool
36 file_exists(const char *filename)
38 // Trying to be somewhat portable by avoiding stat().
39 FILE *file = fopen(filename, "rb");
40 bool ret;
42 if (file != NULL) {
43 fclose(file);
44 ret = true;
45 } else {
46 ret = false;
49 return ret;
53 static FILE *
54 file_create(const char *filename)
56 FILE *file = fopen(filename, "wb");
58 if (file == NULL) {
59 perror(filename);
60 exit(EXIT_FAILURE);
63 return file;
67 static void
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) {
74 perror(filename);
75 exit(EXIT_FAILURE);
80 // File that repeats "abc\n" a few thousand times. This is targeted
81 // especially at Subblock filter's run-length encoder.
82 static void
83 write_abc(FILE *file)
85 for (size_t i = 0; i < 12345; ++i)
86 if (fwrite("abc\n", 4, 1, file) != 1)
87 exit(EXIT_FAILURE);
91 // File that doesn't compress. We always use the same random seed to
92 // generate identical files on all systems.
93 static void
94 write_random(FILE *file)
96 uint32_t n = 5;
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);
109 // Text file
110 static void
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]);
132 if (w % 7 == 6)
133 fprintf(file, "\n");
136 // The rest shall be (hopefully) meaningless combinations of
137 // the same words.
138 uint32_t n = 29;
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)]);
148 if (w % 7 == 6)
149 fprintf(file, "\n");
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);
161 return EXIT_SUCCESS;