celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pnol...@apache.org
Subject [10/20] celix git commit: CELIX-263: replaced utils cunit tests w/ cpputests
Date Wed, 07 Oct 2015 12:45:50 GMT
http://git-wip-us.apache.org/repos/asf/celix/blob/c6d2d59f/utils/private/test/hash_map_test_hash.c
----------------------------------------------------------------------
diff --git a/utils/private/test/hash_map_test_hash.c b/utils/private/test/hash_map_test_hash.c
deleted file mode 100644
index 5c74f03..0000000
--- a/utils/private/test/hash_map_test_hash.c
+++ /dev/null
@@ -1,359 +0,0 @@
-/**
- *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.
- */
-/*
- * hash_map_test_hash.c
- *
- *  \date       Jul 25, 2010
- *  \author    	<a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
- *  \copyright	Apache License, Version 2.0
- */
-#include <stdio.h>
-#include <string.h>
-#include <stddef.h>
-
-#include <CUnit/Automated.h>
-
-#include "hash_map.h"
-#include "hash_map_private.h"
-
-hash_map_pt map;
-
-unsigned int test_hashKeyChar(void * k) {
-	char * str = (char *) k;
-
-	unsigned int hash = 1315423911;
-	unsigned int i    = 0;
-	int len = strlen(str);
-
-	for(i = 0; i < len; str++, i++)
-	{
-	  hash ^= ((hash << 5) + (*str) + (hash >> 2));
-	}
-
-	return hash;
-}
-
-unsigned int test_hashValueChar(void * v) {
-	return 0;
-}
-
-int test_equalsKeyChar(void * k, void * o) {
-	return strcmp((char *)k, (char *) o) == 0;
-}
-
-int test_equalsValueChar(void * v, void * o) {
-	return strcmp((char *)v, (char *) o) == 0;
-}
-
-int setup(void) {
-	printf("test\n");
-	map = hashMap_create(test_hashKeyChar, test_hashValueChar, test_equalsKeyChar, test_equalsValueChar);
-	if(map == NULL) {
-		return 1;
-	}
-	return 0;
-}
-
-void test_hashMap_get(void) {
-	char * key = "key";
-	char * value = "value";
-	char * key2 = "key2";
-	char * value2 = "value2";
-	char * getKey = strdup("key");
-	char * get;
-	char * neKey;
-	char * key3 = NULL;
-	char * value3 = "value3";
-
-	hashMap_clear(map, false, false);
-
-	// Add one entry
-	hashMap_put(map, key, value);
-
-	// Add second entry
-	hashMap_put(map, key2, value2);
-
-	// Get with new created key
-	getKey = strdup("key");
-	get = hashMap_get(map, getKey);
-	CU_ASSERT_PTR_NOT_NULL_FATAL(get);
-	CU_ASSERT_STRING_EQUAL(get, value);
-
-	getKey = strdup("key2");
-	get = hashMap_get(map, getKey);
-	CU_ASSERT_PTR_NOT_NULL_FATAL(get);
-	CU_ASSERT_STRING_EQUAL(get, value2);
-
-	neKey = strdup("notExisting");
-	get = hashMap_get(map, neKey);
-	CU_ASSERT_EQUAL(get, NULL);
-
-	get = hashMap_get(map, NULL);
-	CU_ASSERT_EQUAL(get, NULL);
-
-	// Add third entry with NULL key
-	hashMap_put(map, key3, value3);
-
-	get = hashMap_get(map, NULL);
-	CU_ASSERT_STRING_EQUAL(get, value3);
-}
-
-void test_hashMap_containsKey(void) {
-	char * key = "key";
-	char * value = "value";
-	char * key2 = "key2";
-	char * value2 = "value2";
-	char * containsKey;
-	char * key3 = NULL;
-	char * value3 = "value3";
-
-	hashMap_clear(map, false, false);
-
-	// Add one entry
-	hashMap_put(map, key, value);
-
-	// Add second entry
-	hashMap_put(map, key2, value2);
-
-	containsKey = strdup("key");
-	CU_ASSERT_TRUE(hashMap_containsKey(map, containsKey));
-	containsKey = strdup("key2");
-	CU_ASSERT_TRUE(hashMap_containsKey(map, containsKey));
-	containsKey = strdup("notExisting");
-	CU_ASSERT_FALSE(hashMap_containsKey(map, containsKey));
-	containsKey = NULL;
-	CU_ASSERT_FALSE(hashMap_containsKey(map, containsKey));
-
-	// Add third entry with NULL key
-	hashMap_put(map, key3, value3);
-
-	containsKey = NULL;
-	CU_ASSERT_TRUE(hashMap_containsKey(map, containsKey));
-}
-
-void test_hashMap_getEntry(void) {
-	char * key = "key";
-	char * value = "value";
-	char * key2 = "key2";
-	char * value2 = "value2";
-	char * getEntryKey;
-	char * key3 = NULL;
-	char * value3 = "value3";	
-	hash_map_entry_pt entry;
-	
-	hashMap_clear(map, false, false);
-
-	// Add one entry
-	hashMap_put(map, key, value);
-
-	// Add second entry
-	hashMap_put(map, key2, value2);
-
-	// Get with new created key
-	getEntryKey = strdup("key");
-	entry = hashMap_getEntry(map, getEntryKey);
-	CU_ASSERT_PTR_NOT_NULL_FATAL(entry);
-	CU_ASSERT_STRING_EQUAL(entry->key, key);
-	CU_ASSERT_STRING_EQUAL(entry->value, value);
-
-	getEntryKey = strdup("key2");
-	entry = hashMap_getEntry(map, getEntryKey);
-	CU_ASSERT_PTR_NOT_NULL(entry);
-	CU_ASSERT_STRING_EQUAL(entry->key, key2);
-	CU_ASSERT_STRING_EQUAL(entry->value, value2);
-
-	getEntryKey = strdup("notExisting");
-	entry = hashMap_getEntry(map, getEntryKey);
-	CU_ASSERT_EQUAL(entry, NULL);
-
-	getEntryKey = NULL;
-	entry = hashMap_getEntry(map, getEntryKey);
-	CU_ASSERT_EQUAL(entry, NULL);
-
-	// Add third entry with NULL key
-	hashMap_put(map, key3, value3);
-
-	getEntryKey = NULL;
-	entry = hashMap_getEntry(map, getEntryKey);
-	CU_ASSERT_EQUAL(entry->key, key3);
-	CU_ASSERT_STRING_EQUAL(entry->value, value3);
-}
-
-void test_hashMap_put(void) {
-	char * key = "key";
-	char * value = "value";
-	char * key2 = "key2";
-	char * value2 = "value2";
-	char * getKey;
-	char * get;
-	char * nkey2 = strdup("key2");
-	char * nvalue2 = "value3";
-	char * old;
-	char * key3 = NULL;
-	char * value3 = "value3";
-	char * key4 = "key4";
-	char * value4 = NULL;
-
-	hashMap_clear(map, false, false);
-
-	// Add one entry
-	hashMap_put(map, key, value);
-
-	// Add second entry
-	hashMap_put(map, key2, value2);
-
-	// Get with new key
-	getKey = strdup("key");
-	get = hashMap_get(map, getKey);
-	CU_ASSERT_STRING_EQUAL(get, value);
-
-	getKey = strdup("key2");
-	get = hashMap_get(map, getKey);
-	CU_ASSERT_STRING_EQUAL(get, value2);
-
-	// Overwrite existing entry
-	old = (char *) hashMap_put(map, nkey2, nvalue2);
-	CU_ASSERT_PTR_NOT_NULL_FATAL(old);
-	CU_ASSERT_STRING_EQUAL(old, value2)
-
-	getKey = strdup("key2");
-	get = hashMap_get(map, key2);
-	CU_ASSERT_STRING_EQUAL(get, nvalue2);
-
-	// Add third entry with NULL key
-	hashMap_put(map, key3, value3);
-
-	getKey = NULL;
-	get = hashMap_get(map, key3);
-	CU_ASSERT_STRING_EQUAL(get, value3);
-
-	// Add fourth entry with NULL value
-	hashMap_put(map, key4, value4);
-
-	getKey = strdup("key4");
-	get = hashMap_get(map, key4);
-	CU_ASSERT_EQUAL(get, value4);
-}
-
-void test_hashMap_remove(void) {
-	char * key = "key";
-	char * value = "value";
-	char * key2 = NULL;
-	char * value2 = "value2";
-	char * removeKey;
-
-	hashMap_clear(map, false, false);
-
-	// Add one entry
-	hashMap_put(map, key, value);
-
-	// Add second entry with null key
-	hashMap_put(map, key2, value2);
-
-	// Remove unexisting entry for map
-	removeKey = strdup("unexisting");
-	hashMap_remove(map, removeKey);
-	CU_ASSERT_EQUAL(map->size, 2);
-	CU_ASSERT_FALSE(hashMap_isEmpty(map));
-
-	// Remove entry with new key
-	removeKey = strdup("key");
-	hashMap_remove(map, removeKey);
-	CU_ASSERT_EQUAL(map->size, 1);
-
-	removeKey = NULL;
-	hashMap_remove(map, removeKey);
-	CU_ASSERT_EQUAL(map->size, 0);
-	CU_ASSERT_TRUE(hashMap_isEmpty(map));
-
-	// Remove unexisting entry for empty map
-	removeKey = strdup("unexisting");
-	hashMap_remove(map, removeKey);
-	CU_ASSERT_EQUAL(map->size, 0);
-	CU_ASSERT_TRUE(hashMap_isEmpty(map));
-}
-
-void test_hashMap_containsValue(void) {
-	char * key = "key";
-	char * value = "value";
-	char * key2 = "key2";
-	char * value2 = "value2";
-	char * containsValue;
-	char * key3 = "key3";
-	char * value3 = NULL;
-
-	hashMap_clear(map, false, false);
-
-	// Add one entry
-	hashMap_put(map, key, value);
-
-	// Add second entry
-	hashMap_put(map, key2, value2);
-
-	containsValue = strdup("value");
-	CU_ASSERT_TRUE(hashMap_containsValue(map, containsValue));
-	containsValue = strdup("value2");
-	CU_ASSERT_TRUE(hashMap_containsValue(map, containsValue));
-	containsValue = strdup("notExisting");
-	CU_ASSERT_FALSE(hashMap_containsValue(map, containsValue));
-	containsValue = NULL;
-	CU_ASSERT_FALSE(hashMap_containsValue(map, containsValue));
-
-	// Add third entry with NULL value
-	hashMap_put(map, key3, value3);
-
-	containsValue = NULL;
-	CU_ASSERT_TRUE(hashMap_containsValue(map, containsValue));
-}
-
-int main (int argc, char** argv) {
-	CU_pSuite pSuite = NULL;
-
-	/* initialize the CUnit test registry */
-	if (CUE_SUCCESS != CU_initialize_registry())
-	  return CU_get_error();
-
-	/* add a suite to the registry */
-	pSuite = CU_add_suite("Suite_1", setup, NULL);
-	if (NULL == pSuite) {
-	  CU_cleanup_registry();
-	  return CU_get_error();
-	}
-
-	/* add the tests to the suite */
-	if (NULL == CU_add_test(pSuite, "Map Get Test", test_hashMap_get)
-		|| NULL == CU_add_test(pSuite, "Map Contains Key Test", test_hashMap_containsKey)
-		|| NULL == CU_add_test(pSuite, "Map Get Entry Test", test_hashMap_getEntry)
-		|| NULL == CU_add_test(pSuite, "Map Put Test", test_hashMap_put)
-		|| NULL == CU_add_test(pSuite, "Map Remove Test", test_hashMap_remove)
-		|| NULL == CU_add_test(pSuite, "Map Contains Value Test", test_hashMap_containsValue)
-
-
-	)
-	{
-	  CU_cleanup_registry();
-	  return CU_get_error();
-	}
-
-	CU_set_output_filename(argv[1]);
-	CU_list_tests_to_file();
-	CU_automated_run_tests();
-	CU_cleanup_registry();
-	return CU_get_error();
-}

