Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/test_dict.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_dict.c')
-rw-r--r--tests/test_dict.c98
1 files changed, 91 insertions, 7 deletions
diff --git a/tests/test_dict.c b/tests/test_dict.c
index 4c19c2b..3ce1c5c 100644
--- a/tests/test_dict.c
+++ b/tests/test_dict.c
@@ -25,12 +25,14 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <stdlib.h>
#include "common/dict.h"
#include "compat/attrs.h"
-/* the main program... */
-int main(int UNUSED(argc),char UNUSED(*argv[]))
+/* Simple test that adds a few key/value pairs to the dict and the does
+ most operations. */
+static void test_simple(void)
{
DICT *dict;
const char *key;
@@ -38,16 +40,13 @@ int main(int UNUSED(argc),char UNUSED(*argv[]))
static char *value1="value1";
static char *value2="value2";
static char *replace2="replace2";
-
/* initialize */
dict=dict_new();
-
/* store some entries */
dict_put(dict,"key1",value1);
dict_put(dict,"key2",value2);
dict_put(dict,"key3",dict);
dict_put(dict,"KEY2",replace2);
-
/* check dictionary contents */
val=dict_get(dict,"KeY1");
assert(val==value1);
@@ -57,21 +56,106 @@ int main(int UNUSED(argc),char UNUSED(*argv[]))
assert(val==dict);
val=dict_get(dict,"key4");
assert(val==NULL);
-
/* remove a key */
dict_put(dict,"kEy3",NULL);
val=dict_get(dict,"keY3");
assert(val==NULL);
-
/* loop over dictionary contents */
dict_loop_first(dict);
while (dict_loop_next(dict,&key,&val)!=NULL)
{
assert(((val==value1)||(val==replace2)));
}
+ /* free dictionary */
+ dict_free(dict);
+}
+
+/* Test to insert a large number of elements in the dict. */
+static void test_lotsofelements(void)
+{
+ DICT *dict;
+ char buf[80];
+ int i,r;
+ const char *key;
+ void *val;
+ /* initialize */
+ dict=dict_new();
+ /* insert a number of entries */
+ for (i=0;i<1024;i++)
+ {
+ r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
+ sprintf(buf,"test%04d",r);
+ dict_put(dict,buf,&buf);
+ }
+ /* remove a number of entries */
+ for (i=0;i<100;i++)
+ {
+ r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
+ sprintf(buf,"test%04d",r);
+ dict_put(dict,buf,NULL);
+ }
+ /* add some more entries */
+ for (i=0;i<1024;i++)
+ {
+ r=1+(int)(10000.0*(rand()/(RAND_MAX+1.0)));
+ sprintf(buf,"test%04d",r);
+ dict_put(dict,buf,&buf);
+ }
+ /* loop over dictionary contents */
+ dict_loop_first(dict);
+ while (dict_loop_next(dict,&key,&val)!=NULL)
+ {
+ assert(val==buf);
+ }
+ /* free dictionary */
+ dict_free(dict);
+}
+/* Test to insert a large number of elements in the dict. */
+static void test_readelements(const char *fname)
+{
+ DICT *dict;
+ char buf[80];
+ FILE *fp;
+ const char *key;
+ void *val;
+ /* initialize */
+ dict=dict_new();
+ /* read file and insert all entries */
+ fp=fopen(fname,"r");
+ assert(fp!=NULL);
+ while (fgets(buf,sizeof(buf),fp)!=NULL)
+ {
+ /* strip newline */
+ buf[strlen(buf)-1]='\0';
+ dict_put(dict,buf,&buf);
+ }
+ fclose(fp);
+ /* loop over dictionary contents */
+ dict_loop_first(dict);
+ while (dict_loop_next(dict,&key,&val)!=NULL)
+ {
+ assert(val==buf);
+ }
/* free dictionary */
dict_free(dict);
+}
+/* the main program... */
+int main(int UNUSED(argc),char UNUSED(*argv[]))
+{
+ char *srcdir;
+ char fname[100];
+ /* build the name of the file */
+ srcdir=getenv("srcdir");
+ if (srcdir==NULL)
+ strcpy(fname,"usernames.txt");
+ else
+ snprintf(fname,sizeof(fname),"%s/usernames.txt",srcdir);
+ fname[sizeof(fname)-1]='\0';
+ /* run the tests */
+ test_simple();
+ test_lotsofelements();
+ test_readelements(fname);
return 0;
}