You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/10/17 08:48:22 UTC
svn commit: r1399110 - in /incubator/etch/trunk/binding-cpp/runtime:
include/common/ lib/capu/modules/capu/include/capu/container/
lib/capu/modules/capu/test/container/ src/test/common/
Author: veithm
Date: Wed Oct 17 06:48:21 2012
New Revision: 1399110
URL: http://svn.apache.org/viewvc?rev=1399110&view=rev
Log:
ETCH-250 List refactoring
Change-Id: If3d671e67bfeaa70ee310f0f7a7ee5000122ff74
Modified:
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h
incubator/etch/trunk/binding-cpp/runtime/include/common/EtchList.h
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashSet.h
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashTable.h
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/List.h
incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/container/ListTest.cpp
incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchListTest.cpp
Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h Wed Oct 17 06:48:21 2012
@@ -26,12 +26,12 @@ template <class T>
class EtchComparator {
public:
- inline capu::bool_t operator() (const EtchObject &first, const EtchObject &second) {
+ inline capu::bool_t operator() (const EtchObject &first, const EtchObject &second) const {
return first.equals(&second);
}
- inline capu::bool_t operator() (const EtchObjectType &first, const EtchObjectType &second) {
- return first.equals(&second);
+ inline capu::bool_t operator() (const EtchObjectType &first, const EtchObjectType &second) const {
+ return first.equals(&second);
}
};
@@ -39,11 +39,11 @@ template <class T>
class EtchComparator <T*> {
public:
- inline capu::bool_t operator() (const EtchObject* first, const EtchObject* second) {
+ inline capu::bool_t operator() (const EtchObject* first, const EtchObject* second) const {
return first->equals(second);
}
- inline capu::bool_t operator() (const EtchObjectType* first, const EtchObjectType* second) {
+ inline capu::bool_t operator() (const EtchObjectType* first, const EtchObjectType* second) const {
return first->equals(second);
}
};
@@ -52,11 +52,11 @@ template <class T>
class EtchComparator <capu::SmartPointer<T> > {
public:
- inline capu::bool_t operator() (const capu::SmartPointer<EtchObject>& first, const capu::SmartPointer<EtchObject>& second) {
+ inline capu::bool_t operator() (const capu::SmartPointer<EtchObject>& first, const capu::SmartPointer<EtchObject>& second) const {
return first->equals(second.get());
}
- inline capu::bool_t operator() (const capu::SmartPointer<EtchObjectType>& first, const capu::SmartPointer<EtchObjectType>& second) {
+ inline capu::bool_t operator() (const capu::SmartPointer<EtchObjectType>& first, const capu::SmartPointer<EtchObjectType>& second) const {
return first->equals(second.get());
}
};
Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchList.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchList.h?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchList.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchList.h Wed Oct 17 06:48:21 2012
@@ -100,13 +100,13 @@ public:
* @return true if empty
* false otherwise
*/
- capu::bool_t isEmpty();
+ capu::bool_t isEmpty() const;
/**
* returns an iterator pointing to the beginning of list
* @return iterator
*/
- Iterator begin();
+ Iterator begin() const;
/**
* finds the index of given element in the link list
@@ -116,7 +116,7 @@ public:
* @return -1 if the value either does not exist or given value is NULL
* otherwise index of value on linked list
*/
- capu::int32_t find(const T &element);
+ capu::int32_t find(const T &element) const;
/**
*
@@ -175,7 +175,7 @@ status_t EtchList<T, C>::add(capu::int32
}
template<class T, class C>
-typename EtchList<T, C>::Iterator EtchList<T, C>::begin() {
+typename EtchList<T, C>::Iterator EtchList<T, C>::begin() const {
return mList.begin();
}
@@ -190,7 +190,7 @@ capu::bool_t EtchList<T, C>::contains(co
}
template<class T, class C>
-capu::int32_t EtchList<T, C>::find(const T &element) {
+capu::int32_t EtchList<T, C>::find(const T &element) const {
return mList.find(element);
}
@@ -200,7 +200,7 @@ status_t EtchList<T, C>::get(capu::int32
}
template<class T, class C>
-capu::bool_t EtchList<T, C>::isEmpty() {
+capu::bool_t EtchList<T, C>::isEmpty() const {
return mList.isEmpty();
}
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashSet.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashSet.h?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashSet.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashSet.h Wed Oct 17 06:48:21 2012
@@ -237,7 +237,7 @@ namespace capu {
return CAPU_OK;
}
-
+
template <class T, class C, class H>
bool_t HashSet< T, C, H>::hasElement(const T &value) {
uint_t index = H::Digest(value) % mSize;
@@ -266,12 +266,8 @@ namespace capu {
}
template <class T, class C, class H>
- HashSet< T, C, H>::HashSetIterator::HashSetIterator(List<T, C> * list, uint_t list_size) {
- mCurrentListIndex = 0;
- this->mList = list;
- mMaxListSize = list_size;
- this->mCurrentListIterator = list[mCurrentListIndex].begin();
-
+ HashSet< T, C, H>::HashSetIterator::HashSetIterator(List<T, C> * list, uint_t list_size)
+ : mCurrentListIndex(0), mCurrentListIterator(list[0].begin()), mList(list), mMaxListSize(list_size) {
//to point the first non-empty one
for (uint_t i = 0; i < list_size; ++i) {
if (!mList[i].isEmpty()) {
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashTable.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashTable.h?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashTable.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/HashTable.h Wed Oct 17 06:48:21 2012
@@ -84,6 +84,12 @@ namespace capu {
*/
HashTable(uint_t size);
+
+ /**
+ * Constructs a copy of HashTable.
+ */
+ HashTable(const HashTable& other);
+
/**
* Destructure.
*/
@@ -111,7 +117,7 @@ namespace capu {
* CAPU_EINVAL if value is null
* CAPU_ENOT_EXIST if there is no pair with specified key
*/
- status_t get(const Key &key, T* value);
+ status_t get(const Key &key, T* value) const;
/**
* Remove value associated with key in the hashtable.
@@ -143,7 +149,7 @@ namespace capu {
* Return iterator for iterating key value tuples.
* @return Iterator
*/
- Iterator begin();
+ Iterator begin() const;
private:
@@ -155,7 +161,7 @@ namespace capu {
* @return -1 if the key is unique
* otherwise the index in the linked list
*/
- int_t getKeyIndexFromBucket(uint_t index, const Key &k) {
+ int_t getKeyIndexFromBucket(uint_t index, const Key &k) const {
int_t count = 0;
typename List<Pair<Key, T> >::Iterator it = mLists[index].begin();
Pair<Key, T> pair;
@@ -179,8 +185,7 @@ namespace capu {
template <class Key, class T, class C, class Hash>
HashTable<Key, T, C, Hash>::HashTable()
- : mSize(HASH_TABLE_DEFAULT_SIZE)
- , mCount(0) {
+ : mSize(HASH_TABLE_DEFAULT_SIZE), mCount(0) {
mLists = new List<Pair<Key, T>, Comparator >[mSize];
}
@@ -196,6 +201,19 @@ namespace capu {
}
template <class Key, class T, class C, class Hash>
+ HashTable<Key, T, C, Hash>::HashTable(const HashTable& other)
+ : mSize(other.mSize), mCount(0) {
+ mLists = new List<Pair<Key, T>, Comparator >[mSize];
+ typename HashTable<Key, T, C, Hash>::Iterator iterator = other.begin();
+ Pair<Key, T> pair;
+ while(iterator.hasNext())
+ {
+ iterator.next(&pair);
+ put(pair.first, pair.second, NULL);
+ }
+ }
+
+ template <class Key, class T, class C, class Hash>
HashTable<Key, T, C, Hash>::~HashTable() {
delete [] mLists;
}
@@ -245,7 +263,7 @@ namespace capu {
}
template <class Key, class T, class C, class Hash>
- status_t HashTable<Key, T, C, Hash>::get(const Key &key, T* value) {
+ status_t HashTable<Key, T, C, Hash>::get(const Key &key, T* value) const {
if (value == NULL)
return CAPU_EINVAL;
@@ -312,17 +330,13 @@ namespace capu {
}
template <class Key, class T, class C, class Hash>
- typename HashTable<Key, T, C, Hash>::Iterator HashTable<Key, T, C, Hash>::begin() {
+ typename HashTable<Key, T, C, Hash>::Iterator HashTable<Key, T, C, Hash>::begin() const {
return typename HashTable<Key, T, C, Hash>::Iterator(mLists, mSize);
}
template <class Key, class T, class C, class Hash>
- HashTable<Key, T, C, Hash>::HashTableIterator::HashTableIterator(List<Pair<Key, T> > * list, uint_t list_size) {
- mCurrentListIndex = 0;
- this->mList = list;
- mMaxListSize = list_size;
- this->mCurrentListIterator = list[mCurrentListIndex].begin();
-
+ HashTable<Key, T, C, Hash>::HashTableIterator::HashTableIterator(List<Pair<Key, T> > * list, uint_t list_size)
+ : mCurrentListIndex(0), mCurrentListIterator(list[0].begin()), mList(list), mMaxListSize(list_size) {
//to point the first non-empty one
for (uint_t i = 0; i < list_size; ++i) {
if (!mList[i].isEmpty()) {
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/List.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/List.h?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/List.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/include/capu/container/List.h Wed Oct 17 06:48:21 2012
@@ -1,593 +1,570 @@
/* $Id$
- *
- * 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.
- */
+*
+* 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 __List_H__
+#define __List_H__
-#ifndef __LIST_H__
-#define __LIST_H__
#include "capu/Error.h"
#include "capu/Config.h"
#include "capu/container/Comparator.h"
-namespace capu {
-
- template <class T, class C = Comparator >
- class List {
+namespace capu
+{
+ template <class T, class C = Comparator>
+ class List
+ {
+ private:
+ class ListNode
+ {
+ public:
+
+ ListNode()
+ : mNext(0)
+ , mPrev(0)
+ {
+ }
+
+ ListNode(const T& data)
+ : mData(data)
+ , mNext(0)
+ , mPrev(0)
+ {
+ }
+
+ T mData;
+ ListNode* mNext;
+ ListNode* mPrev;
+ };
+
+ class ListIterator
+ {
+ public:
+ friend class List<T, C>;
+
+ /**
+ * destructor
+ */
+ ~ListIterator();
+
+ /**
+ * Check if iterator has next element.
+ * @return false if the next of current node that is pointed, is null otherwise true
+ */
+ bool_t hasNext();
+
+ /**
+ * Shifts the iterator to the next position and returns the element if next != NULL
+ * @param element
+ * @return CAPU_OK if the next element has been gotten
+ *
+ */
+ status_t next(T* element = 0);
+
+ /**
+ * Get current iterator element.
+ * @param element
+ * @return CAPU_OK if the current element has been gotten
+ */
+ status_t current(T* element);
+
+ /**
+ * Returns the index of the current element.
+ * @return The index of the current element. If there is no current element, the return value is undefined.
+ */
+ uint32_t currentIndex();
+
+ private:
+ ListIterator(ListNode* boundary); // creation only in list.begin()
+ ListNode* mInitialNode;
+ ListNode* mCurrentNode;
+ uint32_t mIndex;
+ };
+
+ ListNode* mBoundary;
+ uint_t mSize;
+ C mComparator;
+
+ void insertElement(ListNode* addPosition, const T& element);
+ void deleteElement(ListNode* deletePosition);
+ ListNode* findElement(uint_t index) const;
+
+ public:
+ typedef typename List<T, C>::ListIterator Iterator;
+
+ /**
+ * Default Constructor
+ */
+ List();
+
+ /**
+ * Copy constructor
+ */
+ List(const List<T, C>& other);
+
+ /**
+ * Destructor
+ */
+ virtual ~List();
+
+
+ List<T,C>& operator=(List<T, C> const& other);
+
+ /**
+ * Add element to the end of list
+ * @param element element that will be added
+ * @return CAPU_ENO_MEMORY if allocation of element is failed
+ * CAPU_OK if the element is successfully added
+ */
+ status_t add(const T& element);
+
+ /**
+ * Add element to specified position
+ *
+ * @param index index of element which will be inserted
+ * @param element new value that will replace the old value
+ *
+ * @return CAPU_EINVAL if given index is invalid.
+ * CAPU_ENO_MEMORY if allocation of element is failed
+ * CAPU_OK if the element is successfully added
+ * CAPU_ERROR otherwise
+ */
+ status_t add(uint_t index, const T& element);
+
+ /**
+ * Add element to specified position
+ *
+ * @param iterator with the position to insert
+ * @param element new value that will replace the old value
+ *
+ * @return CAPU_ENO_MEMORY memory allocation failed.
+ * CAPU_OK otherwise
+ */
+ status_t add(Iterator& iter, const T &element);
+
+ /**
+ * Sets the element at the specified index.
+ * @param index the index of element that will be replaced
+ * @param element element that will be overwritten to existing place
+ * @param elementOld the buffer to keep the existing
+ * @return CAPU_EINVAL if the index is not valid
+ * CAPU_OK otherwise
+ */
+ status_t set(uint_t index, const T &element, T* elementOld = 0);
+
+ /**
+ * remove the element in the specified index and if the element_old
+ * parameter is not NULL, the removed element will be put to element_old
+ * @param index index of element that will be removed
+ * @param elementOld the buffer which will keep the copy of the removed element
+ * @return CAPU_EINVAL invalid index
+ * CAPU_OK if the element is successfully removed
+ */
+ status_t removeAt(uint_t index, T* elementOld = 0);
+
+ /**
+ * remove the element in the specified iterator position and if the element_old
+ * parameter is not NULL, the removed element will be put to element_old
+ * @param iterator of element that will be removed
+ * @param element_old the buffer which will keep the copy of the removed element
+ * @return CAPU_EINVAL invalid iterator
+ * CAPU_OK if the element is successfully removed
+ *
+ */
+ status_t removeAt(Iterator& listIterator, T* elementOld = 0);
+
+ /**
+ * Removes an element.
+ * @param element The element that will get removed.
+ * @return CAPU_EINVAL if element was not found
+ * CAPU_OK if element was removed.
+ */
+ status_t remove(const T& element);
+
+ /**
+ * removes all elements from linked list
+ *
+ * @return CAPU_OK if all of the elements are deleted
+ */
+ status_t clear();
+
+ /**
+ * get a single element on specified index
+ * @param index index of element that will be get
+ * @param result the buffer that the retrieved element will be stored
+ * @return CAPU_EINVAL invalid index
+ * CAPU_OK otherwise
+ */
+ status_t get(uint_t index, T* result) const;
+
+ /**
+ * return size of list
+ * @return return the size of list
+ */
+ int_t size() const;
+
+ /**
+ * check the list is empty or not
+ * @return true if empty
+ * false otherwise
+ */
+ bool_t isEmpty() const;
+
+ /**
+ * returns an iterator pointing to the beginning of list
+ * @return iterator
+ */
+ Iterator begin() const;
+
+ /**
+ * finds the index of given element in the link list
+ * if you are using an object you need to overload == operator
+ *
+ * @param element the value that will be searched
+ * @return -1 if the value either does not exist or given value is NULL
+ * otherwise index of value on linked list
+ */
+ int_t find(const T &element) const;
+
+ /**
+ * check that if the list contains the given parameter or not
+ * if you are using an object you need to overload == operator
+ * @param element element that will be checked
+ * @return true list contains it
+ * false otherwise
+ */
+ bool_t contains(const T &element) const;
+ };
- class ListNode {
- public:
+ template <class T, class C>
+ List<T, C>::List()
+ : mBoundary(new ListNode())
+ , mSize(0)
+ {
+ mBoundary->mNext = mBoundary;
+ mBoundary->mPrev = mBoundary;
+ }
+
+ template <class T, class C>
+ List<T, C>::List(const List<T, C>& other)
+ : mBoundary(new ListNode())
+ , mSize(0)
+ {
+ mBoundary->mNext = mBoundary;
+ mBoundary->mPrev = mBoundary;
+
+ // add all items from the other list
+ Iterator it = other.begin();
+ T current;
+ while(it.hasNext())
+ {
+ it.next(¤t);
+ add(current);
+ }
+ }
+
+ template <class T, class C>
+ List<T, C>::~List()
+ {
+ clear();
+ delete mBoundary;
+ }
+
+ template <class T, class C>
+ status_t List<T, C>::clear()
+ {
+ ListNode* current = mBoundary->mNext;
+ ListNode* toDelete = 0;
+ while (current != mBoundary)
+ {
+ toDelete = current;
+ current = current->mNext;
+ delete toDelete;
+ }
+ mSize = 0;
+ mBoundary->mNext = mBoundary;
+ mBoundary->mPrev = mBoundary;
+ return CAPU_OK;
+ }
- ListNode() {
- mNext = NULL;
- mPrev = NULL;
- }
-
- ListNode(T data) {
- mNext = NULL;
- mPrev = NULL;
- this->mData = data;
- }
-
- T mData;
- ListNode * mNext;
- ListNode * mPrev;
+ template <class T, class C>
+ List<T,C>& List<T, C>::operator=(List<T, C> const& other)
+ {
+ clear();
+ mBoundary->mNext = mBoundary;
+ mBoundary->mPrev = mBoundary;
+
+ // add all items from the other list
+ Iterator it = other.begin();
+ T current;
+ while(it.hasNext())
+ {
+ it.next(¤t);
+ add(current);
+ }
+ return *this;
+ }
+
+ template <class T, class C>
+ int_t List<T, C>::find(const T &element) const
+ {
+ int_t counter = 0;
+ ListNode* current = mBoundary->mNext;
+ while (current != mBoundary)
+ {
+ if (mComparator(current->mData, element))
+ {
+ // element was found, return index
+ return counter;
+ }
+ current = current->mNext;
+ ++counter;
+ }
+
+ // not found
+ return -1;
+ }
+
+ template <class T, class C>
+ inline bool_t List<T, C>::contains(const T &element) const
+ {
+ return find(element) != -1;
+ }
+
+ template <class T, class C>
+ inline status_t List<T, C>::add(Iterator& iter, const T &element)
+ {
+ insertElement(iter.mCurrentNode->mPrev, element); // insert before found position
+ return CAPU_OK;
+ }
- };
+ template <class T, class C>
+ inline status_t List<T, C>::add(uint_t index, const T &element)
+ {
+ if (index > mSize) // if index == mSize, element is added at the end
+ {
+ // invalid index
+ return CAPU_EINVAL;
+ }
- class ListIterator {
- public:
- friend class List<T, C>;
- /**
- * Default Constructor
- */
- ListIterator();
-
- /**
- * constructor
- * @param initial_node where the iterator will be pointing
- */
- ListIterator(ListNode *initialNode);
-
- /**
- * destructor
- */
- ~ListIterator();
-
- /**
- * Check if iterator has next element.
- * @return false if the next of current node that is pointed, is null otherwise true
- */
- bool_t hasNext();
-
- /**
- * Shifts the iterator to the next position and returns the element if next != NULL
- * @param element
- * @return ETCH_ERANGE if the next of current node that is pointed, is null
- * CAPU_OK if the next element has been gotten
- *
- */
- status_t next(T* element = NULL);
-
- /**
- * Get current iterator element.
- * @param element
- * @return ETCH_ERANGE if the current node that is pointed, is null
- * CAPU_EINVAL if the value is NULL
- * CAPU_OK if the current element has been gotten
- *
- */
- status_t current(T* element);
+ insertElement(findElement(index)->mPrev, element); // insert before found position
+ return CAPU_OK;
+ }
- private:
- ListNode *mNextPosition;
- };
+ template <class T, class C>
+ inline status_t List<T, C>::add(const T &element)
+ {
+ insertElement(mBoundary->mPrev, element); // insert at list end (boundary->mPrev)
+ return CAPU_OK;
+ }
- ListNode *mHead;
- ListNode *mTail;
- int_t mSize;
-
- public:
-
- typedef typename List<T, C>::ListIterator Iterator;
-
- /**
- * Default Constructor
- */
- List();
-
- /**
- * Destructor
- */
- virtual ~List();
-
- /**
- * Add element to the end of list
- * @param element element that will be added
- * @return CAPU_ENO_MEMORY if allocation of element is failed
- * CAPU_OK if the element is successfully added
- */
- status_t add(const T &element);
-
- /**
- * Add element to specified position
- *
- * @param index index of element which will be inserted
- * @param element new value that will replace the old value
- *
- * @return CAPU_EINVAL if given index is invalid.
- * CAPU_ENO_MEMORY if allocation of element is failed
- * CAPU_OK if the element is successfully added
- * CAPU_ERROR otherwise
- */
- status_t add(int_t index, const T &element);
-
- /**
- * Add element to specified position
- *
- * @param iterator with the position to insert
- * @param element new value that will replace the old value
- *
- * @return CAPU_ENO_MEMORY memory allocation failed.
- * CAPU_OK otherwise
- */
- status_t add(Iterator& iter, const T &element);
-
- /**
- * remove the element in the specified index and if the element_old
- * parameter is not NULL, the removed element will be put to element_old
- * @param index index of element that will be removed
- * @param elementOld the buffer which will keep the copy of the removed element
- * @return CAPU_EINVAL invalid index
- * CAPU_OK if the element is successfully removed
- */
- status_t removeAt(int_t index, T* elementOld = NULL);
-
- /**
- * remove the element in the specified iterator position and if the element_old
- * parameter is not NULL, the removed element will be put to element_old
- * @param iterator of element that will be removed
- * @param element_old the buffer which will keep the copy of the removed element
- * @return CAPU_EINVAL invalid iterator
- * CAPU_OK if the element is successfully removed
- *
- */
- status_t removeAt(Iterator& listIterator, T* elementOld = NULL);
-
- /**
- * get a single element on specified index
- * @param index index of element that will be get
- * @param result the buffer that the retrieved element will be stored
- * @return CAPU_EINVAL invalid index
- * CAPU_OK otherwise
- */
- status_t get(int_t index, T* result);
-
- /**
- * return size of list
- * @return return the size of list
- */
- int_t size();
-
- /**
- * check the list is empty or not
- * @return true if empty
- * false otherwise
- */
- bool_t isEmpty();
-
- /**
- * returns an iterator pointing to the beginning of list
- * @return iterator
- */
- Iterator begin() const;
-
- /**
- * finds the index of given element in the link list
- * if you are using an object you need to overload == operator
- *
- * @param element the value that will be searched
- * @return -1 if the value either does not exist or given value is NULL
- * otherwise index of value on linked list
- */
- int_t find(const T &element);
-
- /**
- *
- * @param index the index of element that will be replaced
- * @param element element that will be overwritten to existing place
- * @param elementOld the buffer to keep the existing
- * @return CAPU_EINVAL if the index is not valid
- * CAPU_OK otherwise
- */
- status_t set(int_t index, const T &element, T* elementOld = NULL);
-
- /**
- * check that if the list contains the given parameter or not
- * if you are using an object you need to overload == operator
- * @param element element that will be checked
- * @return true list contains it
- * false otherwise
- */
- bool_t contains(const T &element);
-
- /**
- * removes all elements from linked list
- *
- * @return CAPU_OK if all of the elements are deleted
- *
- */
- status_t clear();
- };
-
- template <class T, class C>
- List<T, C>::List()
- : mHead(NULL),
- mTail(NULL),
- mSize(0) {
- //init linked list
- }
-
- template <class T, class C>
- List<T, C>::~List() {
- clear();
- }
-
- template <class T, class C>
- status_t List<T, C>::clear() {
- //on the deallocation delete all elements in the list
- ListNode *cursor = mHead;
- ListNode *tmp = NULL;
-
- while (cursor != NULL) {
- tmp = cursor;
- cursor = cursor->mNext;
- delete tmp;
- }
- mSize = 0;
- mHead = NULL;
- mTail = NULL;
- return CAPU_OK;
- }
-
- template <class T, class C>
- int_t List<T, C>::find(const T &element) {
- int_t counter = 0;
- ListNode * cursor = mHead;
- C comparator;
- while (cursor != NULL) {
- if (comparator(cursor->mData, element)) {
- return counter;
- }
- cursor = cursor->mNext;
- ++counter;
- }
- return -1;
- }
-
- template <class T, class C>
- bool_t List<T, C>::contains(const T &element) {
- return (find(element) != -1);
- }
-
- //add elements to the end of list
-
- template <class T, class C>
- status_t List<T, C>::add(const T &element) {
- ListNode *listElem = new ListNode(element);
- //NOT ALLOCATED
- if (listElem == NULL) {
- return CAPU_ENO_MEMORY;
- }
- //list is empty
- if (mHead == NULL) {
- mHead = listElem;
- mTail = mHead;
- } else {
- //list contains some elements so add it to the end of list
- mTail->mNext = listElem;
- listElem->mPrev = mTail;
- mTail = listElem;
- mTail->mNext = NULL;
- }
- ++mSize;
- return CAPU_OK;
- }
-
- template <class T, class C>
- status_t List<T, C>::add(int_t index, const T &element) {
- if ((index > mSize) || (index < 0)) {
- return CAPU_EINVAL;
- }
-
- ListNode *listElem = new ListNode(element);
- //NOT ALLOCATED
- if (listElem == NULL) {
- return CAPU_ENO_MEMORY;
- }
-
- int_t counter = 0;
- ListNode * cursor = mHead;
- if (cursor == NULL) {
- //empty list
- mHead = listElem;
- mTail = listElem;
- mHead->mPrev = NULL;
- mTail->mNext = NULL;
- ++mSize;
- return CAPU_OK;
- } else if (index == mSize) {
- //list contains some elements so add it to the end of list
- mTail->mNext = listElem;
- listElem->mPrev = mTail;
- mTail = listElem;
- mTail->mNext = NULL;
- ++mSize;
- return CAPU_OK;
- } else if (index == 0) {
- //add it to beginning of the list
- listElem->mNext = mHead;
- mHead->mPrev = listElem;
- listElem->mPrev = NULL;
- mHead = listElem;
- ++mSize;
- return CAPU_OK;
- }
- while (cursor != NULL) {
- if (index == counter) {
- listElem->mNext = cursor;
- listElem->mPrev = cursor->mPrev;
- if (cursor->mPrev != NULL)
- cursor->mPrev->mNext = listElem;
- cursor->mPrev = listElem;
+ template <class T, class C>
+ inline void List<T, C>::insertElement(typename List<T, C>::ListNode* addPosition, const T& element)
+ {
+ ListNode* newNode = new ListNode(element); // copy in
+ newNode->mNext = addPosition->mNext;
+ newNode->mPrev = addPosition;
+ addPosition->mNext->mPrev = newNode;
+ addPosition->mNext = newNode;
++mSize;
+ }
+
+ template <class T, class C>
+ inline void List<T, C>::deleteElement(typename List<T, C>::ListNode* deletePosition)
+ {
+ deletePosition->mPrev->mNext = deletePosition->mNext;
+ deletePosition->mNext->mPrev = deletePosition->mPrev;
+ delete deletePosition;
+ --mSize;
+ }
+
+ template <class T, class C>
+ typename List<T, C>::ListNode* List<T, C>::findElement(uint_t index) const
+ {
+ // search element by running through list from the first element
+ uint_t counter = 0;
+ ListNode* current = mBoundary->mNext;
+ while(counter < index)
+ {
+ ++counter;
+ current = current->mNext;
+ }
+
+ // current is the element that was requested
+ return current;
+ }
+
+
+ template <class T, class C>
+ status_t List<T, C>::remove(const T& element)
+ {
+ ListNode* current = mBoundary->mNext;
+ while (current != mBoundary)
+ {
+ if (mComparator(current->mData, element))
+ {
+ // deletion element found
+ deleteElement(current);
+ return CAPU_OK;
+ }
+ current = current->mNext;
+ }
+ return CAPU_EINVAL;
+ }
+
+ template <class T, class C>
+ status_t List<T, C>::removeAt(uint_t index, T* elementOld)
+ {
+ if (index >= mSize)
+ {
+ // invalid index
+ return CAPU_EINVAL;
+ }
+
+ ListNode* toDelete = findElement(index);
+ if (elementOld)
+ {
+ *elementOld = toDelete->mData; // copy out
+ }
+ deleteElement(toDelete);
return CAPU_OK;
- }
- ++counter;
- cursor = cursor->mNext;
- }
- return CAPU_ERROR;
- }
-
- template <class T, class C>
- status_t List<T, C>::add(Iterator& iter, const T &element) {
- ListNode *listElem = new ListNode(element);
-
- //NOT ALLOCATED
- if (listElem == NULL) {
- return CAPU_ENO_MEMORY;
- }
-
- if (mHead == NULL && mTail == NULL) {
- mHead = listElem;
- mTail = mHead;
- } else if (iter.mNextPosition == NULL) {
- listElem->mNext = NULL;
- listElem->mPrev = mTail;
- mTail->mNext = listElem;
- mTail = listElem;
- } else if (iter.mNextPosition->mNext == NULL) {
- listElem->mNext = NULL;
- listElem->mPrev = iter.mNextPosition;
- iter.mNextPosition->mNext = listElem;
- mTail = listElem;
- } else if (iter.mNextPosition->mPrev == NULL) {
- listElem->mPrev = NULL;
- listElem->mNext = iter.mNextPosition;
- mHead = listElem;
- } else {
- listElem->mPrev = iter.mNextPosition->mPrev;
- listElem->mNext = iter.mNextPosition;
- iter.mNextPosition->mPrev->mNext = listElem;
- }
- iter.mNextPosition = listElem;
-
- ++mSize;
- return CAPU_OK;
-
- }
-
- //remove the specific element indicated by the index in the list
-
- template <class T, class C>
- status_t List<T, C>::removeAt(int_t index, T* elementOld) {
- if ((index < 0) || (index >= mSize)) {
- return CAPU_EINVAL;
- }
- ListNode * tmp = NULL;
- if (mHead == mTail) {
- tmp = mHead;
- mHead = NULL;
- mTail = NULL;
- } else if (index == 0) {
-
- tmp = mHead;
- mHead = mHead->mNext;
- mHead->mPrev = NULL;
- } else if (index == mSize - 1) {
- tmp = mTail;
- mTail = mTail->mPrev;
- mTail->mNext = NULL;
- } else {
- tmp = mHead;
- for (int_t i = 0; i < index; ++i) {
- tmp = tmp->mNext;
- }
- tmp->mNext->mPrev = tmp->mPrev;
- tmp->mPrev->mNext = tmp->mNext;
- }
- if (elementOld != NULL)
- *elementOld = tmp->mData;
- delete tmp;
- mSize--;
- return CAPU_OK;
- }
-
- template <class T, class C>
- status_t List<T, C>::removeAt(Iterator& listIterator, T* elementOld) {
-
- ListNode* tmp = NULL;
-
- if (mHead == mTail) {
- tmp = mHead;
- if (!tmp) {
+ }
+
+ template <class T, class C>
+ status_t List<T, C>::removeAt(Iterator& listIterator, T* elementOld)
+ {
+ if (listIterator.hasNext())
+ {
+ if (elementOld)
+ {
+ *elementOld = listIterator.mCurrentNode->mData; // copy out
+ }
+ ListNode* node = listIterator.mCurrentNode;
+ listIterator.mCurrentNode = listIterator.mCurrentNode->mNext;
+ deleteElement(node);
+ return CAPU_OK;
+ }
return CAPU_EINVAL;
- }
- mHead = NULL;
- mTail = NULL;
- listIterator.mNextPosition = NULL;
- } else if (listIterator.mNextPosition == mHead) {
-
- tmp = mHead;
- mHead = mHead->mNext;
- mHead->mPrev = NULL;
- listIterator.mNextPosition = mHead;
- } else if (listIterator.mNextPosition == mTail) {
- tmp = mTail;
- mTail = mTail->mPrev;
- mTail->mNext = NULL;
- listIterator.mNextPosition = mTail;
- } else {
- tmp = listIterator.mNextPosition;
- listIterator.mNextPosition->mPrev->mNext = listIterator.mNextPosition->mNext;
- listIterator.mNextPosition->mNext->mPrev = listIterator.mNextPosition->mPrev;
- listIterator.mNextPosition = listIterator.mNextPosition->mPrev;
- }
-
- if (elementOld != 0) {
- *elementOld = tmp->mData;
- }
- delete tmp;
- mSize--;
-
- return CAPU_OK;
-
- }
-
- //get the specified element from list
-
- template <class T, class C>
- status_t List<T, C>::get(int_t index, T* result) {
- if (((index < 0) || (index >= mSize)) || (result == NULL)) {
- return CAPU_EINVAL;
- }
-
- if (index == 0) {
- *result = mHead->mData;
- return CAPU_OK;
- } else if (index == mSize - 1) {
- *result = mTail->mData;
- return CAPU_OK;
- }
-
- ListNode *cursor = mHead;
- for (int_t i = 0; i < index; ++i) {
- cursor = cursor->mNext;
- }
- *result = cursor->mData;
- return CAPU_OK;
- }
-
- template <class T, class C>
- status_t List<T, C>::set(int_t index, const T &element, T* elementOld) {
- if ((index < 0) || (index >= mSize))
- return CAPU_EINVAL;
-
- ListNode *cursor = mHead;
- for (int_t i = 0; i < index; ++i) {
- cursor = cursor->mNext;
- }
- if (elementOld != NULL)
- *elementOld = cursor->mData;
- cursor->mData = element;
- return CAPU_OK;
- }
-
- //Return element count of list
-
- template <class T, class C>
- int_t List<T, C>::size() {
- return mSize;
- }
-
- //checks if the list is empty or not
-
- template <class T, class C>
- bool_t List<T, C>::isEmpty() {
- if (mSize == 0)
- return true;
- else
- return false;
- }
-
- template <class T, class C>
- typename List<T, C>::Iterator List<T, C>::begin() const {
- return ListIterator(mHead);
- }
-
- template <class T, class C>
- List<T, C>::ListIterator::ListIterator()
- : mNextPosition(NULL) {
-
- }
-
- template <class T, class C>
- List<T, C>::ListIterator::~ListIterator() {
-
- }
-
- template <class T, class C>
- List<T, C>::ListIterator::ListIterator(ListNode *initialNode) {
- mNextPosition = initialNode;
- }
-
- template <class T, class C>
- bool_t List<T, C>::ListIterator::hasNext() {
- if (mNextPosition == NULL) {
- return false;
- }
- return true;
- }
-
- template <class T, class C>
- status_t List<T, C>::ListIterator::next(T* element) {
- T* result = 0;
- if (mNextPosition == NULL) {
- return CAPU_ERANGE;
- } else {
- result = &mNextPosition->mData;
- mNextPosition = mNextPosition->mNext;
- }
- if (element != NULL) {
- *element = *result;
- }
- return CAPU_OK;
- }
-
- template <class T, class C>
- status_t List<T, C>::ListIterator::current(T* value) {
- if (mNextPosition == 0) {
- return CAPU_ERANGE;
- } else if (value == 0) {
- return CAPU_EINVAL;
- } else {
- *value = mNextPosition->mData;
- return CAPU_OK;
}
- }
-}
-#endif /* __LIST_H__ */
+ template <class T, class C>
+ inline status_t List<T, C>::set(uint_t index, const T &element, T* elementOld)
+ {
+ if (index >= mSize)
+ {
+ // invalid index
+ return CAPU_EINVAL;
+ }
+
+ ListNode* node = findElement(index);
+ if (elementOld)
+ {
+ *elementOld = node->mData; // copy out
+ }
+ node->mData = element; // copy in
+ return CAPU_OK;
+ }
+
+ template <class T, class C>
+ status_t List<T, C>::get(uint_t index, T* result) const
+ {
+ if (index >= mSize)
+ {
+ // invalid index
+ return CAPU_EINVAL;
+ }
+
+ *result = findElement(index)->mData; // copy out
+ return CAPU_OK;
+ }
+
+ template <class T, class C>
+ inline int_t List<T, C>::size() const
+ {
+ return mSize;
+ }
+
+ template <class T, class C>
+ inline bool_t List<T, C>::isEmpty() const
+ {
+ return mSize == 0;
+ }
+
+ template <class T, class C>
+ typename List<T, C>::Iterator List<T, C>::begin() const
+ {
+ return ListIterator(mBoundary);
+ }
+
+ template <class T, class C>
+ List<T, C>::ListIterator::ListIterator(ListNode* boundary)
+ : mInitialNode(boundary)
+ , mCurrentNode(boundary->mNext)
+ , mIndex(0)
+ {
+ }
+
+ template <class T, class C>
+ List<T, C>::ListIterator::~ListIterator()
+ {
+ }
+
+ template <class T, class C>
+ bool_t List<T, C>::ListIterator::hasNext()
+ {
+ return mInitialNode != mCurrentNode;
+ }
+
+ template <class T, class C>
+ status_t List<T, C>::ListIterator::next(T* element)
+ {
+ if (element)
+ {
+ status_t ret = current(element);
+ if (ret != CAPU_OK)
+ {
+ return ret; // don't continue
+ }
+ }
+ mCurrentNode = mCurrentNode->mNext;
+ ++mIndex;
+ return CAPU_OK;
+ }
+
+ template <class T, class C>
+ uint32_t List<T, C>::ListIterator::currentIndex()
+ {
+ return mIndex;
+ }
+
+ template <class T, class C>
+ status_t List<T, C>::ListIterator::current(T* element)
+ {
+ if (!hasNext())
+ {
+ return CAPU_ERANGE;
+ }
+ if (!element)
+ {
+ return CAPU_EINVAL;
+ }
+ *element = mCurrentNode->mData; // copy out
+ return CAPU_OK;
+ }
+}
+#endif /* __List_H__ */
Modified: incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/container/ListTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/container/ListTest.cpp?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/container/ListTest.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/lib/capu/modules/capu/test/container/ListTest.cpp Wed Oct 17 06:48:21 2012
@@ -30,13 +30,11 @@ TEST(List, Constructor_Default) {
TEST(List, addTest) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 0;
capu::status_t result;
- data1 = 32;
- data2 = 43;
//append element to the linked list
result = list->add(data1);
EXPECT_TRUE(result == capu::CAPU_OK);
@@ -57,13 +55,12 @@ TEST(List, addTest) {
TEST(List, addIndexTest) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 0;
capu::status_t result;
- data1 = 32;
- data2 = 43;
+
result = list->add(10, data1);
EXPECT_TRUE(result == capu::CAPU_EINVAL);
@@ -93,9 +90,9 @@ TEST(List, addIndexTest) {
TEST(List, removeAt) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 56;
capu::status_t result;
data1 = 32;
@@ -134,18 +131,59 @@ TEST(List, removeAt) {
delete list;
}
-TEST(List, get) {
- capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
+TEST(List, getElementInConstList) {
+ capu::List<capu::int32_t>* normalList = new capu::List<capu::int32_t > ();
+ const capu::List<capu::int32_t>* constantList = normalList;
capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
- capu::int32_t data4;
capu::status_t result;
data1 = 32;
- data2 = 43;
- data3 = 56;
+
+ result = normalList->add(data1);
+ EXPECT_TRUE(result == capu::CAPU_OK);
+
+ result = constantList->get(0, &data1);
+ EXPECT_TRUE(result == capu::CAPU_OK);
+
+ delete normalList;
+}
+
+
+
+TEST(List, loopThroughConstList) {
+ capu::List<capu::int32_t>* normalList = new capu::List<capu::int32_t > ();
+ const capu::List<capu::int32_t>* constantList = normalList;
+ capu::int32_t data1;
+
+ capu::status_t result;
+
+ data1 = 32;
+
+ result = normalList->add(data1);
+ EXPECT_TRUE(result == capu::CAPU_OK);
+
+
+ capu::List<capu::int32_t>::Iterator iterator = constantList->begin();
+ while (iterator.hasNext())
+ {
+ capu::int32_t* temp=0;;
+ iterator.next(temp);
+ }
+
+ delete normalList;
+}
+
+TEST(List, get) {
+ capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 56;
+ capu::int32_t data4 = 0;
+
+ capu::status_t result;
+
+
//add some element to the linked list
result = list->add(data1);
@@ -178,11 +216,9 @@ TEST(List, get) {
TEST(List, size) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
-
capu::int_t result;
+ capu::int32_t data1 = 32;;
- data1 = 32;
//size of empty list
result = list->size();
EXPECT_TRUE(result == 0);
@@ -213,12 +249,11 @@ TEST(List, size) {
TEST(List, empty) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
-
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
capu::bool_t result;
- data1 = 32;
-
//check the empty list
result = list->isEmpty();
EXPECT_TRUE(result == true);
@@ -233,14 +268,12 @@ TEST(List, empty) {
TEST(List, find) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
capu::int_t result;
- data1 = 32;
- data2 = 43;
- data3 = 44;
+
//check empty list
result = list->find(data1);
@@ -266,9 +299,9 @@ TEST(List, find) {
TEST(List, contains) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
capu::int32_t check_value = 0;
capu::bool_t result;
@@ -297,17 +330,52 @@ TEST(List, contains) {
delete list;
}
+TEST(List, copyConstructor1) {
+ capu::List<capu::int32_t> list;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
+
+ //add some dummy values to the linked list
+ list.add(data1);
+ list.add(data2);
+ list.add(data3);
+
+ capu::List<capu::int32_t> copy(list);
+
+ EXPECT_EQ(list.size(), copy.size());
+ list.clear();
+
+ EXPECT_EQ(0, copy.find(data1));
+ EXPECT_EQ(1, copy.find(data2));
+ EXPECT_EQ(2, copy.find(data3));
+}
+
+TEST(List, copyConstructor2) {
+ // copy empty list and add values afterwards to original list
+ capu::List<capu::int32_t> list;
+ capu::List<capu::int32_t> copy(list);
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
+
+ copy.add(data1);
+ copy.add(data2);
+ copy.add(data3);
+
+ EXPECT_EQ(0, copy.find(data1));
+ EXPECT_EQ(1, copy.find(data2));
+ EXPECT_EQ(2, copy.find(data3));
+ EXPECT_TRUE(list.isEmpty());
+}
+
TEST(List, clear) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
capu::status_t result;
- data1 = 32;
- data2 = 43;
- data3 = 44;
-
//add some dummy values to the linked list
list->add(data1);
list->add(data1);
@@ -328,11 +396,8 @@ TEST(List, clear) {
TEST(List, set) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
-
- data1 = 32;
- data2 = 43;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
EXPECT_TRUE(list->add(data1) == capu::CAPU_OK);
EXPECT_TRUE(list->add(data2) == capu::CAPU_OK);
@@ -346,14 +411,11 @@ TEST(List, set) {
TEST(ListIterator, hasNext) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
capu::List<capu::int32_t>::Iterator it = list->begin();
EXPECT_TRUE(it.hasNext() == false);
- data1 = 32;
- data2 = 43;
-
list->add(data1);
list->add(data2);
@@ -365,14 +427,13 @@ TEST(ListIterator, hasNext) {
TEST(ListIterator, next) {
capu::List<capu::int32_t>* list = new capu::List<capu::int32_t > ();
- capu::int32_t data1;
- capu::int32_t data2;
- capu::int32_t data3;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 0;
capu::List<capu::int32_t>::Iterator it = list->begin();
capu::int32_t cnt = 0;
EXPECT_TRUE(it.hasNext() == false);
- data1 = 32;
- data2 = 43;
+
list->add(data1);
list->add(data2);
it = list->begin();
@@ -456,8 +517,8 @@ TEST(ListIterator, removeAt){
}
TEST(ListIterator, add){
- capu::List<capu::int32_t> list;
- capu::List<capu::int32_t>::Iterator iter;
+ capu::List<capu::int32_t> list;
+ capu::List<capu::int32_t>::Iterator iter = list.begin();
capu::int32_t data1 = 0;
list.add(iter, 1);
@@ -514,10 +575,29 @@ TEST(ListIterator, add){
}
+TEST(ListIterator, currentIndex){
+ capu::List<capu::int32_t> list;
+ capu::int32_t data1 = 32;
+ capu::int32_t data2 = 43;
+ capu::int32_t data3 = 44;
+
+ //add some dummy values to the linked list
+ list.add(data1);
+ list.add(data2);
+ list.add(data3);
+
+ capu::List<capu::int32_t>::Iterator iter = list.begin();
+
+ EXPECT_EQ(0u, iter.currentIndex());
+ iter.next();
+ EXPECT_EQ(1u, iter.currentIndex());
+ iter.next();
+ EXPECT_EQ(2u, iter.currentIndex());
+}
TEST(ListIterator, loopAdd){
capu::List<capu::int32_t> list;
- capu::List<capu::int32_t>::Iterator iter;
+ capu::List<capu::int32_t>::Iterator iter = list.begin();
capu::int32_t data1 = 0;
list.add(iter, 1);
@@ -525,7 +605,7 @@ TEST(ListIterator, loopAdd){
iter = list.begin();
while(iter.hasNext())
{
- iter.next(&data1);
+ iter.next(&data1);
}
list.add(iter, 2);
@@ -540,10 +620,10 @@ TEST(ListIterator, loopAdd){
capu::int32_t i = 1;
while(i <= 3)
{
- EXPECT_TRUE(iter.current(&data1) == capu::CAPU_OK);
- EXPECT_EQ(i, data1);
- iter.next();
- i++;
+ EXPECT_TRUE(iter.current(&data1) == capu::CAPU_OK);
+ EXPECT_EQ(i, data1);
+ iter.next();
+ i++;
}
}
Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchListTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchListTest.cpp?rev=1399110&r1=1399109&r2=1399110&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchListTest.cpp (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/common/EtchListTest.cpp Wed Oct 17 06:48:21 2012
@@ -225,13 +225,13 @@ TEST(EtchList, find) {
//find the elements
result = list->find(data1);
- EXPECT_TRUE(result == 0);
+ EXPECT_EQ(0, result);
result = list->find(data2);
- EXPECT_TRUE(result == 1);
+ EXPECT_EQ(1, result);
result = list->find(data3);
- EXPECT_TRUE(result == 2);
+ EXPECT_EQ(2, result);
delete list;
}