applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[platform/upstream/glib.git] / tests / type-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
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 #undef G_LOG_DOMAIN
28
29 #include <stdio.h>
30 #include <string.h>
31 #include "glib.h"
32
33
34
35 int
36 main (int   argc,
37       char *argv[])
38 {
39   gchar *string;
40   gushort gus;
41   guint gui;
42   gulong gul;
43   gshort gs;
44   gint gi;
45   glong gl;
46   gint16 gi16t1;
47   gint16 gi16t2;
48   gint32 gi32t1;
49   gint32 gi32t2;
50   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
51   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
52 #ifdef G_HAVE_GINT64
53   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
54           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
55   gint64 gi64t1;
56   gint64 gi64t2;
57 #endif
58
59   /* type sizes */
60   g_assert (sizeof (gint8) == 1);
61   g_assert (sizeof (gint16) == 2);
62   g_assert (sizeof (gint32) == 4);
63 #ifdef  G_HAVE_GINT64
64   g_assert (sizeof (gint64) == 8);
65 #endif  /* G_HAVE_GINT64 */
66
67   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
68   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
69 #ifdef G_HAVE_GINT64
70   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
71 #endif
72
73   /* Test the G_(MIN|MAX|MAXU)(SHORT|INT|LONG) macros */
74
75   gus = G_MAXUSHORT;
76   gus++;
77   g_assert (gus == 0);
78
79   gui = G_MAXUINT;
80   gui++;
81   g_assert (gui == 0);
82
83   gul = G_MAXULONG;
84   gul++;
85   g_assert (gul == 0);
86
87   gs = G_MAXSHORT;
88   gs++;
89   g_assert (gs == G_MINSHORT);
90
91   gi = G_MAXINT;
92   gi++;
93   g_assert (gi == G_MININT);
94
95   gl = G_MAXLONG;
96   gl++;
97   g_assert (gl == G_MINLONG);
98
99   /* Test the G_G(U)?INT(16|32|64)_FORMAT macros */
100
101   gi16t1 = -0x3AFA;
102   gu16t1 = 0xFAFA;
103   gi32t1 = -0x3AFAFAFA;
104   gu32t1 = 0xFAFAFAFA; 
105
106 #define FORMAT "%" G_GINT16_FORMAT " %" G_GINT32_FORMAT \
107                " %" G_GUINT16_FORMAT " %" G_GUINT32_FORMAT "\n"
108   string = g_strdup_printf (FORMAT, gi16t1, gi32t1, gu16t1, gu32t1);
109   sscanf (string, FORMAT, &gi16t2, &gi32t2, &gu16t2, &gu32t2);
110   g_free (string);
111   g_assert (gi16t1 == gi16t2);
112   g_assert (gi32t1 == gi32t2);
113   g_assert (gu16t1 == gu16t2);
114   g_assert (gu32t1 == gu32t2);
115
116 #ifdef G_HAVE_GINT64
117   gi64t1 = G_GINT64_CONSTANT (-0x3AFAFAFAFAFAFAFA);
118   gu64t1 = G_GINT64_CONSTANT (0xFAFAFAFAFAFAFAFA); 
119 #define FORMAT64 "%" G_GINT64_FORMAT " %" G_GUINT64_FORMAT "\n"
120   string = g_strdup_printf (FORMAT64, gi64t1, gu64t1);
121   sscanf (string, FORMAT64, &gi64t2, &gu64t2);
122   g_free (string);
123   g_assert (gi64t1 == gi64t2);
124   g_assert (gu64t1 == gu64t2);
125 #endif
126   
127   return 0;
128 }