Added asprintf unit tests
This commit is contained in:
parent
8ece7848b6
commit
cbb8f4b9c6
2 changed files with 66 additions and 0 deletions
|
@ -7,6 +7,7 @@ include_directories(../include ../src)
|
|||
|
||||
set(TEST_SOURCES
|
||||
ordered-set.c
|
||||
asprintf.c
|
||||
redirect.cc
|
||||
)
|
||||
|
||||
|
|
65
test/asprintf.c
Normal file
65
test/asprintf.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include "criterion/criterion.h"
|
||||
#include "criterion/theories.h"
|
||||
#include "criterion/asprintf-compat.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
union anyval {
|
||||
int c;
|
||||
int hd;
|
||||
int d;
|
||||
long ld;
|
||||
long long lld;
|
||||
unsigned int 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 expected[32];
|
||||
snprintf(expected, sizeof (expected), fmt->format, *fmt->val);
|
||||
|
||||
char *actual;
|
||||
cr_asprintf(&actual, fmt->format, *fmt->val);
|
||||
|
||||
cr_expect_str_eq(actual, expected);
|
||||
|
||||
free(actual);
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
# pragma GCC diagnostic ignored "-Wformat"
|
||||
#endif
|
||||
|
||||
Test(asprintf, invalid) {
|
||||
char *actual;
|
||||
cr_expect_eq(cr_asprintf(&actual, "%"), -1);
|
||||
}
|
Loading…
Add table
Reference in a new issue