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