Updated to test _sort, _sort_with_data, _insert_sorted and

* tests/asyncqueue-test.c:
* tests/list-test.c:
* tests/slist-test.c: Updated to test _sort, _sort_with_data,
_insert_sorted and _insert_sorted_with_data API.
This commit is contained in:
Martyn James Russell 2006-02-19 17:17:32 +00:00
parent c773a618e3
commit dde67f284d
7 changed files with 433 additions and 289 deletions

View File

@ -1,3 +1,10 @@
2006-02-19 Martyn Russell <martyn@imendio.com>
* tests/asyncqueue-test.c:
* tests/list-test.c:
* tests/slist-test.c: Updated to test _sort, _sort_with_data,
_insert_sorted and _insert_sorted_with_data API.
2006-02-18 Matthias Clasen <mclasen@redhat.com> 2006-02-18 Matthias Clasen <mclasen@redhat.com>
* tests/gobject/Makefile.am: Add paramspec-test * tests/gobject/Makefile.am: Add paramspec-test

View File

@ -1,3 +1,10 @@
2006-02-19 Martyn Russell <martyn@imendio.com>
* tests/asyncqueue-test.c:
* tests/list-test.c:
* tests/slist-test.c: Updated to test _sort, _sort_with_data,
_insert_sorted and _insert_sorted_with_data API.
2006-02-18 Matthias Clasen <mclasen@redhat.com> 2006-02-18 Matthias Clasen <mclasen@redhat.com>
* tests/gobject/Makefile.am: Add paramspec-test * tests/gobject/Makefile.am: Add paramspec-test

View File

@ -1,3 +1,10 @@
2006-02-19 Martyn Russell <martyn@imendio.com>
* tests/asyncqueue-test.c:
* tests/list-test.c:
* tests/slist-test.c: Updated to test _sort, _sort_with_data,
_insert_sorted and _insert_sorted_with_data API.
2006-02-18 Matthias Clasen <mclasen@redhat.com> 2006-02-18 Matthias Clasen <mclasen@redhat.com>
* tests/gobject/Makefile.am: Add paramspec-test * tests/gobject/Makefile.am: Add paramspec-test

View File

