Fix documentation of of chr's equivalent directive ZEB-672
[idzebra-moved-to-github.git] / test / api / test_sortidx.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <idzebra/bfile.h>
24 #include <sortidx.h>
25 #include "testlib.h"
26
27 static void sort_add_cstr(zebra_sort_index_t si, const char *str,
28                           zint section_id)
29 {
30     WRBUF w = wrbuf_alloc();
31     wrbuf_puts(w, str);
32     wrbuf_putc(w, '\0');
33     zebra_sort_add(si, section_id, w);
34     wrbuf_destroy(w);
35 }
36
37 static void tst1(zebra_sort_index_t si)
38 {
39     zint sysno = 12; /* just some sysno */
40     int my_type = 2; /* just some type ID */
41     WRBUF w = wrbuf_alloc();
42
43     zebra_sort_type(si, my_type);
44
45     zebra_sort_sysno(si, sysno);
46     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
47
48     sort_add_cstr(si, "abcde1", 0);
49
50     zebra_sort_sysno(si, sysno);
51     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 1);
52     YAZ_CHECK(!strcmp(wrbuf_cstr(w), "abcde1"));
53
54     zebra_sort_sysno(si, sysno+1);
55     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
56
57     zebra_sort_sysno(si, sysno-1);
58     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
59
60     zebra_sort_sysno(si, sysno);
61     zebra_sort_delete(si, 0);
62     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
63
64     zebra_sort_type(si, my_type);
65
66     zebra_sort_sysno(si, sysno);
67     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
68
69     wrbuf_rewind(w);
70     sort_add_cstr(si, "abcde1", 0);
71
72     zebra_sort_sysno(si, sysno);
73     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 1);
74     YAZ_CHECK(!strcmp(wrbuf_cstr(w), "abcde1"));
75
76     zebra_sort_sysno(si, sysno);
77     zebra_sort_delete(si, 0);
78
79     wrbuf_destroy(w);
80 }
81
82 static void tst2(zebra_sort_index_t si)
83 {
84     zint sysno = 15; /* just some sysno */
85     int my_type = 2; /* just some type ID */
86     int i;
87
88     zebra_sort_type(si, my_type);
89
90     for (sysno = 1; sysno < 50; sysno++)
91     {
92         zint input_section_id = 12345;
93         zint output_section_id = 0;
94         WRBUF w1 = wrbuf_alloc();
95         WRBUF w2 = wrbuf_alloc();
96         zebra_sort_sysno(si, sysno);
97         YAZ_CHECK_EQ(zebra_sort_read(si, 0, w2), 0);
98
99         for (i = 0; i < 600; i++) /* 600 * 6 < max size =4K */
100             wrbuf_write(w1, "12345", 6);
101
102         zebra_sort_add(si, input_section_id, w1);
103
104         zebra_sort_sysno(si, sysno);
105
106         YAZ_CHECK_EQ(zebra_sort_read(si, &output_section_id, w2), 1);
107
108         YAZ_CHECK_EQ(wrbuf_len(w1), wrbuf_len(w2));
109         YAZ_CHECK(!memcmp(wrbuf_buf(w1), wrbuf_buf(w2), wrbuf_len(w2)));
110         YAZ_CHECK_EQ(input_section_id, output_section_id);
111         wrbuf_destroy(w1);
112         wrbuf_destroy(w2);
113     }
114 }
115
116 static void tst(int argc, char **argv)
117 {
118     BFiles bfs = bfs_create(".:50M", 0);
119     zebra_sort_index_t si;
120
121     YAZ_CHECK(bfs);
122     if (bfs)
123     {
124         bf_reset(bfs);
125         si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_FLAT);
126         YAZ_CHECK(si);
127         if (si)
128         {
129             tst1(si);
130             zebra_sort_close(si);
131         }
132     }
133     if (bfs)
134     {
135         bf_reset(bfs);
136         si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_ISAMB);
137         YAZ_CHECK(si);
138         if (si)
139         {
140             tst1(si);
141             zebra_sort_close(si);
142         }
143     }
144     if (bfs)
145     {
146         bf_reset(bfs);
147         si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_MULTI);
148         YAZ_CHECK(si);
149         if (si)
150         {
151             tst1(si);
152             tst2(si);
153             zebra_sort_close(si);
154         }
155     }
156     if (bfs)
157         bfs_destroy(bfs);
158 }
159
160 TL_MAIN
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * c-file-style: "Stroustrup"
165  * indent-tabs-mode: nil
166  * End:
167  * vim: shiftwidth=4 tabstop=8 expandtab
168  */
169