[daemon-fix] Registering starters: unwanted release_kdbus_name when no error was...
[platform/upstream/dbus.git] / test / internals / printf.c
1 /* Regression test for _dbus_printf_string_upper_bound
2  *
3  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4  * Copyright © 2013 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include <config.h>
28
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-internals.h>
31 #include <dbus/dbus-string.h>
32 #include "test-utils.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 static void
38 do_test (int minimum,
39     const char *format,
40     ...)
41 {
42   va_list ap;
43   int result;
44
45   va_start (ap, format);
46   result = _dbus_printf_string_upper_bound (format, ap);
47   va_end (ap);
48
49   if (result < minimum)
50     {
51       fprintf (stderr, "expected at least %d, got %d\n", minimum, result);
52       abort ();
53     }
54 }
55
56 #define X_TIMES_8 "XXXXXXXX"
57 #define X_TIMES_16   X_TIMES_8   X_TIMES_8
58 #define X_TIMES_32   X_TIMES_16  X_TIMES_16
59 #define X_TIMES_64   X_TIMES_32  X_TIMES_32
60 #define X_TIMES_128  X_TIMES_64  X_TIMES_64
61 #define X_TIMES_256  X_TIMES_128 X_TIMES_128
62 #define X_TIMES_512  X_TIMES_256 X_TIMES_256
63 #define X_TIMES_1024 X_TIMES_512 X_TIMES_512
64
65 int
66 main (int argc,
67     char **argv)
68 {
69   char buf[] = X_TIMES_1024 X_TIMES_1024 X_TIMES_1024 X_TIMES_1024;
70   int i;
71
72   do_test (1, "%d", 0);
73   do_test (7, "%d", 1234567);
74   do_test (3, "%f", 3.5);
75
76   do_test (0, "%s", "");
77   do_test (1024, "%s", X_TIMES_1024);
78   do_test (1025, "%s", X_TIMES_1024 "Y");
79
80   for (i = 4096; i > 0; i--)
81     {
82       buf[i] = '\0';
83       do_test (i, "%s", buf);
84       do_test (i + 3, "%s:%d", buf, 42);
85     }
86
87   return 0;
88 }