@ -1,15 +1,15 @@
#undef G_DISABLE_ASSERT #undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#define d(x) x #include <glib.h>
#define DEBUG_MSG(args)
/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
#define PRINT_MSG(args)
/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
#define MAX_THREADS 50 #define MAX_THREADS 50
#define MAX_SORTS 5 /* only applies if #define MAX_SORTS 5 /* only applies if
@ -43,7 +43,7 @@ sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
id1 = GPOINTER_TO_INT (p1); id1 = GPOINTER_TO_INT (p1);
id2 = GPOINTER_TO_INT (p2); id2 = GPOINTER_TO_INT (p2);
d(g_print ("comparing #1:%d and #2:%d, returning %d\n", DEBUG_MSG (("comparing #1:%d and #2:%d, returning %d",
id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1))); id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1)));
return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1); return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
@ -53,6 +53,8 @@ static gboolean
sort_queue (gpointer user_data) sort_queue (gpointer user_data)
{ {
static gint sorts = 0; static gint sorts = 0;
static gpointer last_p = NULL;
gpointer p;
gboolean can_quit = FALSE; gboolean can_quit = FALSE;
gint sort_multiplier; gint sort_multiplier;
gint len; gint len;
@ -61,7 +63,7 @@ sort_queue (gpointer user_data)
sort_multiplier = GPOINTER_TO_INT (user_data); sort_multiplier = GPOINTER_TO_INT (user_data);
if (SORT_QUEUE_AFTER) { if (SORT_QUEUE_AFTER) {
d(g_print ("sorting async queue...\n")); PRINT_MSG (("sorting async queue..."));
g_async_queue_sort (async_queue, sort_compare, NULL); g_async_queue_sort (async_queue, sort_compare, NULL);
sorts++; sorts++;
@ -73,18 +75,22 @@ sort_queue (gpointer user_data)
g_async_queue_sort (async_queue, sort_compare, NULL); g_async_queue_sort (async_queue, sort_compare, NULL);
len = g_async_queue_length (async_queue); len = g_async_queue_length (async_queue);
d(g_print ("sorted queue (for %d/%d times, size:%d)...\n", sorts, MAX_SORTS, len)); PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len));
} else { } else {
can_quit = TRUE; can_quit = TRUE;
len = g_async_queue_length (async_queue); len = g_async_queue_length (async_queue);
d(g_print ("printing queue (size:%d)...\n", len)); DEBUG_MSG (("printing queue (size:%d)...", len));
} }
for (i = 0; i < len; i++) { for (i = 0, last_p = NULL; i < len; i++) {
gpointer p;
p = g_async_queue_pop (async_queue); p = g_async_queue_pop (async_queue);
d(g_print ("item %d ---> %d\n", i, GPOINTER_TO_INT (p))); DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p)));
if (last_p) {
g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p));
}
last_p = p;
} }
if (can_quit && QUIT_WHEN_DONE) { if (can_quit && QUIT_WHEN_DONE) {
@ -104,7 +110,7 @@ enter_thread (gpointer data, gpointer user_data)
id = GPOINTER_TO_INT (data); id = GPOINTER_TO_INT (data);
ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000); ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000);
d(g_print ("entered thread with id:%d, adding to queue in:%ld ms\n", id, ms)); DEBUG_MSG (("entered thread with id:%d, adding to queue in:%ld ms", id, ms));
g_usleep (ms * 1000); g_usleep (ms * 1000);
@ -116,11 +122,12 @@ enter_thread (gpointer data, gpointer user_data)
len = g_async_queue_length (async_queue); len = g_async_queue_length (async_queue);
d(g_print ("thread id:%d added to async queue (size:%d)\n", DEBUG_MSG (("thread id:%d added to async queue (size:%d)",
id, len)); id, len));
} }
int main (int argc, char *argv[]) int
main (int argc, char *argv[])
{ {
#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE) #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
gint i; gint i;
@ -128,15 +135,16 @@ int main (int argc, char *argv[])
gint max_unused_threads = MAX_THREADS; gint max_unused_threads = MAX_THREADS;
gint sort_multiplier = MAX_SORTS; gint sort_multiplier = MAX_SORTS;
gint sort_interval; gint sort_interval;
gchar *msg;
g_thread_init (NULL); g_thread_init (NULL);
d(g_print ("creating async queue...\n")); PRINT_MSG (("creating async queue..."));
async_queue = g_async_queue_new (); async_queue = g_async_queue_new ();
g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE); g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE);
d(g_print ("creating thread pool with max threads:%d, max unused threads:%d...\n", PRINT_MSG (("creating thread pool with max threads:%d, max unused threads:%d...",
max_threads, max_unused_threads)); max_threads, max_unused_threads));
thread_pool = g_thread_pool_new (enter_thread, thread_pool = g_thread_pool_new (enter_thread,
async_queue, async_queue,
@ -148,8 +156,8 @@ int main (int argc, char *argv[])
g_thread_pool_set_max_unused_threads (max_unused_threads); g_thread_pool_set_max_unused_threads (max_unused_threads);
d(g_print ("creating threads...\n")); PRINT_MSG (("creating threads..."));
for (i = 0; i <= max_threads; i++) { for (i = 1; i <= max_threads; i++) {
GError *error = NULL; GError *error = NULL;
g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error); g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
@ -162,15 +170,21 @@ int main (int argc, char *argv[])
} }
sort_interval = ((MAX_TIME / sort_multiplier) + 2) * 1000; sort_interval = ((MAX_TIME / sort_multiplier) + 2) * 1000;
d(g_print ("adding timeout of %d seconds to sort %d times\n",
sort_interval, sort_multiplier));
g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier)); g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier));
if (SORT_QUEUE_ON_PUSH) { if (SORT_QUEUE_ON_PUSH) {
d(g_print ("sorting when pushing into the queue...\n")); msg = "sorting when pushing into the queue, checking queue is sorted";
} else {
msg = "sorting";
} }
d(g_print ("entering main event loop\n")); PRINT_MSG (("%s %d %s %d ms",
msg,
sort_multiplier,
sort_multiplier == 1 ? "time in" : "times, once every",
sort_interval));
DEBUG_MSG (("entering main event loop"));
main_loop = g_main_loop_new (NULL, FALSE); main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop); g_main_loop_run (main_loop);

