+2006-08-17 Aaron Bockover <abockover@novell.com>
+
+ * test/test.h:
+ * test/test.c: Added group iterator/test driver functionality
+
+ * test/driver.c: Added groups to run using new test functionality
+
+ * test/slist.h:
+ * test/hashtable.h:
+ * test/string-util.h: Test group definitions for string util/hashtable
+
+ * test/slist.c:
+ * test/str.c:
+ * test/hash.c: Added test definition table
+
+ * test/Makefile.am: Added -Wall -Werror -D_FORTIFY_SOURCE=2
+
+ * src/gstr.c: Added implementation for g_str_has_prefix, g_str_has_suffix
+
+ * src/glib.h: Added missing function signatures
+
+ * src/Makefile.am: added -D_FORTIFY_SOURCE=2
+
2006-08-17 Duncan Mak <duncan@a-chinaman.com>
* src/gslist.c (g_slist_remove_link): I misread the function
gslist.c \
gstring.c
-libeglib_la_CFLAGS = -Wall -Werror
+libeglib_la_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2
INCLUDES = -I$(srcdir)
gchar *g_strconcat (const gchar *first, ...);
gchar **g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens);
gchar *g_strreverse (gchar *str);
+gboolean g_str_has_prefix (const gchar *str, const gchar *prefix);
+gboolean g_str_has_suffix (const gchar *str, const gchar *suffix);
/*
* String type
void g_string_append_printf (GString *string, const gchar *format, ...);
GString *g_string_append_c (GString *string, gchar c);
GString *g_string_append (GString *string, const gchar *val);
-
+GString *g_string_append_len (GString *string, const gchar *val, gsize len);
#define g_string_sprintfa g_string_append_printf
/*
GSList *g_slist_remove_link (GSList *list, GSList *link);
GSList *g_slist_delete_link (GSList *list, GSList *link);
GSList *g_slist_insert_sorted (GSList *list, gpointer data, GCompareFunc func);
-
+guint g_slist_length (GSList *list);
#define g_slist_next (slist) ((slist) ? (((GSList *) slist)->next) : NULL)
/*
g_free (orig);
}
+gint
+g_strv_length(gchar **str_array)
+{
+ gint length = 0;
+ g_return_val_if_fail(str_array != NULL, 0);
+ for(length = 0; str_array[length] != NULL; length++);
+ return length;
+}
+
+gboolean
+g_str_has_suffix(const gchar *str, const gchar *suffix)
+{
+ gint str_length;
+ gint suffix_length;
+
+ g_return_val_if_fail(str != NULL, FALSE);
+ g_return_val_if_fail(suffix != NULL, FALSE);
+
+ str_length = strlen(str);
+ suffix_length = strlen(suffix);
+
+ return suffix_length <= str_length ?
+ strncmp(str + str_length - suffix_length, suffix, suffix_length) == 0 :
+ FALSE;
+}
+
+gboolean
+g_str_has_prefix(const gchar *str, const gchar *prefix)
+{
+ gint str_length;
+ gint prefix_length;
+
+ g_return_val_if_fail(str != NULL, FALSE);
+ g_return_val_if_fail(prefix != NULL, FALSE);
+
+ str_length = strlen(str);
+ prefix_length = strlen(prefix);
+
+ return prefix_length <= str_length ?
+ strncmp(str, prefix, prefix_length) == 0 :
+ FALSE;
+}
+
gchar *
g_strdup_vprintf (const gchar *format, va_list args)
{
noinst_PROGRAMS = test
test_SOURCES = \
- test.h \
+ test.c \
driver.c \
hash.c \
str.c \
slist.c
+test_CFLAGS = -Wall -Werror -D_FORTIFY_SOURCE=2
INCLUDES = -I../src
test_LDADD = -L../src -leglib
#include "test.h"
-int main ()
+#include "string-util.h"
+#include "hashtable.h"
+#include "slist.h"
+
+int main()
{
- printf ("hashtable\n");
- test ("hash-1", hash_t1);
- test ("s-freev", test_strfreev);
- test ("s-concat", test_concat);
- test ("s-split", test_split);
- test ("s-gstring", test_gstring);
- test ("s-gstrreverse", test_strreverse);
- test ("s-slist-append", test_slist_append);
- test ("s-slist-concat", test_slist_concat);
- test ("s-slist-find", test_slist_find);
- test ("s-slist-remove", test_slist_remove);
- test ("s-slist-remove-link", test_slist_remove_link);
- test ("s-slist-insert-sorted", test_slist_insert_sorted);
+ run_groups(
+ "string", string_tests_init,
+ "hashtable", hashtable_tests_init,
+ "slist", slist_tests_init,
+ NULL
+ );
+
+ return 0;
}
+
char *hash_t2 (void)
{
-
+ return RESULT("test body not defined");
}
+
+static Test hashtable_tests [] = {
+ {"hash_t1", hash_t1},
+ {"hash_t2", hash_t2},
+ {NULL, NULL}
+};
+
+DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)
+
--- /dev/null
+#include "test.h"
+
+DEFINE_TEST_GROUP_INIT_H(hashtable_tests_init);
+
return NULL;
}
+
+static Test slist_tests [] = {
+ {"slist_append", test_slist_append},
+ {"slist_concat", test_slist_concat},
+ {"slist_find", test_slist_find},
+ {"slist_remove", test_slist_remove},
+ {"slist_remove_link", test_slist_remove_link},
+ {"slist_insert_sorted", test_slist_insert_sorted},
+ {NULL, NULL}
+};
+
+DEFINE_TEST_GROUP_INIT(slist_tests_init, slist_tests)
+
--- /dev/null
+#include "test.h"
+
+DEFINE_TEST_GROUP_INIT_H(slist_tests_init);
+
#include <glib.h>
#include <stdio.h>
+#include "test.h"
/* This test is just to be used with valgrind */
char *
return NULL;
}
-#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return g_strdup_printf ("Failed at %d, expected '%c'", p, k);}
+#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return g_strdup_printf ("Got %s, Failed at %d, expected '%c'", s, p, k);}
char *
test_gstring ()
int i;
if (strcmp (s->str, "My") != 0)
- return "Expected only 'My' on the string";
+ return RESULT("Expected only 'My' on the string");
g_string_free (s, TRUE);
s = g_string_new_len ("My\0\0Rest", 6);
if (s->str [2] != 0)
- return "Null was not copied";
+ return RESULT("Null was not copied");
if (strcmp (s->str+4, "Re") != 0){
- return "Did not find the 'Re' part";
+ return RESULT("Did not find the 'Re' part");
}
g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal");
if (s->str [2] != 0)
- return "Null as not copied";
+ return RESULT("Null as not copied");
if (strncmp (s->str+4, "Relala", 6) != 0){
- printf ("got: %s\n", s->str+4);
- return "Did not copy correctly";
+ return g_strdup_printf("Did not copy correctly, got: %s", s->str+4);
}
g_string_free (s, TRUE);
g_string_append (s, "x");
}
if (strlen (s->str) != 1024){
- printf ("got: %s %d\n", s->str, strlen (s->str));
- return "Incorrect string size";
+ return g_strdup_printf("Incorrect string size, got: %s %d", s->str, strlen (s->str));
}
g_string_free (s, TRUE);
g_string_append_c (s, 'x');
}
if (strlen (s->str) != 1024){
- printf ("got: %s %d\n", s->str, strlen (s->str));
- return "Incorrect string size";
+ return g_strdup_printf("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
}
g_string_free (s, TRUE);
s = g_string_new ("hola");
g_string_sprintfa (s, "%s%d", ", bola", 5);
if (strcmp (s->str, "hola, bola5") != 0){
- printf ("got: %s\n", s->str);
- return "Got incorrect data";
+ return g_strdup_printf("Incorrect data, got: %s\n", s->str);
}
g_string_free (s, TRUE);
s = g_string_new_len ("H\000H", 3);
g_string_append_len (s, "1\0002", 3);
sfail ('H', 0);
- sfail (0, 1);
+ sfail ( 0, 1);
sfail ('H', 2);
sfail ('1', 3);
- sfail (0, 4);
+ sfail ( 0, 4);
sfail ('2', 5);
g_string_free (s, TRUE);
int i = 0;
if(v == NULL) {
- return g_strdup_printf("split failed, got NULL vector");
+ return RESULT("split failed, got NULL vector");
} else {
for(i = 0; v[i] != NULL; i++);
if(i != 7) {
return NULL;
}
+static Test string_tests [] = {
+ {"g_strfreev", test_strfreev},
+ {"g_strconcat", test_concat},
+ {"GString", test_gstring},
+ {"g_strsplit", test_split},
+ {"g_strreverse", test_strreverse},
+ {NULL, NULL}
+};
+
+DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests)
+
--- /dev/null
+#include "test.h"
+
+DEFINE_TEST_GROUP_INIT_H(string_tests_init);
+
--- /dev/null
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "test.h"
+
+void
+run_test(Test *test)
+{
+ char *result;
+ printf(" %s: ", test->name);
+ fflush(stdout);
+ if((result = test->handler()) == NULL) {
+ printf("OK\n");
+ } else {
+ printf("FAILED (%s)\n", result);
+ free(result);
+ }
+}
+
+void
+run_group(const char *name, LoadGroupHandler group_handler)
+{
+ Test *tests = group_handler();
+ int i;
+
+ printf("[%s]\n", name);
+
+ for(i = 0; tests[i].name != NULL; i++) {
+ run_test(&(tests[i]));
+ }
+}
+
+void
+run_groups(const char *first_name, LoadGroupHandler first_group_handler, ...)
+{
+ va_list args;
+ va_start(args, first_group_handler);
+
+ run_group(first_name, first_group_handler);
+
+ while(1) {
+ const char *name;
+ LoadGroupHandler group_handler;
+
+ if((name = (const char *)va_arg(args, const char **)) == NULL) {
+ break;
+ }
+
+ if((group_handler = (LoadGroupHandler)va_arg(args,
+ LoadGroupHandler)) == NULL) {
+ break;
+ }
+
+ run_group(name, group_handler);
+ }
+
+ va_end(args);
+}
+
-#define test(name,func) do { char *r; printf (" test: %s: ", name); fflush (stdout);r = func (); if (r){printf ("failure (%s)\n",r); free (r);} else printf ("OK\n");} while (0);
+#ifndef _TEST_H
+#define _TEST_H
+#include <stdarg.h>
-char *test_concat ();
-char *test_strfreev ();
-char *test_gstring ();
-char *test_split ();
-char *test_strreverse ();
-char *hash_t1 (void);
-char *hash_t2 (void);
-char *test_slist_append ();
-char *test_slist_concat ();
-char *test_slist_find ();
-char *test_slist_remove ();
-char *test_slist_remove_link ();
+typedef struct _Test Test;
+typedef char * (* RunTestHandler)();
+typedef Test * (* LoadGroupHandler)();
+struct _Test {
+ const char *name;
+ RunTestHandler handler;
+};
+void run_test(Test *test);
+void run_group(const char *name, LoadGroupHandler group_handler);
+void run_groups(const char *first_name, LoadGroupHandler first_group_handler, ...);
+#define DEFINE_TEST_GROUP_INIT(name, table) \
+ Test * (name)() { return table; }
+#define DEFINE_TEST_GROUP_INIT_H(name) \
+ Test * (name)();
+#define RESULT(x) g_strdup_printf(x);
+#endif /* _TEST_H */