Tiny bit better sanity check in block_util.c
[xz/debian.git] / tests / create_compress_files.c
blob2e394297b12a6f345e2b14521061de8916c9c150
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file create_compress_files.c
4 /// \brief Creates bunch of test files to be compressed
5 ///
6 /// Using a test file generator program saves space in the source code
7 /// package considerably.
8 //
9 // Copyright (C) 2008 Lasse Collin
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "sysdefs.h"
24 #include <stdio.h>
27 // Avoid re-creating the test files every time the tests are run.
28 #define create_test(name) \
29 do { \
30 if (!file_exists("compress_generated_" #name)) { \
31 FILE *file = file_create("compress_generated_" #name); \
32 write_ ## name(file); \
33 file_finish(file, "compress_generated_" #name); \
34 } \
35 } while (0)
38 static bool
39 file_exists(const char *filename)
41 // Trying to be somewhat portable by avoiding stat().
42 FILE *file = fopen(filename, "rb");
43 bool ret;
45 if (file != NULL) {
46 fclose(file);
47 ret = true;
48 } else {
49 ret = false;
52 return ret;
56 static FILE *
57 file_create(const char *filename)
59 FILE *file = fopen(filename, "wb");
61 if (file == NULL) {
62 perror(filename);
63 exit(1);
66 return file;
70 static void
71 file_finish(FILE *file, const char *filename)
73 const bool ferror_fail = ferror(file);
74 const bool fclose_fail = fclose(file);
76 if (ferror_fail || fclose_fail) {
77 perror(filename);
78 exit(1);
83 // File that repeats "abc\n" a few thousand times. This is targeted
84 // especially at Subblock filter's run-length encoder.
85 static void
86 write_abc(FILE *file)
88 for (size_t i = 0; i < 12345; ++i)
89 fwrite("abc\n", 4, 1, file);
93 // File that doesn't compress. We always use the same random seed to
94 // generate identical files on all systems.
95 static void
96 write_random(FILE *file)
98 uint32_t n = 5;
100 for (size_t i = 0; i < 123456; ++i) {
101 n = 101771 * n + 71777;
103 putc(n & 0xFF, file);
104 putc((n >> 8) & 0xFF, file);
105 putc((n >> 16) & 0xFF, file);
106 putc(n >> 24, file);
111 // Text file
112 static void
113 write_text(FILE *file)
115 static const char *lorem[] = {
116 "Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur",
117 "adipisicing", "elit,", "sed", "do", "eiusmod", "tempor",
118 "incididunt", "ut", "labore", "et", "dolore", "magna",
119 "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis",
120 "nostrud", "exercitation", "ullamco", "laboris", "nisi",
121 "ut", "aliquip", "ex", "ea", "commodo", "consequat.",
122 "Duis", "aute", "irure", "dolor", "in", "reprehenderit",
123 "in", "voluptate", "velit", "esse", "cillum", "dolore",
124 "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint",
125 "occaecat", "cupidatat", "non", "proident,", "sunt", "in",
126 "culpa", "qui", "officia", "deserunt", "mollit", "anim",
127 "id", "est", "laborum."
130 // Let the first paragraph be the original text.
131 for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
132 fprintf(file, "%s ", lorem[w]);
134 if (w % 7 == 6)
135 fprintf(file, "\n");
138 // The rest shall be (hopefully) meaningless combinations of
139 // the same words.
140 uint32_t n = 29;
142 for (size_t p = 0; p < 500; ++p) {
143 fprintf(file, "\n\n");
145 for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
146 n = 101771 * n + 71777;
148 fprintf(file, "%s ", lorem[n % ARRAY_SIZE(lorem)]);
150 if (w % 7 == 6)
151 fprintf(file, "\n");
158 main(void)
160 create_test(abc);
161 create_test(random);
162 create_test(text);
163 return 0;