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