aboutsummaryrefslogtreecommitdiff
path: root/tests/loremOut.c
blob: 9fb48b1cc79fee3ace33854e5065e306548a2a9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 * You may select, at your option, one of the above-listed licenses.
 */

/* Implementation notes:
 * Generates a stream of Lorem ipsum paragraphs to stdout,
 * up to the requested size, which can be very large (> 4 GB).
 * Note that, beyond 1 paragraph, this generator produces
 * a different content than LOREM_genBuffer (even when using same seed).
 */

#include "loremOut.h"
#include <assert.h>
#include <stdio.h>
#include "lorem.h"    /* LOREM_genBlock */
#include "platform.h" /* Compiler options, SET_BINARY_MODE */

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define LOREM_BLOCKSIZE (1 << 10)
void LOREM_genOut(unsigned long long size, unsigned seed)
{
    char buff[LOREM_BLOCKSIZE] = { 0 };
    unsigned long long total   = 0;
    size_t genBlockSize        = (size_t)MIN(size, LOREM_BLOCKSIZE);

    /* init */
    SET_BINARY_MODE(stdout);

    /* Generate Ipsum text, one paragraph at a time */
    while (total < size) {
        size_t generated =
                LOREM_genBlock(buff, genBlockSize, seed++, total == 0, 0);
        assert(generated <= genBlockSize);
        total += generated;
        assert(total <= size);
        fwrite(buff,
               1,
               generated,
               stdout); /* note: should check potential write error */
        if (size - total < genBlockSize)
            genBlockSize = (size_t)(size - total);
    }
    assert(total == size);
}