View File

@ -1,155 +1,212 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#undef G_DISABLE_ASSERT #undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#include <stdio.h> #include <glib.h>
#include <string.h>
#include "glib.h"
int array[10000]; #define DEBUG_MSG(args)
gboolean failed = FALSE; /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
#define PRINT_MSG(args)
/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
#define TEST(m,cond) G_STMT_START { failed = !(cond); \ #define SIZE 50
if (failed) \ #define NUMBER_MIN 0000
{ if (!m) \ #define NUMBER_MAX 9999
g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
else \
g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
} \
else \
g_print ("."); fflush (stdout); \
} G_STMT_END
#define C2P(c) ((gpointer) ((long) (c)))
#define P2C(p) ((gchar) ((long) (p)))
#define GLIB_TEST_STRING "el dorado " static guint32 array[SIZE];
#define GLIB_TEST_STRING_5 "el do"
typedef struct {
guint age;
gchar name[40];
} GlibTestInfo;
static gint static gint
my_list_compare_one (gconstpointer a, gconstpointer b) sort (gconstpointer p1, gconstpointer p2)
{ {
gint one = *((const gint*)a); gint32 a, b;
gint two = *((const gint*)b);
return one-two; a = GPOINTER_TO_INT (p1);
b = GPOINTER_TO_INT (p2);
return (a > b ? +1 : a == b ? 0 : -1);
} }
static gint /*
my_list_compare_two (gconstpointer a, gconstpointer b) * glist sort tests
*/
static void
test_list_sort (void)
{ {
gint one = *((const gint*)a); GList *list = NULL;
gint two = *((const gint*)b); gint i;
return two-one;
PRINT_MSG (("testing g_list_sort()"));
for (i = 0; i < SIZE; i++) {
list = g_list_append (list, GINT_TO_POINTER (array[i]));
}
list = g_list_sort (list, sort);
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_list_nth_data (list, i);
p2 = g_list_nth_data (list, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("list_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
g_list_free (list);
}
static void
test_list_sort_with_data (void)
{
GList *list = NULL;
gint i;
PRINT_MSG (("testing g_list_sort_with_data()"));
for (i = 0; i < SIZE; i++) {
list = g_list_append (list, GINT_TO_POINTER (array[i]));
}
list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_list_nth_data (list, i);
p2 = g_list_nth_data (list, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("list_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
g_list_free (list);
}
static void
test_list_insert_sorted (void)
{
GList *list = NULL;
gint i;
PRINT_MSG (("testing g_list_insert_sorted()"));
for (i = 0; i < SIZE; i++) {
list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
}
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_list_nth_data (list, i);
p2 = g_list_nth_data (list, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("list_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
g_list_free (list);
}
static void
test_list_insert_sorted_with_data (void)
{
GList *list = NULL;
gint i;
PRINT_MSG (("testing g_list_insert_sorted_with_data()"));
for (i = 0; i < SIZE; i++) {
list = g_list_insert_sorted_with_data (list,
GINT_TO_POINTER (array[i]),
(GCompareDataFunc)sort,
NULL);
}
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_list_nth_data (list, i);
p2 = g_list_nth_data (list, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("list_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
g_list_free (list);
}
static void
test_list_reverse (void)
{
GList *list = NULL;
GList *st;
gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
gint i;
PRINT_MSG (("testing g_list_reverse()"));
for (i = 0; i < 10; i++) {
list = g_list_append (list, &nums[i]);
}
list = g_list_reverse (list);
for (i = 0; i < 10; i++) {
st = g_list_nth (list, i);
g_assert (*((gint*) st->data) == (9 - i));
}
g_list_free (list);
}
static void
test_list_nth (void)
{
GList *list = NULL;
GList *st;
gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
gint i;
PRINT_MSG (("testing g_list_nth()"));
for (i = 0; i < 10; i++) {
list = g_list_append (list, &nums[i]);
}
for (i = 0; i < 10; i++) {
st = g_list_nth (list, i);
g_assert (*((gint*) st->data) == i);
}
g_list_free (list);
} }
int int
main (int argc, main (int argc, char *argv[])
char *argv[])
{ {
GList *list, *t;
gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
gint i; gint i;
list = NULL; DEBUG_MSG (("debugging messages turned on"));
for (i = 0; i < 10; i++)
list = g_list_append (list, &nums[i]);
list = g_list_reverse (list);
for (i = 0; i < 10; i++) DEBUG_MSG (("creating %d random numbers", SIZE));
{
t = g_list_nth (list, i); /* Create an array of random numbers. */
g_assert (*((gint*) t->data) == (9 - i)); for (i = 0; i < SIZE; i++) {
array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
} }
for (i = 0; i < 10; i++) /* Start tests. */
g_assert (g_list_position(list, g_list_nth (list, i)) == i); test_list_sort ();
test_list_sort_with_data ();
g_list_free (list); test_list_insert_sorted ();
list = NULL; test_list_insert_sorted_with_data ();
for (i = 0; i < 10; i++) test_list_reverse ();
list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one); test_list_nth ();
/* PRINT_MSG (("testing finished"));
g_print("\n");
g_list_foreach (list, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
t = g_list_nth (list, i);
g_assert (*((gint*) t->data) == i);
}
g_list_free (list);
list = NULL;
for (i = 0; i < 10; i++)
list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
/*
g_print("\n");
g_list_foreach (list, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
t = g_list_nth (list, i);
g_assert (*((gint*) t->data) == (9 - i));
}
g_list_free (list);
list = NULL;
for (i = 0; i < 10; i++)
list = g_list_prepend (list, &morenums[i]);
list = g_list_sort (list, my_list_compare_two);
/*
g_print("\n");
g_list_foreach (list, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
t = g_list_nth (list, i);
g_assert (*((gint*) t->data) == (9 - i));
}
g_list_free (list);
return 0; return 0;
} }

View File

@ -1,16 +1,15 @@
#undef G_DISABLE_ASSERT #undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include <glib.h>
static gboolean verbose = FALSE; static gboolean verbose = FALSE;
static void static void
check_integrity (GQueue *queue) check_integrity (GQueue *queue)
{ {

View File

@ -1,151 +1,204 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#undef G_DISABLE_ASSERT #undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#include <stdio.h> #include <glib.h>
#include <string.h>
#include "glib.h"
int array[10000]; #define DEBUG_MSG(args)
gboolean failed = FALSE; /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
#define PRINT_MSG(args)
/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
#define TEST(m,cond) G_STMT_START { failed = !(cond); \ #define SIZE 50
if (failed) \ #define NUMBER_MIN 0000
{ if (!m) \ #define NUMBER_MAX 9999
g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
else \
g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
} \
else \
g_print ("."); fflush (stdout); \
} G_STMT_END
#define C2P(c) ((gpointer) ((long) (c)))
#define P2C(p) ((gchar) ((long) (p)))
#define GLIB_TEST_STRING "el dorado " static guint32 array[SIZE];
#define GLIB_TEST_STRING_5 "el do"
typedef struct {
guint age;
gchar name[40];
} GlibTestInfo;
static gint static gint
my_list_compare_one (gconstpointer a, gconstpointer b) sort (gconstpointer p1, gconstpointer p2)
{ {
gint one = *((const gint*)a); gint32 a, b;
gint two = *((const gint*)b);
return one-two; a = GPOINTER_TO_INT (p1);
b = GPOINTER_TO_INT (p2);
return (a > b ? +1 : a == b ? 0 : -1);
} }
static gint /*
my_list_compare_two (gconstpointer a, gconstpointer b) * gslist sort tests
*/
static void
test_slist_sort (void)
{ {
gint one = *((const gint*)a); GSList *slist = NULL;
gint two = *((const gint*)b);
return two-one;
}
int
main (int argc,
char *argv[])
{
GSList *slist, *st;
gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
gint i; gint i;
slist = NULL; PRINT_MSG (("testing g_slist_sort()"));
for (i = 0; i < 10; i++)
for (i = 0; i < SIZE; i++) {
slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
}
slist = g_slist_sort (slist, sort);
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_slist_nth_data (slist, i);
p2 = g_slist_nth_data (slist, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("slist_sort #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
}
static void
test_slist_sort_with_data (void)
{
GSList *slist = NULL;
gint i;
PRINT_MSG (("testing g_slist_sort_with_data()"));
for (i = 0; i < SIZE; i++) {
slist = g_slist_append (slist, GINT_TO_POINTER (array[i]));
}
slist = g_slist_sort_with_data (slist, (GCompareDataFunc)sort, NULL);
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_slist_nth_data (slist, i);
p2 = g_slist_nth_data (slist, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("slist_sort_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
}
static void
test_slist_insert_sorted (void)
{
GSList *slist = NULL;
gint i;
PRINT_MSG (("testing g_slist_insert_sorted()"));
for (i = 0; i < SIZE; i++) {
slist = g_slist_insert_sorted (slist, GINT_TO_POINTER (array[i]), sort);
}
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_slist_nth_data (slist, i);
p2 = g_slist_nth_data (slist, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("slist_insert_sorted #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
}
static void
test_slist_insert_sorted_with_data (void)
{
GSList *slist = NULL;
gint i;
PRINT_MSG (("testing g_slist_insert_sorted_with_data()"));
for (i = 0; i < SIZE; i++) {
slist = g_slist_insert_sorted_with_data (slist,
GINT_TO_POINTER (array[i]),
(GCompareDataFunc)sort,
NULL);
}
for (i = 0; i < SIZE - 1; i++) {
gpointer p1, p2;
p1 = g_slist_nth_data (slist, i);
p2 = g_slist_nth_data (slist, i+1);
g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
DEBUG_MSG (("slist_insert_sorted_with_data #%3.3d ---> %d", i, GPOINTER_TO_INT (p1)));
}
}
static void
test_slist_reverse (void)
{
GSList *slist = NULL;
GSList *st;
gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
gint i;
PRINT_MSG (("testing g_slist_reverse()"));
for (i = 0; i < 10; i++) {
slist = g_slist_append (slist, &nums[i]); slist = g_slist_append (slist, &nums[i]);
}
slist = g_slist_reverse (slist); slist = g_slist_reverse (slist);
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++) {
{
st = g_slist_nth (slist, i); st = g_slist_nth (slist, i);
g_assert (*((gint*) st->data) == (9 - i)); g_assert (*((gint*) st->data) == (9 - i));
} }
g_slist_free (slist); g_slist_free (slist);
slist = NULL; }
for (i = 0; i < 10; i++) static void
slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one); test_slist_nth (void)
/*
g_print("\n");
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{ {
GSList *slist = NULL;
GSList *st;
gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
gint i;
PRINT_MSG (("testing g_slist_nth()"));
for (i = 0; i < 10; i++) {
slist = g_slist_append (slist, &nums[i]);
}
for (i = 0; i < 10; i++) {
st = g_slist_nth (slist, i); st = g_slist_nth (slist, i);
g_assert (*((gint*) st->data) == i); g_assert (*((gint*) st->data) == i);
} }
g_slist_free (slist); g_slist_free (slist);
slist = NULL;
for (i = 0; i < 10; i++)
slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
/*
g_print("\n");
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
st = g_slist_nth (slist, i);
g_assert (*((gint*) st->data) == (9 - i));
} }
g_slist_free(slist); int
slist = NULL; main (int argc, char *argv[])
for (i = 0; i < 10; i++)
slist = g_slist_prepend (slist, &morenums[i]);
slist = g_slist_sort (slist, my_list_compare_two);
/*
g_print("\n");
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{ {
st = g_slist_nth (slist, i); gint i;
g_assert (*((gint*) st->data) == (9 - i));
DEBUG_MSG (("debugging messages turned on"));
DEBUG_MSG (("creating %d random numbers", SIZE));
/* Create an array of random numbers. */
for (i = 0; i < SIZE; i++) {
array[i] = g_random_int_range (NUMBER_MIN, NUMBER_MAX);
DEBUG_MSG (("number #%3.3d ---> %d", i, array[i]));
} }
g_slist_free(slist); /* Start tests. */
test_slist_sort ();
test_slist_sort_with_data ();
test_slist_insert_sorted ();
test_slist_insert_sorted_with_data ();
test_slist_reverse ();
test_slist_nth ();
PRINT_MSG (("testing finished"));
return 0; return 0;
} }