]> icculus.org git repositories - icculus/xz.git/blob - tests/create_compress_files.c
Fix a bug in lzma_block_buffer_decode(), although this
[icculus/xz.git] / tests / create_compress_files.c
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
10 //
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.
15 //
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.
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "sysdefs.h"
24 #include <stdio.h>
25
26
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)
36
37
38 static bool
39 file_exists(const char *filename)
40 {
41         // Trying to be somewhat portable by avoiding stat().
42         FILE *file = fopen(filename, "rb");
43         bool ret;
44
45         if (file != NULL) {
46                 fclose(file);
47                 ret = true;
48         } else {
49                 ret = false;
50         }
51
52         return ret;
53 }
54
55
56 static FILE *
57 file_create(const char *filename)
58 {
59         FILE *file = fopen(filename, "wb");
60
61         if (file == NULL) {
62                 perror(filename);
63                 exit(1);
64         }
65
66         return file;
67 }
68
69
70 static void
71 file_finish(FILE *file, const char *filename)
72 {
73         const bool ferror_fail = ferror(file);
74         const bool fclose_fail = fclose(file);
75
76         if (ferror_fail || fclose_fail) {
77                 perror(filename);
78                 exit(1);
79         }
80 }
81
82
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)
87 {
88         for (size_t i = 0; i < 12345; ++i)
89                 fwrite("abc\n", 4, 1, file);
90 }
91
92
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)
97 {
98         uint32_t n = 5;
99
100         for (size_t i = 0; i < 123456; ++i) {
101                 n = 101771 * n + 71777;
102
103                 putc(n & 0xFF, file);
104                 putc((n >> 8) & 0xFF, file);
105                 putc((n >> 16) & 0xFF, file);
106                 putc(n >> 24, file);
107         }
108 }
109
110
111 // Text file
112 static void
113 write_text(FILE *file)
114 {
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."
128         };
129
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]);
133
134                 if (w % 7 == 6)
135                         fprintf(file, "\n");
136         }
137
138         // The rest shall be (hopefully) meaningless combinations of
139         // the same words.
140         uint32_t n = 29;
141
142         for (size_t p = 0; p < 500; ++p) {
143                 fprintf(file, "\n\n");
144
145                 for (size_t w = 0; w < ARRAY_SIZE(lorem); ++w) {
146                         n = 101771 * n + 71777;
147
148                         fprintf(file, "%s ", lorem[n % ARRAY_SIZE(lorem)]);
149
150                         if (w % 7 == 6)
151                                 fprintf(file, "\n");
152                 }
153         }
154 }
155
156
157 int
158 main(void)
159 {
160         create_test(abc);
161         create_test(random);
162         create_test(text);
163         return 0;
164 }