xref: /aosp_15_r20/external/lz4/tests/loremOut.c (revision 27162e4e17433d5aa7cb38e7b6a433a09405fc7f)
1 /*
2     lorem.c - lorem ipsum generator to stdout
3     Copyright (C) Yann Collet 2024
4 
5     GPL v2 License
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21     You can contact the author at :
22    - LZ4 source repository : https://github.com/lz4/lz4
23    - Public forum : https://groups.google.com/forum/#!forum/lz4c
24 */
25 
26 /* Implementation notes:
27  * Generates a stream of Lorem ipsum paragraphs to stdout,
28  * up to the requested size, which can be very large (> 4 GB).
29  * Note that, beyond 1 paragraph, this generator produces
30  * a different content than LOREM_genBuffer (even when using same seed).
31  */
32 
33 #include "platform.h"  /* Compiler options, SET_BINARY_MODE */
34 #include "loremOut.h"
35 #include "lorem.h"     /* LOREM_genBlock */
36 #include <stdio.h>
37 #include <assert.h>
38 
39 
40 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 #define LOREM_BLOCKSIZE (2 << 10)
LOREM_genOut(unsigned long long size,unsigned seed)42 void LOREM_genOut(unsigned long long size, unsigned seed)
43 {
44   char buff[LOREM_BLOCKSIZE] = {0};
45   unsigned long long total = 0;
46   size_t genBlockSize = (size_t)MIN(size, LOREM_BLOCKSIZE);
47 
48   /* init */
49   SET_BINARY_MODE(stdout);
50 
51   /* Generate Ipsum text, one paragraph at a time */
52   while (total < size) {
53     size_t generated = LOREM_genBlock(buff, genBlockSize, seed++, total == 0, 0);
54     assert(generated <= genBlockSize);
55     total += generated;
56     assert(total <= size);
57     fwrite(buff, 1, generated, stdout); /* note: should check potential write error */
58     if (size - total < genBlockSize)
59       genBlockSize = (size_t)(size - total);
60   }
61   assert(total == size);
62 }
63