/* 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"

char *stringtable[] = {
	"test1_1",
	"test4",
	"test2",
	"test3",
	"test1_2",
	"test1_3"
};

char *somethingelse = "somethingelse";

static gint sorter (gconstpointer a, gconstpointer b, gpointer user_data)
{
	const char *str1 = a;
	const char *str2 = b;
	printf ("comparing %s (%p) to %s (%p)\n", str1, str1, str2,str2);
	return strncmp (str1, str2,5);
}

GSequenceIter *
my_sequence_find (GSequence *seq, gpointer ptr,  GCompareDataFunc cmp_func,
gpointer cmp_data)
{
	GSequenceIter *iter;
	iter = g_sequence_search(seq, ptr, cmp_func, cmp_data);

	iter = g_sequence_iter_prev(iter);

	if (!g_sequence_iter_is_end (iter) && g_sequence_get (iter) == ptr)
		return iter;

	return NULL;
}

static void
sequence_find(void)
{
	GSequence *seq;
	gsize i;
	GSequenceIter *iter;

	seq = g_sequence_new (NULL);

	g_assert (my_sequence_find (seq, GSIZE_TO_POINTER (1), sorter, NULL) == NULL);

	for (i = 0; i < G_N_ELEMENTS(stringtable); i++) {
		g_sequence_insert_sorted (seq, stringtable[i], sorter, NULL);
	}

	iter = my_sequence_find (seq, stringtable[0], sorter, NULL);
	g_assert (iter != NULL);
	g_assert (g_sequence_get (iter) == stringtable[0]);


	iter = my_sequence_find (seq, stringtable[3], sorter, NULL);
	g_assert (iter != NULL);
	g_assert (g_sequence_get (iter) == stringtable[3]);

	iter = my_sequence_find (seq, stringtable[4], sorter, NULL);
	g_assert (iter != NULL);
	g_assert (g_sequence_get (iter) == stringtable[4]);

	iter = my_sequence_find (seq, stringtable[5], sorter, NULL);
	g_assert (iter != NULL);
	g_assert (g_sequence_get (iter) == stringtable[5]);

	iter = my_sequence_find (seq, somethingelse, sorter, NULL);
	g_assert (iter == NULL);

}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  /* gsequence tests */
  g_test_add_func ("/gsequence/find", sequence_find);

  return g_test_run ();
}

