static bool testListCopy() {
	ASSERT_TRUE(spListCopy(NULL) == NULL);
	SPList list = spListCreate();
	SPList copy = spListCopy(list);
	ASSERT_TRUE(copy != NULL);
	ASSERT_TRUE(0 == spListGetSize(copy));
	SPListElement e1 = spListElementCreate(1, 1.0);
	SPListElement e2 = spListElementCreate(2, 2.0);
	SPListElement e3 = spListElementCreate(3, 3.0);
	SPListElement e4 = spListElementCreate(4, 4.0);
	spListInsertFirst(list, e1);
	ASSERT_TRUE(0 == spListGetSize(copy));
	SPList list2 = quickList(4, e1, e2, e3, e4);
	SPList copy2 = spListCopy(list2);
	ASSERT_TRUE(4 == spListGetSize(copy2));
	ASSERT_TRUE(spListElementCompare(e1, spListGetFirst(copy2)) == 0);
	ASSERT_TRUE(spListElementCompare(e2, spListGetNext(copy2)) == 0);
	ASSERT_TRUE(spListElementCompare(e3, spListGetNext(copy2)) == 0);
	ASSERT_TRUE(spListElementCompare(e4, spListGetNext(copy2)) == 0);
	ASSERT_TRUE(spListGetNext(copy2) == NULL);
	spListDestroy(list);
	spListDestroy(list2);
	spListDestroy(copy);
	spListDestroy(copy2);
	spListElementDestroy(e1);
	spListElementDestroy(e2);
	spListElementDestroy(e3);
	spListElementDestroy(e4);
	return true;
}
static SPList quickList(int size, ...) {
	va_list items;
	SPList list = spListCreate();
	va_start(items, size);
	for (int i = 0; i < size; i++) {
		spListInsertLast(list, va_arg(items, SPListElement));
	}
	va_end(items);
	return list;
}
Example #3
0
SPBPQueue spBPQueueCreate(int maxSize){
	assert(maxSize>0);
	SPBPQueue queue = (SPBPQueue)malloc(sizeof(*queue));
	if (queue == NULL) {
		return NULL;
	} else {
		queue->list = spListCreate();
		queue->bound = maxSize;
	}
	return queue;
}
SPBPQueue spBPQueueCreate(int maxSize) {
	if (maxSize <= 0) {
		return NULL;
	}
	SPList newList = spListCreate();
	if (newList == NULL) {
		return NULL;
	}
	SPBPQueue createdQueue = spBPQueueInnerCreate(newList, maxSize);
	if (createdQueue == NULL) {
		spListDestroy(newList);
		return NULL;
	}
	return createdQueue;
}
static bool testListClear() {
	SPListElement e1 = spListElementCreate(1, 1.0);
	SPListElement e2 = spListElementCreate(2, 2.0);
	SPListElement e3 = spListElementCreate(3, 3.0);
	SPListElement e4 = spListElementCreate(4, 4.0);
	SPList list2 = quickList(4, e1, e2, e3, e4);
	ASSERT_TRUE(spListClear(list2) == SP_LIST_SUCCESS);
	ASSERT_TRUE(0 == spListGetSize(list2));
	SPList list = spListCreate();
	spListClear(list);
	ASSERT_TRUE(0 == spListGetSize(list));
	spListDestroy(list);
	spListDestroy(list2);
	spListElementDestroy(e1);
	spListElementDestroy(e2);
	spListElementDestroy(e3);
	spListElementDestroy(e4);
	return true;
}
static bool testListGetLast() {
	SPList list = spListCreate();
	ASSERT_TRUE(spListGetLast(list) == NULL);
	SPListElement e1 = spListElementCreate(1, 1.0);
	SPListElement e2 = spListElementCreate(2, 2.0);
	SPListElement e3 = spListElementCreate(3, 3.0);
	SPListElement e4 = spListElementCreate(4, 4.0);
	SPList list2 = quickList(4, e1, e2, e3, e4);
	SPListElement last = spListGetLast(list2);
	ASSERT_TRUE(spListElementCompare(e4, last) == 0);
	ASSERT_TRUE(
			spListElementCompare(last, spListGetLast(list2)) == 0
					&& spListGetLast(list2) == last);
	spListDestroy(list);
	spListDestroy(list2);
	spListElementDestroy(e1);
	spListElementDestroy(e2);
	spListElementDestroy(e3);
	spListElementDestroy(e4);
	return true;
}
Example #7
0
SPList spListCopy(SPList list) {
	if (list == NULL) {
		return NULL;
	}
	SPList copyList = spListCreate();
	if (copyList == NULL) {
		return NULL;
	}
	SPListElement currentElement = spListGetFirst(list);
	while (currentElement) {
		if (spListInsertLast(copyList, currentElement) != SP_LIST_SUCCESS) {
			spListDestroy(copyList);
			list->current = NULL;
			return NULL;
		}
		currentElement = spListGetNext(list);
	}
	list->current = NULL;
	copyList->current = NULL;
	return copyList;
}
SPBPQueue spBPQueueCreateWrapper(int maxSize, SPBPQueue source_queue, bool createNewList) {
	SPBPQueue newQueue;
	spMinimalVerifyArgumentsRn(maxSize > 0);

	spCalloc(newQueue, sp_bp_queue_t, 1);

	newQueue->capacity = maxSize;


	if (createNewList) {
		newQueue->queue = spListCreate();
		newQueue->maxElement = NULL;
	}
	else {
		newQueue->queue = spListCopy(source_queue->queue);
		newQueue->maxElement = spListElementCopy(source_queue->maxElement);
	}

	//allocation error, or given source queue is NULL and createNewList if false
	if (newQueue->queue == NULL)
		return NULL;

	return newQueue;
}
static bool testListCreate() {
	SPList list = spListCreate();
	ASSERT_TRUE(list != NULL);
	spListDestroy(list);
	return true;
}