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