File: asprintf.c

package info (click to toggle)
criterion 2.3.3git1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,832 kB
  • sloc: ansic: 17,852; cpp: 795; python: 72; sh: 27; makefile: 23
file content (58 lines) | stat: -rw-r--r-- 1,532 bytes parent folder | download
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
51
52
53
54
55
56
57
58
#include "criterion/criterion.h"
#include "criterion/theories.h"
#include "criterion/internal/asprintf-compat.h"
#include "criterion/new/assert.h"

#include <stdio.h>

union anyval {
    char c;
    short hd;
    int d;
    long ld;
    long long lld;
    unsigned short hu;
    unsigned int u;
    unsigned long lu;
    unsigned long long llu;
    double f;
    const char *s;
    void *p;
};

struct format_test {
    const char *format;
    union anyval *val;
};

#define VALUE(Fmt, Val)    & (struct format_test) { .format = "%" #Fmt, .val = &(union anyval) { .Fmt = Val } }

TheoryDataPoints(asprintf, valid) = {
    DataPoints(struct format_test *,
            VALUE(c,                'a'),
            VALUE(hd,               42),
            VALUE(d,                42),
            VALUE(ld,               42),
            VALUE(lld,              42),
            VALUE(hu,               42),
            VALUE(u,                42),
            VALUE(lu,               42),
            VALUE(llu,              42),
            VALUE(f,                3.14),
            VALUE(s,                "foo"),
            VALUE(p,                NULL),
            ),
};

Theory((struct format_test *fmt), asprintf, valid) {
    char *actual;
    char expected[32];

    int rc_expected = snprintf(expected, sizeof (expected), fmt->format, *fmt->val);
    int rc_actual = cr_asprintf(&actual, fmt->format, *fmt->val);

    cr_expect(eq(int, rc_actual, rc_expected));
    cr_expect(eq(str, actual, expected));

    free(actual);
}