Version 5.12.1
[yaz-moved-to-github.git] / test / test_wrbuf.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include <yaz/wrbuf.h>
14 #include <yaz/thread_create.h>
15 #include <yaz/test.h>
16
17 static int sha1_test(WRBUF wr, const char *msg, const char *expect)
18 {
19     wrbuf_rewind(wr);
20 #if HAVE_GCRYPT_H
21     wrbuf_sha1_write(wr, msg, strlen(msg), 1);
22     if (!strcmp(wrbuf_cstr(wr), expect))
23         return 1;
24     return 0;
25 #else
26     return 1;
27 #endif
28 }
29
30 #if YAZ_POSIX_THREADS
31 static void *my_handler(void *arg)
32 {
33     WRBUF wr = wrbuf_alloc();
34     int i;
35     for (i = 0; i < 1000; i++)
36     {
37         char buf[100];
38         sprintf(buf, "Hello world %d", i);
39 #if HAVE_GCRYPT_H
40         wrbuf_sha1_write(wr, buf, strlen(buf), 1);
41 #endif
42         wrbuf_rewind(wr);
43     }
44     wrbuf_destroy(wr);
45     return 0;
46 }
47
48 #define NO_THREADS 10
49 static void thread_testing(void)
50 {
51     yaz_thread_t tid[NO_THREADS];
52     int i;
53
54     for (i = 0; i < NO_THREADS; i++)
55     {
56         tid[i] = yaz_thread_create(my_handler, 0);
57     }
58     for (i = 0; i < NO_THREADS; i++)
59     {
60         void *return_data;
61         yaz_thread_join(tid + i, &return_data);
62     }
63 }
64 #endif
65
66 static void tstwrbuf(void)
67 {
68     int step;
69     WRBUF wr;
70
71     wr = 0;
72     wrbuf_destroy(wr);
73
74     wr = wrbuf_alloc();
75     YAZ_CHECK(wr);
76     wrbuf_destroy(wr);
77
78     wr = wrbuf_alloc();
79
80     YAZ_CHECK(wr);
81
82     for (step = 1; step < 65; step++)
83     {
84         int i, j, k;
85         int len;
86         char buf[64];
87         char *cp;
88         for (j = 1; j<step; j++)
89         {
90             for (i = 0; i<j; i++)
91                 buf[i] = i+1;
92             buf[i] = '\0';
93             wrbuf_puts(wr, buf);
94         }
95
96         cp = wrbuf_buf(wr);
97         len = wrbuf_len(wr);
98         YAZ_CHECK(len == step * (step-1) / 2);
99         k = 0;
100         for (j = 1; j<step; j++)
101             for (i = 0; i<j; i++)
102             {
103                 YAZ_CHECK(cp[k] == i+1);
104                 k++;
105             }
106         wrbuf_rewind(wr);
107     }
108
109     wrbuf_rewind(wr);
110     wrbuf_puts(wr, "1234");
111     wrbuf_insert(wr, 0, "abc", 3);
112     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "abc1234"));
113
114     wrbuf_rewind(wr);
115     wrbuf_puts(wr, "1234");
116     wrbuf_insert(wr, 1, "abc", 3);
117     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1abc234"));
118
119     wrbuf_rewind(wr);
120     wrbuf_puts(wr, "1234");
121     wrbuf_insert(wr, 4, "abc", 3);
122     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1234abc"));
123
124     wrbuf_rewind(wr);
125     wrbuf_puts(wr, "1234");
126     wrbuf_insert(wr, 5, "abc", 3);
127     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1234"));
128
129     YAZ_CHECK(sha1_test(wr, 
130                         "Hello world\n",
131                         "33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d"));
132
133 #if YAZ_POSIX_THREADS
134     thread_testing();
135 #endif
136     wrbuf_destroy(wr);
137 }
138
139 static void tst_cstr(void)
140 {
141     int i;
142     WRBUF w = wrbuf_alloc();
143     for (i = 0; i < 8000; i++)
144     {
145         const char *cp = wrbuf_cstr(w);
146         YAZ_CHECK(strlen(cp) == i);
147         wrbuf_putc(w, 'a');
148     }
149     wrbuf_destroy(w);
150
151     w = wrbuf_alloc();
152     for (i = 0; i < 8000; i++)
153     {
154         const char *cp = wrbuf_cstr(w);
155         YAZ_CHECK(strlen(cp) == i);
156         wrbuf_puts(w, "a");
157     }
158     wrbuf_destroy(w);
159
160 }
161
162 int main (int argc, char **argv)
163 {
164     YAZ_CHECK_INIT(argc, argv);
165     tstwrbuf();
166     tst_cstr();
167     YAZ_CHECK_TERM;
168 }
169
170 /*
171  * Local variables:
172  * c-basic-offset: 4
173  * c-file-style: "Stroustrup"
174  * indent-tabs-mode: nil
175  * End:
176  * vim: shiftwidth=4 tabstop=8 expandtab
177  */
178