Add paramspec-test
[platform/upstream/glib.git] / tests / gobject / paramspec-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_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <string.h>
31
32 #include <glib.h>
33 #include <glib-object.h>
34
35 static void
36 test_param_spec_char (void)
37 {
38   GParamSpec *pspec;
39   GValue value = { 0, };
40   gboolean modified;
41  
42   pspec = g_param_spec_char ("char", "nick", "blurb",
43                              20, 40, 30, G_PARAM_READWRITE);
44
45   g_assert (strcmp (g_param_spec_get_name (pspec), "char") == 0);
46   g_assert (strcmp (g_param_spec_get_nick (pspec), "nick") == 0);
47   g_assert (strcmp (g_param_spec_get_blurb (pspec), "blurb") == 0);
48
49   g_value_init (&value, G_TYPE_CHAR);
50   g_value_set_char (&value, 30);
51
52   g_assert (g_param_value_defaults (pspec, &value));
53   
54   g_value_set_char (&value, 0);
55   modified = g_param_value_validate (pspec, &value);
56   g_assert (modified && g_value_get_char (&value) == 20);
57
58   g_value_set_char (&value, 20);
59   modified = g_param_value_validate (pspec, &value);
60   g_assert (!modified && g_value_get_char (&value) == 20);
61
62   g_value_set_char (&value, 40);
63   modified = g_param_value_validate (pspec, &value);
64   g_assert (!modified && g_value_get_char (&value) == 40);
65
66   g_value_set_char (&value, 60);
67   modified = g_param_value_validate (pspec, &value);
68   g_assert (modified && g_value_get_char (&value) == 40);
69
70   g_param_spec_unref (pspec);
71 }
72
73 static void
74 test_param_spec_override (void)
75 {
76   GParamSpec *ospec, *pspec;
77   GValue value = { 0, };
78   gboolean modified;
79  
80   ospec = g_param_spec_char ("char", "nick", "blurb",
81                              20, 40, 30, G_PARAM_READWRITE);
82
83   pspec = g_param_spec_override ("override", ospec);
84
85   g_assert (strcmp (g_param_spec_get_name (pspec), "override") == 0);
86   g_assert (strcmp (g_param_spec_get_nick (pspec), "nick") == 0);
87   g_assert (strcmp (g_param_spec_get_blurb (pspec), "blurb") == 0);
88
89   g_value_init (&value, G_TYPE_CHAR);
90   g_value_set_char (&value, 30);
91
92   g_assert (g_param_value_defaults (pspec, &value));
93   
94   g_value_set_char (&value, 0);
95   modified = g_param_value_validate (pspec, &value);
96   g_assert (modified && g_value_get_char (&value) == 20);
97
98   g_value_set_char (&value, 20);
99   modified = g_param_value_validate (pspec, &value);
100   g_assert (!modified && g_value_get_char (&value) == 20);
101
102   g_value_set_char (&value, 40);
103   modified = g_param_value_validate (pspec, &value);
104   g_assert (!modified && g_value_get_char (&value) == 40);
105
106   g_value_set_char (&value, 60);
107   modified = g_param_value_validate (pspec, &value);
108   g_assert (modified && g_value_get_char (&value) == 40);
109
110   g_param_spec_unref (pspec);
111 }
112
113 static void
114 test_param_spec_gtype (void)
115 {
116   GParamSpec *pspec;
117   GValue value = { 0, };
118   gboolean modified;
119   
120   pspec = g_param_spec_gtype ("gtype", "nick", "blurb",
121                               G_TYPE_PARAM, G_PARAM_READWRITE);
122   
123   g_value_init (&value, G_TYPE_GTYPE);
124   g_value_set_gtype (&value, G_TYPE_NONE);
125
126   g_assert (g_param_value_defaults (pspec, &value));
127   
128   g_value_set_gtype (&value, G_TYPE_INT);
129   modified = g_param_value_validate (pspec, &value);
130   g_assert (modified && g_value_get_gtype (&value) == G_TYPE_NONE);
131
132   g_value_set_gtype (&value, G_TYPE_PARAM_INT);
133   modified = g_param_value_validate (pspec, &value);
134   g_assert (!modified && g_value_get_gtype (&value) == G_TYPE_PARAM_INT);
135 }
136
137 int
138 main (int argc, char *argv[])
139 {
140   g_type_init (); 
141   
142   test_param_spec_char ();
143   test_param_spec_override ();
144   test_param_spec_gtype ();
145
146   return 0;
147 }