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