Added cql_transform_define_pattern. Renamed rpn2cql funcs.
[yaz-moved-to-github.git] / test / tst_rpn2cql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/test.h>
10 #include <yaz/log.h>
11 #include <yaz/rpn2cql.h>
12 #include <yaz/wrbuf.h>
13 #include <yaz/pquery.h>
14
15 static int compare(cql_transform_t ct, const char *pqf, const char *cql)
16 {
17     int ret = 0;
18     ODR odr = odr_createmem(ODR_ENCODE);
19     WRBUF w = wrbuf_alloc();
20     Z_RPNQuery *q = p_query_rpn(odr, pqf);
21     
22     if (q)
23     {
24         int r = cql_transform_rpn2cql_wrbuf(ct, w, q);
25
26         if (r != 0)
27         {
28             /* transform error */
29             yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
30             if (!cql) /* also expected error? */
31                 ret = 1;
32         }
33         else if (r == 0)
34         {
35             yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
36             if (cql && !strcmp(wrbuf_cstr(w), cql))
37                 ret = 1;
38         }
39     }
40     wrbuf_destroy(w);
41     odr_destroy(odr);
42     return ret;
43 }
44
45 static void tst(void)
46 {
47     cql_transform_t ct = cql_transform_create();    
48     YAZ_CHECK(compare(ct, "abc", "abc"));
49     YAZ_CHECK(compare(ct, "@and a b", "a and b"));
50     cql_transform_close(ct);
51 }
52
53 int main (int argc, char **argv)
54 {
55     YAZ_CHECK_INIT(argc, argv);
56     YAZ_CHECK_LOG();
57     tst();
58     YAZ_CHECK_TERM;
59 }
60
61 /*
62  * Local variables:
63  * c-basic-offset: 4
64  * indent-tabs-mode: nil
65  * End:
66  * vim: shiftwidth=4 tabstop=8 expandtab
67  */
68