Added tests for pql2cql using relation and truncation.
[yaz-moved-to-github.git] / test / tstpquery.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8
9 #include <yaz/wrbuf.h>
10 #include <yaz/querytowrbuf.h>
11 #include <yaz/pquery.h>
12 #include <yaz/test.h>
13
14 int expect_pqf(const char *pqf, const char *expect_pqf, int expect_error)
15 {
16     YAZ_PQF_Parser parser = yaz_pqf_create();
17     int res = 0;
18     ODR odr = odr_createmem(ODR_ENCODE);
19     Z_RPNQuery *rpn;
20
21     if (!parser)
22         return 0;
23
24     if (!odr)
25         return 0;
26
27     rpn = yaz_pqf_parse(parser, odr, pqf);
28
29     if (!rpn)
30     {
31         const char *msg;
32         size_t offset;
33         int got_error = yaz_pqf_error (parser, &msg, &offset);
34
35         if (expect_error == got_error)
36             res = 1;
37     }
38     else if (expect_error == YAZ_PQF_ERROR_NONE)
39     {
40         WRBUF wrbuf = wrbuf_alloc();
41         
42         if (wrbuf)
43         {
44             yaz_rpnquery_to_wrbuf(wrbuf, rpn);
45             
46             if (!strcmp(wrbuf_cstr(wrbuf), expect_pqf))
47                 res = 1;
48             wrbuf_destroy(wrbuf);
49         }
50     }
51     yaz_pqf_destroy(parser);
52     odr_destroy(odr);
53     return res;
54 }
55
56 static void tst(void)
57 {
58     YAZ_CHECK(expect_pqf("a", "@attrset Bib-1 a", YAZ_PQF_ERROR_NONE));
59     YAZ_CHECK(expect_pqf("@attr 1=4 a", "@attrset Bib-1 @attr 1=4 a", YAZ_PQF_ERROR_NONE));
60     YAZ_CHECK(expect_pqf("a b", "", YAZ_PQF_ERROR_EXTRA));
61     YAZ_CHECK(expect_pqf("@and a", "", YAZ_PQF_ERROR_MISSING));
62     YAZ_CHECK(expect_pqf("@attr p=q a", "", YAZ_PQF_ERROR_BAD_INTEGER));
63     YAZ_CHECK(expect_pqf("@prox 0 0 0 0 k 0 a b",
64                          "@attrset Bib-1 @prox 0 0 0 0 k 0 a b",
65                          YAZ_PQF_ERROR_NONE));
66     YAZ_CHECK(expect_pqf("@prox 0 0 0 0 3 0 a b", "",
67                          YAZ_PQF_ERROR_PROXIMITY));
68 }
69
70 int main (int argc, char **argv)
71 {
72     YAZ_CHECK_INIT(argc, argv);
73     tst();
74     YAZ_CHECK_TERM;
75 }
76
77 /*
78  * Local variables:
79  * c-basic-offset: 4
80  * c-file-style: "Stroustrup"
81  * indent-tabs-mode: nil
82  * End:
83  * vim: shiftwidth=4 tabstop=8 expandtab
84  */
85