From: Aina Niemetz Date: Fri, 4 Dec 2020 15:07:22 +0000 (-0800) Subject: google test: context: Migrate cdlist_black. (#5582) X-Git-Tag: cvc5-1.0.0~2503 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c71bc865f60c2011af2e96950cc0bc13268c3376;p=cvc5.git google test: context: Migrate cdlist_black. (#5582) --- diff --git a/test/unit/context/CMakeLists.txt b/test/unit/context/CMakeLists.txt index 51465c622..d88ee2661 100644 --- a/test/unit/context/CMakeLists.txt +++ b/test/unit/context/CMakeLists.txt @@ -11,7 +11,7 @@ #-----------------------------------------------------------------------------# # Add unit tests -cvc4_add_cxx_unit_test_black(cdlist_black context) +cvc4_add_unit_test_black(cdlist_black context) cvc4_add_cxx_unit_test_black(cdmap_black context) cvc4_add_cxx_unit_test_white(cdmap_white context) cvc4_add_cxx_unit_test_black(cdo_black context) diff --git a/test/unit/context/cdlist_black.cpp b/test/unit/context/cdlist_black.cpp new file mode 100644 index 000000000..71da10d0c --- /dev/null +++ b/test/unit/context/cdlist_black.cpp @@ -0,0 +1,166 @@ +/********************* */ +/*! \file cdlist_black.cpp + ** \verbatim + ** Top contributors (to current version): + ** Aina Niemetz, Morgan Deters, Tim King + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS + ** in the top-level source directory and their institutional affiliations. + ** All rights reserved. See the file COPYING in the top-level source + ** directory for licensing information.\endverbatim + ** + ** \brief Black box testing of CVC4::context::CDList<>. + ** + ** Black box testing of CVC4::context::CDList<>. + **/ + +#include + +#include +#include + +#include "base/exception.h" +#include "context/cdlist.h" +#include "memory.h" +#include "test_context.h" + +namespace CVC4 { + +using namespace context; + +namespace test { + +struct DtorSensitiveObject +{ + bool& d_dtorCalled; + DtorSensitiveObject(bool& dtorCalled) : d_dtorCalled(dtorCalled) {} + ~DtorSensitiveObject() { d_dtorCalled = true; } +}; + +class TestContextCDListBlack : public TestContext +{ + protected: + void list_test(int n) + { + list_test(n, true); + list_test(n, false); + } + + void list_test(int32_t n, bool callDestructor) + { + CDList list(d_context.get(), callDestructor); + + ASSERT_TRUE(list.empty()); + for (int32_t i = 0; i < n; ++i) + { + EXPECT_EQ(list.size(), (uint32_t)i); + list.push_back(i); + ASSERT_FALSE(list.empty()); + EXPECT_EQ(list.back(), i); + int32_t i2 = 0; + for (CDList::const_iterator j = list.begin(); j != list.end(); + ++j) + { + EXPECT_EQ(*j, i2++); + } + } + EXPECT_EQ(list.size(), (uint32_t)n); + + for (int32_t i = 0; i < n; ++i) + { + EXPECT_EQ(list[i], i); + } + } +}; + +TEST_F(TestContextCDListBlack, CDList10) { list_test(10); } + +TEST_F(TestContextCDListBlack, CDList15) { list_test(15); } + +TEST_F(TestContextCDListBlack, CDList20) { list_test(20); } + +TEST_F(TestContextCDListBlack, CDList35) { list_test(35); } + +TEST_F(TestContextCDListBlack, CDList50) { list_test(50); } + +TEST_F(TestContextCDListBlack, CDList99) { list_test(99); } + +TEST_F(TestContextCDListBlack, destructor_called) +{ + bool shouldRemainFalse = false; + bool shouldFlipToTrue = false; + bool alsoFlipToTrue = false; + bool shouldAlsoRemainFalse = false; + bool aThirdFalse = false; + + CDList listT(d_context.get(), true); + CDList listF(d_context.get(), false); + + DtorSensitiveObject shouldRemainFalseDSO(shouldRemainFalse); + DtorSensitiveObject shouldFlipToTrueDSO(shouldFlipToTrue); + DtorSensitiveObject alsoFlipToTrueDSO(alsoFlipToTrue); + DtorSensitiveObject shouldAlsoRemainFalseDSO(shouldAlsoRemainFalse); + DtorSensitiveObject aThirdFalseDSO(aThirdFalse); + + listT.push_back(shouldAlsoRemainFalseDSO); + listF.push_back(shouldAlsoRemainFalseDSO); + + d_context->push(); + + listT.push_back(shouldFlipToTrueDSO); + listT.push_back(alsoFlipToTrueDSO); + + listF.push_back(shouldRemainFalseDSO); + listF.push_back(shouldAlsoRemainFalseDSO); + listF.push_back(aThirdFalseDSO); + + EXPECT_EQ(shouldRemainFalse, false); + EXPECT_EQ(shouldFlipToTrue, false); + EXPECT_EQ(alsoFlipToTrue, false); + EXPECT_EQ(shouldAlsoRemainFalse, false); + EXPECT_EQ(aThirdFalse, false); + + d_context->pop(); + + EXPECT_EQ(shouldRemainFalse, false); + EXPECT_EQ(shouldFlipToTrue, true); + EXPECT_EQ(alsoFlipToTrue, true); + EXPECT_EQ(shouldAlsoRemainFalse, false); + EXPECT_EQ(aThirdFalse, false); +} + +TEST_F(TestContextCDListBlack, empty_iterator) +{ + CDList* list = new (true) CDList(d_context.get()); + EXPECT_EQ(list->begin(), list->end()); + list->deleteSelf(); +} + +TEST_F(TestContextCDListBlack, out_of_memory) +{ +#ifndef CVC4_MEMORY_LIMITING_DISABLED + CDList list(d_context.get()); + test::WithLimitedMemory wlm(1); + + ASSERT_THROW( + { + // We cap it at UINT32_MAX, preferring to terminate with a + // failure than run indefinitely. + for (uint32_t i = 0; i < UINT32_MAX; ++i) + { + list.push_back(i); + } + }, + std::bad_alloc); +#endif +} + +TEST_F(TestContextCDListBlack, pop_below_level_created) +{ + d_context->push(); + CDList list(d_context.get()); + d_context->popto(0); + list.push_back(42); +} +} // namespace test +} // namespace CVC4 diff --git a/test/unit/context/cdlist_black.h b/test/unit/context/cdlist_black.h deleted file mode 100644 index 845bc72c4..000000000 --- a/test/unit/context/cdlist_black.h +++ /dev/null @@ -1,162 +0,0 @@ -/********************* */ -/*! \file cdlist_black.h - ** \verbatim - ** Top contributors (to current version): - ** Morgan Deters, Tim King, Andres Noetzli - ** This file is part of the CVC4 project. - ** Copyright (c) 2009-2020 by the authors listed in the file AUTHORS - ** in the top-level source directory and their institutional affiliations. - ** All rights reserved. See the file COPYING in the top-level source - ** directory for licensing information.\endverbatim - ** - ** \brief Black box testing of CVC4::context::CDList<>. - ** - ** Black box testing of CVC4::context::CDList<>. - **/ - -#include - -#include -#include -#include - -#include "base/exception.h" -#include "context/cdlist.h" -#include "context/context.h" -#include "memory.h" - -using namespace std; -using namespace CVC4::context; -using namespace CVC4; - -struct DtorSensitiveObject { - bool& d_dtorCalled; - DtorSensitiveObject(bool& dtorCalled) : d_dtorCalled(dtorCalled) {} - ~DtorSensitiveObject() { d_dtorCalled = true; } -}; - -class CDListBlack : public CxxTest::TestSuite { - private: - Context* d_context; - - public: - void setUp() override { d_context = new Context(); } - - void tearDown() override { delete d_context; } - - // test at different sizes. this triggers grow() behavior differently. - // grow() was completely broken in revision 256 - void testCDList10() { listTest(10); } - void testCDList15() { listTest(15); } - void testCDList20() { listTest(20); } - void testCDList35() { listTest(35); } - void testCDList50() { listTest(50); } - void testCDList99() { listTest(99); } - - void listTest(int N) { - listTest(N, true); - listTest(N, false); - } - - void listTest(int N, bool callDestructor) { - CDList list(d_context, callDestructor); - - TS_ASSERT(list.empty()); - for (int i = 0; i < N; ++i) { - TS_ASSERT_EQUALS(list.size(), unsigned(i)); - list.push_back(i); - TS_ASSERT(!list.empty()); - TS_ASSERT_EQUALS(list.back(), i); - int i2 = 0; - for (CDList::const_iterator j = list.begin(); j != list.end(); ++j) { - TS_ASSERT_EQUALS(*j, i2++); - } - } - TS_ASSERT_EQUALS(list.size(), unsigned(N)); - - for (int i = 0; i < N; ++i) { - TS_ASSERT_EQUALS(list[i], i); - } - } - - void testDtorCalled() { - bool shouldRemainFalse = false; - bool shouldFlipToTrue = false; - bool alsoFlipToTrue = false; - bool shouldAlsoRemainFalse = false; - bool aThirdFalse = false; - - CDList listT(d_context, true); - CDList listF(d_context, false); - - DtorSensitiveObject shouldRemainFalseDSO(shouldRemainFalse); - DtorSensitiveObject shouldFlipToTrueDSO(shouldFlipToTrue); - DtorSensitiveObject alsoFlipToTrueDSO(alsoFlipToTrue); - DtorSensitiveObject shouldAlsoRemainFalseDSO(shouldAlsoRemainFalse); - DtorSensitiveObject aThirdFalseDSO(aThirdFalse); - - listT.push_back(shouldAlsoRemainFalseDSO); - listF.push_back(shouldAlsoRemainFalseDSO); - - d_context->push(); - - listT.push_back(shouldFlipToTrueDSO); - listT.push_back(alsoFlipToTrueDSO); - - listF.push_back(shouldRemainFalseDSO); - listF.push_back(shouldAlsoRemainFalseDSO); - listF.push_back(aThirdFalseDSO); - - TS_ASSERT_EQUALS(shouldRemainFalse, false); - TS_ASSERT_EQUALS(shouldFlipToTrue, false); - TS_ASSERT_EQUALS(alsoFlipToTrue, false); - TS_ASSERT_EQUALS(shouldAlsoRemainFalse, false); - TS_ASSERT_EQUALS(aThirdFalse, false); - - d_context->pop(); - - TS_ASSERT_EQUALS(shouldRemainFalse, false); - TS_ASSERT_EQUALS(shouldFlipToTrue, true); - TS_ASSERT_EQUALS(alsoFlipToTrue, true); - TS_ASSERT_EQUALS(shouldAlsoRemainFalse, false); - TS_ASSERT_EQUALS(aThirdFalse, false); - } - - void testEmptyIterator() { - CDList* list = new (true) CDList(d_context); - TS_ASSERT_EQUALS(list->begin(), list->end()); - list->deleteSelf(); - } - - void testOutOfMemory() { -#ifdef CVC4_MEMORY_LIMITING_DISABLED - - test::WarnWithLimitedMemoryDisabledReason(); - -#else /* CVC4_MEMORY_LIMITING_DISABLED */ - - CDList list(d_context); - test::WithLimitedMemory wlm(1); - - TS_ASSERT_THROWS( - { - // We cap it at UINT_MAX, preferring to terminate with a - // failure than run indefinitely. - for (unsigned i = 0; i < UINT_MAX; ++i) - { - list.push_back(i); - } - }, - bad_alloc&); - -#endif /* CVC4_MEMORY_LIMITING_DISABLED */ - } - - void testPopBelowLevelCreated() - { - d_context->push(); - CDList list(d_context); - d_context->popto(0); - list.push_back(42); - } -}; diff --git a/test/unit/memory.h b/test/unit/memory.h index 6aac8af26..a2f4ec52d 100644 --- a/test/unit/memory.h +++ b/test/unit/memory.h @@ -27,10 +27,10 @@ ** uses TS_WARN to alert users that these tests are disabled. **/ -#include +#include "test.h" -#ifndef __CVC4__TEST__MEMORY_H -#define __CVC4__TEST__MEMORY_H +#ifndef __CVC4__TEST__UNIT__MEMORY_H +#define __CVC4__TEST__UNIT__MEMORY_H #include #include @@ -56,16 +56,7 @@ namespace CVC4 { namespace test { -#ifdef CVC4_MEMORY_LIMITING_DISABLED - -inline void WarnWithLimitedMemoryDisabledReason() { - const std::string reason = std::string("WithLimitedMemory is disabled: ") + - std::string(CVC4_MEMORY_LIMITING_DISABLED_REASON); - TS_WARN(reason.c_str()); -} - -#else /* CVC4_MEMORY_LIMITING_DISABLED */ - +#ifndef CVC4_MEMORY_LIMITING_DISABLED class WithLimitedMemory { public: WithLimitedMemory() { remember(); } @@ -81,20 +72,19 @@ class WithLimitedMemory { struct rlimit rlim; rlim.rlim_cur = amount; rlim.rlim_max = RLIM_INFINITY; - TS_ASSERT_EQUALS(setrlimit(RLIMIT_AS, &rlim), 0); + ASSERT_EQ(setrlimit(RLIMIT_AS, &rlim), 0); } private: void remember() { struct rlimit rlim; - TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0); + ASSERT_EQ(getrlimit(RLIMIT_AS, &rlim), 0); d_prevAmount = rlim.rlim_cur; } rlim_t d_prevAmount; }; /* class WithLimitedMemory */ - -#endif /* CVC4_MEMORY_LIMITING_DISABLED */ +#endif } /* CVC4::test namespace */ } /* CVC4 namespace */