http://git-wip-us.apache.org/repos/asf/celix/blob/c6d2d59f/utils/private/test/linked_list_test.c
----------------------------------------------------------------------
diff --git a/utils/private/test/linked_list_test.c b/utils/private/test/linked_list_test.c
deleted file mode 100644
index 2635e88..0000000
--- a/utils/private/test/linked_list_test.c
+++ /dev/null
@@ -1,96 +0,0 @@
-/**
- *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.
- */
-/*
- * linked_list_test.c
- *
- *  \date       Jul 16, 2010
- *  \author    	<a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
- *  \copyright	Apache License, Version 2.0
- */
-#include <stdio.h>
-#include <stddef.h>
-
-#include <CUnit/Automated.h>
-#include "linked_list.h"
-
-linked_list_pt list;
-
-int setup(void) {
-    linkedList_create(&list);
-    if (list) {
-        return 0;
-    } else {
-        // failure during setup
-        return 1;
-    }
-}
-
-int teardown(void) {
-    return 0;
-}
-
-void test_linkedList_create(void) {
-    CU_ASSERT_PTR_NOT_NULL_FATAL(list);
-    CU_ASSERT_EQUAL(linkedList_size(list), 0);
-}
-
-void test_linkedList_add(void) {
-    CU_ASSERT_EQUAL(linkedList_size(list), 0);
-    linkedList_addElement(list, "element");
-	CU_ASSERT_EQUAL(linkedList_size(list), 1);
-}
-
-void test_linkedList_remove(void) {
-    CU_ASSERT_EQUAL(linkedList_size(list), 0);
-    linkedList_addElement(list, "element");
-    CU_ASSERT_EQUAL(linkedList_size(list), 1);
-
-    linkedList_removeElement(list, "element");
-    CU_ASSERT_EQUAL(linkedList_size(list), 0);
-}
-
-int main (int argc, char** argv) {
-	CU_pSuite pSuite = NULL;
-
-	/* initialize the CUnit test registry */
-	if (CUE_SUCCESS != CU_initialize_registry())
-	  return CU_get_error();
-
-	/* add a suite to the registry */
-	pSuite = CU_add_suite("Suite_1", setup, teardown);
-	if (NULL == pSuite) {
-	  CU_cleanup_registry();
-	  return CU_get_error();
-	}
-
-	/* add the tests to the suite */
-	if (NULL == CU_add_test(pSuite, "List Creation Test", test_linkedList_create) ||
-		NULL == CU_add_test(pSuite, "List Add Test", test_linkedList_add) ||
-		NULL == CU_add_test(pSuite, "List Remove Test", test_linkedList_remove))
-	{
-	  CU_cleanup_registry();
-	  return CU_get_error();
-	}
-
-	CU_set_output_filename(argv[1]);
-	CU_list_tests_to_file();
-	CU_automated_run_tests();
-	CU_cleanup_registry();
-	return CU_get_error();
-}

