1 /* 2 * asprintf and vasprintf test suite. 3 * 4 * The canonical version of this file is maintained in the rra-c-util package, 5 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>. 6 * 7 * Written by Russ Allbery <eagle@eyrie.org> 8 * Copyright 2014, 2018 Russ Allbery <eagle@eyrie.org> 9 * Copyright 2006-2009, 2011 10 * The Board of Trustees of the Leland Stanford Junior University 11 * 12 * Copying and distribution of this file, with or without modification, are 13 * permitted in any medium without royalty provided the copyright notice and 14 * this notice are preserved. This file is offered as-is, without any 15 * warranty. 16 * 17 * SPDX-License-Identifier: FSFAP 18 */ 19 20 #include <config.h> 21 #include <portable/macros.h> 22 #include <portable/system.h> 23 24 #include <tests/tap/basic.h> 25 26 int test_asprintf(char **, const char *, ...) 27 __attribute__((__format__(printf, 2, 3))); 28 int test_vasprintf(char **, const char *, va_list) 29 __attribute__((__format__(printf, 2, 0))); 30 31 static int __attribute__((__format__(printf, 2, 3))) 32 vatest(char **result, const char *format, ...) 33 { 34 va_list args; 35 int status; 36 37 va_start(args, format); 38 status = test_vasprintf(result, format, args); 39 va_end(args); 40 return status; 41 } 42 43 int 44 main(void) 45 { 46 char *result = NULL; 47 48 plan(12); 49 50 is_int(7, test_asprintf(&result, "%s", "testing"), "asprintf length"); 51 is_string("testing", result, "asprintf result"); 52 free(result); 53 ok(3, "free asprintf"); 54 is_int(0, test_asprintf(&result, "%s", ""), "asprintf empty length"); 55 is_string("", result, "asprintf empty string"); 56 free(result); 57 ok(6, "free asprintf of empty string"); 58 59 is_int(6, vatest(&result, "%d %s", 2, "test"), "vasprintf length"); 60 is_string("2 test", result, "vasprintf result"); 61 free(result); 62 ok(9, "free vasprintf"); 63 is_int(0, vatest(&result, "%s", ""), "vasprintf empty length"); 64 is_string("", result, "vasprintf empty string"); 65 free(result); 66 ok(12, "free vasprintf of empty string"); 67 68 return 0; 69 } 70