diff options
author | Tim King <taking@google.com> | 2016-01-08 18:19:30 -0800 |
---|---|---|
committer | Tim King <taking@google.com> | 2016-01-08 18:19:30 -0800 |
commit | 3a973bd4fb586707a20d5e73146b79ff9fd6a77a (patch) | |
tree | 506d13a137c620751d350bf03ed2c888656e9918 /test/unit/util | |
parent | f4ef7af0a2295691f281ee1604dfeb4082fe229c (diff) |
Adding a new Listener utility class. Changing the ResourceManager to use Listeners for reporting hard and soft resource out() events.
Diffstat (limited to 'test/unit/util')
-rw-r--r-- | test/unit/util/listener_black.h | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/test/unit/util/listener_black.h b/test/unit/util/listener_black.h new file mode 100644 index 000000000..cd3a08d4e --- /dev/null +++ b/test/unit/util/listener_black.h @@ -0,0 +1,156 @@ +/********************* */ +/*! \file output_black.h + ** \verbatim + ** Original author: Morgan Deters + ** Major contributors: none + ** Minor contributors (to current version): none + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2014 New York University and The University of Iowa + ** See the file COPYING in the top-level source directory for licensing + ** information.\endverbatim + ** + ** \brief Black box testing of CVC4 output classes. + ** + ** Black box testing of CVC4 output classes. + **/ + +#include <cxxtest/TestSuite.h> +#include <sstream> + +#include "base/listener.h" + +using namespace CVC4; +using namespace std; + +class ListenerBlack : public CxxTest::TestSuite { + + std::multiset<std::string> d_events; + + class EventListener : public Listener { + public: + EventListener(std::multiset<std::string>& events, std::string name) + : d_events(events), d_name(name) {} + ~EventListener(){} + + virtual void notify() { d_events.insert(d_name); } + + private: + std::multiset<std::string>& d_events; + std::string d_name; + }; + +public: + + static std::multiset<std::string> mkMultiset(std::string arr[], int len){ + return std::multiset<std::string>(arr, arr + len); + } + + void setUp() { + d_events.clear(); + } + + void tearDown() { + d_events.clear(); + } + + void testEmptyCollection() { + // Makes an new collection and tests that it is empty. + ListenerCollection* collection = new ListenerCollection; + TS_ASSERT(collection->empty()); + TS_ASSERT_THROWS_NOTHING( delete collection ); + } + + void testSingletonCollection() { + ListenerCollection collection; + std::string expected[1] = {"a"}; + { + RegisterListener a(&collection, new EventListener(d_events, "a")); + collection.notify(); + TS_ASSERT(not collection.empty()); + } + TS_ASSERT(collection.empty()); + TS_ASSERT_EQUALS(d_events, mkMultiset(expected, 1)); + } + + void testSingleRegisterTearDown() { + // Tests that collection succeeds at destruction after + // registering a single event. + ListenerCollection* collection = new ListenerCollection; + { + RegisterListener a(collection, new EventListener(d_events, "a")); + TS_ASSERT(not collection->empty()); + // The destructor for a now runs. + } + TS_ASSERT(collection->empty()); + TS_ASSERT_THROWS_NOTHING( delete collection ); + } + + void testMultipleCollection() { + ListenerCollection* collection = new ListenerCollection; + { + RegisterListener c(collection, new EventListener(d_events, "c")); + collection->notify(); + // d_events == {"c"} + RegisterListener b(collection, new EventListener(d_events, "b")); + RegisterListener a(collection, new EventListener(d_events, "a")); + collection->notify(); + // d_events == {"a", "b", "c", "c"} + TS_ASSERT(not collection->empty()); + } + TS_ASSERT(collection->empty()); + std::string expected[4] = {"a", "b", "c", "c"}; + TS_ASSERT_EQUALS(d_events, mkMultiset(expected, 4)); + } + + void testRegisterMiddleTearDown() { + // Tests that collection succeeds at destruction after + // registering several events. + ListenerCollection* collection = new ListenerCollection; + { + RegisterListener a(collection, new EventListener(d_events, "a")); + RegisterListener* b = + new RegisterListener(collection, new EventListener(d_events, "b")); + RegisterListener c(collection, new EventListener(d_events, "c")); + + collection->notify(); + delete b; + collection->notify(); + // The destructor for a and c now run. + TS_ASSERT(not collection->empty()); + } + TS_ASSERT(collection->empty()); + TS_ASSERT_THROWS_NOTHING( delete collection ); + } + + + + void testRegisterMultiple() { + // This tests adds and notify multiple times. + ListenerCollection collection; + + std::vector<RegisterListener*> listeners; + for(int i = 0; i < 4 ; ++i){ + stringstream ss; ss << i; + Listener* listener = new EventListener(d_events, ss.str()); + listeners.push_back(new RegisterListener(&collection, listener)); + collection.notify(); + } + + TS_ASSERT(not collection.empty()); + for(int i=0; i < listeners.size(); ++i){ + RegisterListener* at_i = listeners[i]; + delete at_i; + } + listeners.clear(); + TS_ASSERT(collection.empty()); + + std::string expected[10] = + {"0", "0", "0", "0", "1", "1", "1", "2", "2", "3"}; + TS_ASSERT_EQUALS(d_events, mkMultiset(expected, 10)); + + // No more events occur. + collection.notify(); + TS_ASSERT_EQUALS(d_events, mkMultiset(expected, 10)); + } + +}; |