xref: /netbsd-src/external/bsd/zstd/dist/tests/loremOut.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos 
11*3117ece4Schristos /* Implementation notes:
12*3117ece4Schristos  * Generates a stream of Lorem ipsum paragraphs to stdout,
13*3117ece4Schristos  * up to the requested size, which can be very large (> 4 GB).
14*3117ece4Schristos  * Note that, beyond 1 paragraph, this generator produces
15*3117ece4Schristos  * a different content than LOREM_genBuffer (even when using same seed).
16*3117ece4Schristos  */
17*3117ece4Schristos 
18*3117ece4Schristos #include "loremOut.h"
19*3117ece4Schristos #include <assert.h>
20*3117ece4Schristos #include <stdio.h>
21*3117ece4Schristos #include "lorem.h"    /* LOREM_genBlock */
22*3117ece4Schristos #include "platform.h" /* Compiler options, SET_BINARY_MODE */
23*3117ece4Schristos 
24*3117ece4Schristos #define MIN(a, b) ((a) < (b) ? (a) : (b))
25*3117ece4Schristos #define LOREM_BLOCKSIZE (1 << 10)
26*3117ece4Schristos void LOREM_genOut(unsigned long long size, unsigned seed)
27*3117ece4Schristos {
28*3117ece4Schristos     char buff[LOREM_BLOCKSIZE] = { 0 };
29*3117ece4Schristos     unsigned long long total   = 0;
30*3117ece4Schristos     size_t genBlockSize        = (size_t)MIN(size, LOREM_BLOCKSIZE);
31*3117ece4Schristos 
32*3117ece4Schristos     /* init */
33*3117ece4Schristos     SET_BINARY_MODE(stdout);
34*3117ece4Schristos 
35*3117ece4Schristos     /* Generate Ipsum text, one paragraph at a time */
36*3117ece4Schristos     while (total < size) {
37*3117ece4Schristos         size_t generated =
38*3117ece4Schristos                 LOREM_genBlock(buff, genBlockSize, seed++, total == 0, 0);
39*3117ece4Schristos         assert(generated <= genBlockSize);
40*3117ece4Schristos         total += generated;
41*3117ece4Schristos         assert(total <= size);
42*3117ece4Schristos         fwrite(buff,
43*3117ece4Schristos                1,
44*3117ece4Schristos                generated,
45*3117ece4Schristos                stdout); /* note: should check potential write error */
46*3117ece4Schristos         if (size - total < genBlockSize)
47*3117ece4Schristos             genBlockSize = (size_t)(size - total);
48*3117ece4Schristos     }
49*3117ece4Schristos     assert(total == size);
50*3117ece4Schristos }
51