glib/tests/slist-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 
       
    23 #define DEBUG_MSG(args) 
       
    24 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
       
    25 #define PRINT_MSG(args) 
       
    26 /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
       
    27 
       
    28 #define SIZE       50
       
    29 #define NUMBER_MIN 0000
       
    30 #define NUMBER_MAX 9999
       
    31 
       
    32 #ifdef __SYMBIAN32__
       
    33 #include "mrt2_glib2_test.h"
       
    34 #endif /*__SYMBIAN32__*/
       
    35 
       
    36 
       
    37 static guint32 array[SIZE];
       
    38 
       
    39 
       
    40 static gint
       
    41 sort (gconstpointer p1, gconstpointer p2)
       
    42 {
       
    43   gint32 a, b;
       
    44 
       
    45   a = GPOINTER_TO_INT (p1);
       
    46   b = GPOINTER_TO_INT (p2);
       
    47 
       
    48   return (a > b ? +1 : a == b ? 0 : -1);
       
    49 }
       
    50 
       
    51 /*
       
    52  * gslist sort tests
       
    53  */
       
    54 static void
       
    55 test_slist_sort (void)
       
    56 {
       
    57   GSList *slist = NULL;
       
    58   gint    i;
       
    59 
       
    60   PRINT_MSG (("testing g_slist_sort()"));
       
    61 
       
    62   for (i = 0; i < SIZE; i++) {
       
    63     slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
       
    64   }
       
    65 
       
    66   slist = g_slist_sort (slist, sort);
       
    67   for (i = 0; i < SIZE - 1; i++) {
       
    68     gpointer p1, p2;
       
    69 
       
    70     p1 = g_slist_nth_data (slist, i);
       
    71     p2 = g_slist_nth_data (slist, i+1);
       
    72 
       
    73     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
    74     DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
    75   }
       
    76 }
       
    77 
       
    78 static void
       
    79 test_slist_sort_with_data (void)
       
    80 {
       
    81   GSList *slist = NULL;
       
    82   gint    i;
       
    83 
       
    84   PRINT_MSG (("testing g_slist_sort_with_data()"));
       
    85 
       
    86   for (i = 0; i < SIZE; i++) {
       
    87     slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
       
    88   }
       
    89 
       
    90   slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
       
    91   for (i = 0; i < SIZE - 1; i++) {
       
    92     gpointer p1, p2;
       
    93 
       
    94     p1 = g_slist_nth_data (slist, i);
       
    95     p2 = g_slist_nth_data (slist, i+1);
       
    96 
       
    97     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
    98     DEBUG_MSG (("slist_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
    99   }
       
   100 }
       
   101 
       
   102 static void
       
   103 test_slist_insert_sorted (void)
       
   104 {
       
   105   GSList *slist = NULL;
       
   106   gint    i;
       
   107 
       
   108   PRINT_MSG (("testing g_slist_insert_sorted()"));
       
   109 
       
   110   for (i = 0; i < SIZE; i++) {
       
   111     slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
       
   112   }
       
   113 
       
   114   for (i = 0; i < SIZE - 1; i++) {
       
   115     gpointer p1, p2;
       
   116 
       
   117     p1 = g_slist_nth_data (slist, i);
       
   118     p2 = g_slist_nth_data (slist, i+1);
       
   119 
       
   120     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
   121     DEBUG_MSG (("slist_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
   122   }
       
   123 }
       
   124 
       
   125 static void
       
   126 test_slist_insert_sorted_with_data (void)
       
   127 {
       
   128   GSList *slist = NULL;
       
   129   gint    i;
       
   130 
       
   131   PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
       
   132 
       
   133   for (i = 0; i < SIZE; i++) {
       
   134     slist = g_slist_insert_sorted_with_data (slist, 
       
   135 					   GINT_TO_POINTER (array[i]), 
       
   136 					   (GCompareDataFunc)sort, 
       
   137 					   NULL);
       
   138   }
       
   139 
       
   140   for (i = 0; i < SIZE - 1; i++) {
       
   141     gpointer p1, p2;
       
   142 
       
   143     p1 = g_slist_nth_data (slist, i);
       
   144     p2 = g_slist_nth_data (slist, i+1);
       
   145 
       
   146     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
       
   147     DEBUG_MSG (("slist_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
       
   148   }
       
   149 }
       
   150 
       
   151 static void
       
   152 test_slist_reverse (void)
       
   153 {
       
   154   GSList *slist = NULL;
       
   155   GSList *st;
       
   156   gint    nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       
   157   gint    i;
       
   158 
       
   159   PRINT_MSG (("testing g_slist_reverse()"));
       
   160 
       
   161   for (i = 0; i < 10; i++) {
       
   162     slist = g_slist_append (slist, &nums[i]);
       
   163   }
       
   164 
       
   165   slist = g_slist_reverse (slist);
       
   166 
       
   167   for (i = 0; i < 10; i++) {
       
   168     st = g_slist_nth (slist, i);
       
   169     g_assert (*((gint*) st->data) == (9 - i));
       
   170   }
       
   171 
       
   172   g_slist_free (slist);
       
   173 }
       
   174 
       
   175 static void
       
   176 test_slist_nth (void)
       
   177 {
       
   178   GSList *slist = NULL;
       
   179   GSList *st;
       
   180   gint    nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       
   181   gint    i;
       
   182 
       
   183   PRINT_MSG (("testing g_slist_nth()"));
       
   184 
       
   185   for (i = 0; i < 10; i++) {
       
   186     slist = g_slist_append (slist, &nums[i]);
       
   187   }
       
   188 
       
   189   for (i = 0; i < 10; i++) {
       
   190     st = g_slist_nth (slist, i);
       
   191     g_assert (*((gint*) st->data) == i);
       
   192   }
       
   193 
       
   194   g_slist_free (slist);
       
   195 }
       
   196 
       
   197 int
       
   198 main (int argc, char *argv[])
       
   199 {
       
   200   gint i;
       
   201 
       
   202   #ifdef __SYMBIAN32__
       
   203   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);
       
   204   g_set_print_handler(mrtPrintHandler);
       
   205   #endif /*__SYMBIAN32__*/
       
   206 
       
   207   
       
   208   
       
   209 
       
   210   DEBUG_MSG (("debugging messages turned on"));
       
   211 
       
   212   DEBUG_MSG (("creating %d random numbers", SIZE));
       
   213 
       
   214   /* Create an array of random numbers. */
       
   215   for (i = 0; i < SIZE; i++) {
       
   216     array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
       
   217     DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
       
   218   }
       
   219 
       
   220   /* Start tests. */
       
   221   test_slist_sort ();
       
   222   test_slist_sort_with_data ();
       
   223 
       
   224   test_slist_insert_sorted ();
       
   225   test_slist_insert_sorted_with_data ();
       
   226 
       
   227   test_slist_reverse ();
       
   228   test_slist_nth ();
       
   229 
       
   230   PRINT_MSG (("testing finished"));
       
   231 
       
   232 #ifdef __SYMBIAN32__
       
   233   testResultXml("slist-test");
       
   234 #endif /* EMULATOR */
       
   235   return 0;
       
   236 }