os/ossrv/glib/tests/refcount/properties2.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     3 */
     4 #include <unistd.h>
     5 #include <glib.h>
     6 #include <glib-object.h>
     7 
     8 #ifdef __SYMBIAN32__
     9 #include "mrt2_glib2_test.h"
    10 #endif /*__SYMBIAN32__*/
    11 #define G_TYPE_TEST                (my_test_get_type ())
    12 #define MY_TEST(test)              (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
    13 #define MY_IS_TEST(test)           (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
    14 #define MY_TEST_CLASS(tclass)      (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
    15 #define MY_IS_TEST_CLASS(tclass)   (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
    16 #define MY_TEST_GET_CLASS(test)    (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
    17 
    18 enum {
    19   PROP_0,
    20   PROP_DUMMY
    21 };
    22 
    23 typedef struct _GTest GTest;
    24 typedef struct _GTestClass GTestClass;
    25 
    26 struct _GTest
    27 {
    28   GObject object;
    29 
    30   gint dummy;
    31 };
    32 
    33 struct _GTestClass
    34 {
    35   GObjectClass parent_class;
    36 };
    37 
    38 static GType my_test_get_type (void);
    39 
    40 static void my_test_class_init (GTestClass * klass);
    41 static void my_test_init (GTest * test);
    42 static void my_test_dispose (GObject * object);
    43 static void my_test_get_property (GObject    *object,
    44                                   guint       prop_id,
    45                                   GValue     *value,
    46                                   GParamSpec *pspec);
    47 static void my_test_set_property (GObject      *object,
    48                                   guint         prop_id,
    49                                   const GValue *value,
    50                                   GParamSpec   *pspec);
    51 
    52 static GObjectClass *parent_class = NULL;
    53 
    54 static GType
    55 my_test_get_type (void)
    56 {
    57   static GType test_type = 0;
    58 
    59   if (!test_type) {
    60     static const GTypeInfo test_info = {
    61       sizeof (GTestClass),
    62       NULL,
    63       NULL,
    64       (GClassInitFunc) my_test_class_init,
    65       NULL,
    66       NULL,
    67       sizeof (GTest),
    68       0,
    69       (GInstanceInitFunc) my_test_init,
    70       NULL
    71     };
    72 
    73     test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
    74         &test_info, 0);
    75   }
    76   return test_type;
    77 }
    78 
    79 static void
    80 my_test_class_init (GTestClass * klass)
    81 {
    82   GObjectClass *gobject_class;
    83 
    84   gobject_class = (GObjectClass *) klass;
    85 
    86   parent_class = g_type_class_ref (G_TYPE_OBJECT);
    87 
    88   gobject_class->dispose = my_test_dispose;
    89   gobject_class->get_property = my_test_get_property;
    90   gobject_class->set_property = my_test_set_property;
    91 
    92   g_object_class_install_property (gobject_class,
    93 				   PROP_DUMMY,
    94 				   g_param_spec_int ("dummy",
    95 						     NULL, 
    96 						     NULL,
    97 						     0, G_MAXINT, 0,
    98 						     G_PARAM_READWRITE));
    99 }
   100 
   101 static void
   102 my_test_init (GTest * test)
   103 {
   104   g_print ("init %p\n", test);
   105 }
   106 
   107 static void
   108 my_test_dispose (GObject * object)
   109 {
   110   GTest *test;
   111 
   112   test = MY_TEST (object);
   113 
   114   g_print ("dispose %p!\n", object);
   115 
   116   G_OBJECT_CLASS (parent_class)->dispose (object);
   117 }
   118 
   119 static void 
   120 my_test_get_property (GObject    *object,
   121                       guint       prop_id,
   122                       GValue     *value,
   123                       GParamSpec *pspec)
   124 {
   125   GTest *test;
   126 
   127   test = MY_TEST (object);
   128 
   129   switch (prop_id)
   130     {
   131     case PROP_DUMMY:
   132       g_value_set_int (value, test->dummy);
   133       break;
   134     default:
   135       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   136       break;
   137     }
   138 }
   139 
   140 static void 
   141 my_test_set_property (GObject      *object,
   142                       guint         prop_id,
   143                       const GValue *value,
   144                       GParamSpec   *pspec)
   145 {
   146   GTest *test;
   147 
   148   test = MY_TEST (object);
   149 
   150   switch (prop_id)
   151     {
   152     case PROP_DUMMY:
   153       test->dummy = g_value_get_int (value);
   154       break;
   155     default:
   156       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   157       break;
   158     }
   159 }
   160 
   161 static gint count = 0;
   162 
   163 static void
   164 dummy_notify (GObject    *object,
   165               GParamSpec *pspec)
   166 {
   167   count++;
   168   if (count % 10000 == 0)
   169     g_print (".");
   170 }
   171 
   172 static void
   173 my_test_do_property (GTest * test)
   174 {
   175   gint dummy;
   176 
   177   g_object_get (test, "dummy", &dummy, NULL);
   178   g_object_set (test, "dummy", dummy + 1, NULL);
   179 }
   180 
   181 int
   182 main (int argc, char **argv)
   183 {
   184   gint i;
   185   GTest *test;
   186 
   187   #ifdef __SYMBIAN32__
   188  
   189   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);
   190   g_set_print_handler(mrtPrintHandler);
   191   #endif /*__SYMBIAN32__*/
   192   g_thread_init (NULL);
   193   g_print ("START: %s\n", argv[0]);
   194   g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
   195   g_type_init ();
   196   
   197   test = g_object_new (G_TYPE_TEST, NULL);
   198 
   199   g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
   200 
   201   g_assert (count == test->dummy);
   202 
   203 #ifdef __SYMBIAN32__
   204   for (i=0; i<10000; i++) {
   205 #else
   206   for (i=0; i<1000000; i++) {
   207 #endif//__SYMBIAN32__  
   208     my_test_do_property (test);
   209   }
   210 
   211   g_assert (count == test->dummy);
   212 #ifdef __SYMBIAN32__
   213   testResultXml("properties2");
   214 #endif /* EMULATOR */
   215 
   216   return 0;
   217 }