Author: tabish
Date: Tue Mar 19 18:52:34 2013
New Revision: 1458455
URL: http://svn.apache.org/r1458455
Log:
Adds a HashSet impl and tests
Added:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.cpp
(with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.h
(with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.cpp
(with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.h
(with props)
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/AbstractSet.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/ArrayList.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Collection.h
activemq/activemq-cpp/trunk/activemq-cpp/src/test/Makefile.am
activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am?rev=1458455&r1=1458454&r2=1458455&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am Tue Mar 19
18:52:34 2013
@@ -550,6 +550,7 @@ cc_sources = \
decaf/util/Deque.cpp \
decaf/util/HashCode.cpp \
decaf/util/HashMap.cpp \
+ decaf/util/HashSet.cpp \
decaf/util/Iterator.cpp \
decaf/util/LRUCache.cpp \
decaf/util/LinkedList.cpp \
@@ -1198,6 +1199,7 @@ h_sources = \
decaf/util/Deque.h \
decaf/util/HashCode.h \
decaf/util/HashMap.h \
+ decaf/util/HashSet.h \
decaf/util/Iterator.h \
decaf/util/LRUCache.h \
decaf/util/LinkedList.h \
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/AbstractSet.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/AbstractSet.h?rev=1458455&r1=1458454&r2=1458455&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/AbstractSet.h
(original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/AbstractSet.h
Tue Mar 19 18:52:34 2013
@@ -64,29 +64,29 @@ namespace util {
* Note that this implementation will throw an
UnsupportedOperationException if the
* iterator returned by the iterator method does not implement the
remove method.
*/
- virtual bool removeAll( const Collection<E>& collection ) {
+ virtual bool removeAll(const Collection<E>& collection) {
- bool result = false;
- if( this->size() <= collection.size() ) {
+ bool result = false;
+ if (this->size() <= collection.size()) {
- std::auto_ptr< Iterator<E> > iter( this->iterator() );
- while( iter->hasNext() ) {
- if( collection.contains( iter->next() ) ) {
- iter->remove();
- result = true;
+ std::auto_ptr<Iterator<E> > iter(this->iterator());
+ while (iter->hasNext()) {
+ if (collection.contains(iter->next())) {
+ iter->remove();
+ result = true;
+ }
}
- }
- } else {
+ } else {
- std::auto_ptr< Iterator<E> > iter( collection.iterator() );
- while( iter->hasNext() ) {
- result = this->remove( iter->next() ) || result;
+ std::auto_ptr<Iterator<E> > iter(collection.iterator());
+ while (iter->hasNext()) {
+ result = this->remove(iter->next()) || result;
+ }
}
- }
- return result;
- }
+ return result;
+ }
};
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/ArrayList.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/ArrayList.h?rev=1458455&r1=1458454&r2=1458455&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/ArrayList.h
(original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/ArrayList.h
Tue Mar 19 18:52:34 2013
@@ -46,50 +46,49 @@ namespace util {
public:
- ArrayList() : AbstractList<E>(), elements( NULL ), capacity( 0 ),
head( 0 ), curSize( 0 ) {
- this->ensureCapacity( 10 );
+ ArrayList() : AbstractList<E>(), elements(NULL), capacity(0), head(0),
curSize(0) {
+ this->ensureCapacity(10);
}
- ArrayList( const Collection<E>& collection ) : AbstractList<E>(),
elements( NULL ),
- capacity( 0 ), head( 0
), curSize( 0 ) {
+ ArrayList(const Collection<E>& collection) :
+ AbstractList<E>(), elements(NULL), capacity(0), head(0),
curSize(0) {
- this->capacity = collection.size() + ( collection.size() / 10 );
+ this->capacity = collection.size() + (collection.size() / 10);
this->elements = new E[this->capacity];
- std::auto_ptr< Iterator<E> > iter( collection.iterator() );
- while( iter->hasNext() ) {
+ std::auto_ptr<Iterator<E> > iter(collection.iterator());
+ while (iter->hasNext()) {
this->elements[this->head++] = iter->next();
this->curSize++;
}
}
- ArrayList( const ArrayList<E>& arrayList ) : AbstractList<E>(),
elements( NULL ),
- capacity( 0 ), head( 0 ),
curSize( 0 ) {
+ ArrayList(const ArrayList<E>& arrayList) :
+ AbstractList<E>(), elements(NULL), capacity(0), head(0),
curSize(0) {
- this->capacity = arrayList.size() + ( arrayList.size() / 10 );
+ this->capacity = arrayList.size() + (arrayList.size() / 10);
this->elements = new E[this->capacity];
- std::auto_ptr< Iterator<E> > iter( arrayList.iterator() );
- while( iter->hasNext() ) {
+ std::auto_ptr<Iterator<E> > iter(arrayList.iterator());
+ while (iter->hasNext()) {
this->elements[this->head++] = iter->next();
this->curSize++;
}
}
- ArrayList( int initialCapacity ) : AbstractList<E>(), elements( NULL ),
- capacity( initialCapacity ), head(
0 ), curSize( 0 ) {
+ ArrayList(int initialCapacity) :
+ AbstractList<E>(), elements(NULL), capacity(initialCapacity),
head(0), curSize(0) {
- if( initialCapacity < 0 ) {
- throw decaf::lang::exceptions::IllegalArgumentException(
- __FILE__, __LINE__, "Initial Capacity argument cannot be
negative." );
+ if (initialCapacity < 0) {
+ throw
decaf::lang::exceptions::IllegalArgumentException(__FILE__, __LINE__, "Initial
Capacity argument cannot be negative.");
}
this->elements = new E[this->capacity];
}
virtual ~ArrayList() {
- try{
- delete [] elements;
+ try {
+ delete[] elements;
}
DECAF_CATCHALL_NOTHROW()
}
@@ -182,8 +181,8 @@ namespace util {
if( index < 0 || index >= this->curSize ) {
throw decaf::lang::exceptions::IndexOutOfBoundsException(
- __FILE__, __LINE__,
- "List::get - Index greater than size() or negative" );
+ __FILE__, __LINE__,
+ "List::get - Index greater than size() or negative" );
}
E oldValue = this->elements[index];
@@ -196,8 +195,8 @@ namespace util {
if( index < 0 || index >= this->curSize ) {
throw decaf::lang::exceptions::IndexOutOfBoundsException(
- __FILE__, __LINE__,
- "List::get - Index greater than size() or negative" );
+ __FILE__, __LINE__,
+ "List::get - Index greater than size() or negative" );
}
return this->elements[index];
@@ -216,8 +215,8 @@ namespace util {
if( index < 0 || index > this->curSize ) {
throw decaf::lang::exceptions::IndexOutOfBoundsException(
- __FILE__, __LINE__,
- "Index was negative or greater than size()" );
+ __FILE__, __LINE__,
+ "Index was negative or greater than size()" );
}
if( index == 0 ) {
@@ -257,8 +256,8 @@ namespace util {
if( index < 0 || index > this->curSize ) {
throw decaf::lang::exceptions::IndexOutOfBoundsException(
- __FILE__, __LINE__,
- "List::addAll - Index greater than size()" );
+ __FILE__, __LINE__,
+ "List::addAll - Index greater than size()" );
}
int csize = collection.size();
@@ -300,8 +299,8 @@ namespace util {
if( index < 0 || index >= this->curSize ) {
throw decaf::lang::exceptions::IndexOutOfBoundsException(
- __FILE__, __LINE__,
- "List::removeAt - Index greater than size() or negative" );
+ __FILE__, __LINE__,
+ "List::removeAt - Index greater than size() or
negative" );
}
E old = this->elements[index];
@@ -335,7 +334,7 @@ namespace util {
virtual int lastIndexOf( const E& value ) const {
- for( int i = this->curSize - 1; i >= 0 ; --i ) {
+ for( int i = this->curSize - 1; i >= 0; --i ) {
if( this->elements[i] == value ) {
return i;
}
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Collection.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Collection.h?rev=1458455&r1=1458454&r2=1458455&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Collection.h
(original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/Collection.h
Tue Mar 19 18:52:34 2013
@@ -26,8 +26,8 @@
#include <decaf/util/Iterator.h>
#include <decaf/util/concurrent/Synchronizable.h>
-namespace decaf{
-namespace util{
+namespace decaf {
+namespace util {
/**
* The root interface in the collection hierarchy. A collection represents
@@ -81,7 +81,7 @@ namespace util{
* @throws IllegalStateException if the elements cannot be added at
this time due
* to insertion restrictions.
*/
- virtual void copy( const Collection<E>& collection ) = 0;
+ virtual void copy(const Collection<E>& collection) = 0;
/**
* Returns true if this collection changed as a result of the call.
@@ -119,7 +119,7 @@ namespace util{
* @throws IllegalStateException if the element cannot be added at
this time due
* to insertion restrictions.
*/
- virtual bool add( const E& value ) = 0;
+ virtual bool add(const E& value) = 0;
/**
* Adds all of the elements in the specified collection to this
@@ -142,7 +142,7 @@ namespace util{
* @throws IllegalStateException if an element cannot be added at this
time due
* to insertion restrictions.
*/
- virtual bool addAll( const Collection<E>& collection ) = 0;
+ virtual bool addAll(const Collection<E>& collection) = 0;
/**
* Removes all of the elements from this collection (optional
operation).
@@ -166,7 +166,7 @@ namespace util{
* @throws NullPointerException if the Collection contains pointers
and the
* Collection does not allow for NULL elements (optional
check).
*/
- virtual bool contains( const E& value ) const = 0;
+ virtual bool contains(const E& value) const = 0;
/**
* Returns true if this collection contains all of the elements in
@@ -178,7 +178,7 @@ namespace util{
* @throws NullPointerException if the Collection contains pointers
and the
* Collection does not allow for NULL elements (optional
check).
*/
- virtual bool containsAll( const Collection<E>& collection ) const = 0;
+ virtual bool containsAll(const Collection<E>& collection) const = 0;
/**
* Compares the passed collection to this one, if they contain the
@@ -187,7 +187,7 @@ namespace util{
*
* @returns true if the Collections contain the same elements.
*/
- virtual bool equals( const Collection<E>& value ) const = 0;
+ virtual bool equals(const Collection<E>& value) const = 0;
/**
* @returns true if this collection contains no elements.
@@ -211,7 +211,7 @@ namespace util{
* @throws NullPointerException if the Collection is a container of
pointers
* and does not allow NULL values.
*/
- virtual bool remove( const E& value ) = 0;
+ virtual bool remove(const E& value) = 0;
/**
* Removes all this collection's elements that are also contained in
@@ -228,7 +228,7 @@ namespace util{
* @throws NullPointerException if the Collection is a container of
pointers
* and does not allow NULL values.
*/
- virtual bool removeAll( const Collection<E>& collection ) = 0;
+ virtual bool removeAll(const Collection<E>& collection) = 0;
/**
* Retains only the elements in this collection that are contained in
the
@@ -245,7 +245,7 @@ namespace util{
* @throws NullPointerException if the Collection is a container of
pointers
* and does not allow NULL values.
*/
- virtual bool retainAll( const Collection<E>& collection ) = 0;
+ virtual bool retainAll(const Collection<E>& collection) = 0;
/**
* Returns the number of elements in this collection. If this
collection
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.cpp?rev=1458455&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.cpp
(added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.cpp
Tue Mar 19 18:52:34 2013
@@ -0,0 +1,18 @@
+/*
+ * 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 "HashSet.h"
Propchange:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.h?rev=1458455&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.h
(added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.h Tue
Mar 19 18:52:34 2013
@@ -0,0 +1,269 @@
+/*
+ * 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.
+ */
+
+#ifndef _DECAF_UTIL_HASHSET_H_
+#define _DECAF_UTIL_HASHSET_H_
+
+#include <decaf/util/Config.h>
+
+#include <decaf/util/AbstractSet.h>
+#include <decaf/util/HashMap.h>
+#include <decaf/util/HashCode.h>
+#include <decaf/lang/Pointer.h>
+#include <decaf/lang/Integer.h>
+#include <decaf/util/ConcurrentModificationException.h>
+#include <decaf/lang/exceptions/UnsupportedOperationException.h>
+
+namespace decaf {
+namespace util {
+
+ /**
+ * This class implements the Set interface, backed by a hash table
(actually a HashMap instance).
+ * It makes no guarantees as to the iteration order of the set; in
particular, it does not
+ * guarantee that the order will remain constant over time.
+ *
+ * This class offers constant time performance for the basic operations
(add, remove, contains
+ * and size), assuming the hash function disperses the elements properly
among the buckets.
+ * Iterating over this set requires time proportional to the sum of the
HashSet instance's size
+ * (the number of elements) plus the "capacity" of the backing HashMap
instance (the number of
+ * buckets). Thus, it's very important not to set the initial capacity too
high (or the load
+ * factor too low) if iteration performance is important.
+ *
+ * Note that this implementation is not synchronized. If multiple threads
access a hash set
+ * concurrently, and at least one of the threads modifies the set, it must
be synchronized
+ * externally. This is typically accomplished by synchronizing on some
object that naturally
+ * encapsulates the set. If no such object exists, the set should be
"wrapped" using the
+ * Collections::synchronizedSet method. This is best done at creation
time, to prevent
+ * accidental unsynchronized access to the set:
+ *
+ * Set<E>* s = Collections::synchronizedSet(new HashSet<E>(...));
+ *
+ * The iterators returned by this class's iterator method are fail-fast:
if the set is modified
+ * at any time after the iterator is created, in any way except through
the iterator's own
+ * remove method, the Iterator throws a ConcurrentModificationException.
Thus, in the face of
+ * concurrent modification, the iterator fails quickly and cleanly, rather
than risking arbitrary,
+ * non-deterministic behavior at an undetermined time in the future.
+ *
+ * Note that the fail-fast behavior of an iterator cannot be guaranteed as
it is, generally
+ * speaking, impossible to make any hard guarantees in the presence of
unsynchronized concurrent
+ * modification. Fail-fast iterators throw ConcurrentModificationException
on a best-effort basis.
+ * Therefore, it would be wrong to write a program that depended on this
exception for its
+ * correctness: the fail-fast behavior of iterators should be used only to
detect bugs.
+ *
+ * @since 1.0
+ */
+ template<typename E, typename HASHCODE = HashCode<E> >
+ class HashSet : public AbstractSet<E> {
+ protected:
+
+ HashMap<E, Set<E>*, HASHCODE>* backingMap;
+
+ public:
+
+ /**
+ * Constructs a new, empty set; the backing HashMap instance has
default initial
+ * capacity (16) and load factor (0.75).
+ */
+ HashSet() : AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*,
HASHCODE>()) {
+ }
+
+ /**
+ * Constructs a new, empty set; the backing HashMap instance has the
specified initial
+ * capacity and default load factor (0.75).
+ *
+ * @param capacity
+ * The initial capacity of this HashSet.
+ */
+ HashSet(int capacity) : AbstractSet<E>(), backingMap(new HashMap<E,
Set<E>*, HASHCODE>(capacity)) {
+ }
+
+ /**
+ * Constructs a new instance of {@code HashSet} with the specified
capacity
+ * and load factor.
+ *
+ * @param capacity
+ * The initial capacity for this HashSet.
+ * @param loadFactor
+ * The initial load factor for this HashSet.
+ */
+ HashSet(int capacity, float loadFactor) :
+ AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*,
HASHCODE>(capacity, loadFactor)) {
+ }
+
+ /**
+ * Constructs a new set containing the elements in the specified
collection.
+ *
+ * The HashMap is created with default load factor (0.75) and an
initial capacity
+ * sufficient to contain the elements in the specified collection.
+ *
+ * @param collection
+ * The collection of elements to add to this HashSet.
+ */
+ HashSet(const Collection<E>& collection) : AbstractSet<E>(),
backingMap() {
+
+ this->backingMap = new HashMap<E, Set<E>*, HASHCODE>(
+ (collection.size() < 6 ? 11 : collection.size() * 2));
+
+ decaf::lang::Pointer<Iterator<E> > iter(collection.iterator());
+ while (iter->hasNext()) {
+ this->add(iter->next());
+ }
+ }
+
+ virtual ~HashSet() {
+ try {
+ delete this->backingMap;
+ }
+ DECAF_CATCHALL_NOTHROW()
+ }
+
+ public:
+
+ HashSet<E>& operator= (const Collection<E>& collection) {
+ this->clear();
+ this->addAll(collection);
+ return *this;
+ }
+
+ public:
+
+ /**
+ * Adds the specified element to this set if it is not already
present. More formally,
+ * adds the specified element e to this set if this set contains no
element e2 such
+ * that (e == e2). If this set already contains the element, the call
leaves the set
+ * unchanged and returns false.
+ *
+ * @param object
+ * The object to add.
+ *
+ * @return true when this HashSet did not already contain the
object,false otherwise.
+ */
+ virtual bool add(const E& value) {
+ return this->backingMap->put(value, this);
+ }
+
+ /**
+ * Removes all elements from this {@code HashSet}, leaving it empty.
+ *
+ * @see #isEmpty
+ * @see #size
+ */
+ virtual void clear() {
+ this->backingMap->clear();
+ }
+
+ /**
+ * Returns a new {@code HashSet} with the same elements and size as
this
+ * {@code HashSet}.
+ *
+ * @return a shallow copy of this {@code HashSet}.
+ * @see java.lang.Cloneable
+ */
+// virtual Object clone() {
+// try {
+// HashSet<E> clone = (HashSet<E>) super.clone();
+// clone.backingMap = (HashMap<E, HashSet<E>>)
backingMap.clone();
+// return clone;
+// } catch (CloneNotSupportedException e) {
+// return null;
+// }
+// }
+
+ /**
+ * Searches this {@code HashSet} for the specified object.
+ *
+ * @param object
+ * the object to search for.
+ * @return {@code true} if {@code object} is an element of this
+ * {@code HashSet}, {@code false} otherwise.
+ */
+ virtual bool contains(const E& value) const {
+ return this->backingMap->containsKey(value);
+ }
+
+ /**
+ * Returns true if this {@code HashSet} has no elements, false
otherwise.
+ *
+ * @return {@code true} if this {@code HashSet} has no elements,
+ * {@code false} otherwise.
+ * @see #size
+ */
+ virtual bool isEmpty() const {
+ return this->backingMap->isEmpty();
+ }
+
+ /**
+ * Returns an Iterator on the elements of this {@code HashSet}.
+ *
+ * @return an Iterator on the elements of this {@code HashSet}.
+ * @see Iterator
+ */
+ virtual Iterator<E>* iterator() {
+ return this->backingMap->keySet().iterator();
+ }
+
+ virtual Iterator<E>* iterator() const {
+ return this->backingMap->keySet().iterator();
+ }
+
+ /**
+ * Removes the specified element from this set if it is present. More
formally,
+ * removes an element e such that (e == value), if this set contains
such an element.
+ * Returns true if this set contained the element (or equivalently, if
this set
+ * changed as a result of the call). (This set will not contain the
element once
+ * the call returns.)
+ *
+ * @param value
+ * The value to remove from this set.
+ *
+ * @return true if the value was removed, false otherwise.
+ */
+ virtual bool remove(const E& value) {
+ try {
+ this->backingMap->remove(value);
+ } catch(decaf::util::NoSuchElementException& ex) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Returns the number of elements in this {@code HashSet}.
+ *
+ * @return the number of elements in this {@code HashSet}.
+ */
+ virtual int size() const {
+ return this->backingMap->size();
+ }
+
+ virtual std::string toString() const {
+
+ std::string result;
+
+ result.append("decaf::util::HashSet [ size = ");
+ result.append(decaf::lang::Integer::toString(this->size()));
+ result.append(" ]");
+
+ return result;
+ }
+
+ };
+
+}}
+
+#endif /* _DECAF_UTIL_HASHSET_H_ */
Propchange:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/HashSet.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/Makefile.am
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/Makefile.am?rev=1458455&r1=1458454&r2=1458455&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/Makefile.am Tue Mar 19
18:52:34 2013
@@ -204,6 +204,7 @@ cc_sources = \
decaf/util/Endian.cpp \
decaf/util/HashCodeTest.cpp \
decaf/util/HashMapTest.cpp \
+ decaf/util/HashSetTest.cpp \
decaf/util/LRUCacheTest.cpp \
decaf/util/LinkedListTest.cpp \
decaf/util/ListTest.cpp \
@@ -448,6 +449,7 @@ h_sources = \
decaf/util/Endian.h \
decaf/util/HashCodeTest.h \
decaf/util/HashMapTest.h \
+ decaf/util/HashSetTest.h \
decaf/util/LRUCacheTest.h \
decaf/util/LinkedListTest.h \
decaf/util/ListTest.h \
Added:
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.cpp?rev=1458455&view=auto
==============================================================================
---
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.cpp
(added)
+++
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.cpp
Tue Mar 19 18:52:34 2013
@@ -0,0 +1,365 @@
+/*
+ * 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 "HashSetTest.h"
+
+#include <decaf/util/HashSet.h>
+#include <decaf/util/Iterator.h>
+#include <decaf/util/HashMap.h>
+#include <decaf/util/StlMap.h>
+#include <decaf/util/ArrayList.h>
+#include <decaf/util/LinkedList.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::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+ const int SET_SIZE = 1000;
+
+ void populateSet(HashSet<int>& hashSet) {
+ for (int i = 0; i < SET_SIZE; ++i) {
+ hashSet.add(i);
+ }
+ }
+
+ void populateSet(HashSet<int>& hashSet, int count) {
+ for (int i = 0; i < count; ++i) {
+ hashSet.add(i);
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+HashSetTest::HashSetTest() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+HashSetTest::~HashSetTest() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testConstructor() {
+
+ HashSet<int> set;
+ CPPUNIT_ASSERT(set.isEmpty());
+ CPPUNIT_ASSERT_EQUAL(0, set.size());
+ CPPUNIT_ASSERT_EQUAL(false, set.contains(1));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testConstructorI() {
+
+ HashSet<int> set;
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Created incorrect HashSet", 0, set.size());
+
+ try {
+ HashSet<int> set(-1);
+ } catch (IllegalArgumentException& e) {
+ return;
+ }
+
+ CPPUNIT_FAIL("Failed to throw IllegalArgumentException for capacity < 0");
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testConstructorIF() {
+
+ HashSet<int> set(5, 0.5);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE("Created incorrect HashSet", 0, set.size());
+
+ try {
+ HashSet<int> set(0, 0);
+ } catch (IllegalArgumentException& e) {
+ return;
+ }
+
+ CPPUNIT_FAIL("Failed to throw IllegalArgumentException for initial load
factor <= 0");
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testConstructorCollection() {
+
+ ArrayList<int> intList;
+ intList.add(1);
+ intList.add(1);
+ intList.add(2);
+ intList.add(3);
+ intList.add(4);
+
+ HashSet<int> set(intList);
+ for (int counter = 0; counter < intList.size(); counter++) {
+ CPPUNIT_ASSERT_MESSAGE("HashSet does not contain correct elements",
+ set.contains(intList.get(counter)));
+ }
+
+ CPPUNIT_ASSERT_MESSAGE("HashSet created from collection incorrect size",
+ set.size() == intList.size() - 1);
+}
+
+//////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testEquals() {
+
+ HashSet<int> set1;
+ populateSet(set1);
+ HashSet<int> set2;
+ populateSet(set2);
+
+ CPPUNIT_ASSERT(set1.equals(set2));
+ CPPUNIT_ASSERT(set2.equals(set1));
+
+ set1.add(SET_SIZE + 1);
+ CPPUNIT_ASSERT(!set1.equals(set2));
+ CPPUNIT_ASSERT(!set2.equals(set1));
+ set2.add(SET_SIZE + 1);
+ CPPUNIT_ASSERT(set1.equals(set2));
+ CPPUNIT_ASSERT(set2.equals(set1));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testAdd() {
+
+ HashSet<int> set;
+ populateSet(set);
+ int size = set.size();
+
+ set.add(8);
+ CPPUNIT_ASSERT_MESSAGE("Added element already contained by set",
set.size() == size);
+ set.add(-9);
+ CPPUNIT_ASSERT_MESSAGE("Failed to increment set size after add",
set.size() == size + 1);
+ CPPUNIT_ASSERT_MESSAGE("Failed to add element to set", set.contains(-9));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testClear() {
+
+ HashSet<int> set;
+ populateSet(set);
+
+ CPPUNIT_ASSERT(set.size() > 0);
+ set.clear();
+ CPPUNIT_ASSERT(set.size() == 0);
+ CPPUNIT_ASSERT(!set.contains(1));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testContains() {
+
+ HashSet<int> set;
+ populateSet(set);
+
+ CPPUNIT_ASSERT_MESSAGE("Returned false for valid object",
set.contains(90));
+ CPPUNIT_ASSERT_MESSAGE("Returned true for invalid Object",
!set.contains(SET_SIZE + 1));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testIsEmpty() {
+ HashSet<int> set;
+ CPPUNIT_ASSERT_MESSAGE("Empty set returned true", set.isEmpty());
+ set.add(1);
+ CPPUNIT_ASSERT_MESSAGE("Non-empty set returned true", !set.isEmpty());
+
+ CPPUNIT_ASSERT_MESSAGE("Empty set returned false",
HashSet<std::string>().isEmpty());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testIterator() {
+
+ HashSet<int> set;
+ populateSet(set);
+ Pointer< Iterator<int> > iter(set.iterator());
+ int x = 0;
+ while (iter->hasNext()) {
+ CPPUNIT_ASSERT_MESSAGE("Failed to iterate over all elements",
set.contains(iter->next()));
+ ++x;
+ }
+ CPPUNIT_ASSERT_MESSAGE("Returned iteration of incorrect size", set.size()
== x);
+
+ {
+ HashSet<string> set;
+
+ set.add( "fred1" );
+ set.add( "fred2" );
+ set.add( "fred3" );
+
+ Iterator<string>* iterator1 = set.iterator();
+ CPPUNIT_ASSERT( iterator1 != NULL );
+ CPPUNIT_ASSERT( iterator1->hasNext() == true );
+
+ int count = 0;
+ while( iterator1->hasNext() ) {
+ iterator1->next();
+ ++count;
+ }
+
+ CPPUNIT_ASSERT( count == set.size() );
+
+ Iterator<string>* iterator2 = set.iterator();
+
+ while( iterator2->hasNext() ) {
+ iterator2->next();
+ iterator2->remove();
+ }
+
+ CPPUNIT_ASSERT( set.isEmpty() );
+
+ delete iterator1;
+ delete iterator2;
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testRemove() {
+
+ HashSet<int> set;
+ populateSet(set);
+ int size = set.size();
+ set.remove(98);
+ CPPUNIT_ASSERT_MESSAGE("Failed to remove element", !set.contains(98));
+ CPPUNIT_ASSERT_MESSAGE("Failed to decrement set size", set.size() == size
- 1);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testSize() {
+
+ HashSet<int> set;
+ populateSet(set);
+
+ CPPUNIT_ASSERT_MESSAGE("Returned incorrect size", set.size() == SET_SIZE);
+ set.clear();
+ CPPUNIT_ASSERT_MESSAGE("Cleared set returned non-zero size", 0 ==
set.size());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testToString() {
+ HashSet<std::string> s;
+ std::string result = s.toString();
+ CPPUNIT_ASSERT_MESSAGE("toString returned bad value",
result.find("HashSet") != std::string::npos);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testToArray() {
+
+ HashSet<int> set;
+ populateSet(set);
+
+ std::vector<int> array = set.toArray();
+ CPPUNIT_ASSERT((int)array.size() == SET_SIZE);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testCopy1() {
+
+ HashSet<int> set1;
+
+ for (int i = 0; i < 50; ++i) {
+ set1.add(i);
+ }
+
+ HashSet<int> set2;
+
+ set2.copy(set1);
+
+ CPPUNIT_ASSERT(set1.size() == set2.size());
+
+ for (int i = 0; i < 50; ++i) {
+ CPPUNIT_ASSERT(set2.contains(i));
+ }
+
+ CPPUNIT_ASSERT(set2.equals(set1));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testCopy2() {
+
+ LinkedList<int> collection;
+
+ for (int i = 0; i < 50; ++i) {
+ collection.add(i);
+ }
+
+ HashSet<int> set;
+
+ set.copy(collection);
+
+ CPPUNIT_ASSERT(collection.size() == set.size());
+
+ for (int i = 0; i < 50; ++i) {
+ CPPUNIT_ASSERT(set.contains(i));
+ }
+
+ CPPUNIT_ASSERT(set.equals(collection));
+}
+
+//////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testRemoveAll() {
+
+ HashSet<int> set;
+ populateSet(set, 3);
+
+ ArrayList<int> collection;
+ collection.add(1);
+ collection.add(2);
+
+ set.removeAll(collection);
+
+ CPPUNIT_ASSERT_EQUAL(1, set.size());
+
+ HashSet<int> set2;
+ set2.removeAll(collection);
+ CPPUNIT_ASSERT_EQUAL(0, set2.size());
+
+ HashSet<int> set3;
+ populateSet(set3, 3);
+ collection.clear();
+
+ set3.removeAll(collection);
+ CPPUNIT_ASSERT_EQUAL(3, set3.size());
+}
+
+//////////////////////////////////////////////////////////////////////////////
+void HashSetTest::testRetainAll() {
+
+ HashSet<int> set;
+ populateSet(set, 3);
+
+ ArrayList<int> collection;
+ collection.add(1);
+ collection.add(2);
+
+ set.retainAll(collection);
+
+ CPPUNIT_ASSERT_EQUAL(2, set.size());
+
+ HashSet<int> set2;
+ set2.retainAll(collection);
+ CPPUNIT_ASSERT_EQUAL(0, set2.size());
+
+ HashSet<int> set3;
+ populateSet(set3, 3);
+ collection.clear();
+
+ set3.retainAll(collection);
+ CPPUNIT_ASSERT_EQUAL(0, set3.size());
+}
+
Propchange:
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added:
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.h
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.h?rev=1458455&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.h
(added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.h
Tue Mar 19 18:52:34 2013
@@ -0,0 +1,78 @@
+/*
+ * 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.
+ */
+
+#ifndef _DECAF_UTIL_HASHSETTEST_H_
+#define _DECAF_UTIL_HASHSETTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace decaf {
+namespace util {
+
+ class HashSetTest : public CppUnit::TestFixture {
+
+ CPPUNIT_TEST_SUITE( HashSetTest );
+ CPPUNIT_TEST( testConstructor );
+ CPPUNIT_TEST( testConstructorI );
+ CPPUNIT_TEST( testConstructorIF );
+ CPPUNIT_TEST( testConstructorCollection );
+ CPPUNIT_TEST( testAdd );
+ CPPUNIT_TEST( testClear );
+ CPPUNIT_TEST( testContains );
+ CPPUNIT_TEST( testIsEmpty );
+ CPPUNIT_TEST( testIterator );
+ CPPUNIT_TEST( testRemove );
+ CPPUNIT_TEST( testSize );
+ CPPUNIT_TEST( testToString );
+ CPPUNIT_TEST( testToArray );
+ CPPUNIT_TEST( testCopy1 );
+ CPPUNIT_TEST( testCopy2 );
+ CPPUNIT_TEST( testEquals );
+ CPPUNIT_TEST( testRemoveAll );
+ CPPUNIT_TEST( testRetainAll );
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+
+ HashSetTest();
+ virtual ~HashSetTest();
+
+ void testConstructor();
+ void testConstructorI();
+ void testConstructorIF();
+ void testConstructorCollection();
+ void testAdd();
+ void testClear();
+ void testContains();
+ void testIsEmpty();
+ void testIterator();
+ void testRemove();
+ void testSize();
+ void testToString();
+ void testToArray();
+ void testCopy1();
+ void testCopy2();
+ void testEquals();
+ void testRemoveAll();
+ void testRetainAll();
+
+ };
+
+}}
+
+#endif /* _DECAF_UTIL_HASHSETTEST_H_ */
Propchange:
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/util/HashSetTest.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp
URL:
http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp?rev=1458455&r1=1458454&r2=1458455&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp
(original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/testRegistry.cpp Tue Mar
19 18:52:34 2013
@@ -326,6 +326,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( decaf::
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::HashCodeTest );
#include <decaf/util/HashMapTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::HashMapTest );
+#include <decaf/util/HashSetTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::HashSetTest );
#include <decaf/util/AbstractCollectionTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::AbstractCollectionTest );
#include <decaf/util/AbstractListTest.h>
@@ -385,9 +387,9 @@ CPPUNIT_TEST_SUITE_REGISTRATION( decaf::
#include <decaf/security/MessageDigestTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::security::MessageDigestTest );
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////// Marshaler Tests
-//////
+//////////////////////////////////////////////////////////////////////////////////////////
+// Marshaler Tests
+//
#include
<activemq/wireformat/openwire/marshal/generated/ActiveMQBlobMessageMarshallerTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION(
activemq::wireformat::openwire::marshal::generated::ActiveMQBlobMessageMarshallerTest
);
#include
<activemq/wireformat/openwire/marshal/generated/ActiveMQBytesMessageMarshallerTest.h>