os/ossrv/glib/tests/refcount/properties.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tests/refcount/properties.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,269 @@
     1.4 +/*
     1.5 +* Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     1.6 +*/
     1.7 +#include <unistd.h>
     1.8 +#include <glib.h>
     1.9 +#include <glib-object.h>
    1.10 +
    1.11 +#ifdef __SYMBIAN32__
    1.12 +#include <glib_global.h>
    1.13 +#include "mrt2_glib2_test.h"
    1.14 +#endif /*__SYMBIAN32__*/
    1.15 +#define G_TYPE_TEST               (my_test_get_type ())
    1.16 +#define MY_TEST(test)              (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
    1.17 +#define MY_IS_TEST(test)           (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
    1.18 +#define MY_TEST_CLASS(tclass)      (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
    1.19 +#define MY_IS_TEST_CLASS(tclass)   (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
    1.20 +#define MY_TEST_GET_CLASS(test)    (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
    1.21 +
    1.22 +enum {
    1.23 +  PROP_0,
    1.24 +  PROP_DUMMY
    1.25 +};
    1.26 +
    1.27 +typedef struct _GTest GTest;
    1.28 +typedef struct _GTestClass GTestClass;
    1.29 +
    1.30 +struct _GTest
    1.31 +{
    1.32 +  GObject object;
    1.33 +  gint id;
    1.34 +  gint dummy;
    1.35 +
    1.36 +  gint count;
    1.37 +};
    1.38 +
    1.39 +struct _GTestClass
    1.40 +{
    1.41 +  GObjectClass parent_class;
    1.42 +};
    1.43 +
    1.44 +static GType my_test_get_type (void);
    1.45 +static volatile gboolean stopping;
    1.46 +
    1.47 +static void my_test_class_init (GTestClass * klass);
    1.48 +static void my_test_init (GTest * test);
    1.49 +static void my_test_dispose (GObject * object);
    1.50 +static void my_test_get_property (GObject    *object,
    1.51 +                                  guint       prop_id,
    1.52 +                                  GValue     *value,
    1.53 +                                  GParamSpec *pspec);
    1.54 +static void my_test_set_property (GObject      *object,
    1.55 +                                  guint         prop_id,
    1.56 +                                  const GValue *value,
    1.57 +                                  GParamSpec   *pspec);
    1.58 +
    1.59 +static GObjectClass *parent_class = NULL;
    1.60 +
    1.61 +static GType
    1.62 +my_test_get_type (void)
    1.63 +{
    1.64 +  static GType test_type = 0;
    1.65 +
    1.66 +  if (!test_type) {
    1.67 +    static const GTypeInfo test_info = {
    1.68 +      sizeof (GTestClass),
    1.69 +      NULL,
    1.70 +      NULL,
    1.71 +      (GClassInitFunc) my_test_class_init,
    1.72 +      NULL,
    1.73 +      NULL,
    1.74 +      sizeof (GTest),
    1.75 +      0,
    1.76 +      (GInstanceInitFunc) my_test_init,
    1.77 +      NULL
    1.78 +    };
    1.79 +
    1.80 +    test_type = g_type_register_static (G_TYPE_OBJECT, "GTest", &test_info, 0);
    1.81 +  }
    1.82 +  return test_type;
    1.83 +}
    1.84 +
    1.85 +static void
    1.86 +my_test_class_init (GTestClass * klass)
    1.87 +{
    1.88 +  GObjectClass *gobject_class;
    1.89 +
    1.90 +  gobject_class = (GObjectClass *) klass;
    1.91 +
    1.92 +  parent_class = g_type_class_ref (G_TYPE_OBJECT);
    1.93 +
    1.94 +  gobject_class->dispose = my_test_dispose;
    1.95 +  gobject_class->get_property = my_test_get_property;
    1.96 +  gobject_class->set_property = my_test_set_property;
    1.97 +
    1.98 +  g_object_class_install_property (gobject_class,
    1.99 +				   PROP_DUMMY,
   1.100 +				   g_param_spec_int ("dummy",
   1.101 +						     NULL, 
   1.102 +						     NULL,
   1.103 +						     0, G_MAXINT, 0,
   1.104 +						     G_PARAM_READWRITE));
   1.105 +}
   1.106 +
   1.107 +static void
   1.108 +my_test_init (GTest * test)
   1.109 +{
   1.110 +  static guint static_id = 1;
   1.111 +  test->id = static_id++;
   1.112 +}
   1.113 +
   1.114 +static void
   1.115 +my_test_dispose (GObject * object)
   1.116 +{
   1.117 +  GTest *test;
   1.118 +
   1.119 +  test = MY_TEST (object);
   1.120 +
   1.121 +  G_OBJECT_CLASS (parent_class)->dispose (object);
   1.122 +}
   1.123 +
   1.124 +static void 
   1.125 +my_test_get_property (GObject    *object,
   1.126 +                      guint       prop_id,
   1.127 +                      GValue     *value,
   1.128 +                      GParamSpec *pspec)
   1.129 +{
   1.130 +  GTest *test;
   1.131 +
   1.132 +  test = MY_TEST (object);
   1.133 +
   1.134 +  switch (prop_id)
   1.135 +    {
   1.136 +    case PROP_DUMMY:
   1.137 +      g_value_set_int (value, test->dummy);
   1.138 +      break;
   1.139 +    default:
   1.140 +      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   1.141 +      break;
   1.142 +    }
   1.143 +}
   1.144 +
   1.145 +static void 
   1.146 +my_test_set_property (GObject      *object,
   1.147 +                      guint         prop_id,
   1.148 +                      const GValue *value,
   1.149 +                      GParamSpec   *pspec)
   1.150 +{
   1.151 +  GTest *test;
   1.152 +
   1.153 +  test = MY_TEST (object);
   1.154 +
   1.155 +  switch (prop_id)
   1.156 +    {
   1.157 +    case PROP_DUMMY:
   1.158 +      test->dummy = g_value_get_int (value);
   1.159 +      break;
   1.160 +    default:
   1.161 +      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
   1.162 +      break;
   1.163 +    }
   1.164 +}
   1.165 +
   1.166 +static void
   1.167 +dummy_notify (GObject    *object,
   1.168 +              GParamSpec *pspec)
   1.169 +{
   1.170 +  GTest *test;
   1.171 +
   1.172 +  test = MY_TEST (object);
   1.173 +
   1.174 +  test->count++;  
   1.175 +}
   1.176 +
   1.177 +static void
   1.178 +my_test_do_property (GTest * test)
   1.179 +{
   1.180 +  gint dummy;
   1.181 +
   1.182 +  g_object_get (test, "dummy", &dummy, NULL);
   1.183 +  g_object_set (test, "dummy", dummy + 1, NULL);
   1.184 +}
   1.185 +
   1.186 +static gpointer
   1.187 +run_thread (GTest * test)
   1.188 +{
   1.189 +  gint i = 1;
   1.190 +  
   1.191 +  while (!stopping) {
   1.192 +    my_test_do_property (test);
   1.193 +    if ((i++ % 10000) == 0)
   1.194 +      {
   1.195 +        g_print (".%c", 'a' + test->id);
   1.196 +        g_thread_yield(); /* force context switch */
   1.197 +      }
   1.198 +  }
   1.199 +
   1.200 +  return NULL;
   1.201 +}
   1.202 +
   1.203 +int
   1.204 +main (int argc, char **argv)
   1.205 +{
   1.206 +  gint i;
   1.207 +  GArray *test_objects;
   1.208 +  GArray *test_threads;
   1.209 +  const gint n_threads = 5;
   1.210 +
   1.211 +  #ifdef __SYMBIAN32__
   1.212 +  
   1.213 +  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);
   1.214 +  g_set_print_handler(mrtPrintHandler);
   1.215 +  #endif /*__SYMBIAN32__*/
   1.216 +  g_thread_init (NULL);
   1.217 +  g_print ("START: %s\n", argv[0]);
   1.218 +  g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
   1.219 +  g_type_init ();
   1.220 +
   1.221 +  test_objects = g_array_new (FALSE, FALSE, sizeof (GTest *));
   1.222 +
   1.223 +  for (i = 0; i < n_threads; i++) {
   1.224 +    GTest *test;
   1.225 +    
   1.226 +    test = g_object_new (G_TYPE_TEST, NULL);
   1.227 +    g_array_append_val (test_objects, test);
   1.228 +
   1.229 +    g_assert (test->count == test->dummy);
   1.230 +    g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
   1.231 +  }
   1.232 +    
   1.233 +  test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
   1.234 +
   1.235 +  stopping = FALSE;
   1.236 +
   1.237 +  for (i = 0; i < n_threads; i++) {
   1.238 +    GThread *thread;
   1.239 +    GTest *test;
   1.240 +
   1.241 +    test = g_array_index (test_objects, GTest *, i);
   1.242 +
   1.243 +    thread = g_thread_create ((GThreadFunc) run_thread, test, TRUE, NULL);
   1.244 +    g_array_append_val (test_threads, thread);
   1.245 +  }
   1.246 +  g_usleep (3000000);
   1.247 +
   1.248 +  stopping = TRUE;
   1.249 +  g_print ("\nstopping\n");
   1.250 +
   1.251 +  /* join all threads */
   1.252 +  for (i = 0; i < n_threads; i++) {
   1.253 +    GThread *thread;
   1.254 +
   1.255 +    thread = g_array_index (test_threads, GThread *, i);
   1.256 +    g_thread_join (thread);
   1.257 +  }
   1.258 +
   1.259 +  g_print ("stopped\n");
   1.260 +
   1.261 +  for (i = 0; i < n_threads; i++) {
   1.262 +    GTest *test;
   1.263 +
   1.264 +    test = g_array_index (test_objects, GTest *, i);
   1.265 +
   1.266 +    g_assert (test->count == test->dummy);
   1.267 +  }
   1.268 +#ifdef __SYMBIAN32__
   1.269 +  testResultXml("properties");
   1.270 +#endif /* EMULATOR */
   1.271 +  return 0;
   1.272 +}