sl@0: /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
sl@0: #undef G_DISABLE_ASSERT
sl@0: #undef G_LOG_DOMAIN
sl@0: 
sl@0: #include <glib.h>
sl@0: 
sl@0: #define DEBUG_MSG(args) 
sl@0: /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
sl@0: #define PRINT_MSG(args) 
sl@0: /* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
sl@0: 
sl@0: #define SIZE       50
sl@0: #define NUMBER_MIN 0000
sl@0: #define NUMBER_MAX 9999
sl@0: 
sl@0: #ifdef SYMBIAN
sl@0: #include "mrt2_glib2_test.h"
sl@0: #endif /*SYMBIAN*/
sl@0: 
sl@0: 
sl@0: static guint32 array[SIZE];
sl@0: 
sl@0: 
sl@0: static gint
sl@0: sort (gconstpointer p1, gconstpointer p2)
sl@0: {
sl@0:   gint32 a, b;
sl@0: 
sl@0:   a = GPOINTER_TO_INT (p1);
sl@0:   b = GPOINTER_TO_INT (p2);
sl@0: 
sl@0:   return (a > b ? +1 : a == b ? 0 : -1);
sl@0: }
sl@0: 
sl@0: /*
sl@0:  * gslist sort tests
sl@0:  */
sl@0: static void
sl@0: test_slist_sort (void)
sl@0: {
sl@0:   GSList *slist = NULL;
sl@0:   gint    i;
sl@0: 
sl@0:   PRINT_MSG (("testing g_slist_sort()"));
sl@0: 
sl@0:   for (i = 0; i < SIZE; i++) {
sl@0:     slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
sl@0:   }
sl@0: 
sl@0:   slist = g_slist_sort (slist, sort);
sl@0:   for (i = 0; i < SIZE - 1; i++) {
sl@0:     gpointer p1, p2;
sl@0: 
sl@0:     p1 = g_slist_nth_data (slist, i);
sl@0:     p2 = g_slist_nth_data (slist, i+1);
sl@0: 
sl@0:     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
sl@0:     DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
sl@0:   }
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_slist_sort_with_data (void)
sl@0: {
sl@0:   GSList *slist = NULL;
sl@0:   gint    i;
sl@0: 
sl@0:   PRINT_MSG (("testing g_slist_sort_with_data()"));
sl@0: 
sl@0:   for (i = 0; i < SIZE; i++) {
sl@0:     slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
sl@0:   }
sl@0: 
sl@0:   slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
sl@0:   for (i = 0; i < SIZE - 1; i++) {
sl@0:     gpointer p1, p2;
sl@0: 
sl@0:     p1 = g_slist_nth_data (slist, i);
sl@0:     p2 = g_slist_nth_data (slist, i+1);
sl@0: 
sl@0:     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
sl@0:     DEBUG_MSG (("slist_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
sl@0:   }
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_slist_insert_sorted (void)
sl@0: {
sl@0:   GSList *slist = NULL;
sl@0:   gint    i;
sl@0: 
sl@0:   PRINT_MSG (("testing g_slist_insert_sorted()"));
sl@0: 
sl@0:   for (i = 0; i < SIZE; i++) {
sl@0:     slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
sl@0:   }
sl@0: 
sl@0:   for (i = 0; i < SIZE - 1; i++) {
sl@0:     gpointer p1, p2;
sl@0: 
sl@0:     p1 = g_slist_nth_data (slist, i);
sl@0:     p2 = g_slist_nth_data (slist, i+1);
sl@0: 
sl@0:     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
sl@0:     DEBUG_MSG (("slist_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
sl@0:   }
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_slist_insert_sorted_with_data (void)
sl@0: {
sl@0:   GSList *slist = NULL;
sl@0:   gint    i;
sl@0: 
sl@0:   PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
sl@0: 
sl@0:   for (i = 0; i < SIZE; i++) {
sl@0:     slist = g_slist_insert_sorted_with_data (slist, 
sl@0: 					   GINT_TO_POINTER (array[i]), 
sl@0: 					   (GCompareDataFunc)sort, 
sl@0: 					   NULL);
sl@0:   }
sl@0: 
sl@0:   for (i = 0; i < SIZE - 1; i++) {
sl@0:     gpointer p1, p2;
sl@0: 
sl@0:     p1 = g_slist_nth_data (slist, i);
sl@0:     p2 = g_slist_nth_data (slist, i+1);
sl@0: 
sl@0:     g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
sl@0: }
sl@0: }
sl@0: static void
sl@0: test_slist_reverse (void)
sl@0: {
sl@0:   GSList *slist = NULL;
sl@0:   GSList *st;
sl@0:   gint    nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
sl@0:   gint    i;
sl@0: 
sl@0:   PRINT_MSG (("testing g_slist_reverse()"));
sl@0: 
sl@0:   for (i = 0; i < 10; i++) {
sl@0:     slist = g_slist_append (slist, &nums[i]);
sl@0:   }
sl@0: 
sl@0:   slist = g_slist_reverse (slist);
sl@0: 
sl@0:   for (i = 0; i < 10; i++) {
sl@0:     st = g_slist_nth (slist, i);
sl@0:     g_assert (*((gint*) st->data) == (9 - i));
sl@0:   }
sl@0: 
sl@0:   g_slist_free (slist);
sl@0: }
sl@0: 
sl@0: static void
sl@0: test_slist_nth (void)
sl@0: {
sl@0:   GSList *slist = NULL;
sl@0:   GSList *st;
sl@0:   gint    nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
sl@0:   gint    i;
sl@0: 
sl@0:   PRINT_MSG (("testing g_slist_nth()"));
sl@0: 
sl@0:   for (i = 0; i < 10; i++) {
sl@0:     slist = g_slist_append (slist, &nums[i]);
sl@0:   }
sl@0: 
sl@0:   for (i = 0; i < 10; i++) {
sl@0:     st = g_slist_nth (slist, i);
sl@0:     g_assert (*((gint*) st->data) == i);
sl@0:   }
sl@0: 
sl@0:   g_slist_free (slist);
sl@0: }
sl@0: 
sl@0: int
sl@0: main (int argc, char *argv[])
sl@0: {
sl@0: 	gint i;
sl@0: 
sl@0:   #ifdef SYMBIAN
sl@0:   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:   g_set_print_handler(mrtPrintHandler);
sl@0:   #endif /*SYMBIAN*/
sl@0: 
sl@0:   
sl@0:   
sl@0: 
sl@0:   DEBUG_MSG (("debugging messages turned on"));
sl@0: 
sl@0:   DEBUG_MSG (("creating %d random numbers", SIZE));
sl@0: 
sl@0:   /* Create an array of random numbers. */
sl@0:   for (i = 0; i < SIZE; i++) {
sl@0:     array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
sl@0:     DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
sl@0:   }
sl@0: 
sl@0:   /* Start tests. */
sl@0:   test_slist_sort ();
sl@0:   test_slist_sort_with_data ();
sl@0: 
sl@0:   test_slist_insert_sorted ();
sl@0:   test_slist_insert_sorted_with_data ();
sl@0: 
sl@0:   test_slist_reverse ();
sl@0:   test_slist_nth ();
sl@0: 
sl@0:   PRINT_MSG (("testing finished"));
sl@0: 
sl@0: #ifdef SYMBIAN
sl@0:   testResultXml("slist-test");
sl@0: #endif /* EMULATOR */
sl@0:   return 0;
sl@0: }