From 5b0e5e7e9b91c4f70a6ecfb21c7b511b1261581e Mon Sep 17 00:00:00 2001 From: Juerg Billeter Date: Tue, 26 Feb 2008 20:48:57 +0000 Subject: [PATCH] add unit tests 2008-02-26 Juerg Billeter * Makefile.am, Makefile.decl, configure.ac, gee/Makefile.am, tests/Makefile.am, tests/testarraylist.vala, tests/testhashmap.vala, tests/testhashset.vala: add unit tests svn path=/trunk/; revision=22 --- ChangeLog | 6 ++++ Makefile.am | 5 ++- Makefile.decl | 57 +++++++++++++++++++++++++++++ configure.ac | 3 +- gee/Makefile.am | 4 ++- tests/Makefile.am | 39 ++++++++++++++++++++ tests/testarraylist.vala | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/testhashmap.vala | 73 +++++++++++++++++++++++++++++++++++++ tests/testhashset.vala | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 370 insertions(+), 3 deletions(-) create mode 100644 Makefile.decl create mode 100644 tests/Makefile.am create mode 100644 tests/testarraylist.vala create mode 100644 tests/testhashmap.vala create mode 100644 tests/testhashset.vala diff --git a/ChangeLog b/ChangeLog index a9f844d..e4b2152 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2008-02-26 Jürg Billeter + * Makefile.am, Makefile.decl, configure.ac, gee/Makefile.am, + tests/Makefile.am, tests/testarraylist.vala, + tests/testhashmap.vala, tests/testhashset.vala: add unit tests + +2008-02-26 Jürg Billeter + * gee/hashset.vala: fix iterator, patch by Ali Sabil 2008-02-05 Jürg Billeter diff --git a/Makefile.am b/Makefile.am index 3465028..566aa23 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,13 +1,16 @@ +include $(top_srcdir)/Makefile.decl + NULL = SUBDIRS = \ gee \ + tests \ $(NULL) pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = gee-1.0.pc -EXTRA_DIST = \ +EXTRA_DIST += \ MAINTAINERS \ gee-1.0.pc.in \ $(NULL) diff --git a/Makefile.decl b/Makefile.decl new file mode 100644 index 0000000..20ad96d --- /dev/null +++ b/Makefile.decl @@ -0,0 +1,57 @@ +# GLIB - Library of useful C routines + +GTESTER = gtester +GTESTER_REPORT = gtester-report + +# initialize variables for unconditional += appending +EXTRA_DIST = +TEST_PROGS = + +### testing rules + +# test: run all tests in cwd and subdirs +test: ${TEST_PROGS} + @test -z "${TEST_PROGS}" || ${GTESTER} --verbose ${TEST_PROGS} + @ for subdir in $(SUBDIRS) . ; do \ + test "$$subdir" = "." -o "$$subdir" = "po" || \ + ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \ + done +# test-report: run tests in subdirs and generate report +# perf-report: run tests in subdirs with -m perf and generate report +# full-report: like test-report: with -m perf and -m slow +test-report perf-report full-report: ${TEST_PROGS} + @test -z "${TEST_PROGS}" || { \ + case $@ in \ + test-report) test_options="-k";; \ + perf-report) test_options="-k -m=perf";; \ + full-report) test_options="-k -m=perf -m=slow";; \ + esac ; \ + if test -z "$$GTESTER_LOGDIR" ; then \ + ${GTESTER} --verbose $$test_options -o test-report.xml ${TEST_PROGS} ; \ + elif test -n "${TEST_PROGS}" ; then \ + ${GTESTER} --verbose $$test_options -o `mktemp "$$GTESTER_LOGDIR/log-XXXXXX"` ${TEST_PROGS} ; \ + fi ; \ + } + @ ignore_logdir=true ; \ + if test -z "$$GTESTER_LOGDIR" ; then \ + GTESTER_LOGDIR=`mktemp -d "\`pwd\`/.testlogs-XXXXXX"`; export GTESTER_LOGDIR ; \ + ignore_logdir=false ; \ + fi ; \ + for subdir in $(SUBDIRS) . ; do \ + test "$$subdir" = "." -o "$$subdir" = "po" || \ + ( cd $$subdir && $(MAKE) $(AM_MAKEFLAGS) $@ ) || exit $? ; \ + done ; \ + $$ignore_logdir || { \ + echo '' > $@.xml ; \ + echo '' >> $@.xml ; \ + for lf in `ls -L "$$GTESTER_LOGDIR"/.` ; do \ + sed '1,1s/^?]*?>//' <"$$GTESTER_LOGDIR"/"$$lf" >> $@.xml ; \ + done ; \ + echo >> $@.xml ; \ + echo '' >> $@.xml ; \ + rm -rf "$$GTESTER_LOGDIR"/ ; \ + ${GTESTER_REPORT} --version 2>/dev/null 1>&2 ; test "$$?" != 0 || ${GTESTER_REPORT} $@.xml >$@.html ; \ + } +.PHONY: test test-report perf-report full-report +# run make test as part of make check +check-local: test diff --git a/configure.ac b/configure.ac index 275434d..b975c03 100644 --- a/configure.ac +++ b/configure.ac @@ -24,6 +24,7 @@ AC_SUBST(GLIB_LIBS) AC_CONFIG_FILES([Makefile gee-1.0.pc - gee/Makefile]) + gee/Makefile + tests/Makefile]) AC_OUTPUT diff --git a/gee/Makefile.am b/gee/Makefile.am index 7309a7d..b3fe725 100644 --- a/gee/Makefile.am +++ b/gee/Makefile.am @@ -1,3 +1,5 @@ +include $(top_srcdir)/Makefile.decl + NULL = AM_CPPFLAGS = \ @@ -53,4 +55,4 @@ dist_vapi_DATA = \ gee-1.0.vapi \ $(NULL) -EXTRA_DIST = $(libgee_la_VALASOURCES) gee-1.0.vapi gee.vala.stamp +EXTRA_DIST += $(libgee_la_VALASOURCES) gee-1.0.vapi gee.vala.stamp diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..fe56e4f --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,39 @@ +include $(top_srcdir)/Makefile.decl + +NULL = + +AM_CPPFLAGS = \ + -I$(top_srcdir) \ + $(GLIB_CFLAGS) \ + $(NULL) + +noinst_PROGRAMS = $(TEST_PROGS) + +progs_ldadd = $(GLIB_LIBS) ../gee/libgee.la + +TEST_PROGS += testarraylist +testarraylist_VALASOURCES = testarraylist.vala +testarraylist_SOURCES = testarraylist.c testarraylist.h +$(testarraylist_SOURCES): $(testarraylist_VALASOURCES) + $(VALAC) --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^ + touch $@ +testarraylist_LDADD = $(progs_ldadd) + +TEST_PROGS += testhashmap +testhashmap_VALASOURCES = testhashmap.vala +testhashmap_SOURCES = testhashmap.c testhashmap.h +$(testhashmap_SOURCES): $(testhashmap_VALASOURCES) + $(VALAC) --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^ + touch $@ +testhashmap_LDADD = $(progs_ldadd) + +TEST_PROGS += testhashset +testhashset_VALASOURCES = testhashset.vala +testhashset_SOURCES = testhashset.c testhashset.h +$(testhashset_SOURCES): $(testhashset_VALASOURCES) + $(VALAC) --basedir $(top_srcdir) --vapidir $(top_srcdir)/gee --pkg gee-1.0 $^ + touch $@ +testhashset_LDADD = $(progs_ldadd) + +EXTRA_DIST += $(testhashset_VALASOURCES) + diff --git a/tests/testarraylist.vala b/tests/testarraylist.vala new file mode 100644 index 0000000..d3e8635 --- /dev/null +++ b/tests/testarraylist.vala @@ -0,0 +1,93 @@ +/* testarraylist.vala + * + * Copyright (C) 2008 Jürg Billeter + * + * 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Jürg Billeter + */ + +using GLib; +using Gee; + +void test_arraylist_int_add () { + var arraylist = new ArrayList (); + + arraylist.add (42); + assert (arraylist.contains (42)); + assert (arraylist.size == 1); +} + +void test_arraylist_int_iterator () { + var arraylist = new ArrayList (); + arraylist.add (42); + + var it = arraylist.iterator (); + assert (it.next ()); + assert (it.get () == 42); + assert (!it.next ()); +} + +void test_arraylist_int_remove () { + var arraylist = new ArrayList (); + arraylist.add (42); + + arraylist.remove (42); + assert (!arraylist.contains (42)); + assert (arraylist.size == 0); +} + +void test_arraylist_string_add () { + var arraylist = new ArrayList (str_equal); + + arraylist.add ("hello"); + assert (arraylist.contains ("hello")); + assert (arraylist.size == 1); +} + +void test_arraylist_string_iterator () { + var arraylist = new ArrayList (str_equal); + arraylist.add ("hello"); + + var it = arraylist.iterator (); + assert (it.next ()); + assert (it.get () == "hello"); + assert (!it.next ()); +} + +void test_arraylist_string_remove () { + var arraylist = new ArrayList (str_equal); + arraylist.add ("hello"); + + arraylist.remove ("hello"); + assert (!arraylist.contains ("hello")); + assert (arraylist.size == 0); +} + +void main (string[] args) { + Test.init (ref args); + + Test.add_func ("/arraylist/int/add", test_arraylist_int_add); + Test.add_func ("/arraylist/int/iterator", test_arraylist_int_iterator); + Test.add_func ("/arraylist/int/remove", test_arraylist_int_remove); + + Test.add_func ("/arraylist/string/add", test_arraylist_string_add); + Test.add_func ("/arraylist/string/iterator", test_arraylist_string_iterator); + Test.add_func ("/arraylist/string/remove", test_arraylist_string_remove); + + Test.run (); +} + diff --git a/tests/testhashmap.vala b/tests/testhashmap.vala new file mode 100644 index 0000000..20b0f80 --- /dev/null +++ b/tests/testhashmap.vala @@ -0,0 +1,73 @@ +/* testhashmap.vala + * + * Copyright (C) 2008 Jürg Billeter + * + * 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Jürg Billeter + */ + +using GLib; +using Gee; + +void test_hashmap_int_int_remove () { + var hashmap = new HashMap (); + hashmap.set (42, 23); + + hashmap.remove (42); + assert (!hashmap.contains (42)); + assert (hashmap.size == 0); +} + +void test_hashmap_int_int_set () { + var hashmap = new HashMap (); + + hashmap.set (42, 23); + assert (hashmap.contains (42)); + assert (hashmap.get (42) == 23); + assert (hashmap.size == 1); +} + +void test_hashmap_string_string_remove () { + var hashmap = new HashMap (str_hash, str_equal, str_equal); + hashmap.set ("hello", "world"); + + hashmap.remove ("hello"); + assert (!hashmap.contains ("hello")); + assert (hashmap.size == 0); +} + +void test_hashmap_string_string_set () { + var hashmap = new HashMap (str_hash, str_equal, str_equal); + + hashmap.set ("hello", "world"); + assert (hashmap.contains ("hello")); + assert (hashmap.get ("hello") == "world"); + assert (hashmap.size == 1); +} + +void main (string[] args) { + Test.init (ref args); + + Test.add_func ("/hashmap/int-int/remove", test_hashmap_int_int_remove); + Test.add_func ("/hashmap/int-int/set", test_hashmap_int_int_set); + + Test.add_func ("/hashmap/string-string/remove", test_hashmap_string_string_remove); + Test.add_func ("/hashmap/string-string/set", test_hashmap_string_string_set); + + Test.run (); +} + diff --git a/tests/testhashset.vala b/tests/testhashset.vala new file mode 100644 index 0000000..555ffd6 --- /dev/null +++ b/tests/testhashset.vala @@ -0,0 +1,93 @@ +/* testhashset.vala + * + * Copyright (C) 2008 Jürg Billeter + * + * 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Jürg Billeter + */ + +using GLib; +using Gee; + +void test_hashset_int_add () { + var hashset = new HashSet (); + + hashset.add (42); + assert (hashset.contains (42)); + assert (hashset.size == 1); +} + +void test_hashset_int_iterator () { + var hashset = new HashSet (); + hashset.add (42); + + var it = hashset.iterator (); + assert (it.next ()); + assert (it.get () == 42); + assert (!it.next ()); +} + +void test_hashset_int_remove () { + var hashset = new HashSet (); + hashset.add (42); + + hashset.remove (42); + assert (!hashset.contains (42)); + assert (hashset.size == 0); +} + +void test_hashset_string_add () { + var hashset = new HashSet (str_hash, str_equal); + + hashset.add ("hello"); + assert (hashset.contains ("hello")); + assert (hashset.size == 1); +} + +void test_hashset_string_iterator () { + var hashset = new HashSet (str_hash, str_equal); + hashset.add ("hello"); + + var it = hashset.iterator (); + assert (it.next ()); + assert (it.get () == "hello"); + assert (!it.next ()); +} + +void test_hashset_string_remove () { + var hashset = new HashSet (str_hash, str_equal); + hashset.add ("hello"); + + hashset.remove ("hello"); + assert (!hashset.contains ("hello")); + assert (hashset.size == 0); +} + +void main (string[] args) { + Test.init (ref args); + + Test.add_func ("/hashset/int/add", test_hashset_int_add); + Test.add_func ("/hashset/int/iterator", test_hashset_int_iterator); + Test.add_func ("/hashset/int/remove", test_hashset_int_remove); + + Test.add_func ("/hashset/string/add", test_hashset_string_add); + Test.add_func ("/hashset/string/iterator", test_hashset_string_iterator); + Test.add_func ("/hashset/string/remove", test_hashset_string_remove); + + Test.run (); +} + -- 2.7.4