~ubuntu-branches/ubuntu/raring/maradns/raring

1 by Jaakko Niemi
Import upstream version 1.0.23
1
/* Given in to the public domain 2000,2001 by Sam Trenholme */
2
/* This is a series of regression tests for the js_string library */
3
4
#include "MaraHash.h"
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
9
extern int mhash_firstkey();
10
extern int mhash_nextkey();
11
12
int main() {
13
    js_string *s1,*s2,*get;
14
    mhash *dict;
15
    mhash_e e;
16
    char strn[256];
17
    mhash_offset counter = 0;
18
    int see_ret;
1.2.3 by Kai Hendry
Import upstream version 1.4.02
19
    char *nowarn;
1 by Jaakko Niemi
Import upstream version 1.0.23
20
21
    /* creation of strings */
22
    s1 = js_create(256,1);
23
    s2 = js_create(256,1);
24
25
    printf("\n");
26
27
    /* Test the ability to put and get info from a dictionary */
28
    /* First, create the dictionary */
29
    printf("Interactive session: create dictionary\n");
30
    printf("Number of bits in hash (default 8): ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
31
    nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
32
    if(strn[strlen(strn) - 1] == '\n')
33
        strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
34
    if(atoi(strn) >= 8 && atoi(strn) < 31)
35
        dict = (mhash *)mhash_create(atoi(strn));
36
    else
37
        dict = (mhash *)mhash_create(8);
38
    if(dict == 0) {
39
        printf("Fatal: Couldn't make Dictionary!\n");
40
        exit(1);
41
        }
42
    /* Add some elements to the dictionary */
43
    js_qstr2js(s1,"key");
44
    js_qstr2js(s2,"value");
45
    mhash_put_js(dict,s1,s2);
46
    js_qstr2js(s1,"otherkey");
47
    js_qstr2js(s2,"hihihi");
48
    mhash_put_js(dict,s1,s2);
49
    get = mhash_get_js(dict,s1);
50
    printf("%s\n","You should see:\nhihihi\nvalue\n\n");
51
    js_show_stdout(get);
52
    printf("\n");
53
    js_qstr2js(s1,"key");
54
    get = mhash_get_js(dict,s1);
55
    js_show_stdout(get);
56
    printf("\n");
57
58
    /* Some more hash testing */
59
    printf("Interactive session: add/remove/view mhash elements\n");
60
    printf("key = value, otherkey = hihihi, otherwise empty mhash\n");
61
    for(;;) {
62
        printf("a to add, v to view, d to delete, s to see the entire ");
63
        printf("hash, r to resize,\n");
64
        printf("g to add element with autogrow check,and q to quit\n");
65
        printf("Enter command: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
66
        nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
67
        if(strn[strlen(strn) - 1] == '\n')
68
            strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
69
        if(*strn == 'r') {
70
            printf("New size of hash (in hash_bits): ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
71
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
72
            if(strn[strlen(strn) - 1] == '\n')
73
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
74
            if(atoi(strn) >= 8 && atoi(strn) < 31)
75
                see_ret = mhash_resize(dict,atoi(strn));
76
            else
77
                see_ret = mhash_resize(dict,atoi(strn));
78
            printf("mhash_resize returned %d\n",see_ret);
79
            }
80
        if(*strn == 's') {
81
            printf("Viewing the assosciative array\n");
82
            mhash_firstkey(dict,s1);
83
            do {
84
                printf("Hash: %d\n",mhash_js(s1,dict->hash_bits));
85
                printf("Key: ");
86
                js_show_stdout(s1);
87
                printf(" Value: ");
88
                e = mhash_get(dict,s1);
89
                if(e.datatype == MARA_JS)
90
                    js_show_stdout(e.value);
91
                else
92
                    printf("pointer to %p",
93
                           dict->hash_table[counter]->value);
94
                printf("\n\n");
95
                } while(mhash_nextkey(dict,s1) != 0);
96
            }
97
        else if(*strn == 'a') {
98
            printf("Element to add: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
99
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
100
            if(strn[strlen(strn) - 1] == '\n')
101
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
102
            js_qstr2js(s1,strn);
103
            printf("Value of element: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
104
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
105
            if(strn[strlen(strn) - 1] == '\n')
106
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
107
            js_qstr2js(s2,strn);
108
            printf("mhash_put_js returned %d\n",mhash_put_js(dict,s1,s2));
109
            }
110
        else if(*strn == 'g') {
111
            printf("Element to add: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
112
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
113
            if(strn[strlen(strn) - 1] == '\n')
114
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
115
            js_qstr2js(s1,strn);
116
            printf("Value of element: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
117
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
118
            if(strn[strlen(strn) - 1] == '\n')
119
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
120
            js_qstr2js(s2,strn);
121
            printf("mhash_put_js returned %d\n",mhash_put_js(dict,s1,s2));
122
            printf("mhash_autogrow returned %d\n",mhash_autogrow(dict));
123
            }
124
        else if(*strn == 'v') {
125
            printf("Element to view: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
126
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
127
            if(strn[strlen(strn) - 1] == '\n')
128
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
129
            js_qstr2js(s1,strn);
130
            get = mhash_get_js(dict,s1);
131
            printf("mhash_get_js returned %p\n",get);
132
            printf("Viewing element %s: ",strn);
133
            js_show_stdout(get);
134
            printf("\n");
135
            }
136
        else if(*strn == 'd') {
137
            printf("Element to delete: ");
1.2.3 by Kai Hendry
Import upstream version 1.4.02
138
            nowarn = fgets(strn,200,stdin);
1.1.3 by Kai Hendry
Import upstream version 1.2.07.5
139
            if(strn[strlen(strn) - 1] == '\n')
140
                strn[strlen(strn) - 1] = '\0';
1 by Jaakko Niemi
Import upstream version 1.0.23
141
            js_qstr2js(s1,strn);
142
            printf("mhash_undef_js returned %d\n",mhash_undef_js(dict,s1));
143
            }
144
        else if(*strn == 'q')
145
            break;
146
        }
147
148
    return 0;
149
    }