http://git-wip-us.apache.org/repos/asf/celix/blob/c6d2d59f/utils/private/test/linked_list_test.cpp
----------------------------------------------------------------------
diff --git a/utils/private/test/linked_list_test.cpp b/utils/private/test/linked_list_test.cpp
new file mode 100644
index 0000000..6a9e589
--- /dev/null
+++ b/utils/private/test/linked_list_test.cpp
@@ -0,0 +1,794 @@
+/**
+ *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.
+ */
+/*
+ * linked_list_test.cpp
+ *
+ * 	\date       Sep 15, 2015
+ *  \author    	Menno van der Graaf
+ *  \copyright	Apache License, Version 2.0
+ */
+#include <stdio.h>
+#include <stddef.h>
+#include <string.h>
+
+#include "CppUTest/TestHarness.h"
+#include "CppUTest/TestHarness_c.h"
+#include "CppUTest/CommandLineTestRunner.h"
+
+extern "C"
+{
+#include "linked_list.h"
+#include "linked_list_private.h"
+#include "linked_list_iterator.h"
+}
+
+int main(int argc, char** argv) {
+	return RUN_ALL_TESTS(argc, argv);
+}
+
+static char* my_strdup(const char* s){
+	char *d = (char*) malloc (strlen (s) + 1);
+	if (d == NULL) return NULL;
+	strcpy (d,s);
+	return d;
+}
+
+//----------------------TESTGROUP DEFINES----------------------
+
+TEST_GROUP(linked_list){
+	linked_list_pt list;
+
+	void setup(void) {
+		linkedList_create(&list);
+	}
+
+	void teardown(void) {
+		linkedList_destroy(list);
+	}
+};
+
+TEST_GROUP(linked_list_iterator){
+	linked_list_pt list;
+
+	void setup(void) {
+		linkedList_create(&list);
+	}
+
+	void teardown(void) {
+		linkedList_destroy(list);
+	}
+};
+
+//----------------------LINKED LIST TESTS----------------------
+
+TEST(linked_list, create){
+	CHECK(list != NULL);
+	LONGS_EQUAL(0, linkedList_size(list));
+}
+
+TEST(linked_list, add){
+	char * value = my_strdup("element");
+
+	LONGS_EQUAL(0, linkedList_size(list));
+	linkedList_addElement(list, value);
+	LONGS_EQUAL(1, linkedList_size(list));
+
+	free(value);
+}
+
+TEST(linked_list, addFirst){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+
+	STRCMP_EQUAL(value, (char*) linkedList_getFirst(list));
+
+	linkedList_addFirst(list, value3);
+
+	STRCMP_EQUAL(value3, (char*) linkedList_getFirst(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, addLast){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_getLast(list));
+
+	linkedList_addLast(list, value3);
+
+	STRCMP_EQUAL(value3, (char*) linkedList_getLast(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, addIndex){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	char * value4 = my_strdup("element4");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	linkedList_addIndex(list, 2, value4);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value4, (char*) linkedList_get(list, 2));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 3));
+
+	free(value);
+	free(value2);
+	free(value3);
+	free(value4);
+}
+
+TEST(linked_list, addBefore){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	char * value4 = my_strdup("element4");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	linkedList_addBefore(list, value4, linkedList_entry(list, 2));
+
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value4, (char*) linkedList_get(list, 2));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 3));
+
+	free(value);
+	free(value2);
+	free(value3);
+	free(value4);
+}
+
+TEST(linked_list, remove){
+	char * value = strdup("element");
+
+	LONGS_EQUAL(0, linkedList_size(list));
+	linkedList_addElement(list, value);
+	LONGS_EQUAL(1, linkedList_size(list));
+
+	linkedList_removeElement(list, value);
+	LONGS_EQUAL(0, linkedList_size(list));
+
+	free(value);
+}
+
+TEST(linked_list, removeFirst){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value, (char*) linkedList_getFirst(list));
+
+	linkedList_removeFirst(list);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_getFirst(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, removeLast){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value3, (char*) linkedList_getLast(list));
+
+	linkedList_removeLast(list);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_getLast(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, removeElement){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	//add 6 elemets
+	linkedList_addElement(list, NULL);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, NULL);
+	linkedList_addElement(list, value3);
+	LONGS_EQUAL(6, list->size);
+
+	linkedList_removeElement(list, NULL);
+	linkedList_removeElement(list, NULL);
+	LONGS_EQUAL(4, list->size);
+
+	linkedList_removeElement(list, value2);
+	LONGS_EQUAL(3, list->size);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, removeIndex){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+
+	linkedList_removeIndex(list, 1);
+
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, removeEntry){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	linkedList_removeEntry(list, list->header);
+	LONGS_EQUAL(3, list->size);
+
+	linkedList_removeEntry(list, linkedList_entry(list, 0));
+	LONGS_EQUAL(2, list->size);
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, clone){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_pt list2;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	linkedList_clone(list, &list2);
+	LONGS_EQUAL(list->size, list2->size);
+	STRCMP_EQUAL( (char*) linkedList_getFirst(list), (char*) linkedList_getFirst(list2));
+	STRCMP_EQUAL( (char*) linkedList_getLast(list), (char*) linkedList_getLast(list2));
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedList_destroy(list2);
+}
+
+TEST(linked_list, clear){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	LONGS_EQUAL(3, list->size);
+
+	linkedList_clear(list);
+	LONGS_EQUAL(0, list->size);
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, get){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, getFirst){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value, (char*) linkedList_getFirst(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, getLast){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value3, (char*) linkedList_getLast(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, set){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	linkedList_set(list, 1, value3);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, contains){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	linkedList_addElement(list, value);
+	CHECK(linkedList_contains(list, value));
+	CHECK(!linkedList_contains(list, value2));
+	CHECK(!linkedList_contains(list, value3));
+
+	linkedList_addElement(list, value2);
+	CHECK(linkedList_contains(list, value));
+	CHECK(linkedList_contains(list, value2));
+	CHECK(!linkedList_contains(list, value3));
+
+	linkedList_addElement(list, value3);
+	CHECK(linkedList_contains(list, value));
+	CHECK(linkedList_contains(list, value2));
+	CHECK(linkedList_contains(list, value3));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, size){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	LONGS_EQUAL(0, linkedList_size(list));
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	LONGS_EQUAL(3, linkedList_size(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, isEmpty){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	CHECK(linkedList_isEmpty(list));
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	CHECK(!linkedList_isEmpty(list));
+
+	linkedList_clear(list);
+	CHECK(linkedList_isEmpty(list));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+TEST(linked_list, entry){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	char * value4 = my_strdup("element4");
+	char * value5 = my_strdup("element5");
+
+	POINTERS_EQUAL(NULL, linkedList_entry(list, 666));
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	linkedList_addElement(list, value4);
+	linkedList_addElement(list, value5);
+
+	STRCMP_EQUAL(value2, (char*) linkedList_entry(list, 1)->element);
+	STRCMP_EQUAL(value4, (char*) linkedList_entry(list, 3)->element);
+
+	free(value);
+	free(value2);
+	free(value3);
+	free(value4);
+	free(value5);
+}
+
+TEST(linked_list, indexOf){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+
+	LONGS_EQUAL(-1, linkedList_indexOf(list, value));
+	LONGS_EQUAL(-1, linkedList_indexOf(list, value2));
+	LONGS_EQUAL(-1, linkedList_indexOf(list, value3));
+	LONGS_EQUAL(-1, linkedList_indexOf(list, NULL));
+
+	linkedList_addElement(list, value);
+	LONGS_EQUAL(0, linkedList_indexOf(list, value));
+
+	linkedList_addElement(list, value2);
+	LONGS_EQUAL(1, linkedList_indexOf(list, value2));
+
+	linkedList_addElement(list, value3);
+	LONGS_EQUAL(2, linkedList_indexOf(list, value3));
+
+	linkedList_addElement(list, NULL);
+	LONGS_EQUAL(3, linkedList_indexOf(list, NULL));
+
+	free(value);
+	free(value2);
+	free(value3);
+}
+
+//----------------------LINKED LIST ITERATOR TESTS----------------------
+
+TEST(linked_list_iterator, create){
+	linked_list_iterator_pt it_list = linkedListIterator_create(list,0);
+	CHECK(it_list != NULL);
+	linkedListIterator_destroy(it_list);
+
+	it_list = linkedListIterator_create(list,666);
+	POINTERS_EQUAL(NULL, it_list);
+}
+
+TEST(linked_list_iterator, hasNext){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	it_list = linkedListIterator_create(list, 0);
+	CHECK(linkedListIterator_hasNext(it_list));
+
+	linkedListIterator_next(it_list);
+	CHECK(linkedListIterator_hasNext(it_list));
+
+	linkedListIterator_next(it_list);
+	CHECK(linkedListIterator_hasNext(it_list));
+
+	linkedListIterator_next(it_list);
+	CHECK(!linkedListIterator_hasNext(it_list));
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, hasPrevious){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+
+	it_list = linkedListIterator_create(list, 3);
+	CHECK(linkedListIterator_hasPrevious(it_list));
+
+	linkedListIterator_previous(it_list);
+	CHECK(linkedListIterator_hasPrevious(it_list));
+
+	linkedListIterator_previous(it_list);
+	CHECK(linkedListIterator_hasPrevious(it_list));
+
+	linkedListIterator_previous(it_list);
+	CHECK(!linkedListIterator_hasPrevious(it_list));
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, next){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 0);
+	STRCMP_EQUAL(value, (char*) linkedListIterator_next(it_list));
+	STRCMP_EQUAL(value2, (char*) linkedListIterator_next(it_list));
+	STRCMP_EQUAL(value3, (char*) linkedListIterator_next(it_list));
+	POINTERS_EQUAL(NULL, linkedListIterator_next(it_list));
+
+	//mess up the expected and real changecount, code should check and handle
+	linkedList_addElement(list, value);
+	linkedListIterator_next(it_list);
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, previous){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 3);
+	STRCMP_EQUAL(value3, (char*) linkedListIterator_previous(it_list));
+	STRCMP_EQUAL(value2, (char*) linkedListIterator_previous(it_list));
+	STRCMP_EQUAL(value, (char*) linkedListIterator_previous(it_list));
+	POINTERS_EQUAL(NULL, linkedListIterator_previous(it_list));
+
+	//mess up the expected and real changecount, code should check and handle
+	linkedListIterator_destroy(it_list);
+	it_list = linkedListIterator_create(list, 3);
+	linkedList_addElement(list, value);
+	linkedListIterator_previous(it_list);
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, nextIndex){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 0);
+	LONGS_EQUAL(0, linkedListIterator_nextIndex(it_list));
+	linkedListIterator_next(it_list);
+	LONGS_EQUAL(1, linkedListIterator_nextIndex(it_list));
+	linkedListIterator_next(it_list);
+	LONGS_EQUAL(2, linkedListIterator_nextIndex(it_list));
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, previousIndex){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 3);
+	LONGS_EQUAL(2, linkedListIterator_previousIndex(it_list));
+	linkedListIterator_previous(it_list);
+	LONGS_EQUAL(1, linkedListIterator_previousIndex(it_list));
+	linkedListIterator_previous(it_list);
+	LONGS_EQUAL(0, linkedListIterator_previousIndex(it_list));
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, set){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 0);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	linkedListIterator_set(it_list, NULL);
+	linkedListIterator_next(it_list);
+	linkedListIterator_next(it_list);
+	linkedListIterator_set(it_list, value3);
+	linkedListIterator_next(it_list);
+	linkedListIterator_set(it_list, value2);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 2));
+
+	//mess up the expected and real changecount, code should check and handle
+	linkedList_addElement(list, value);
+	linkedListIterator_set(it_list, value);
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, add){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 0);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1));
+
+	linkedListIterator_next(it_list);
+	linkedListIterator_add(it_list, value2);
+
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	//mess up the expected and real changecount, code should check and handle
+	linkedList_addElement(list, value);
+	linkedListIterator_add(it_list, value2);
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}
+
+TEST(linked_list_iterator, remove){
+	char * value = my_strdup("element");
+	char * value2 = my_strdup("element2");
+	char * value3 = my_strdup("element3");
+	linked_list_iterator_pt it_list;
+
+	linkedList_addElement(list, value);
+	linkedList_addElement(list, value2);
+	linkedList_addElement(list, value3);
+	it_list = linkedListIterator_create(list, 0);
+
+	LONGS_EQUAL(3, list->size);
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value2, (char*) linkedList_get(list, 1));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 2));
+
+	linkedListIterator_next(it_list);
+	linkedListIterator_next(it_list);
+	linkedListIterator_remove(it_list);
+	LONGS_EQUAL(2, list->size);
+	STRCMP_EQUAL(value, (char*) linkedList_get(list, 0));
+	STRCMP_EQUAL(value3, (char*) linkedList_get(list, 1));
+
+	//mess up the expected and real changecount, code should check and handle
+	linkedList_addElement(list, value);
+	linkedListIterator_remove(it_list);
+
+	free(value);
+	free(value2);
+	free(value3);
+	linkedListIterator_destroy(it_list);
+}


Mime
View raw message