xref: /third_party/ffmpeg/libavutil/tests/bprint.c (revision cabdff1a)
1/*
2 * Copyright (c) 2012 Nicolas George
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/avassert.h"
22#include "libavutil/bprint.c"
23
24#undef printf
25
26static void bprint_pascal(AVBPrint *b, unsigned size)
27{
28    unsigned i, j;
29    unsigned p[42];
30
31    av_assert0(size < FF_ARRAY_ELEMS(p));
32
33    p[0] = 1;
34    av_bprintf(b, "%8d\n", 1);
35    for (i = 1; i <= size; i++) {
36        p[i] = 1;
37        for (j = i - 1; j > 0; j--)
38            p[j] = p[j] + p[j - 1];
39        for (j = 0; j <= i; j++)
40            av_bprintf(b, "%8d", p[j]);
41        av_bprintf(b, "\n");
42    }
43}
44
45int main(void)
46{
47    AVBPrint b;
48    char buf[256];
49    struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
50
51    av_bprint_init(&b, 0, AV_BPRINT_SIZE_UNLIMITED);
52    bprint_pascal(&b, 5);
53    printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
54    printf("%s\n", b.str);
55    av_bprint_finalize(&b, NULL);
56
57    av_bprint_init(&b, 0, AV_BPRINT_SIZE_UNLIMITED);
58    bprint_pascal(&b, 25);
59    printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
60    av_bprint_finalize(&b, NULL);
61
62    av_bprint_init(&b, 0, 2048);
63    bprint_pascal(&b, 25);
64    printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
65    av_bprint_finalize(&b, NULL);
66
67    av_bprint_init(&b, 0, AV_BPRINT_SIZE_AUTOMATIC);
68    bprint_pascal(&b, 5);
69    printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
70
71    av_bprint_init(&b, 0, AV_BPRINT_SIZE_AUTOMATIC);
72    bprint_pascal(&b, 25);
73    printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len);
74    /* Note that the size of the automatic buffer is arch-dependent. */
75
76    av_bprint_init(&b, 0, AV_BPRINT_SIZE_COUNT_ONLY);
77    bprint_pascal(&b, 25);
78    printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len);
79
80    av_bprint_init_for_buffer(&b, buf, sizeof(buf));
81    bprint_pascal(&b, 25);
82    printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
83
84    av_bprint_init(&b, 0, AV_BPRINT_SIZE_UNLIMITED);
85    av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
86    printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
87    av_bprint_finalize(&b, NULL);
88
89    av_bprint_init(&b, 0, 8);
90    av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
91    printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
92
93    return 0;
94}
95