Line data Source code
1 : /* SPDX-License-Identifier: GPL-2.0-or-later */
2 : /*
3 : * Purpose: Test dbstrbuild function
4 : */
5 :
6 : #include "common.h"
7 :
8 : static bool failed = false;
9 :
10 : static void
11 80 : test(const char *args, const char *output, RETCODE rc, const char *expected, int line)
12 : {
13 80 : printf("RETCODE %d\n", rc);
14 :
15 80 : if (strcmp(output, expected) == 0)
16 : return;
17 :
18 0 : failed = true;
19 0 : fprintf(stderr, "Wrong formatting\n");
20 0 : fprintf(stderr, " output: \"%s\"\n", output);
21 0 : fprintf(stderr, "expected: \"%s\"\n", expected);
22 0 : fprintf(stderr, "%d: dbstrbuild%s\n", line, args);
23 : }
24 :
25 : #define COMMON_ NULL, buf, sizeof(buf),
26 : #define TEST(args, out) do { \
27 : memset(buf, 0, sizeof(buf)); \
28 : rc = dbstrbuild args; \
29 : test(#args, buf, rc, out, __LINE__); \
30 : } while(0)
31 :
32 10 : TEST_MAIN()
33 : {
34 : int rc;
35 : char buf[128];
36 :
37 10 : TEST((COMMON_ "%1!", "%d", 123), "123");
38 10 : TEST((COMMON_ "%1! %2!", "%d %d", 123, 456), "123 456");
39 10 : TEST((COMMON_ "%1!", "%s", " xxx "), " xxx ");
40 10 : TEST((COMMON_ "%1!", " %6s", "one"), " one");
41 :
42 : /* floating point with precision */
43 10 : TEST((COMMON_ "%1!", "%.3g", 1.23), "1.23");
44 :
45 : /* hexadecimal with flag */
46 10 : TEST((COMMON_ "%1!", "%#x", 2), "0x2");
47 :
48 : /* if wrong placeholder stops */
49 10 : TEST((COMMON_ "%1! two %2! other %3!", "%d", 123), "123 two ");
50 :
51 : #if defined(DBTDS_7_4)
52 : /* \377 character should never appear, but make sure we don't overflow */
53 10 : TEST((COMMON_ "%1!", "%s", "start \377\377\377\377 end"), "start ");
54 : #endif
55 :
56 10 : return failed ? EXIT_FAILURE : EXIT_SUCCESS;
57 : }
|