2013-06-12 13:56:39 +01:00
|
|
|
/* Regression test for _dbus_printf_string_upper_bound
|
|
|
|
|
*
|
|
|
|
|
* Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
|
|
|
|
|
* Copyright © 2013 Intel Corporation
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
|
* subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
#include <dbus/dbus-internals.h>
|
|
|
|
|
#include <dbus/dbus-string.h>
|
|
|
|
|
#include "test-utils.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2016-11-09 12:59:18 +00:00
|
|
|
static void do_test (int minimum,
|
|
|
|
|
const char *format,
|
|
|
|
|
...) _DBUS_GNUC_PRINTF (2, 3);
|
|
|
|
|
|
2013-06-12 13:56:39 +01:00
|
|
|
static void
|
|
|
|
|
do_test (int minimum,
|
|
|
|
|
const char *format,
|
|
|
|
|
...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
va_start (ap, format);
|
|
|
|
|
result = _dbus_printf_string_upper_bound (format, ap);
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
|
|
if (result < minimum)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "expected at least %d, got %d\n", minimum, result);
|
|
|
|
|
abort ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define X_TIMES_8 "XXXXXXXX"
|
|
|
|
|
#define X_TIMES_16 X_TIMES_8 X_TIMES_8
|
|
|
|
|
#define X_TIMES_32 X_TIMES_16 X_TIMES_16
|
|
|
|
|
#define X_TIMES_64 X_TIMES_32 X_TIMES_32
|
|
|
|
|
#define X_TIMES_128 X_TIMES_64 X_TIMES_64
|
|
|
|
|
#define X_TIMES_256 X_TIMES_128 X_TIMES_128
|
|
|
|
|
#define X_TIMES_512 X_TIMES_256 X_TIMES_256
|
|
|
|
|
#define X_TIMES_1024 X_TIMES_512 X_TIMES_512
|
|
|
|
|
|
2022-04-01 15:57:07 +01:00
|
|
|
static void
|
|
|
|
|
print64 (void)
|
|
|
|
|
{
|
|
|
|
|
dbus_int64_t i = -123;
|
|
|
|
|
dbus_uint64_t u = 456;
|
|
|
|
|
DBusString buf = _DBUS_STRING_INIT_INVALID;
|
|
|
|
|
const char expected[] = "i=-123;u=456;x=1c8";
|
|
|
|
|
|
|
|
|
|
if (!_dbus_string_init (&buf))
|
|
|
|
|
_dbus_test_fatal ("out of memory");
|
|
|
|
|
|
|
|
|
|
if (!_dbus_string_append_printf (&buf,
|
|
|
|
|
"i=%" DBUS_INT64_MODIFIER "d;"
|
|
|
|
|
"u=%" DBUS_INT64_MODIFIER "u;"
|
|
|
|
|
"x=%" DBUS_INT64_MODIFIER "x",
|
|
|
|
|
i, u, u))
|
|
|
|
|
_dbus_test_fatal ("out of memory");
|
|
|
|
|
|
|
|
|
|
if (_dbus_string_get_length (&buf) != (int) strlen (expected) ||
|
|
|
|
|
strcmp (_dbus_string_get_const_data (&buf), expected) != 0)
|
|
|
|
|
_dbus_test_fatal ("expected: \"%s\", got: %d chars \"%s\"",
|
|
|
|
|
expected,
|
|
|
|
|
_dbus_string_get_length (&buf),
|
|
|
|
|
_dbus_string_get_const_data (&buf));
|
|
|
|
|
|
|
|
|
|
_dbus_string_free (&buf);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 17:39:20 +00:00
|
|
|
/* This test outputs TAP syntax: http://testanything.org/ */
|
2013-06-12 13:56:39 +01:00
|
|
|
int
|
|
|
|
|
main (int argc,
|
|
|
|
|
char **argv)
|
|
|
|
|
{
|
|
|
|
|
char buf[] = X_TIMES_1024 X_TIMES_1024 X_TIMES_1024 X_TIMES_1024;
|
|
|
|
|
int i;
|
2015-02-26 17:39:20 +00:00
|
|
|
int test_num = 0;
|
2013-06-12 13:56:39 +01:00
|
|
|
|
|
|
|
|
do_test (1, "%d", 0);
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
|
|
|
|
|
2013-06-12 13:56:39 +01:00
|
|
|
do_test (7, "%d", 1234567);
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
|
|
|
|
|
2013-06-12 13:56:39 +01:00
|
|
|
do_test (3, "%f", 3.5);
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
2013-06-12 13:56:39 +01:00
|
|
|
|
|
|
|
|
do_test (0, "%s", "");
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
|
|
|
|
|
2013-06-12 13:56:39 +01:00
|
|
|
do_test (1024, "%s", X_TIMES_1024);
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
|
|
|
|
|
2013-06-12 13:56:39 +01:00
|
|
|
do_test (1025, "%s", X_TIMES_1024 "Y");
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
2013-06-12 13:56:39 +01:00
|
|
|
|
|
|
|
|
for (i = 4096; i > 0; i--)
|
|
|
|
|
{
|
|
|
|
|
buf[i] = '\0';
|
|
|
|
|
do_test (i, "%s", buf);
|
|
|
|
|
do_test (i + 3, "%s:%d", buf, 42);
|
|
|
|
|
}
|
2015-02-26 17:39:20 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
2013-06-12 13:56:39 +01:00
|
|
|
|
2022-04-01 15:57:07 +01:00
|
|
|
print64 ();
|
|
|
|
|
printf ("ok %d\n", ++test_num);
|
|
|
|
|
|
2015-02-26 17:39:20 +00:00
|
|
|
/* Tell the TAP driver that we have done all the tests we plan to do.
|
|
|
|
|
* This is how it can distinguish between an unexpected exit and
|
|
|
|
|
* successful completion. */
|
|
|
|
|
printf ("1..%d\n", test_num);
|
2018-07-11 16:40:12 +01:00
|
|
|
dbus_shutdown ();
|
2013-06-12 13:56:39 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|