glib/tests/gobject/gvalue-test.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLIB - Library of useful routines for C programming
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
       
     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 #include "gobject/gvaluecollector.h"
       
    35 #ifdef __SYMBIAN32__
       
    36 #include "mrt2_glib2_test.h"
       
    37 #endif /*__SYMBIAN32__*/
       
    38 
       
    39 static void
       
    40 test_enum_transformation (void)
       
    41 { 
       
    42   GType type; 
       
    43   GValue orig = { 0, };
       
    44   GValue xform = { 0, }; 
       
    45   GEnumValue values[] = { {0,"0","0"}, {1,"1","1"}}; 
       
    46   
       
    47  type = g_enum_register_static ("TestEnum", values); 
       
    48   
       
    49  g_value_init (&orig, type); 
       
    50  g_value_set_enum (&orig, 1); 
       
    51 
       
    52  memset (&xform, 0, sizeof (GValue));
       
    53  g_value_init (&xform, G_TYPE_CHAR); 
       
    54  g_value_transform (&orig, &xform); 
       
    55  g_assert (g_value_get_char (&xform) == 1);
       
    56 
       
    57  memset (&xform, 0, sizeof (GValue));
       
    58  g_value_init (&xform, G_TYPE_UCHAR); 
       
    59  g_value_transform (&orig, &xform); 
       
    60  g_assert (g_value_get_uchar (&xform) == 1);
       
    61 
       
    62  memset (&xform, 0, sizeof (GValue));
       
    63  g_value_init (&xform, G_TYPE_INT); 
       
    64  g_value_transform (&orig, &xform); 
       
    65  g_assert (g_value_get_int (&xform) == 1);
       
    66 
       
    67  memset (&xform, 0, sizeof (GValue));
       
    68  g_value_init (&xform, G_TYPE_UINT); 
       
    69  g_value_transform (&orig, &xform); 
       
    70  g_assert (g_value_get_uint (&xform) == 1);
       
    71 
       
    72  memset (&xform, 0, sizeof (GValue));
       
    73  g_value_init (&xform, G_TYPE_LONG); 
       
    74  g_value_transform (&orig, &xform); 
       
    75  g_assert (g_value_get_long (&xform) == 1);
       
    76 
       
    77  memset (&xform, 0, sizeof (GValue));
       
    78  g_value_init (&xform, G_TYPE_ULONG); 
       
    79  g_value_transform (&orig, &xform); 
       
    80  g_assert (g_value_get_ulong (&xform) == 1);
       
    81 
       
    82  memset (&xform, 0, sizeof (GValue));
       
    83  g_value_init (&xform, G_TYPE_INT64); 
       
    84  g_value_transform (&orig, &xform); 
       
    85  g_assert (g_value_get_int64 (&xform) == 1);
       
    86 
       
    87  memset (&xform, 0, sizeof (GValue));
       
    88  g_value_init (&xform, G_TYPE_UINT64); 
       
    89  g_value_transform (&orig, &xform); 
       
    90  g_assert (g_value_get_uint64 (&xform) == 1);
       
    91 }
       
    92 
       
    93 
       
    94 static void
       
    95 test_gtype_value (void)
       
    96 {
       
    97   GType type;
       
    98   GValue value = { 0, };
       
    99   GValue copy = { 0, };
       
   100 
       
   101   g_value_init (&value, G_TYPE_GTYPE);
       
   102 
       
   103   g_value_set_gtype (&value, G_TYPE_BOXED);
       
   104   type = g_value_get_gtype (&value);
       
   105   g_assert (type == G_TYPE_BOXED);
       
   106 
       
   107   g_value_init (&copy, G_TYPE_GTYPE);
       
   108   g_value_copy (&value, &copy);
       
   109   type = g_value_get_gtype (&copy);
       
   110   g_assert (type == G_TYPE_BOXED);
       
   111 }
       
   112 
       
   113 static gchar *
       
   114 collect (GValue *value, ...)
       
   115 {
       
   116   gchar *error;
       
   117   va_list var_args;
       
   118 
       
   119   error = NULL;
       
   120 
       
   121   va_start (var_args, value);
       
   122   G_VALUE_COLLECT (value, var_args, 0, &error);
       
   123   va_end (var_args);
       
   124 
       
   125   return error;
       
   126 }	 
       
   127 
       
   128 static gchar *
       
   129 lcopy (GValue *value, ...)
       
   130 {
       
   131   gchar *error;
       
   132   va_list var_args;
       
   133 
       
   134   error = NULL;
       
   135 
       
   136   va_start (var_args, value);
       
   137   G_VALUE_LCOPY (value, var_args, 0, &error);
       
   138   va_end (var_args);
       
   139 
       
   140   return error;
       
   141 }	 
       
   142 
       
   143 static void
       
   144 test_collection (void)
       
   145 {
       
   146   GValue value = { 0, };
       
   147   gchar *error;
       
   148   
       
   149   g_value_init (&value, G_TYPE_CHAR);
       
   150   error = collect (&value, 'c');
       
   151   g_assert (error == NULL);
       
   152   g_assert (g_value_get_char (&value) == 'c');
       
   153   
       
   154   g_value_unset (&value);
       
   155   g_value_init (&value, G_TYPE_UCHAR);
       
   156   error = collect (&value, 129);
       
   157   g_assert (error == NULL);
       
   158   g_assert (g_value_get_uchar (&value) == 129);
       
   159   
       
   160   g_value_unset (&value);
       
   161   g_value_init (&value, G_TYPE_BOOLEAN);
       
   162   error = collect (&value, TRUE);
       
   163   g_assert (error == NULL);
       
   164   g_assert (g_value_get_boolean (&value) == TRUE);
       
   165   
       
   166   g_value_unset (&value);
       
   167   g_value_init (&value, G_TYPE_INT);
       
   168   error = collect (&value, G_MAXINT);
       
   169   g_assert (error == NULL);
       
   170   g_assert (g_value_get_int (&value) == G_MAXINT);
       
   171   
       
   172   g_value_unset (&value);
       
   173   g_value_init (&value, G_TYPE_UINT);
       
   174   error = collect (&value, G_MAXUINT);
       
   175   g_assert (error == NULL);
       
   176   g_assert (g_value_get_uint (&value) == G_MAXUINT);
       
   177   
       
   178   g_value_unset (&value);  
       
   179   g_value_init (&value, G_TYPE_LONG);
       
   180   error = collect (&value, G_MAXLONG);
       
   181   g_assert (error == NULL);
       
   182   g_assert (g_value_get_long (&value) == G_MAXLONG);
       
   183   
       
   184   g_value_unset (&value);
       
   185   g_value_init (&value, G_TYPE_ULONG);
       
   186   error = collect (&value, G_MAXULONG);
       
   187   g_assert (error == NULL);
       
   188   g_assert (g_value_get_ulong (&value) == G_MAXULONG);
       
   189   
       
   190   g_value_unset (&value);  
       
   191   g_value_init (&value, G_TYPE_INT64);
       
   192   error = collect (&value, G_MAXINT64);
       
   193   g_assert (error == NULL);
       
   194   g_assert (g_value_get_int64 (&value) == G_MAXINT64);
       
   195   
       
   196   g_value_unset (&value);
       
   197   g_value_init (&value, G_TYPE_UINT64);
       
   198   error = collect (&value, G_MAXUINT64);
       
   199   g_assert (error == NULL);
       
   200   g_assert (g_value_get_uint64 (&value) == G_MAXUINT64);
       
   201   
       
   202   g_value_unset (&value);
       
   203   g_value_init (&value, G_TYPE_FLOAT);
       
   204   error = collect (&value, G_MAXFLOAT);
       
   205   g_assert (error == NULL);
       
   206   g_assert (g_value_get_float (&value) == G_MAXFLOAT);
       
   207   
       
   208   g_value_unset (&value);
       
   209   g_value_init (&value, G_TYPE_DOUBLE);
       
   210   error = collect (&value, G_MAXDOUBLE);
       
   211   g_assert (error == NULL);
       
   212   g_assert (g_value_get_double (&value) == G_MAXDOUBLE);
       
   213   
       
   214   g_value_unset (&value);
       
   215   g_value_init (&value, G_TYPE_STRING);
       
   216   error = collect (&value, "string ?");
       
   217   g_assert (error == NULL);
       
   218   g_assert (strcmp (g_value_get_string (&value), "string ?") == 0);
       
   219   
       
   220   g_value_unset (&value);
       
   221   g_value_init (&value, G_TYPE_GTYPE);
       
   222   error = collect (&value, G_TYPE_BOXED);
       
   223   g_assert (error == NULL);
       
   224   g_assert (g_value_get_gtype (&value) == G_TYPE_BOXED);
       
   225 }
       
   226 
       
   227 static void
       
   228 test_copying (void)
       
   229 {
       
   230   GValue value = { 0, };
       
   231   gchar *error;
       
   232 
       
   233   {
       
   234     gchar c = 0;
       
   235 
       
   236     g_value_init (&value, G_TYPE_CHAR);
       
   237     g_value_set_char (&value, 'c');
       
   238     error = lcopy (&value, &c);
       
   239     g_assert (error == NULL);
       
   240     g_assert (c == 'c');
       
   241   }  
       
   242 
       
   243   {
       
   244     guchar c = 0;
       
   245 
       
   246     g_value_unset (&value);
       
   247     g_value_init (&value, G_TYPE_UCHAR);
       
   248     g_value_set_uchar (&value, 129);
       
   249     error = lcopy (&value, &c);
       
   250     g_assert (error == NULL);
       
   251     g_assert (c == 129);
       
   252   }  
       
   253 
       
   254   {
       
   255     gint c = 0;
       
   256 
       
   257     g_value_unset (&value);
       
   258     g_value_init (&value, G_TYPE_INT);
       
   259     g_value_set_int (&value, G_MAXINT);
       
   260     error = lcopy (&value, &c);
       
   261     g_assert (error == NULL);
       
   262     g_assert (c == G_MAXINT);
       
   263   }  
       
   264 
       
   265   {
       
   266     guint c = 0;
       
   267 
       
   268     g_value_unset (&value);
       
   269     g_value_init (&value, G_TYPE_UINT);
       
   270     g_value_set_uint (&value, G_MAXUINT);
       
   271     error = lcopy (&value, &c);
       
   272     g_assert (error == NULL);
       
   273     g_assert (c == G_MAXUINT);
       
   274   }  
       
   275 
       
   276   {
       
   277     glong c = 0;
       
   278 
       
   279     g_value_unset (&value);
       
   280     g_value_init (&value, G_TYPE_LONG);
       
   281     g_value_set_long (&value, G_MAXLONG);
       
   282     error = lcopy (&value, &c);
       
   283     g_assert (error == NULL);
       
   284     g_assert (c == G_MAXLONG);
       
   285   }  
       
   286 
       
   287   {
       
   288     gulong c = 0;
       
   289 
       
   290     g_value_unset (&value);
       
   291     g_value_init (&value, G_TYPE_ULONG);
       
   292     g_value_set_ulong (&value, G_MAXULONG);
       
   293     error = lcopy (&value, &c);
       
   294     g_assert (error == NULL);
       
   295     g_assert (c == G_MAXULONG);
       
   296   }  
       
   297 
       
   298   {
       
   299     gint64 c = 0;
       
   300 
       
   301     g_value_unset (&value);
       
   302     g_value_init (&value, G_TYPE_INT64);
       
   303     g_value_set_int64 (&value, G_MAXINT64);
       
   304     error = lcopy (&value, &c);
       
   305     g_assert (error == NULL);
       
   306     g_assert (c == G_MAXINT64);
       
   307   }  
       
   308 
       
   309   {
       
   310     guint64 c = 0;
       
   311 
       
   312     g_value_unset (&value);
       
   313     g_value_init (&value, G_TYPE_UINT64);
       
   314     g_value_set_uint64 (&value, G_MAXUINT64);
       
   315     error = lcopy (&value, &c);
       
   316     g_assert (error == NULL);
       
   317     g_assert (c == G_MAXUINT64);
       
   318   }  
       
   319 
       
   320   {
       
   321     gfloat c = 0;
       
   322 
       
   323     g_value_unset (&value);
       
   324     g_value_init (&value, G_TYPE_FLOAT);
       
   325     g_value_set_float (&value, G_MAXFLOAT);
       
   326     error = lcopy (&value, &c);
       
   327     g_assert (error == NULL);
       
   328     g_assert (c == G_MAXFLOAT);
       
   329   }  
       
   330 
       
   331   {
       
   332     gdouble c = 0;
       
   333 
       
   334     g_value_unset (&value);
       
   335     g_value_init (&value, G_TYPE_DOUBLE);
       
   336     g_value_set_double (&value, G_MAXDOUBLE);
       
   337     error = lcopy (&value, &c);
       
   338     g_assert (error == NULL);
       
   339     g_assert (c == G_MAXDOUBLE);
       
   340   }  
       
   341 
       
   342   {
       
   343     gchar *c = NULL;
       
   344 
       
   345     g_value_unset (&value);
       
   346     g_value_init (&value, G_TYPE_STRING);
       
   347     g_value_set_string (&value, "string ?");
       
   348     error = lcopy (&value, &c);
       
   349     g_assert (error == NULL);
       
   350     g_assert (strcmp (c, "string ?") == 0);
       
   351   }  
       
   352 
       
   353   {
       
   354     GType c = G_TYPE_NONE;
       
   355 
       
   356     g_value_unset (&value);
       
   357     g_value_init (&value, G_TYPE_GTYPE);
       
   358     g_value_set_gtype (&value, G_TYPE_BOXED);
       
   359     error = lcopy (&value, &c);
       
   360     g_assert (error == NULL);
       
   361     g_assert (c == G_TYPE_BOXED);
       
   362   }  
       
   363 }
       
   364 
       
   365 
       
   366 int
       
   367 main (int argc, char *argv[])
       
   368 {
       
   369   #ifdef __SYMBIAN32__
       
   370   
       
   371   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   372   g_set_print_handler(mrtPrintHandler);
       
   373   #endif /*__SYMBIAN32__*/
       
   374   g_type_init (); 
       
   375   
       
   376   test_enum_transformation ();
       
   377   test_gtype_value ();
       
   378   test_collection ();
       
   379   test_copying ();
       
   380   #if __SYMBIAN32__
       
   381   testResultXml("gvalue-test");
       
   382   #endif /* EMULATOR */
       
   383 
       
   384   return 0;
       
   385 }