From 24a138f8cd6c4c2c47d5c8d9b8efd7b19173b600 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 7 May 2010 11:21:02 +0200 Subject: [PATCH] Add test_shared_ptr --- test/Makefile.am | 3 +- test/test_shared_ptr.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 test/test_shared_ptr.c diff --git a/test/Makefile.am b/test/Makefile.am index 762e372..da02fd6 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -6,7 +6,7 @@ check_PROGRAMS = test_xmalloc test_iconv test_nmem test_matchstr test_wrbuf \ test_soap1 test_soap2 test_odrstack test_log_thread test_xmlquery test_pquery \ test_comstack test_filepath test_record_conv test_retrieval test_tpath \ test_timing test_query_charset test_oid test_icu test_match_glob \ - test_rpn2cql test_json test_xml_include test_file_glob + test_rpn2cql test_json test_xml_include test_file_glob test_shared_ptr check_SCRIPTS = tstmarc.sh tstmarccol.sh tstcql2xcql.sh tstcql2pqf.sh tsticu.sh @@ -80,3 +80,4 @@ test_rpn2cql_SOURCES = test_rpn2cql.c test_json_SOURCES = test_json.c test_xml_include_SOURCES = test_xml_include.c test_file_glob_SOURCES = test_file_glob.c +test_shared_ptr_SOURCES = test_shared_ptr.c diff --git a/test/test_shared_ptr.c b/test/test_shared_ptr.c new file mode 100644 index 0000000..cff3ee0 --- /dev/null +++ b/test/test_shared_ptr.c @@ -0,0 +1,96 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data + * See the file LICENSE for details. + */ + +/** + * \file test_shared_ptr.c + * \brief test shared pointer + */ + +#if HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include +#include +#include + +#define YAZ_SHPTR_TYPE(type) \ + struct type##_shptr \ + { \ + type ptr; \ + int ref; \ + YAZ_MUTEX mutex; \ + }; \ + typedef struct type##_shptr *type##_shptr_t; + +#define YAZ_SHPTR_INIT(p,n) { \ + p = xmalloc(sizeof(*p)); \ + p->ptr = n; \ + p->ref = 1; \ + p->mutex = 0; \ + yaz_mutex_create(&p->mutex); \ + } + +#define YAZ_SHPTR_INC(p) { \ + yaz_mutex_enter(p->mutex); \ + p->ref++; \ + yaz_mutex_leave(p->mutex); \ + } + +#define YAZ_SHPTR_DEC(p, destroy) { \ + yaz_mutex_enter(p->mutex); \ + if (--p->ref == 0) { \ + yaz_mutex_leave(p->mutex); \ + destroy(p->ptr); \ + yaz_mutex_destroy(&p->mutex); \ + xfree(p); \ + p = 0; \ + } else { \ + yaz_mutex_leave(p->mutex); \ + } \ + } + +YAZ_SHPTR_TYPE(WRBUF) + +static void test(void) +{ + WRBUF w = wrbuf_alloc(); + + WRBUF_shptr_t t = 0; + + YAZ_SHPTR_INIT(t, w); + YAZ_CHECK(t); + + YAZ_SHPTR_INC(t); + YAZ_CHECK(t); + + YAZ_SHPTR_DEC(t, wrbuf_destroy); + YAZ_CHECK(t); + + YAZ_SHPTR_DEC(t, wrbuf_destroy); + YAZ_CHECK(!t); +} + +int main (int argc, char **argv) +{ + YAZ_CHECK_INIT(argc, argv); + test(); + 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