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