glib2.0/glib/tests/types.c

157 lines
4.2 KiB
C
Raw Normal View History

2022-06-29 16:02:05 +08:00
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
2023-02-14 16:00:02 +08:00
* SPDX-License-Identifier: LGPL-2.1-or-later
*
2022-06-29 16:02:05 +08:00
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
2023-02-14 16:00:02 +08:00
* GLib at ftp://ftp.gtk.org/pub/gtk/.
2022-06-29 16:02:05 +08:00
*/
#include <stdio.h>
2023-02-14 16:00:02 +08:00
#include <glib.h>
2022-06-29 16:02:05 +08:00
2023-02-14 16:00:02 +08:00
static void
test_basic_types (void)
2022-06-29 16:02:05 +08:00
{
gchar *string;
gushort gus;
guint gui;
gulong gul;
gsize gsz;
gshort gs;
gint gi;
glong gl;
gint16 gi16t1;
gint16 gi16t2;
gint32 gi32t1;
gint32 gi32t2;
guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
2023-02-14 16:00:02 +08:00
gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
2022-06-29 16:02:05 +08:00
gint64 gi64t1;
gint64 gi64t2;
gssize gsst1;
gssize gsst2;
gsize gst1;
gsize gst2;
/* type sizes */
2023-02-14 16:00:02 +08:00
g_assert_cmpint (sizeof (gint8), ==, 1);
g_assert_cmpint (sizeof (gint16), ==, 2);
g_assert_cmpint (sizeof (gint32), ==, 4);
g_assert_cmpint (sizeof (gint64), ==, 8);
2022-06-29 16:02:05 +08:00
2023-02-14 16:00:02 +08:00
g_assert_cmpuint (GUINT16_SWAP_LE_BE (gu16t1), ==, gu16t2);
g_assert_cmpuint (GUINT32_SWAP_LE_BE (gu32t1), ==, gu32t2);
g_assert_cmpuint (GUINT64_SWAP_LE_BE (gu64t1), ==, gu64t2);
2022-06-29 16:02:05 +08:00
/* Test the G_(MIN|MAX|MAXU)(SHORT|INT|LONG) macros */
gus = G_MAXUSHORT;
gus++;
2023-02-14 16:00:02 +08:00
g_assert_cmpuint (gus, ==, 0);
2022-06-29 16:02:05 +08:00
gui = G_MAXUINT;
gui++;
2023-02-14 16:00:02 +08:00
g_assert_cmpuint (gui, ==, 0);
2022-06-29 16:02:05 +08:00
gul = G_MAXULONG;
gul++;
2023-02-14 16:00:02 +08:00
g_assert_cmpuint (gul, ==, 0);
2022-06-29 16:02:05 +08:00
gsz = G_MAXSIZE;
gsz++;
2023-02-14 16:00:02 +08:00
g_assert_cmpuint (gsz, ==, 0);
2022-06-29 16:02:05 +08:00
gs = G_MAXSHORT;
gs = (gshort) (1 + (gushort) gs);
2023-02-14 16:00:02 +08:00
g_assert_cmpint (gs, ==, G_MINSHORT);
2022-06-29 16:02:05 +08:00
gi = G_MAXINT;
gi = (gint) (1 + (guint) gi);
2023-02-14 16:00:02 +08:00
g_assert_cmpint (gi, ==, G_MININT);
2022-06-29 16:02:05 +08:00
gl = G_MAXLONG;
gl = (glong) (1 + (gulong) gl);
2023-02-14 16:00:02 +08:00
g_assert_cmpint (gl, ==, G_MINLONG);
2022-06-29 16:02:05 +08:00
/* Test the G_G(U)?INT(16|32|64)_FORMAT macros */
gi16t1 = -0x3AFA;
gu16t1 = 0xFAFA;
gi32t1 = -0x3AFAFAFA;
2023-02-14 16:00:02 +08:00
gu32t1 = 0xFAFAFAFA;
2022-06-29 16:02:05 +08:00
#define FORMAT "%" G_GINT16_FORMAT " %" G_GINT32_FORMAT \
" %" G_GUINT16_FORMAT " %" G_GUINT32_FORMAT "\n"
string = g_strdup_printf (FORMAT, gi16t1, gi32t1, gu16t1, gu32t1);
sscanf (string, FORMAT, &gi16t2, &gi32t2, &gu16t2, &gu32t2);
g_free (string);
2023-02-14 16:00:02 +08:00
g_assert_cmpint (gi16t1, ==, gi16t2);
g_assert_cmpint (gi32t1, ==, gi32t2);
g_assert_cmpint (gu16t1, ==, gu16t2);
g_assert_cmpint (gu32t1, ==, gu32t2);
2022-06-29 16:02:05 +08:00
gi64t1 = G_GINT64_CONSTANT (-0x3AFAFAFAFAFAFAFA);
2023-02-14 16:00:02 +08:00
gu64t1 = G_GINT64_CONSTANT (0xFAFAFAFAFAFAFAFA);
2022-06-29 16:02:05 +08:00
#define FORMAT64 "%" G_GINT64_FORMAT " %" G_GUINT64_FORMAT "\n"
#ifndef G_OS_WIN32
# define SCAN_FORMAT64 FORMAT64
#else
# define sscanf sscanf_s
# define SCAN_FORMAT64 "%I64d %I64u\n"
#endif
string = g_strdup_printf (FORMAT64, gi64t1, gu64t1);
sscanf (string, SCAN_FORMAT64, &gi64t2, &gu64t2);
g_free (string);
2023-02-14 16:00:02 +08:00
g_assert_cmpint (gi64t1, ==, gi64t2);
g_assert_cmpint (gu64t1, ==, gu64t2);
2022-06-29 16:02:05 +08:00
gsst1 = -0x3AFAFAFA;
2023-02-14 16:00:02 +08:00
gst1 = 0xFAFAFAFA;
2022-06-29 16:02:05 +08:00
#define FORMATSIZE "%" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT "\n"
#ifndef G_OS_WIN32
# define SCAN_FORMATSIZE FORMATSIZE
#else
# define sscanf sscanf_s
# define SCAN_FORMATSIZE "%Id %Iu\n"
#endif
string = g_strdup_printf (FORMATSIZE, gsst1, gst1);
sscanf (string, SCAN_FORMATSIZE, &gsst2, &gst2);
g_free (string);
2023-02-14 16:00:02 +08:00
g_assert_cmpint (gsst1, ==, gsst2);
g_assert_cmpint (gst1, ==, gst2);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/types/basic_types", test_basic_types);
return g_test_run ();
2022-06-29 16:02:05 +08:00
}