This is an automated email from the ASF dual-hosted git repository. jpeach pushed a commit to branch master in repository https://git-dual.apache.org/repos/asf/trafficserver.git
commit 2a47b9fa42823d3fa01b147297da850602dc5d2a Author: James Peach <[email protected]> AuthorDate: Sat May 7 14:24:12 2016 -0700 TS-4425: Add tests for Ptr<T>. --- lib/ts/Makefile.am | 7 +++- lib/ts/test_Ptr.cc | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/lib/ts/Makefile.am b/lib/ts/Makefile.am index cb0e076..da6d149 100644 --- a/lib/ts/Makefile.am +++ b/lib/ts/Makefile.am @@ -21,7 +21,8 @@ library_includedir=$(includedir)/ts library_include_HEADERS = apidefs.h noinst_PROGRAMS = mkdfa CompileParseRules -check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Map test_Regex test_PriorityQueue test_Vec test_X509HostnameValidator +check_PROGRAMS = test_arena test_atomic test_freelist test_geometry test_List test_Map test_Regex test_PriorityQueue test_Vec test_X509HostnameValidator test_Ptr + TESTS = $(check_PROGRAMS) AM_CPPFLAGS = -I$(top_srcdir)/lib @@ -235,6 +236,10 @@ test_X509HostnameValidator_SOURCES = test_X509HostnameValidator.cc test_X509HostnameValidator_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@ @OPENSSL_LIBS@ test_X509HostnameValidator_LDFLAGS = @EXTRA_CXX_LDFLAGS@ @LIBTOOL_LINK_FLAGS@ +test_Ptr_SOURCES = test_Ptr.cc +test_Ptr_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@ +test_Ptr_LDFLAGS = @EXTRA_CXX_LDFLAGS@ @LIBTOOL_LINK_FLAGS@ + CompileParseRules_SOURCES = CompileParseRules.cc test:: $(TESTS) diff --git a/lib/ts/test_Ptr.cc b/lib/ts/test_Ptr.cc new file mode 100644 index 0000000..3f7f826 --- /dev/null +++ b/lib/ts/test_Ptr.cc @@ -0,0 +1,93 @@ +/* + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "ts/Ptr.h" +#include "ts/TestBox.h" + +struct PtrObject : RefCountObj { + PtrObject(unsigned *_c) : count(_c) { ++(*count); } + ~PtrObject() { --(*count); } + unsigned *count; +}; + +REGRESSION_TEST(Ptr_detach)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +{ + TestBox box(t, pstatus); + box = REGRESSION_TEST_PASSED; + unsigned alive = 0; + + Ptr<PtrObject> p1 = make_ptr(new PtrObject(&alive)); + PtrObject *p2 = p1.detach(); + + box.check(p1.get() == NULL, "Ptr<T>::detach NULLs the stored pointer"); + box.check(p2->refcount() == 1, "Ptr<T>::detach preserves the refcount"); + + // Note that there's no symmetric reattach. + p1 = p2; + box.check(p2->refcount() == 2, "reattaching increments the refcount again"); + p1->refcount_dec(); +} + +REGRESSION_TEST(Ptr_clear)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +{ + TestBox box(t, pstatus); + box = REGRESSION_TEST_PASSED; + unsigned alive = 0; + + Ptr<PtrObject> p1 = make_ptr(new PtrObject(&alive)); + box.check(alive == 1, "we have a live object"); + p1.clear(); + box.check(p1.get() == NULL, "Ptr<T>::clear NULLs the pointer"); + box.check(alive == 0, "Ptr<T>::clear drops the refcount"); + + p1 = make_ptr(new PtrObject(&alive)); + box.check(alive == 1, "we have a live object"); + p1 = 0; + box.check(alive == 0, "assigning NULL drops the refcount"); +} + +REGRESSION_TEST(Ptr_refcount)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) +{ + TestBox box(t, pstatus); + box = REGRESSION_TEST_PASSED; + unsigned alive = 0; + + { + Ptr<PtrObject> p1 = make_ptr(new PtrObject(&alive)); + + box.check(p1->refcount() == 1, "initial refcount is 1"); + + Ptr<PtrObject> p2(p1); + box.check(p1->refcount() == 2, "initial refcount is 1"); + + Ptr<PtrObject> p3 = p1; + } + + // Everything goes out of scope, so the refcounts should drop to zero. + box.check(alive == 0, "refcounts dropped"); +} + +int +main(int /* argc ATS_UNUSED */, char ** /* argv ATS_UNUSED */) +{ + RegressionTest::run(); + return RegressionTest::final_status == REGRESSION_TEST_PASSED ? 0 : 1; +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
