From c7e6e65dc7e8204798a41f6326a9e04632f1f507 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 4 Jul 2008 11:26:52 +0200 Subject: [PATCH] Testing of RPN to CQL conversion. --- include/yaz/cql.h | 6 +++++ src/cqltransform.c | 18 ++++++++++---- test/Makefile.am | 3 ++- test/tst_rpn2cql.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 test/tst_rpn2cql.c diff --git a/include/yaz/cql.h b/include/yaz/cql.h index bf88187..8cc3c85 100644 --- a/include/yaz/cql.h +++ b/include/yaz/cql.h @@ -227,6 +227,12 @@ void cql_fputs(const char *buf, void *client_data); */ typedef struct cql_transform_t_ *cql_transform_t; +/** \brief creates a CQL transform handle + \returns transform handle or NULL for failure +*/ +YAZ_EXPORT +cql_transform_t cql_transform_create(void); + /** \brief creates a CQL transform handle from am opened file handle \param f file where transformation spec is read \returns transform handle or NULL for failure diff --git a/src/cqltransform.c b/src/cqltransform.c index 199425d..d350242 100644 --- a/src/cqltransform.c +++ b/src/cqltransform.c @@ -41,17 +41,25 @@ struct cql_transform_t_ { WRBUF w; }; -cql_transform_t cql_transform_open_FILE(FILE *f) + +cql_transform_t cql_transform_create(void) { - char line[1024]; cql_transform_t ct = (cql_transform_t) xmalloc(sizeof(*ct)); - struct cql_prop_entry **pp = &ct->entry; ct->tok_cfg = yaz_tok_cfg_create(); ct->w = wrbuf_alloc(); - - yaz_tok_cfg_single_tokens(ct->tok_cfg, "="); ct->error = 0; ct->addinfo = 0; + ct->entry = 0; + return ct; +} + +cql_transform_t cql_transform_open_FILE(FILE *f) +{ + cql_transform_t ct = cql_transform_create(); + char line[1024]; + struct cql_prop_entry **pp = &ct->entry; + + yaz_tok_cfg_single_tokens(ct->tok_cfg, "="); while (fgets(line, sizeof(line)-1, f)) { diff --git a/test/Makefile.am b/test/Makefile.am index 05ac610..676a0a4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,7 +4,7 @@ check_PROGRAMS = tsticonv tstnmem tstmatchstr tstwrbuf tstodr tstccl tstlog \ tstsoap1 tstsoap2 tstodrstack tstlogthread tstxmlquery tstpquery \ tst_comstack tst_filepath tst_record_conv tst_retrieval tst_tpath \ - tst_timing tst_query_charset tst_oid tst_icu_I18N tst_match_glob + tst_timing tst_query_charset tst_oid tst_icu_I18N tst_match_glob tst_rpn2cql check_SCRIPTS = tstmarc.sh tstmarccol.sh tstcql2xcql.sh tstcql2pqf.sh TESTS = $(check_PROGRAMS) $(check_SCRIPTS) @@ -71,3 +71,4 @@ tst_timing_SOURCES = tst_timing.c tst_query_charset_SOURCES = tst_query_charset.c tst_icu_I18N_SOURCES = tst_icu_I18N.c tst_match_glob_SOURCES = tst_match_glob.c +tst_rpn2cql_SOURCES = tst_rpn2cql.c diff --git a/test/tst_rpn2cql.c b/test/tst_rpn2cql.c new file mode 100644 index 0000000..bed3d66 --- /dev/null +++ b/test/tst_rpn2cql.c @@ -0,0 +1,68 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2008 Index Data + * See the file LICENSE for details. + */ + +#include +#include + +#include +#include +#include +#include +#include + +static int compare(cql_transform_t ct, const char *pqf, const char *cql) +{ + int ret = 0; + ODR odr = odr_createmem(ODR_ENCODE); + WRBUF w = wrbuf_alloc(); + Z_RPNQuery *q = p_query_rpn(odr, pqf); + + if (q) + { + int r = cql_transform_rpn2cql(ct, wrbuf_vputs, w, q); + + if (r != 0) + { + /* transform error */ + yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r); + if (!cql) /* also expected error? */ + ret = 1; + } + else if (r == 0) + { + yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w)); + if (cql && !strcmp(wrbuf_cstr(w), cql)) + ret = 1; + } + } + wrbuf_destroy(w); + odr_destroy(odr); + return ret; +} + +static void tst(void) +{ + cql_transform_t ct = cql_transform_create(); + YAZ_CHECK(compare(ct, "abc", "abc")); + YAZ_CHECK(compare(ct, "@and a b", "a and b")); + cql_transform_close(ct); +} + +int main (int argc, char **argv) +{ + YAZ_CHECK_INIT(argc, argv); + YAZ_CHECK_LOG(); + tst(); + YAZ_CHECK_TERM; +} + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + -- 1.7.10.4