f7febe62ab6229f65e8fdfed8410e03fe534ec84
[idzebra-moved-to-github.git] / test / api / test_sortidx.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 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 #include <idzebra/bfile.h>
21 #include <sortidx.h>
22 #include "testlib.h"
23
24 static void sort_add_cstr(zebra_sort_index_t si, const char *str,
25                           zint section_id)
26 {
27     WRBUF w = wrbuf_alloc();
28     wrbuf_puts(w, str);
29     wrbuf_putc(w, '\0');
30     zebra_sort_add(si, section_id, w);
31     wrbuf_destroy(w);
32 }
33
34 static void tst1(zebra_sort_index_t si)
35 {
36     zint sysno = 12; /* just some sysno */
37     int my_type = 2; /* just some type ID */
38     WRBUF w = wrbuf_alloc();
39
40     zebra_sort_type(si, my_type);
41
42     zebra_sort_sysno(si, sysno);
43     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
44
45     sort_add_cstr(si, "abcde1", 0);
46
47     zebra_sort_sysno(si, sysno);
48     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 1);
49     YAZ_CHECK(!strcmp(wrbuf_cstr(w), "abcde1"));
50
51     zebra_sort_sysno(si, sysno+1);
52     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
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);
58     zebra_sort_delete(si, 0);
59     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
60
61     zebra_sort_type(si, my_type);
62
63     zebra_sort_sysno(si, sysno);
64     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 0);
65
66     wrbuf_rewind(w);
67     sort_add_cstr(si, "abcde1", 0);
68
69     zebra_sort_sysno(si, sysno);
70     YAZ_CHECK_EQ(zebra_sort_read(si, 0, w), 1);
71     YAZ_CHECK(!strcmp(wrbuf_cstr(w), "abcde1"));
72
73     zebra_sort_sysno(si, sysno);
74     zebra_sort_delete(si, 0);
75
76     wrbuf_destroy(w);
77 }
78
79 static void tst2(zebra_sort_index_t si)
80 {
81     zint sysno = 15; /* just some sysno */
82     int my_type = 2; /* just some type ID */
83     int i;
84
85     zebra_sort_type(si, my_type);
86
87     for (sysno = 1; sysno < 50; sysno++)
88     {
89         zint input_section_id = 12345;
90         zint output_section_id = 0;
91         WRBUF w1 = wrbuf_alloc();
92         WRBUF w2 = wrbuf_alloc();
93         zebra_sort_sysno(si, sysno);
94         YAZ_CHECK_EQ(zebra_sort_read(si, 0, w2), 0);
95         
96         for (i = 0; i < 600; i++) /* 600 * 6 < max size =4K */
97             wrbuf_write(w1, "12345", 6);
98         
99         zebra_sort_add(si, input_section_id, w1);
100         
101         zebra_sort_sysno(si, sysno);
102         
103         YAZ_CHECK_EQ(zebra_sort_read(si, &output_section_id, w2), 1);
104         
105         YAZ_CHECK_EQ(wrbuf_len(w1), wrbuf_len(w2));
106         YAZ_CHECK(!memcmp(wrbuf_buf(w1), wrbuf_buf(w2), wrbuf_len(w2)));
107         YAZ_CHECK_EQ(input_section_id, output_section_id);
108         wrbuf_destroy(w1);
109         wrbuf_destroy(w2);
110     }
111 }
112
113 static void tst(int argc, char **argv)
114 {
115     BFiles bfs = bfs_create(".:50M", 0);
116     zebra_sort_index_t si;
117
118     YAZ_CHECK(bfs);
119     if (bfs)
120     {
121         bf_reset(bfs);
122         si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_FLAT);
123         YAZ_CHECK(si);
124         if (si)
125         {
126             tst1(si);
127             zebra_sort_close(si);
128         }
129     }
130     if (bfs)
131     {
132         bf_reset(bfs);
133         si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_ISAMB);
134         YAZ_CHECK(si);
135         if (si)
136         {
137             tst1(si);
138             zebra_sort_close(si);
139         }
140     }
141     if (bfs)
142     {
143         bf_reset(bfs);
144         si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_MULTI);
145         YAZ_CHECK(si);
146         if (si)
147         {
148             tst1(si);
149             tst2(si);
150             zebra_sort_close(si);
151         }
152     }
153     if (bfs)
154         bfs_destroy(bfs);
155 }
156
157 TL_MAIN
158 /*
159  * Local variables:
160  * c-basic-offset: 4
161  * indent-tabs-mode: nil
162  * End:
163  * vim: shiftwidth=4 tabstop=8 expandtab
164  */
165