From e47569dbfbafa2ff798e0fe3ad5a32f26f3fc972 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 29 Aug 2011 12:24:08 +0200 Subject: [PATCH] Add test of cql_to_ccl --- test/.gitignore | 1 + test/Makefile.am | 4 +- test/test_cql2ccl.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/test_cql2ccl.c diff --git a/test/.gitignore b/test/.gitignore index 98e4284..4b086b2 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -4,6 +4,7 @@ Makefile.in test_odrcodec.c test_odrcodec.h .libs +test_cql2ccl test_ccl test_iconv test_matchstr diff --git a/test/Makefile.am b/test/Makefile.am index 478f6f8..3dcb5d3 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,8 @@ ## This file is part of the YAZ toolkit. ## Copyright (C) 1995-2011 Index Data -check_PROGRAMS = test_xmalloc test_iconv test_nmem test_matchstr test_wrbuf \ +check_PROGRAMS = test_cql2ccl \ + test_xmalloc test_iconv test_nmem test_matchstr test_wrbuf \ test_odr test_ccl test_log test_mutex \ test_soap1 test_soap2 test_solr test_odrstack test_log_thread \ test_xmlquery test_pquery \ @@ -57,6 +58,7 @@ test_libstemmer_LDADD = ../src/libyaz_icu.la ../src/libyaz.la $(ICU_LIBS) CONFIG_CLEAN_FILES=*.log +test_cql2ccl_SOURCES = test_cql2ccl.c test_xmalloc_SOURCES = test_xmalloc.c test_iconv_SOURCES = test_iconv.c test_nmem_SOURCES = test_nmem.c diff --git a/test/test_cql2ccl.c b/test/test_cql2ccl.c new file mode 100644 index 0000000..cd1547b --- /dev/null +++ b/test/test_cql2ccl.c @@ -0,0 +1,111 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2011 Index Data + * See the file LICENSE for details. + */ +#if HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + + +static int tst_query(const char *cql, const char *expected_ccl) +{ + int ret = 0; + CQL_parser cp = cql_parser_create(); + int r = cql_parser_string(cp, cql); + + if (r) + { + yaz_log(YLOG_WARN, "cql: parse error: %s", cql); + } + else + { /* cql parse OK */ + WRBUF w = wrbuf_alloc(); + r = cql_to_ccl(cql_parser_result(cp), wrbuf_vp_puts, w); + + if (expected_ccl && r == 0 && strcmp(wrbuf_cstr(w), expected_ccl) == 0) + { + ret = 1; + } + else if (!expected_ccl) + { + if (r) + ret = 1; /* expected conversion error, OK */ + else + yaz_log(YLOG_WARN, "cql: expected conversion error: %s", cql); + } + else + { + yaz_log(YLOG_WARN, "cql: diff: %s", cql); + yaz_log(YLOG_WARN, " expected %s", expected_ccl); + yaz_log(YLOG_WARN, " got %s", wrbuf_cstr(w)); + } + wrbuf_destroy(w); + } + cql_parser_destroy(cp); + return ret; +} + +static void tst(void) +{ + YAZ_CHECK(tst_query("\"\"", "\"\"")); + YAZ_CHECK(tst_query("x", "\"x\"")); + YAZ_CHECK(tst_query("\"x\"", "\"x\"")); + YAZ_CHECK(tst_query("\"xy\"", "\"xy\"")); + + YAZ_CHECK(tst_query("?", "#")); + YAZ_CHECK(tst_query("?a?", "#\"a\"#")); + YAZ_CHECK(tst_query("?a", "#\"a\"")); + YAZ_CHECK(tst_query("a?", "\"a\"#")); + YAZ_CHECK(tst_query("\"?\"", "#")); + YAZ_CHECK(tst_query("\\?", "\"?\"")); + YAZ_CHECK(tst_query("\"\\?\"", "\"?\"")); + + YAZ_CHECK(tst_query("*", "?")); + YAZ_CHECK(tst_query("*a*", "?\"a\"?")); + YAZ_CHECK(tst_query("*a", "?\"a\"")); + YAZ_CHECK(tst_query("a*", "\"a\"?")); + YAZ_CHECK(tst_query("\"*\"", "?")); + YAZ_CHECK(tst_query("\\*", "\"*\"")); + YAZ_CHECK(tst_query("\"\\*\"", "\"*\"")); + + YAZ_CHECK(tst_query("a b", "\"a\" \"b\"")); + YAZ_CHECK(tst_query("ab bc", "\"ab\" \"bc\"")); + + YAZ_CHECK(tst_query("\\\\", "\"\\\"\"")); + YAZ_CHECK(tst_query("\\\"", "\"\\\"\"")); + + YAZ_CHECK(tst_query("\\*", "\"*\"")); + YAZ_CHECK(tst_query("\"\\*\"", "\"*\"")); + YAZ_CHECK(tst_query("\\#", "\"#\"")); + YAZ_CHECK(tst_query("\"\\#\"", "\"#\"")); + + YAZ_CHECK(tst_query("title=x", "title=\"x\"")); + YAZ_CHECK(tst_query("title=x or author=y", + "(title=\"x\" or author=\"y\")")); + + + YAZ_CHECK(tst_query("title all x", "title=x")); +} + +int main(int argc, char **argv) +{ + YAZ_CHECK_INIT(argc, argv); + YAZ_CHECK_LOG(); + tst(); + YAZ_CHECK_TERM; +} +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + -- 1.7.10.4