Version 5.0.1
[yaz-moved-to-github.git] / test / test_wrbuf.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 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     wrbuf_destroy(wr);
80 }
81
82 int main (int argc, char **argv)
83 {
84     YAZ_CHECK_INIT(argc, argv);
85     tstwrbuf();
86     YAZ_CHECK_TERM;
87 }
88
89 /*
90  * Local variables:
91  * c-basic-offset: 4
92  * c-file-style: "Stroustrup"
93  * indent-tabs-mode: nil
94  * End:
95  * vim: shiftwidth=4 tabstop=8 expandtab
96  */
97