Add wrbuf_sha1_write
[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/test.h>
15
16 static void tstwrbuf(void)
17 {
18     int step;
19     WRBUF wr;
20
21     wr = 0;
22     wrbuf_destroy(wr);
23
24     wr = wrbuf_alloc();
25     YAZ_CHECK(wr);
26     wrbuf_destroy(wr);
27
28     wr = wrbuf_alloc();
29
30     YAZ_CHECK(wr);
31
32     for (step = 1; step < 65; step++)
33     {
34         int i, j, k;
35         int len;
36         char buf[64];
37         char *cp;
38         for (j = 1; j<step; j++)
39         {
40             for (i = 0; i<j; i++)
41                 buf[i] = i+1;
42             buf[i] = '\0';
43             wrbuf_puts(wr, buf);
44         }
45
46         cp = wrbuf_buf(wr);
47         len = wrbuf_len(wr);
48         YAZ_CHECK(len == step * (step-1) / 2);
49         k = 0;
50         for (j = 1; j<step; j++)
51             for (i = 0; i<j; i++)
52             {
53                 YAZ_CHECK(cp[k] == i+1);
54                 k++;
55             }
56         wrbuf_rewind(wr);
57     }
58
59     wrbuf_rewind(wr);
60     wrbuf_puts(wr, "1234");
61     wrbuf_insert(wr, 0, "abc", 3);
62     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "abc1234"));
63
64     wrbuf_rewind(wr);
65     wrbuf_puts(wr, "1234");
66     wrbuf_insert(wr, 1, "abc", 3);
67     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1abc234"));
68
69     wrbuf_rewind(wr);
70     wrbuf_puts(wr, "1234");
71     wrbuf_insert(wr, 4, "abc", 3);
72     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1234abc"));
73
74     wrbuf_rewind(wr);
75     wrbuf_puts(wr, "1234");
76     wrbuf_insert(wr, 5, "abc", 3);
77     YAZ_CHECK(!strcmp(wrbuf_cstr(wr), "1234"));
78
79 #if HAVE_GCRYPT_H
80     {
81         const char *msg = "Hello world\n";
82         wrbuf_rewind(wr);
83         wrbuf_sha1_write(wr, msg, strlen(msg), 1);
84         YAZ_CHECK(!strcmp(wrbuf_cstr(wr), 
85                           "33ab5639bfd8e7b95eb1d8d0b87781d4ffea4d5d"));
86     }
87 #endif
88     wrbuf_destroy(wr);
89 }
90
91 int main (int argc, char **argv)
92 {
93     YAZ_CHECK_INIT(argc, argv);
94     tstwrbuf();
95     YAZ_CHECK_TERM;
96 }
97
98 /*
99  * Local variables:
100  * c-basic-offset: 4
101  * c-file-style: "Stroustrup"
102  * indent-tabs-mode: nil
103  * End:
104  * vim: shiftwidth=4 tabstop=8 expandtab
105  */
106