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