Now non-libxml2 in tmarc*.xml
[yaz-moved-to-github.git] / test / tst_rpn2cql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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             {
38                 ret = 1;
39             }
40         }
41     }
42     wrbuf_destroy(w);
43     odr_destroy(odr);
44     return ret;
45 }
46
47 static void tst1(void)
48 {
49     cql_transform_t ct = cql_transform_create();    
50
51     YAZ_CHECK(compare(ct, "abc", "abc"));
52     YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
53     YAZ_CHECK(compare(ct, "@and a b", "a and b"));
54     YAZ_CHECK(compare(ct, "@attr 1=field abc", "field=abc"));
55     YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */
56
57     cql_transform_define_pattern(ct, "index.title", "1=4");
58     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title=abc"));
59
60     cql_transform_define_pattern(ct, "index.foo", "1=bar");
61     YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo=abc"));
62
63     cql_transform_close(ct);
64 }
65
66 static void tst2(void)
67 {
68     WRBUF w = wrbuf_alloc();
69     cql_transform_t ct = 0;
70     const char *srcdir = getenv("srcdir");
71     if (srcdir)
72     {
73         wrbuf_puts(w, srcdir);
74         wrbuf_puts(w, "/");
75     }
76     wrbuf_puts(w, "../etc/pqf.properties");
77     
78     ct = cql_transform_open_fname(wrbuf_cstr(w));
79     YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title=abc"));
80     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title=/exact abc"));
81     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title=abc"));
82     YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc",
83                       "dc.title=abc"));
84     YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
85     YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date<1980"));
86     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date=1980"));
87     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date>1980"));
88     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date<=1980"));
89     YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date>=1980"));
90
91     YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "cql.allRecords=1"));
92     YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
93     cql_transform_close(ct);
94     wrbuf_destroy(w);
95 }
96
97 int main (int argc, char **argv)
98 {
99     YAZ_CHECK_INIT(argc, argv);
100     YAZ_CHECK_LOG();
101     tst1();
102     tst2();
103     YAZ_CHECK_TERM;
104 }
105
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * c-file-style: "Stroustrup"
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114