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