You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/07/24 14:23:42 UTC
[trafficserver] branch master updated: Test: Convert test_Ptr.cc to
Catch.
This is an automated email from the ASF dual-hosted git repository.
amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push:
new f78dfc4 Test: Convert test_Ptr.cc to Catch.
f78dfc4 is described below
commit f78dfc4ca00134066c54e587ead50d09c1cc5315
Author: Alan M. Carroll <am...@apache.org>
AuthorDate: Thu Jul 19 19:56:06 2018 -0500
Test: Convert test_Ptr.cc to Catch.
---
CMakeLists.txt | 2 +-
lib/ts/Makefile.am | 2 +-
lib/ts/test_Ptr.cc | 99 -------------------------------------------
lib/ts/unit-tests/test_Ptr.cc | 80 ++++++++++++++++++++++++++++++++++
4 files changed, 82 insertions(+), 101 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1c15297..ca9fda7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1135,7 +1135,6 @@ add_library(libtsutil SHARED
lib/ts/test_List.cc
lib/ts/test_Map.cc
lib/ts/test_PriorityQueue.cc
- lib/ts/test_Ptr.cc
lib/ts/test_Vec.cc
lib/ts/test_X509HostnameValidator.cc
lib/ts/TestBox.h
@@ -1167,6 +1166,7 @@ add_executable(test_tslib
lib/ts/unit-tests/test_MemArena.cc
lib/ts/unit-tests/test_MemSpan.cc
lib/ts/unit-tests/test_MT_hashtable.cc
+ lib/ts/unit-tests/test_Ptr.cc
lib/ts/unit-tests/test_Regex.cc
lib/ts/unit-tests/test_Scalar.cc
lib/ts/unit-tests/test_scoped_resource.cc
diff --git a/lib/ts/Makefile.am b/lib/ts/Makefile.am
index 3f30afd..8b500a9 100644
--- a/lib/ts/Makefile.am
+++ b/lib/ts/Makefile.am
@@ -254,7 +254,6 @@ test_X509HostnameValidator_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@ @OPENSSL_LIBS
test_tsutil_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@
test_tsutil_SOURCES = \
test_PriorityQueue.cc \
- test_Ptr.cc \
tests.cc
test_tslib_CPPFLAGS = $(AM_CPPFLAGS)\
@@ -277,6 +276,7 @@ test_tslib_SOURCES = \
unit-tests/test_MemSpan.cc \
unit-tests/test_MemArena.cc \
unit-tests/test_MT_hashtable.cc \
+ unit-tests/test_Ptr.cc \
unit-tests/test_Regex.cc \
unit-tests/test_Scalar.cc \
unit-tests/test_scoped_resource.cc \
diff --git a/lib/ts/test_Ptr.cc b/lib/ts/test_Ptr.cc
deleted file mode 100644
index 9acd4c8..0000000
--- a/lib/ts/test_Ptr.cc
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
-
- @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() override { --(*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() == nullptr, "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() == nullptr, "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 = nullptr;
- 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");
-}
-
-REGRESSION_TEST(Ptr_bool)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
-{
- TestBox box(t, pstatus);
- box = REGRESSION_TEST_PASSED;
- unsigned alive = 0;
-
- Ptr<PtrObject> none;
- Ptr<PtrObject> some = make_ptr(new PtrObject(&alive));
-
- box.check(!none, "Empty Ptr<T> is false");
- box.check(bool(some), "Non-empty Ptr<T> is true");
-}
diff --git a/lib/ts/unit-tests/test_Ptr.cc b/lib/ts/unit-tests/test_Ptr.cc
new file mode 100644
index 0000000..ea38db4
--- /dev/null
+++ b/lib/ts/unit-tests/test_Ptr.cc
@@ -0,0 +1,80 @@
+/*
+
+ @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 "catch.hpp"
+
+struct PtrObject : RefCountObj {
+ PtrObject(unsigned *_c) : count(_c) { ++(*count); }
+ ~PtrObject() override { --(*count); }
+ unsigned *count;
+};
+
+TEST_CASE("Ptr", "[libts][ptr]")
+{
+ unsigned alive{0};
+
+ Ptr<PtrObject> p1 = make_ptr(new PtrObject(&alive));
+ PtrObject *p2 = p1.detach();
+
+ REQUIRE(p1.get() == nullptr);
+ REQUIRE(p2->refcount() == 1);
+
+ // Note that there's no symmetric attach.
+ p1 = p2;
+ REQUIRE(p2->refcount() == 2);
+ p1 = p1;
+ REQUIRE(p1->refcount() == 2);
+ p2->refcount_dec();
+ delete p1.detach();
+ // If that doesn't work, the subsequent alive counts will be off.
+
+ p1 = make_ptr(new PtrObject(&alive));
+ REQUIRE(alive == 1);
+ p1.clear();
+ REQUIRE(p1.get() == nullptr);
+ REQUIRE(alive == 0);
+
+ p1 = make_ptr(new PtrObject(&alive));
+ REQUIRE(alive == 1);
+ p1 = nullptr;
+ REQUIRE(alive == 0);
+
+ { // check scope based cleanup.
+ Ptr<PtrObject> pn1 = make_ptr(new PtrObject(&alive));
+
+ REQUIRE(pn1->refcount() == 1);
+
+ Ptr<PtrObject> pn2(pn1);
+ REQUIRE(pn1->refcount() == 2);
+
+ Ptr<PtrObject> pn3 = p1;
+ }
+
+ // Everything goes out of scope, so the refcounts should drop to zero.
+ REQUIRE(alive == 0);
+
+ Ptr<PtrObject> none;
+ Ptr<PtrObject> some = make_ptr(new PtrObject(&alive));
+
+ REQUIRE(!none);
+ REQUIRE(bool(some) == true);
+}