You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2012/07/26 01:40:51 UTC

svn commit: r1365840 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent: ConcurrentStlMapTest.cpp ConcurrentStlMapTest.h

Author: tabish
Date: Wed Jul 25 23:40:51 2012
New Revision: 1365840

URL: http://svn.apache.org/viewvc?rev=1365840&view=rev
Log:
Add some additional test cases to the unit tests. 

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.cpp?rev=1365840&r1=1365839&r2=1365840&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.cpp Wed Jul 25 23:40:51 2012
@@ -18,11 +18,80 @@
 #include "ConcurrentStlMapTest.h"
 #include <string>
 #include <decaf/util/concurrent/ConcurrentStlMap.h>
+#include <decaf/util/HashMap.h>
+#include <decaf/util/StlMap.h>
+#include <decaf/util/ArrayList.h>
+#include <decaf/lang/Integer.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
 
 using namespace std;
 using namespace decaf;
 using namespace decaf::util;
 using namespace decaf::util::concurrent;
+using namespace decaf::lang;
+
+////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+    const int MAP_SIZE = 1000;
+
+    void populateMap(StlMap<int, std::string>& map) {
+        for (int i = 0; i < MAP_SIZE; ++i) {
+            map.put(i, Integer::toString(i));
+        }
+    }
+
+    void populateMap(ConcurrentStlMap<int, std::string>& map) {
+        for (int i = 0; i < MAP_SIZE; ++i) {
+            map.put(i, Integer::toString(i));
+        }
+    }
+
+    void populateMap(HashMap<int, std::string>& map) {
+        for (int i = 0; i < MAP_SIZE; ++i) {
+            map.put(i, Integer::toString(i));
+        }
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testConstructor() {
+
+    ConcurrentStlMap<string, int> map1;
+    CPPUNIT_ASSERT( map1.isEmpty() );
+    CPPUNIT_ASSERT( map1.size() == 0 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should Throw a NoSuchElementException",
+        map1.get( "TEST" ),
+        decaf::util::NoSuchElementException );
+
+    HashMap<string, int> srcMap;
+    srcMap.put( "A", 1 );
+    srcMap.put( "B", 1 );
+    srcMap.put( "C", 1 );
+
+    ConcurrentStlMap<string, int> destMap( srcMap );
+
+    CPPUNIT_ASSERT( srcMap.size() == 3 );
+    CPPUNIT_ASSERT( destMap.size() == 3 );
+    CPPUNIT_ASSERT( destMap.get( "B" ) == 1 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testConstructorMap() {
+
+    ConcurrentStlMap<int, int> myMap;
+    for (int counter = 0; counter < 125; counter++) {
+        myMap.put(counter, counter);
+    }
+
+    ConcurrentStlMap<int, int> map(myMap);
+    for (int counter = 0; counter < 125; counter++) {
+        CPPUNIT_ASSERT_MESSAGE("Failed to construct correct HashMap",
+            myMap.get(counter) == map.get(counter));
+    }
+}
 
 ////////////////////////////////////////////////////////////////////////////////
 void ConcurrentStlMapTest::testContainsKey(){
@@ -46,6 +115,74 @@ void ConcurrentStlMapTest::testClear(){
     CPPUNIT_ASSERT(boolMap.size() == 2 );
     boolMap.clear();
     CPPUNIT_ASSERT(boolMap.size() == 0 );
+
+    {
+        ConcurrentStlMap<int, std::string> map;
+        map.put(1, "one");
+        map.put(3, "three");
+        map.put(2, "two");
+
+        map.clear();
+        CPPUNIT_ASSERT_EQUAL_MESSAGE("Clear failed to reset size", 0, map.size());
+        for (int i = 0; i < 125; i++) {
+            CPPUNIT_ASSERT_THROW_MESSAGE(
+                "Failed to clear all elements",
+                map.get(i),
+                NoSuchElementException);
+        }
+
+        // Check clear on a large loaded map of Integer keys
+        ConcurrentStlMap<int, std::string> map2;
+        for (int i = -32767; i < 32768; i++) {
+            map2.put(i, "foobar");
+        }
+        map2.clear();
+        CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to reset size on large integer map", 0, map2.size());
+        for (int i = -32767; i < 32768; i++) {
+            CPPUNIT_ASSERT_THROW_MESSAGE(
+                "Failed to clear all elements",
+                map2.get(i),
+                NoSuchElementException);
+        }
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testCopy() {
+
+    ConcurrentStlMap<string, int> destMap;
+    HashMap<string, int> srcMap;
+    ConcurrentStlMap<string, int> srcMap2;
+
+    CPPUNIT_ASSERT( destMap.size() == 0 );
+
+    srcMap.put( "A", 1 );
+    srcMap.put( "B", 2 );
+    srcMap.put( "C", 3 );
+    srcMap.put( "D", 4 );
+    srcMap.put( "E", 5 );
+    srcMap.put( "F", 6 );
+
+    destMap.copy( srcMap );
+    CPPUNIT_ASSERT( destMap.size() == 6 );
+    CPPUNIT_ASSERT( destMap.get( "A" ) == 1 );
+    CPPUNIT_ASSERT( destMap.get( "B" ) == 2 );
+    CPPUNIT_ASSERT( destMap.get( "C" ) == 3 );
+    CPPUNIT_ASSERT( destMap.get( "D" ) == 4 );
+    CPPUNIT_ASSERT( destMap.get( "E" ) == 5 );
+    CPPUNIT_ASSERT( destMap.get( "F" ) == 6 );
+
+    destMap.copy( srcMap2 );
+    CPPUNIT_ASSERT( destMap.size() == 0 );
+
+    srcMap2.put( "A", 1 );
+    srcMap2.put( "B", 2 );
+    srcMap2.put( "C", 3 );
+    srcMap2.put( "D", 4 );
+    srcMap2.put( "E", 5 );
+
+    destMap.copy( srcMap2 );
+    CPPUNIT_ASSERT( destMap.size() == 5 );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -73,7 +210,67 @@ void ConcurrentStlMapTest::testSize(){
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void ConcurrentStlMapTest::testValue(){
+void ConcurrentStlMapTest::testGet() {
+
+    ConcurrentStlMap<string, bool> boolMap;
+
+    boolMap.put( "fred", true );
+    CPPUNIT_ASSERT( boolMap.get("fred") == true );
+
+    boolMap.put( "bob", false );
+    CPPUNIT_ASSERT( boolMap.get("bob") == false );
+    CPPUNIT_ASSERT( boolMap.get("fred") == true );
+
+    try{
+        boolMap.get( "mike" );
+        CPPUNIT_ASSERT(false);
+    } catch( decaf::util::NoSuchElementException& e ){
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testPut() {
+
+    ConcurrentStlMap<string, bool> boolMap;
+
+    boolMap.put( "fred", true );
+    CPPUNIT_ASSERT( boolMap.get("fred") == true );
+
+    boolMap.put( "bob", false );
+    CPPUNIT_ASSERT( boolMap.get("bob") == false );
+    CPPUNIT_ASSERT( boolMap.get("fred") == true );
+
+    boolMap.put( "bob", true );
+    CPPUNIT_ASSERT( boolMap.get("bob") == true );
+    CPPUNIT_ASSERT( boolMap.get("fred") == true );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testPutAll() {
+
+    ConcurrentStlMap<string, int> destMap;
+    HashMap<string, int> srcMap;
+    HashMap<string, int> srcMap2;
+
+    srcMap.put( "A", 1 );
+    srcMap.put( "B", 1 );
+    srcMap.put( "C", 1 );
+
+    CPPUNIT_ASSERT( srcMap.size() == 3 );
+    CPPUNIT_ASSERT( destMap.size() == 0 );
+
+    srcMap.put( "D", 1 );
+    srcMap.put( "E", 1 );
+    srcMap.put( "F", 1 );
+
+    destMap.putAll( srcMap );
+    CPPUNIT_ASSERT( destMap.size() == 6 );
+    destMap.putAll( srcMap2 );
+    CPPUNIT_ASSERT( destMap.size() == 6 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testValue() {
 
     ConcurrentStlMap<string, bool> boolMap;
 
@@ -111,3 +308,114 @@ void ConcurrentStlMapTest::testContiansV
     boolMap.remove( "fred" );
     CPPUNIT_ASSERT( boolMap.containsValue(true) == false );
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testEntrySet() {
+
+    ConcurrentStlMap<int, std::string> map;
+
+    for (int i = 0; i < 50; i++) {
+        map.put(i, Integer::toString(i));
+    }
+
+    Set<MapEntry<int, std::string> >& set = map.entrySet();
+    Pointer< Iterator<MapEntry<int, std::string> > > iterator(set.iterator());
+
+    CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size", map.size() == set.size());
+    while (iterator->hasNext()) {
+        MapEntry<int, std::string> entry = iterator->next();
+        CPPUNIT_ASSERT_MESSAGE("Returned incorrect entry set",
+                               map.containsKey(entry.getKey()) && map.containsValue(entry.getValue()));
+    }
+
+    iterator.reset(set.iterator());
+    set.remove(iterator->next());
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("Remove on set didn't take", 49, set.size());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testKeySet() {
+
+    ConcurrentStlMap<int, std::string> map;
+    populateMap(map);
+    Set<int>& set = map.keySet();
+    CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size()", set.size() == map.size());
+    for (int i = 0; i < MAP_SIZE; i++) {
+        CPPUNIT_ASSERT_MESSAGE("Returned set does not contain all keys", set.contains(i));
+    }
+
+    {
+        ConcurrentStlMap<int, std::string> localMap;
+        localMap.put(0, "test");
+        Set<int>& intSet = localMap.keySet();
+        CPPUNIT_ASSERT_MESSAGE("Failed with zero key", intSet.contains(0));
+    }
+    {
+        ConcurrentStlMap<int, std::string> localMap;
+        localMap.put(1, "1");
+        localMap.put(102, "102");
+        localMap.put(203, "203");
+
+        Set<int>& intSet = localMap.keySet();
+        Pointer< Iterator<int> > it(intSet.iterator());
+        int remove1 = it->next();
+        it->hasNext();
+        it->remove();
+        int remove2 = it->next();
+        it->remove();
+
+        ArrayList<int> list;
+        list.add(1);
+        list.add(102);
+        list.add(203);
+
+        list.remove(remove1);
+        list.remove(remove2);
+
+        CPPUNIT_ASSERT_MESSAGE("Wrong result", it->next() == list.get(0));
+        CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong size", 1, localMap.size());
+        it.reset(intSet.iterator());
+        CPPUNIT_ASSERT_MESSAGE("Wrong contents", it->next() == list.get(0));
+    }
+    {
+        ConcurrentStlMap<int, std::string> map2;
+        map2.put(1, "1");
+        map2.put(4, "4");
+
+        Set<int>& intSet = map2.keySet();
+        Pointer< Iterator<int> > it2(intSet.iterator());
+
+        int remove3 = it2->next();
+        int next;
+
+        if (remove3 == 1) {
+            next = 4;
+        } else {
+            next = 1;
+        }
+        it2->hasNext();
+        it2->remove();
+        CPPUNIT_ASSERT_MESSAGE("Wrong result 2", it2->next() == next);
+        CPPUNIT_ASSERT_EQUAL_MESSAGE("Wrong size 2", 1, map2.size());
+        it2.reset(intSet.iterator());
+        CPPUNIT_ASSERT_MESSAGE("Wrong contents 2", it2->next() == next);
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ConcurrentStlMapTest::testValues() {
+
+    ConcurrentStlMap<int, std::string> map;
+    populateMap(map);
+
+    Collection<std::string>& c = map.values();
+    CPPUNIT_ASSERT_MESSAGE("Returned collection of incorrect size()", c.size() == map.size());
+    for (int i = 0; i < MAP_SIZE; i++) {
+        CPPUNIT_ASSERT_MESSAGE("Returned collection does not contain all keys",
+                               c.contains(Integer::toString(i)));
+    }
+
+    c.remove("10");
+    CPPUNIT_ASSERT_MESSAGE("Removing from collection should alter Map",
+                           !map.containsKey(10));
+}

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.h?rev=1365840&r1=1365839&r2=1365840&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/concurrent/ConcurrentStlMapTest.h Wed Jul 25 23:40:51 2012
@@ -28,13 +28,22 @@ namespace concurrent {
     class ConcurrentStlMapTest : public CppUnit::TestFixture
     {
         CPPUNIT_TEST_SUITE( ConcurrentStlMapTest );
+        CPPUNIT_TEST( testConstructor );
+        CPPUNIT_TEST( testConstructorMap );
         CPPUNIT_TEST( testContainsKey );
         CPPUNIT_TEST( testClear );
+        CPPUNIT_TEST( testCopy );
         CPPUNIT_TEST( testSize );
         CPPUNIT_TEST( testValue );
+        CPPUNIT_TEST( testGet );
+        CPPUNIT_TEST( testPut );
+        CPPUNIT_TEST( testPutAll );
         CPPUNIT_TEST( testRemove );
         CPPUNIT_TEST( testContiansValue );
         CPPUNIT_TEST( testIsEmpty );
+        CPPUNIT_TEST( testEntrySet );
+        CPPUNIT_TEST( testKeySet );
+        CPPUNIT_TEST( testValues );
         CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -42,13 +51,22 @@ namespace concurrent {
         ConcurrentStlMapTest() {}
         virtual ~ConcurrentStlMapTest() {}
 
+        void testConstructor();
+        void testConstructorMap();
         void testContainsKey();
         void testClear();
+        void testCopy();
         void testSize();
         void testValue();
+        void testGet();
+        void testPut();
+        void testPutAll();
         void testRemove();
         void testContiansValue();
         void testIsEmpty();
+        void testEntrySet();
+        void testKeySet();
+        void testValues();
 
     };