Fix leak in test
[yaz-moved-to-github.git] / test / tst_query_charset.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tst_query_charset.c,v 1.1 2007-04-10 14:42:31 adam Exp $
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 #include <yaz/query-charset.h>
12 #include <yaz/pquery.h>
13 #include <yaz/querytowrbuf.h>
14 #include <yaz/test.h>
15
16 enum query_charset_status {
17     NO_ERROR,
18     PQF_FAILED,
19     MATCH,
20     NO_MATCH,
21     CONV_FAILED
22 };
23
24 enum query_charset_status t(yaz_iconv_t cd, 
25                             const char *pqf, const char *expect_pqf)
26 {
27     YAZ_PQF_Parser parser = yaz_pqf_create();
28     ODR odr = odr_createmem(ODR_ENCODE);
29     Z_RPNQuery *rpn;
30     enum query_charset_status status = NO_ERROR;
31
32     YAZ_CHECK(parser);
33
34     YAZ_CHECK(odr);
35
36     rpn = yaz_pqf_parse(parser, odr, pqf);
37
38     yaz_pqf_destroy(parser);
39
40     if (!rpn)
41         status = PQF_FAILED;
42     else
43     {
44         WRBUF w = wrbuf_alloc();
45
46         yaz_query_charset_convert_rpnquery(rpn, odr, cd);
47         yaz_rpnquery_to_wrbuf(w, rpn);
48         if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0)
49             status = MATCH;
50         else
51         {
52             status = NO_MATCH;
53             printf("Result: %s\n", wrbuf_cstr(w));
54         }
55         wrbuf_destroy(w);
56     }
57     odr_destroy(odr);
58     return status;
59 }
60
61 static void tst(void)
62 {
63     yaz_iconv_t cd = yaz_iconv_open("iso-8859-1", "utf-8");
64
65     YAZ_CHECK(cd);
66     if (!cd)
67         return;
68
69     YAZ_CHECK_EQ(t(cd, "@attr 1=4 bad query", 0), PQF_FAILED);
70     YAZ_CHECK_EQ(t(cd, "@attr 1=4 ok", "@attrset Bib-1 @attr 1=4 ok"), MATCH);
71
72     /* m followed by latin smaller letter ae */
73     YAZ_CHECK_EQ(t(cd, "@attr 1=4 m\xc3\xa6", "@attrset Bib-1 @attr 1=4 m\xe6"), MATCH);
74
75     yaz_iconv_close(cd);
76 }
77
78 int main (int argc, char **argv)
79 {
80     YAZ_CHECK_INIT(argc, argv);
81     tst();
82     YAZ_CHECK_TERM;
83 }
84
85 /*
86  * Local variables:
87  * c-basic-offset: 4
88  * indent-tabs-mode: nil
89  * End:
90  * vim: shiftwidth=4 tabstop=8 expandtab
91  */
92