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