📄 test_dict.c
字号:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include "dictionary.h"
int compareIt (void *first, void *second)
{
char *term1 = (char *)first;
char *term2 = (char *)second;
printf("compareIt: [%s] [%s]\n", term1, term2);
return(strcmp(term1, term2));
}
void dict_print (DICTIONARY *toPrint)
{
int i;
char *key;
char *value;
printf ("dict_print:");
printf ("\tsize: %d\n", toPrint->size);
printf ("\tcapacity: %d\n", toPrint->capacity);
printf ("\tcompar: %x\n", toPrint->compar);
for(i = 0; i < toPrint->size; i++) {
key = (char *)toPrint->key[i];
value = (char *)toPrint->value[i];
printf ("Dictionary item %i\n", i);
printf ("Key[%i]: %s\n", i, key);
printf ("Value[%i]: %s\n", i, value);
}
printf ("\n");
}
void do_dict_put(DICTIONARY *the_dictionary)
{
char test_key[80], test_value[80];
printf("do_dict_put::\n");
printf("key: ");
gets(test_key);
printf("value: ");
gets(test_value);
(void)dict_put(the_dictionary, (void *)strdup(test_key),
(void *)strdup(test_value));
dict_print(the_dictionary);
}
void do_dict_put_nonunique(DICTIONARY *the_dictionary)
{
char test_key[80], test_value[80];
printf("do_dict_put_nonunique::\n");
printf("key: ");
gets(test_key);
printf("value: ");
gets(test_value);
(void)dict_put_nonunique(the_dictionary, (void *)strdup(test_key),
(void *)strdup(test_value));
dict_print(the_dictionary);
}
void do_dict_get(DICTIONARY *the_dictionary)
{
void *a_value;
char test_key[80];
printf("do_dict_get::\n");
printf("key: ");
gets(test_key);
if ((a_value = dict_get(the_dictionary, (void *)test_key))
!= (void *)NULL)
fprintf(stderr, "dict_get: returned %s\n", (char *)a_value);
}
void do_dict_get_nth(DICTIONARY *the_dictionary)
{
void *a_value;
void *a_key;
char nth_str[80];
int nth;
printf("do_dict_get_nth::\n");
printf("nth: ");
gets(nth_str);
nth = atoi(nth_str);
if ((dict_get_nth(the_dictionary, &a_key, &a_value, nth)) > 0)
printf("dict_get_nth: returned [%s] [%s]\n", (char *)a_key,
(char *)a_value);
else
printf("dict_get_nth: value for %i not found\n", nth);
}
void do_dict_remove(DICTIONARY *the_dictionary)
{
char test_key[80];
void *a_value;
printf ("do_dict_remove::\n");
printf("key: ");
gets(test_key);
if ((a_value = dict_remove(the_dictionary,
(void *)test_key)) != (void *)NULL) {
printf("dict_remove returned [%s]\n", (char *)a_value);
free(a_value);
}
else
printf("dict_remove no entry for key [%s]\n", test_key);
}
void do_dict_remove_specific(DICTIONARY *the_dictionary)
{
char test_key[80], test_value[80];
void *a_value;
printf ("do_dict_remove_specific::\n");
printf("key: ");
gets(test_key);
printf("value: ");
gets(test_value);
if ((a_value = dict_remove_specific(the_dictionary, (void *)test_key,
(void *)test_value)) != (void *)NULL) {
printf("dict_remove_specific returned [%s]\n", (char *)a_value);
free(a_value);
}
else
printf("dict_remove_specific no entry for key/value [%s][%s]\n",
test_key, test_value);
}
void do_dict_remove_all(DICTIONARY *the_dictionary)
{
char test_key[80];
int num_found = 0;
void **values;
printf ("do_dict_remove_all::\n");
printf("key: ");
gets(test_key);
if ((values = dict_remove_all(the_dictionary,
(void *)test_key, &num_found)) > 0) {
int i;
printf("dict_remove_all removed %d entries: ", num_found);
for (i=0; i<num_found; i++) {
printf("[%s] ", (char *)(values[i]));
free((values[i]));
}
free(values);
printf("\n");
}
else
printf("dict_remove_all no entry for key [%s]\n", test_key);
}
void main(int argc, char *argv[])
{
DICTIONARY *my_dict;
char command[80];
my_dict = dict_new(compareIt, free);
if (my_dict == (DICTIONARY *)NULL)
printf("dict_new() failed\n");
else
printf("dict_new() succeeded\n");
while (1) {
printf("command:");
gets(command);
switch (command[0]) {
case ('e'):
case ('q'):
if ((strcmp("exit", command) == 0) ||
(strcmp("quit", command) == 0)) {
dict_free(my_dict);
exit (1);
}
case ('g'):
/* get or get_nth */
if (strcmp("get", command) == 0)
do_dict_get(my_dict);
else if (strcmp("get_nth", command) == 0)
do_dict_get_nth(my_dict);
else
goto fail;
break;
case ('p'):
/* put or put_nonunique */
if (strcmp("put", command) == 0)
do_dict_put(my_dict);
else if (strcmp("put_nonunique", command) == 0)
do_dict_put_nonunique(my_dict);
else if (strcmp("print", command) == 0)
dict_print(my_dict);
else
goto fail;
break;
case ('r'):
/* remove, remove_specific, remove_all */
if (strcmp("remove", command) == 0)
do_dict_remove(my_dict);
else if (strcmp("remove_specific", command) == 0)
do_dict_remove_specific(my_dict);
else if (strcmp("remove_all", command) == 0)
do_dict_remove_all(my_dict);
else
goto fail;
break;
fail: default: /* Didn't match */
printf("%s must be one of [exit, get, get_nth, put, put_nonunique, quit, remove, remove_specific, remove_all, print]\n", command);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -