1999-01-09 20:10:45 +00:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 11:02:02 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1999-01-09 20:10:45 +00:00
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-28 14:09:39 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
1999-01-09 20:10:45 +00:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 11:02:02 +00:00
|
|
|
* Lesser General Public License for more details.
|
1999-01-09 20:10:45 +00:00
|
|
|
*
|
2000-07-26 11:02:02 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
1999-01-09 20:10:45 +00:00
|
|
|
*/
|
1999-02-24 06:14:27 +00:00
|
|
|
|
|
|
|
/*
|
2000-07-26 11:02:02 +00:00
|
|
|
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
1999-02-24 06:14:27 +00:00
|
|
|
* file for a list of people on the GLib Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
2001-07-19 20:07:41 +00:00
|
|
|
#undef G_DISABLE_ASSERT
|
1999-01-09 20:10:45 +00:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "glib.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
1999-03-16 16:02:05 +00:00
|
|
|
gchar *string;
|
|
|
|
gushort gus;
|
|
|
|
guint gui;
|
|
|
|
gulong gul;
|
2004-07-23 14:39:25 +00:00
|
|
|
gsize gsz;
|
1999-03-16 16:02:05 +00:00
|
|
|
gshort gs;
|
|
|
|
gint gi;
|
|
|
|
glong gl;
|
|
|
|
gint16 gi16t1;
|
|
|
|
gint16 gi16t2;
|
|
|
|
gint32 gi32t1;
|
|
|
|
gint32 gi32t2;
|
1999-01-09 20:10:45 +00:00
|
|
|
guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
|
|
|
|
guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
|
|
|
|
guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
|
|
|
|
gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
|
1999-03-16 16:02:05 +00:00
|
|
|
gint64 gi64t1;
|
|
|
|
gint64 gi64t2;
|
2003-11-20 23:12:05 +00:00
|
|
|
gssize gsst1;
|
|
|
|
gssize gsst2;
|
|
|
|
gsize gst1;
|
|
|
|
gsize gst2;
|
1999-01-09 20:10:45 +00:00
|
|
|
|
|
|
|
/* type sizes */
|
|
|
|
g_assert (sizeof (gint8) == 1);
|
|
|
|
g_assert (sizeof (gint16) == 2);
|
|
|
|
g_assert (sizeof (gint32) == 4);
|
|
|
|
g_assert (sizeof (gint64) == 8);
|
|
|
|
|
|
|
|
g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
|
|
|
|
g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
|
|
|
|
g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
|
|
|
|
|
1999-03-16 16:02:05 +00:00
|
|
|
/* Test the G_(MIN|MAX|MAXU)(SHORT|INT|LONG) macros */
|
|
|
|
|
|
|
|
gus = G_MAXUSHORT;
|
|
|
|
gus++;
|
|
|
|
g_assert (gus == 0);
|
|
|
|
|
|
|
|
gui = G_MAXUINT;
|
|
|
|
gui++;
|
|
|
|
g_assert (gui == 0);
|
|
|
|
|
|
|
|
gul = G_MAXULONG;
|
|
|
|
gul++;
|
|
|
|
g_assert (gul == 0);
|
|
|
|
|
2004-01-12 00:16:18 +00:00
|
|
|
gsz = G_MAXSIZE;
|
|
|
|
gsz++;
|
2004-07-23 14:39:25 +00:00
|
|
|
|
2004-01-12 00:16:18 +00:00
|
|
|
g_assert (gsz == 0);
|
|
|
|
|
1999-03-16 16:02:05 +00:00
|
|
|
gs = G_MAXSHORT;
|
2016-12-02 10:22:00 +00:00
|
|
|
gs = (gshort) (1 + (gushort) gs);
|
1999-03-16 16:02:05 +00:00
|
|
|
g_assert (gs == G_MINSHORT);
|
|
|
|
|
|
|
|
gi = G_MAXINT;
|
2016-12-02 10:22:00 +00:00
|
|
|
gi = (gint) (1 + (guint) gi);
|
1999-03-16 16:02:05 +00:00
|
|
|
g_assert (gi == G_MININT);
|
|
|
|
|
|
|
|
gl = G_MAXLONG;
|
2016-12-02 10:22:00 +00:00
|
|
|
gl = (glong) (1 + (gulong) gl);
|
1999-03-16 16:02:05 +00:00
|
|
|
g_assert (gl == G_MINLONG);
|
|
|
|
|
|
|
|
/* Test the G_G(U)?INT(16|32|64)_FORMAT macros */
|
|
|
|
|
|
|
|
gi16t1 = -0x3AFA;
|
|
|
|
gu16t1 = 0xFAFA;
|
|
|
|
gi32t1 = -0x3AFAFAFA;
|
|
|
|
gu32t1 = 0xFAFAFAFA;
|
|
|
|
|
Use C99 integer types where appropriate
GLib depends on various parts of a valid C99 toolchain, so it's time to
use C99 integer types wherever possible, instead of doing configure-time
discovery like it's 1997.
So:
gint8 → int8_t guint8 → uint8_t
gint16 → int16_t guint16 → uint16_t
gint32 → int32_t guint32 → uint32_t
gint64 → int64_t guint64 → uint64_t
gsize → size_t
gintptr → intptr_t guintptr → uintptr_t
There are some side effects on switching to C99:
- the G_*_MODIFIER macros do not really make any more sense, and should
be deprecated and replaced by an empty string
- the G_*_FORMAT macros are marked for use with both printf() and
scanf(), except that's not really true at all; C99 defines format
macros for print() and scanf() separately, and GLib should do the
same. The old macros should be deprecated, but if we want to be evil
about it, we could alias them with the PRINTF_FORMAT, as it's likely
their more dominant use
In general, there shouldn't be any ABI changes, unless the toolchain in
use is not conformant to C99.
Closes #1484
2019-12-19 15:45:32 +00:00
|
|
|
#define PRINTF_FORMAT "%" G_GINT16_FORMAT \
|
|
|
|
" %" G_GINT32_FORMAT \
|
|
|
|
" %" G_GUINT16_FORMAT \
|
|
|
|
" %" G_GUINT32_FORMAT \
|
|
|
|
"\n"
|
|
|
|
string = g_strdup_printf (PRINTF_FORMAT, gi16t1, gi32t1, gu16t1, gu32t1);
|
|
|
|
|
|
|
|
#define SCANF_FORMAT "%" G_GINT16_SCANF_FORMAT \
|
|
|
|
" %" G_GINT32_SCANF_FORMAT \
|
|
|
|
" %" G_GUINT16_SCANF_FORMAT \
|
|
|
|
" %" G_GUINT32_SCANF_FORMAT \
|
|
|
|
"\n"
|
|
|
|
sscanf (string, SCANF_FORMAT, &gi16t2, &gi32t2, &gu16t2, &gu32t2);
|
1999-03-16 16:02:05 +00:00
|
|
|
g_free (string);
|
|
|
|
g_assert (gi16t1 == gi16t2);
|
|
|
|
g_assert (gi32t1 == gi32t2);
|
|
|
|
g_assert (gu16t1 == gu16t2);
|
|
|
|
g_assert (gu32t1 == gu32t2);
|
|
|
|
|
|
|
|
gi64t1 = G_GINT64_CONSTANT (-0x3AFAFAFAFAFAFAFA);
|
|
|
|
gu64t1 = G_GINT64_CONSTANT (0xFAFAFAFAFAFAFAFA);
|
2002-12-12 22:59:55 +00:00
|
|
|
|
1999-03-16 16:02:05 +00:00
|
|
|
#define FORMAT64 "%" G_GINT64_FORMAT " %" G_GUINT64_FORMAT "\n"
|
2018-09-20 05:21:38 +00:00
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
# define SCAN_FORMAT64 FORMAT64
|
|
|
|
#else
|
2019-08-10 19:55:53 +02:00
|
|
|
# define sscanf sscanf_s
|
2018-09-20 05:21:38 +00:00
|
|
|
# define SCAN_FORMAT64 "%I64d %I64u\n"
|
|
|
|
#endif
|
1999-03-16 16:02:05 +00:00
|
|
|
string = g_strdup_printf (FORMAT64, gi64t1, gu64t1);
|
2018-09-20 05:21:38 +00:00
|
|
|
sscanf (string, SCAN_FORMAT64, &gi64t2, &gu64t2);
|
1999-03-16 16:02:05 +00:00
|
|
|
g_free (string);
|
|
|
|
g_assert (gi64t1 == gi64t2);
|
|
|
|
g_assert (gu64t1 == gu64t2);
|
2003-11-20 23:12:05 +00:00
|
|
|
|
|
|
|
gsst1 = -0x3AFAFAFA;
|
|
|
|
gst1 = 0xFAFAFAFA;
|
|
|
|
|
|
|
|
#define FORMATSIZE "%" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT "\n"
|
2018-09-20 05:21:38 +00:00
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
# define SCAN_FORMATSIZE FORMATSIZE
|
|
|
|
#else
|
2019-08-10 19:55:53 +02:00
|
|
|
# define sscanf sscanf_s
|
2018-09-20 05:21:38 +00:00
|
|
|
# define SCAN_FORMATSIZE "%Id %Iu\n"
|
|
|
|
#endif
|
2003-11-20 23:12:05 +00:00
|
|
|
string = g_strdup_printf (FORMATSIZE, gsst1, gst1);
|
2018-09-20 05:21:38 +00:00
|
|
|
sscanf (string, SCAN_FORMATSIZE, &gsst2, &gst2);
|
2003-11-20 23:12:05 +00:00
|
|
|
g_free (string);
|
|
|
|
g_assert (gsst1 == gsst2);
|
|
|
|
g_assert (gst1 == gst2);
|
1999-03-16 16:02:05 +00:00
|
|
|
|
1999-01-09 20:10:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|