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