1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27 #include <stdio.h>
28
29 #include <glib.h>
30
31 static void
32 test_basic_types (void)
33 {
34 gchar *string;
35 gushort gus;
36 guint gui;
37 gulong gul;
38 gsize gsz;
39 gshort gs;
40 gint gi;
41 glong gl;
42 gint16 gi16t1;
43 gint16 gi16t2;
44 gint32 gi32t1;
45 gint32 gi32t2;
46 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
47 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
48 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
49 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
50 gint64 gi64t1;
51 gint64 gi64t2;
52 gssize gsst1;
53 gssize gsst2;
54 gsize gst1;
55 gsize gst2;
56
57 /* type sizes */
58 g_assert_cmpint (sizeof (gint8), ==, 1);
59 g_assert_cmpint (sizeof (gint16), ==, 2);
60 g_assert_cmpint (sizeof (gint32), ==, 4);
61 g_assert_cmpint (sizeof (gint64), ==, 8);
62
63 g_assert_cmpuint (GUINT16_SWAP_LE_BE (gu16t1), ==, gu16t2);
64 g_assert_cmpuint (GUINT32_SWAP_LE_BE (gu32t1), ==, gu32t2);
65 g_assert_cmpuint (GUINT64_SWAP_LE_BE (gu64t1), ==, gu64t2);
66
67 /* Test the G_(MIN|MAX|MAXU)(SHORT|INT|LONG) macros */
68
69 gus = G_MAXUSHORT;
70 gus++;
71 g_assert_cmpuint (gus, ==, 0);
72
73 gui = G_MAXUINT;
74 gui++;
75 g_assert_cmpuint (gui, ==, 0);
76
77 gul = G_MAXULONG;
78 gul++;
79 g_assert_cmpuint (gul, ==, 0);
80
81 gsz = G_MAXSIZE;
82 gsz++;
83
84 g_assert_cmpuint (gsz, ==, 0);
85
86 gs = G_MAXSHORT;
87 gs = (gshort) (1 + (gushort) gs);
88 g_assert_cmpint (gs, ==, G_MINSHORT);
89
90 gi = G_MAXINT;
91 gi = (gint) (1 + (guint) gi);
92 g_assert_cmpint (gi, ==, G_MININT);
93
94 gl = G_MAXLONG;
95 gl = (glong) (1 + (gulong) gl);
96 g_assert_cmpint (gl, ==, G_MINLONG);
97
98 /* Test the G_G(U)?INT(16|32|64)_FORMAT macros */
99
100 gi16t1 = -0x3AFA;
101 gu16t1 = 0xFAFA;
102 gi32t1 = -0x3AFAFAFA;
103 gu32t1 = 0xFAFAFAFA;
104
105 #define FORMAT "%" G_GINT16_FORMAT " %" G_GINT32_FORMAT \
106 " %" G_GUINT16_FORMAT " %" G_GUINT32_FORMAT "\n"
107 string = g_strdup_printf (FORMAT, gi16t1, gi32t1, gu16t1, gu32t1);
108 sscanf (string, FORMAT, &gi16t2, &gi32t2, &gu16t2, &gu32t2);
109 g_free (string);
110 g_assert_cmpint (gi16t1, ==, gi16t2);
111 g_assert_cmpint (gi32t1, ==, gi32t2);
112 g_assert_cmpint (gu16t1, ==, gu16t2);
113 g_assert_cmpint (gu32t1, ==, gu32t2);
114
115 gi64t1 = G_GINT64_CONSTANT (-0x3AFAFAFAFAFAFAFA);
116 gu64t1 = G_GINT64_CONSTANT (0xFAFAFAFAFAFAFAFA);
117
118 #define FORMAT64 "%" G_GINT64_FORMAT " %" G_GUINT64_FORMAT "\n"
119 #ifndef G_OS_WIN32
120 # define SCAN_FORMAT64 FORMAT64
121 #else
122 # define sscanf sscanf_s
123 # define SCAN_FORMAT64 "%I64d %I64u\n"
124 #endif
125 string = g_strdup_printf (FORMAT64, gi64t1, gu64t1);
126 sscanf (string, SCAN_FORMAT64, &gi64t2, &gu64t2);
127 g_free (string);
128 g_assert_cmpint (gi64t1, ==, gi64t2);
129 g_assert_cmpint (gu64t1, ==, gu64t2);
130
131 gsst1 = -0x3AFAFAFA;
132 gst1 = 0xFAFAFAFA;
133
134 #define FORMATSIZE "%" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT "\n"
135 #ifndef G_OS_WIN32
136 # define SCAN_FORMATSIZE FORMATSIZE
137 #else
138 # define sscanf sscanf_s
139 # define SCAN_FORMATSIZE "%Id %Iu\n"
140 #endif
141 string = g_strdup_printf (FORMATSIZE, gsst1, gst1);
142 sscanf (string, SCAN_FORMATSIZE, &gsst2, &gst2);
143 g_free (string);
144 g_assert_cmpint (gsst1, ==, gsst2);
145 g_assert_cmpint (gst1, ==, gst2);
146 }
147
148 int
149 main (int argc, char *argv[])
150 {
151 g_test_init (&argc, &argv, NULL);
152
153 g_test_add_func ("/types/basic_types", test_basic_types);
154
155 return g_test_run ();
156 }