sl@0: /* sl@0: * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General Public sl@0: * License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * Description: ?Description sl@0: * sl@0: */ sl@0: sl@0: sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: #define C2P(c) ((gpointer) ((long) (c))) sl@0: #define GINT_TO_POINTER(i) ((gpointer) (i)) sl@0: #define GPOINTER_TO_INT(p) ((gint) (p)) sl@0: #define TESTPASS 1 sl@0: #define TESTFAIL 0 sl@0: sl@0: sl@0: //Ascending sl@0: gint compare_fun_gr(gconstpointer a,gconstpointer b) sl@0: { sl@0: return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1)); sl@0: } sl@0: sl@0: //Data sl@0: gint compare_fun_gr_data(gconstpointer a,gconstpointer b,gpointer data) sl@0: { sl@0: return ((*(int *)a==*(int *)b)?0:((*(int *)a>*(int *)b)?1:-1)); sl@0: } sl@0: sl@0: // Tests for slist sl@0: void tg_slist_tests() sl@0: { sl@0: GSList *slist,*st,*rem; sl@0: gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; sl@0: gint chk_buf[20]; sl@0: gint i; sl@0: gint g_slist_insert_data; sl@0: gint g_slist_insert_before_data; sl@0: gint ip1 = 10; sl@0: gint ip2 = 15; sl@0: gint ip3 = 5; sl@0: gint ip4 = 12; sl@0: gint g_slist_nth_data_op,g_slist_find_custom_op; sl@0: sl@0: //Trying to use the allocators so that even they get tested! sl@0: GAllocator* alloc = g_allocator_new ("alloc_slist",5000); sl@0: g_slist_push_allocator (alloc); sl@0: sl@0: sl@0: slist = NULL; sl@0: for (i = 0; i < 10; i++) sl@0: slist = g_slist_append (slist, &nums[i]); sl@0: sl@0: //List looks like: sl@0: // 0 1 2 3 4 5 6 7 8 9 sl@0: sl@0: //Test for g_slist_insert....inserted 10 at pos 4 sl@0: g_slist_insert(slist,&ip1,4); sl@0: st = g_slist_nth (slist,0); sl@0: for(i = 0;i < 4;i++) sl@0: st = st->next; sl@0: g_slist_insert_data = *((gint*) st->data); sl@0: g_assert(g_slist_insert_data == 10); sl@0: sl@0: /* for (i = 0; i < 10; i++) sl@0: { sl@0: st = g_slist_nth (slist, i); sl@0: chk_buf[i] = *((gint*) st->data); sl@0: }*/ sl@0: sl@0: //List looks like: sl@0: // 0 1 2 3 10 4 5 6 7 8 9 sl@0: sl@0: //Test for g_slist_insert_before....inserted 15 at pos 7 sl@0: st = g_slist_nth (slist,7); sl@0: g_slist_insert_before(slist,st,&ip2); sl@0: st = g_slist_nth (slist,0); sl@0: for(i = 0;i < 7;i++) sl@0: st = st->next; sl@0: g_slist_insert_before_data = *((gint*) st->data); sl@0: g_assert(g_slist_insert_before_data == 15); sl@0: sl@0: //List looks like: sl@0: // 0 1 2 3 10 4 5 15 6 7 8 9 sl@0: sl@0: //Test for g_slist_index....finding 15 at pos 7 sl@0: st = g_slist_nth (slist,0); sl@0: g_assert(g_slist_index(st,&ip2)==7); sl@0: sl@0: //Test for g_slist_nth_data....getting 6 at position 8 sl@0: g_slist_nth_data_op = *((gint*) g_slist_nth_data(slist,8)); sl@0: g_assert(g_slist_nth_data_op == 6) ; sl@0: sl@0: //Test for g_slist_position sl@0: st = g_slist_nth (slist,7); sl@0: g_assert(g_slist_position (slist,st) == 7); sl@0: sl@0: //Test for g_slist_find_custom sl@0: st = g_slist_find_custom(slist,&ip3,compare_fun_gr); sl@0: g_slist_find_custom_op = *((gint*) st->data); sl@0: g_assert(g_slist_find_custom_op == 5); sl@0: sl@0: //Test for g_slist_sort_with_data sl@0: st = g_slist_sort_with_data(slist,compare_fun_gr_data,&ip3); sl@0: for (i = 0; i < 10; i++) sl@0: { sl@0: st = g_slist_nth (slist, i); sl@0: g_assert (*((gint*) st->data) == i); sl@0: } sl@0: sl@0: //List looks like: sl@0: // 0 1 2 3 4 5 6 7 8 9 10 15 sl@0: sl@0: //Test for g_slist_remove_link sl@0: st = g_slist_nth (slist, 5); sl@0: rem = g_slist_remove_link(slist , st); sl@0: st = g_slist_nth (slist, 5); sl@0: g_assert (*((gint*) st->data) == 6); sl@0: sl@0: //List looks like: sl@0: // 0 1 2 3 4 6 7 8 9 10 15 sl@0: sl@0: //Test for g_slist_remove_all sl@0: g_slist_insert(slist,&ip4,4); sl@0: g_slist_insert(slist,&ip4,6); sl@0: g_slist_insert(slist,&ip4,8); sl@0: //List looks like: sl@0: // 0 1 2 3 4 12 6 7 12 8 12 9 10 15 sl@0: g_slist_remove_all(slist ,&ip4); sl@0: sl@0: g_slist_free (slist); sl@0: g_slist_pop_allocator (); sl@0: } sl@0: sl@0: sl@0: int main (int argc,char *argv[]) sl@0: { sl@0: sl@0: #ifdef SYMBIAN sl@0: 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: #endif /*SYMBIAN*/ sl@0: sl@0: tg_slist_tests(); sl@0: #ifdef SYMBIAN sl@0: testResultXml("tslist"); sl@0: #endif /* EMULATOR */ sl@0: return 0; sl@0: }