glib/tests/list-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #include <glib.h>
       
    22 #ifdef __SYMBIAN32__
       
    23 #include "mrt2_glib2_test.h"
       
    24 #endif /*__SYMBIAN32__*/
       
    25 
       
    26 #define DEBUG_MSG(args)
       
    27 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  */
       
    28 #define PRINT_MSG(args)
       
    29 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
       
    30 
       
    31 #define SIZE       50
       
    32 #define NUMBER_MIN 0000
       
    33 #define NUMBER_MAX 9999
       
    34 
       
    35 
       
    36 static guint32 array[SIZE];
       
    37 
       
    38 
       
    39 static gint
       
    40 sort (gconstpointer p1, gconstpointer p2)
       
    41 {
       
    42   gint32 a, b;
       
    43 
       
    44   a = GPOINTER_TO_INT (p1);
       
    45   b = GPOINTER_TO_INT (p2);
       
    46 
       
    47   return (a > b ? +1 : a == b ? 0 : -1);
       
    48 }
       
    49 
       
    50 /*
       
    51  * glist sort tests
       
    52  */
       
    53 static void
       
    54 test_list_sort (void)
       
    55 {
       
    56   GList *list = NULL;
       
    57   gint   i;
       
    58 
       
    59   PRINT_MSG (("testing g_list_sort()"));
       
    60 
       
    61   for (i = 0; i < SIZE; i++) {
       
    62     list = g_list_append (list, GINT_TO_POINTER (array[i]));
       
    63   }
       
    64 
       
    65   list = g_list_sort (list, sort);
       
    66   for (i = 0; i < SIZE - 1; i++) {
       
    67     gpointer p1, p2;
       
    68 
       
    69     p1 = g_list_nth_data (list, i);
       
    70     p2 = g_list_nth_data (list, i+1);
       
    71 
       
    72     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
    73     DEBUG_MSG (("list_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
    74   }
       
    75 
       
    76   g_list_free (list);
       
    77 }
       
    78 
       
    79 static void
       
    80 test_list_sort_with_data (void)
       
    81 {
       
    82   GList *list = NULL;
       
    83   gint   i;
       
    84 
       
    85   PRINT_MSG (("testing g_list_sort_with_data()"));
       
    86 
       
    87   for (i = 0; i < SIZE; i++) {
       
    88     list = g_list_append (list, GINT_TO_POINTER (array[i]));
       
    89   }
       
    90 
       
    91   list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
       
    92   for (i = 0; i < SIZE - 1; i++) {
       
    93     gpointer p1, p2;
       
    94 
       
    95     p1 = g_list_nth_data (list, i);
       
    96     p2 = g_list_nth_data (list, i+1);
       
    97 
       
    98     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
    99     DEBUG_MSG (("list_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
   100   }
       
   101 
       
   102   g_list_free (list);
       
   103 }
       
   104 
       
   105 static void
       
   106 test_list_insert_sorted (void)
       
   107 {
       
   108   GList *list = NULL;
       
   109   gint   i;
       
   110 
       
   111   PRINT_MSG (("testing g_list_insert_sorted()"));
       
   112 
       
   113   for (i = 0; i < SIZE; i++) {
       
   114     list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
       
   115   }
       
   116 
       
   117   for (i = 0; i < SIZE - 1; i++) {
       
   118     gpointer p1, p2;
       
   119 
       
   120     p1 = g_list_nth_data (list, i);
       
   121     p2 = g_list_nth_data (list, i+1);
       
   122 
       
   123     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
   124     DEBUG_MSG (("list_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
   125   }
       
   126 
       
   127   g_list_free (list);
       
   128 }
       
   129 
       
   130 static void
       
   131 test_list_insert_sorted_with_data (void)
       
   132 {
       
   133   GList *list = NULL;
       
   134   gint   i;
       
   135 
       
   136   PRINT_MSG (("testing g_list_insert_sorted_with_data()"));
       
   137 
       
   138   for (i = 0; i < SIZE; i++) {
       
   139     list = g_list_insert_sorted_with_data (list, 
       
   140 					   GINT_TO_POINTER (array[i]), 
       
   141 					   (GCompareDataFunc)sort, 
       
   142 					   NULL);
       
   143   }
       
   144 
       
   145   for (i = 0; i < SIZE - 1; i++) {
       
   146     gpointer p1, p2;
       
   147 
       
   148     p1 = g_list_nth_data (list, i);
       
   149     p2 = g_list_nth_data (list, i+1);
       
   150 
       
   151     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
   152     DEBUG_MSG (("list_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
   153   }
       
   154 
       
   155   g_list_free (list);
       
   156 }
       
   157 
       
   158 static void
       
   159 test_list_reverse (void)
       
   160 {
       
   161   GList *list = NULL;
       
   162   GList *st;
       
   163   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       
   164   gint   i;
       
   165 
       
   166   PRINT_MSG (("testing g_list_reverse()"));
       
   167 
       
   168   for (i = 0; i < 10; i++) {
       
   169     list = g_list_append (list, &nums[i]);
       
   170   }
       
   171 
       
   172   list = g_list_reverse (list);
       
   173 
       
   174   for (i = 0; i < 10; i++) {
       
   175     st = g_list_nth (list, i);
       
   176     g_assert (*((gint*) st->data) == (9 - i));
       
   177   }
       
   178 
       
   179   g_list_free (list);
       
   180 }
       
   181 
       
   182 static void
       
   183 test_list_nth (void)
       
   184 {
       
   185   GList *list = NULL;
       
   186   GList *st;
       
   187   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       
   188   gint   i;
       
   189 
       
   190   PRINT_MSG (("testing g_list_nth()"));
       
   191 
       
   192   for (i = 0; i < 10; i++) {
       
   193     list = g_list_append (list, &nums[i]);
       
   194   }
       
   195 
       
   196   for (i = 0; i < 10; i++) {
       
   197     st = g_list_nth (list, i);
       
   198     g_assert (*((gint*) st->data) == i);
       
   199   }
       
   200 
       
   201   g_list_free (list);
       
   202 }
       
   203 
       
   204 int
       
   205 main (int argc, char *argv[])
       
   206 {
       
   207   gint i;
       
   208 
       
   209   DEBUG_MSG (("debugging messages turned on"));
       
   210 
       
   211   DEBUG_MSG (("creating %d random numbers", SIZE));
       
   212   #ifdef __SYMBIAN32__
       
   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);
       
   214   g_set_print_handler(mrtPrintHandler);
       
   215   #endif /*__SYMBIAN32__*/
       
   216 	  
       
   217 
       
   218   /* Create an array of random numbers. */
       
   219   for (i = 0; i < SIZE; i++) {
       
   220     array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
       
   221     DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
       
   222   }
       
   223 
       
   224   /* Start tests. */
       
   225   test_list_sort ();
       
   226   test_list_sort_with_data ();
       
   227 
       
   228   test_list_insert_sorted ();
       
   229   test_list_insert_sorted_with_data ();
       
   230 
       
   231   test_list_reverse ();
       
   232   test_list_nth ();
       
   233 
       
   234   PRINT_MSG (("testing finished"));
       
   235   #ifdef __SYMBIAN32__
       
   236   testResultXml("list-test");
       
   237   #endif /* EMULATOR */
       
   238 
       
   239   return 0;
       
   240 }