mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-25 15:06:14 +01:00
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:
parent
c773a618e3
commit
dde67f284d
@ -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>
|
||||
|
||||
* tests/gobject/Makefile.am: Add paramspec-test
|
||||
|
@ -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>
|
||||
|
||||
* tests/gobject/Makefile.am: Add paramspec-test
|
||||
|
@ -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>
|
||||
|
||||
* tests/gobject/Makefile.am: Add paramspec-test
|
||||
|
@ -1,15 +1,15 @@
|
||||
#undef G_DISABLE_ASSERT
|
||||
#undef G_LOG_DOMAIN
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
#include <glib.h>
|
||||
|
||||
#include <time.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_SORTS 5 /* only applies if
|
||||
@ -29,7 +29,7 @@
|
||||
#endif
|
||||
|
||||
|
||||
static GMainLoop *main_loop = NULL;
|
||||
static GMainLoop *main_loop = NULL;
|
||||
static GThreadPool *thread_pool = NULL;
|
||||
static GAsyncQueue *async_queue = NULL;
|
||||
|
||||
@ -43,7 +43,7 @@ sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
|
||||
id1 = GPOINTER_TO_INT (p1);
|
||||
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)));
|
||||
|
||||
return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
|
||||
@ -52,16 +52,18 @@ sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
|
||||
static gboolean
|
||||
sort_queue (gpointer user_data)
|
||||
{
|
||||
static gint sorts = 0;
|
||||
gboolean can_quit = FALSE;
|
||||
gint sort_multiplier;
|
||||
gint len;
|
||||
gint i;
|
||||
static gint sorts = 0;
|
||||
static gpointer last_p = NULL;
|
||||
gpointer p;
|
||||
gboolean can_quit = FALSE;
|
||||
gint sort_multiplier;
|
||||
gint len;
|
||||
gint i;
|
||||
|
||||
sort_multiplier = GPOINTER_TO_INT (user_data);
|
||||
|
||||
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);
|
||||
|
||||
sorts++;
|
||||
@ -73,18 +75,22 @@ sort_queue (gpointer user_data)
|
||||
g_async_queue_sort (async_queue, sort_compare, NULL);
|
||||
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 {
|
||||
can_quit = TRUE;
|
||||
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++) {
|
||||
gpointer p;
|
||||
|
||||
for (i = 0, last_p = NULL; i < len; i++) {
|
||||
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) {
|
||||
@ -104,7 +110,7 @@ enter_thread (gpointer data, gpointer user_data)
|
||||
id = GPOINTER_TO_INT (data);
|
||||
|
||||
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);
|
||||
|
||||
@ -116,27 +122,29 @@ enter_thread (gpointer data, gpointer user_data)
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
|
||||
gint i;
|
||||
gint max_threads = MAX_THREADS;
|
||||
gint max_unused_threads = MAX_THREADS;
|
||||
gint sort_multiplier = MAX_SORTS;
|
||||
gint sort_interval;
|
||||
gint i;
|
||||
gint max_threads = MAX_THREADS;
|
||||
gint max_unused_threads = MAX_THREADS;
|
||||
gint sort_multiplier = MAX_SORTS;
|
||||
gint sort_interval;
|
||||
gchar *msg;
|
||||
|
||||
g_thread_init (NULL);
|
||||
|
||||
d(g_print ("creating async queue...\n"));
|
||||
PRINT_MSG (("creating async queue..."));
|
||||
async_queue = g_async_queue_new ();
|
||||
|
||||
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));
|
||||
thread_pool = g_thread_pool_new (enter_thread,
|
||||
async_queue,
|
||||
@ -148,8 +156,8 @@ int main (int argc, char *argv[])
|
||||
|
||||
g_thread_pool_set_max_unused_threads (max_unused_threads);
|
||||
|
||||
d(g_print ("creating threads...\n"));
|
||||
for (i = 0; i <= max_threads; i++) {
|
||||
PRINT_MSG (("creating threads..."));
|
||||
for (i = 1; i <= max_threads; i++) {
|
||||
GError *error = NULL;
|
||||
|
||||
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;
|
||||
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));
|
||||
|
||||
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);
|
||||
g_main_loop_run (main_loop);
|
||||
|
@ -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_LOG_DOMAIN
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "glib.h"
|
||||
#include <glib.h>
|
||||
|
||||
int array[10000];
|
||||
gboolean failed = FALSE;
|
||||
#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 TEST(m,cond) G_STMT_START { failed = !(cond); \
|
||||
if (failed) \
|
||||
{ if (!m) \
|
||||
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 SIZE 50
|
||||
#define NUMBER_MIN 0000
|
||||
#define NUMBER_MAX 9999
|
||||
|
||||
#define C2P(c) ((gpointer) ((long) (c)))
|
||||
#define P2C(p) ((gchar) ((long) (p)))
|
||||
|
||||
#define GLIB_TEST_STRING "el dorado "
|
||||
#define GLIB_TEST_STRING_5 "el do"
|
||||
static guint32 array[SIZE];
|
||||
|
||||
typedef struct {
|
||||
guint age;
|
||||
gchar name[40];
|
||||
} GlibTestInfo;
|
||||
|
||||
static gint
|
||||
my_list_compare_one (gconstpointer a, gconstpointer b)
|
||||
sort (gconstpointer p1, gconstpointer p2)
|
||||
{
|
||||
gint one = *((const gint*)a);
|
||||
gint two = *((const gint*)b);
|
||||
return one-two;
|
||||
gint32 a, b;
|
||||
|
||||
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);
|
||||
gint two = *((const gint*)b);
|
||||
return two-one;
|
||||
GList *list = NULL;
|
||||
gint i;
|
||||
|
||||
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
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
main (int argc, 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;
|
||||
|
||||
list = NULL;
|
||||
for (i = 0; i < 10; i++)
|
||||
list = g_list_append (list, &nums[i]);
|
||||
list = g_list_reverse (list);
|
||||
DEBUG_MSG (("debugging messages turned on"));
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
t = g_list_nth (list, i);
|
||||
g_assert (*((gint*) t->data) == (9 - i));
|
||||
}
|
||||
DEBUG_MSG (("creating %d random numbers", SIZE));
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
g_assert (g_list_position(list, g_list_nth (list, i)) == i);
|
||||
/* 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_list_free (list);
|
||||
list = NULL;
|
||||
/* Start tests. */
|
||||
test_list_sort ();
|
||||
test_list_sort_with_data ();
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
|
||||
test_list_insert_sorted ();
|
||||
test_list_insert_sorted_with_data ();
|
||||
|
||||
/*
|
||||
g_print("\n");
|
||||
g_list_foreach (list, my_list_print, NULL);
|
||||
*/
|
||||
test_list_reverse ();
|
||||
test_list_nth ();
|
||||
|
||||
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);
|
||||
PRINT_MSG (("testing finished"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1,16 +1,15 @@
|
||||
#undef G_DISABLE_ASSERT
|
||||
#undef G_LOG_DOMAIN
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
#include <glib.h>
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
|
||||
static gboolean verbose = FALSE;
|
||||
|
||||
|
||||
static void
|
||||
check_integrity (GQueue *queue)
|
||||
{
|
||||
|
@ -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_LOG_DOMAIN
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "glib.h"
|
||||
#include <glib.h>
|
||||
|
||||
int array[10000];
|
||||
gboolean failed = FALSE;
|
||||
#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 TEST(m,cond) G_STMT_START { failed = !(cond); \
|
||||
if (failed) \
|
||||
{ if (!m) \
|
||||
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 SIZE 50
|
||||
#define NUMBER_MIN 0000
|
||||
#define NUMBER_MAX 9999
|
||||
|
||||
#define C2P(c) ((gpointer) ((long) (c)))
|
||||
#define P2C(p) ((gchar) ((long) (p)))
|
||||
|
||||
#define GLIB_TEST_STRING "el dorado "
|
||||
#define GLIB_TEST_STRING_5 "el do"
|
||||
static guint32 array[SIZE];
|
||||
|
||||
typedef struct {
|
||||
guint age;
|
||||
gchar name[40];
|
||||
} GlibTestInfo;
|
||||
|
||||
static gint
|
||||
my_list_compare_one (gconstpointer a, gconstpointer b)
|
||||
sort (gconstpointer p1, gconstpointer p2)
|
||||
{
|
||||
gint one = *((const gint*)a);
|
||||
gint two = *((const gint*)b);
|
||||
return one-two;
|
||||
gint32 a, b;
|
||||
|
||||
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);
|
||||
gint two = *((const gint*)b);
|
||||
return two-one;
|
||||
GSList *slist = NULL;
|
||||
gint i;
|
||||
|
||||
PRINT_MSG (("testing g_slist_sort()"));
|
||||
|
||||
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_reverse (slist);
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
st = g_slist_nth (slist, i);
|
||||
g_assert (*((gint*) st->data) == (9 - i));
|
||||
}
|
||||
|
||||
g_slist_free (slist);
|
||||
}
|
||||
|
||||
static void
|
||||
test_slist_nth (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_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);
|
||||
g_assert (*((gint*) st->data) == i);
|
||||
}
|
||||
|
||||
g_slist_free (slist);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
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;
|
||||
|
||||
slist = NULL;
|
||||
for (i = 0; i < 10; i++)
|
||||
slist = g_slist_append (slist, &nums[i]);
|
||||
slist = g_slist_reverse (slist);
|
||||
DEBUG_MSG (("debugging messages turned on"));
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
st = g_slist_nth (slist, i);
|
||||
g_assert (*((gint*) st->data) == (9 - i));
|
||||
}
|
||||
DEBUG_MSG (("creating %d random numbers", SIZE));
|
||||
|
||||
g_slist_free (slist);
|
||||
slist = NULL;
|
||||
/* 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]));
|
||||
}
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
|
||||
/* Start tests. */
|
||||
test_slist_sort ();
|
||||
test_slist_sort_with_data ();
|
||||
|
||||
/*
|
||||
g_print("\n");
|
||||
g_slist_foreach (slist, my_list_print, NULL);
|
||||
*/
|
||||
test_slist_insert_sorted ();
|
||||
test_slist_insert_sorted_with_data ();
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
st = g_slist_nth (slist, i);
|
||||
g_assert (*((gint*) st->data) == i);
|
||||
}
|
||||
test_slist_reverse ();
|
||||
test_slist_nth ();
|
||||
|
||||
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);
|
||||
slist = NULL;
|
||||
|
||||
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);
|
||||
g_assert (*((gint*) st->data) == (9 - i));
|
||||
}
|
||||
|
||||
g_slist_free(slist);
|
||||
PRINT_MSG (("testing finished"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user