1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/slist-test.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,218 @@
1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
1.5 +#undef G_DISABLE_ASSERT
1.6 +#undef G_LOG_DOMAIN
1.7 +
1.8 +#include <glib.h>
1.9 +
1.10 +#define DEBUG_MSG(args)
1.11 +/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
1.12 +#define PRINT_MSG(args)
1.13 +/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
1.14 +
1.15 +#define SIZE 50
1.16 +#define NUMBER_MIN 0000
1.17 +#define NUMBER_MAX 9999
1.18 +
1.19 +#ifdef SYMBIAN
1.20 +#include "mrt2_glib2_test.h"
1.21 +#endif /*SYMBIAN*/
1.22 +
1.23 +
1.24 +static guint32 array[SIZE];
1.25 +
1.26 +
1.27 +static gint
1.28 +sort (gconstpointer p1, gconstpointer p2)
1.29 +{
1.30 + gint32 a, b;
1.31 +
1.32 + a = GPOINTER_TO_INT (p1);
1.33 + b = GPOINTER_TO_INT (p2);
1.34 +
1.35 + return (a > b ? +1 : a == b ? 0 : -1);
1.36 +}
1.37 +
1.38 +/*
1.39 + * gslist sort tests
1.40 + */
1.41 +static void
1.42 +test_slist_sort (void)
1.43 +{
1.44 + GSList *slist = NULL;
1.45 + gint i;
1.46 +
1.47 + PRINT_MSG (("testing g_slist_sort()"));
1.48 +
1.49 + for (i = 0; i < SIZE; i++) {
1.50 + slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
1.51 + }
1.52 +
1.53 + slist = g_slist_sort (slist, sort);
1.54 + for (i = 0; i < SIZE - 1; i++) {
1.55 + gpointer p1, p2;
1.56 +
1.57 + p1 = g_slist_nth_data (slist, i);
1.58 + p2 = g_slist_nth_data (slist, i+1);
1.59 +
1.60 + g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
1.61 + DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
1.62 + }
1.63 +}
1.64 +
1.65 +static void
1.66 +test_slist_sort_with_data (void)
1.67 +{
1.68 + GSList *slist = NULL;
1.69 + gint i;
1.70 +
1.71 + PRINT_MSG (("testing g_slist_sort_with_data()"));
1.72 +
1.73 + for (i = 0; i < SIZE; i++) {
1.74 + slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
1.75 + }
1.76 +
1.77 + slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
1.78 + for (i = 0; i < SIZE - 1; i++) {
1.79 + gpointer p1, p2;
1.80 +
1.81 + p1 = g_slist_nth_data (slist, i);
1.82 + p2 = g_slist_nth_data (slist, i+1);
1.83 +
1.84 + g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
1.85 + DEBUG_MSG (("slist_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
1.86 + }
1.87 +}
1.88 +
1.89 +static void
1.90 +test_slist_insert_sorted (void)
1.91 +{
1.92 + GSList *slist = NULL;
1.93 + gint i;
1.94 +
1.95 + PRINT_MSG (("testing g_slist_insert_sorted()"));
1.96 +
1.97 + for (i = 0; i < SIZE; i++) {
1.98 + slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
1.99 + }
1.100 +
1.101 + for (i = 0; i < SIZE - 1; i++) {
1.102 + gpointer p1, p2;
1.103 +
1.104 + p1 = g_slist_nth_data (slist, i);
1.105 + p2 = g_slist_nth_data (slist, i+1);
1.106 +
1.107 + g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
1.108 + DEBUG_MSG (("slist_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
1.109 + }
1.110 +}
1.111 +
1.112 +static void
1.113 +test_slist_insert_sorted_with_data (void)
1.114 +{
1.115 + GSList *slist = NULL;
1.116 + gint i;
1.117 +
1.118 + PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
1.119 +
1.120 + for (i = 0; i < SIZE; i++) {
1.121 + slist = g_slist_insert_sorted_with_data (slist,
1.122 + GINT_TO_POINTER (array[i]),
1.123 + (GCompareDataFunc)sort,
1.124 + NULL);
1.125 + }
1.126 +
1.127 + for (i = 0; i < SIZE - 1; i++) {
1.128 + gpointer p1, p2;
1.129 +
1.130 + p1 = g_slist_nth_data (slist, i);
1.131 + p2 = g_slist_nth_data (slist, i+1);
1.132 +
1.133 + g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
1.134 +}
1.135 +}
1.136 +static void
1.137 +test_slist_reverse (void)
1.138 +{
1.139 + GSList *slist = NULL;
1.140 + GSList *st;
1.141 + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1.142 + gint i;
1.143 +
1.144 + PRINT_MSG (("testing g_slist_reverse()"));
1.145 +
1.146 + for (i = 0; i < 10; i++) {
1.147 + slist = g_slist_append (slist, &nums[i]);
1.148 + }
1.149 +
1.150 + slist = g_slist_reverse (slist);
1.151 +
1.152 + for (i = 0; i < 10; i++) {
1.153 + st = g_slist_nth (slist, i);
1.154 + g_assert (*((gint*) st->data) == (9 - i));
1.155 + }
1.156 +
1.157 + g_slist_free (slist);
1.158 +}
1.159 +
1.160 +static void
1.161 +test_slist_nth (void)
1.162 +{
1.163 + GSList *slist = NULL;
1.164 + GSList *st;
1.165 + gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1.166 + gint i;
1.167 +
1.168 + PRINT_MSG (("testing g_slist_nth()"));
1.169 +
1.170 + for (i = 0; i < 10; i++) {
1.171 + slist = g_slist_append (slist, &nums[i]);
1.172 + }
1.173 +
1.174 + for (i = 0; i < 10; i++) {
1.175 + st = g_slist_nth (slist, i);
1.176 + g_assert (*((gint*) st->data) == i);
1.177 + }
1.178 +
1.179 + g_slist_free (slist);
1.180 +}
1.181 +
1.182 +int
1.183 +main (int argc, char *argv[])
1.184 +{
1.185 + gint i;
1.186 +
1.187 + #ifdef SYMBIAN
1.188 + 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.189 + g_set_print_handler(mrtPrintHandler);
1.190 + #endif /*SYMBIAN*/
1.191 +
1.192 +
1.193 +
1.194 +
1.195 + DEBUG_MSG (("debugging messages turned on"));
1.196 +
1.197 + DEBUG_MSG (("creating %d random numbers", SIZE));
1.198 +
1.199 + /* Create an array of random numbers. */
1.200 + for (i = 0; i < SIZE; i++) {
1.201 + array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
1.202 + DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
1.203 + }
1.204 +
1.205 + /* Start tests. */
1.206 + test_slist_sort ();
1.207 + test_slist_sort_with_data ();
1.208 +
1.209 + test_slist_insert_sorted ();
1.210 + test_slist_insert_sorted_with_data ();
1.211 +
1.212 + test_slist_reverse ();
1.213 + test_slist_nth ();
1.214 +
1.215 + PRINT_MSG (("testing finished"));
1.216 +
1.217 +#ifdef SYMBIAN
1.218 + testResultXml("slist-test");
1.219 +#endif /* EMULATOR */
1.220 + return 0;
1.221 +}