コード例 #1
0
ファイル: list_mtm.c プロジェクト: TaniaPewah/hw2
/**
* Creates a copy of target list.
*
* The new copy will contain all the elements from the source list in the same
* order and will use the same functions as the original list for copying and
* freeing elements.
*
* @param list The target list to copy
* @return
* NULL if a NULL was sent or a memory allocation failed.
* A List containing the same elements with same order as list otherwise.
*/
List listCopy(List list) {
	List copy = NULL;
	if (list != NULL) {
		copy = listCreate(list->copyElementFunc, list->freeElementFunc);
	}
	if ((copy != NULL) && (list->First != NULL)) {
		ListItem item_to_copy = list->First;
		while ((item_to_copy != NULL) &&  (copy != NULL)) {
			if (listInsertLast(copy, item_to_copy->Element) == LIST_SUCCESS) {
				if (list->Current == item_to_copy){
					int size = GetListLength(copy);
					listGetFirst(copy);
					for (int i = 1; i < size; i++) {
						listGetNext(copy);
					}
				}
				item_to_copy = item_to_copy->Next;
			} else {
				listDestroy(copy);
				copy = NULL;
			}
		}
	}
	return copy;
}
コード例 #2
0
ファイル: clustermap.c プロジェクト: iamrohit/cacheismo
static int connectionMakeGetRequest(connectionContext_t* pCContext){
	int count = listGetSize(pCContext->currentRequests);
	if (count < 1) {
		return 0;
	}
	request_t* pCurrent = listGetFirst(pCContext->currentRequests);
	int estimatedBufferSize   = 0;
	while (pCurrent) {
		estimatedBufferSize = strlen(pCurrent->key) + 1;
		pCurrent = listGetNext(pCContext->currentRequests, pCurrent);
	}
	estimatedBufferSize -= 1;     //don't need space after last key
	estimatedBufferSize += 4 + 2; // strlen("get ") + strlen("\r\n")

	u_int32_t offset     = 0;
	char*     buffer     = 0;
	int       written    = 0;
	buffer = connectionGetBuffer(pCContext->connection, pCContext->fallocator,
			        estimatedBufferSize, &offset);
    if (buffer) {
    	request_t* pCurrent = listGetFirst(pCContext->currentRequests);
    	strncpy(buffer+offset, "get ", 4);
    	written += 4;
		while (pCurrent) {
			request_t* pNext = listGetNext(pCContext->currentRequests, pCurrent);
			int keyLength = strlen(pCurrent->key);
			strncpy(buffer+offset+written, pCurrent->key, keyLength);
			written+= keyLength;
			if (pNext) {
				strncpy(buffer+offset+written, " ", 1);
				written+= 1;
			}
			pCurrent = pNext;
		}
		strncpy(buffer+offset+written, "\r\n", 2);
		written += 2;
		LOG(DEBUG, "created request %.*s", written, buffer+offset);
		dataStreamAppendData(pCContext->writeStream, buffer, offset, written);
		//TODO : check return value
    }
	return written;
}
コード例 #3
0
static bool testClientsManagerGetSortedPayments(){
	Email email = NULL, mail1 = NULL ,mail2 = NULL, mail3 = NULL;
	emailCreate("gaba@ganosh", &email);
	emailCreate("baba@gash", &mail1);
	emailCreate("baba@gash2", &mail2);
	emailCreate("baaa@gash", &mail3);
	ClientsManager manager = clientsManagerCreate();
	clientsManagerAdd( manager, email, 1, 1, 200);
	clientsManagerAdd( manager, mail1, 1, 2, 1000);
	clientsManagerAdd( manager, mail2, 1, 2, 1000);
	clientsManagerAdd( manager, mail3, 1, 2, 1000);

	List clients_list = NULL;
	ASSERT_TEST( clientsManagerGetSortedPayments(NULL, &clients_list) ==
			CLIENT_MANAGER_INVALID_PARAMETERS);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager,NULL) ==
			CLIENT_MANAGER_INVALID_PARAMETERS);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) ==
				CLIENT_MANAGER_SUCCESS);
	ASSERT_TEST( listGetSize(clients_list) == 0);
	listDestroy(clients_list);

	clientsManagerExecutePurchase( manager, email, 330);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) ==
				CLIENT_MANAGER_SUCCESS);
	ASSERT_TEST( listGetSize(clients_list) == 1);
	listDestroy(clients_list);

	clientsManagerExecutePurchase( manager, mail1, 900);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) ==
			CLIENT_MANAGER_SUCCESS);
	ASSERT_TEST( listGetSize(clients_list) == 2);
	ClientPurchaseBill top_bill = listGetFirst(clients_list);
	ASSERT_TEST( emailAreEqual(
			clientPurchaseBillGetClientEmail(top_bill), mail1));
	ClientPurchaseBill second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), email));
	listDestroy(clients_list);

	clientsManagerExecutePurchase( manager, mail2, 900);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) ==
			CLIENT_MANAGER_SUCCESS);
	ASSERT_TEST( listGetSize(clients_list) == 3);
	top_bill = listGetFirst(clients_list);
	ASSERT_TEST( emailAreEqual(
			clientPurchaseBillGetClientEmail(top_bill), mail1));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), mail2));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), email));
	listDestroy(clients_list);

	clientsManagerExecutePurchase( manager, mail3, 900);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) ==
			CLIENT_MANAGER_SUCCESS);
	ASSERT_TEST( listGetSize(clients_list) == 4);
	top_bill = listGetFirst(clients_list);
	ASSERT_TEST( emailAreEqual(
			clientPurchaseBillGetClientEmail(top_bill), mail3));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), mail1));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), mail2));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), email));
	listDestroy(clients_list);

	clientsManagerExecutePurchase(manager, email, 900);
	ASSERT_TEST( clientsManagerGetSortedPayments(manager, &clients_list) ==
			CLIENT_MANAGER_SUCCESS);
	ASSERT_TEST( listGetSize(clients_list) == 4);
	top_bill = listGetFirst(clients_list);
	ASSERT_TEST( emailAreEqual(
			clientPurchaseBillGetClientEmail(top_bill), email));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), mail3));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), mail1));
	second_bill = listGetNext(clients_list);
	ASSERT_TEST( emailAreEqual(
		clientPurchaseBillGetClientEmail(second_bill), mail2));
	listDestroy(clients_list);

	emailDestroy(email);
	emailDestroy(mail1);
	emailDestroy(mail2);
	emailDestroy(mail3);
	clientsManagerDestroy(manager);
	return true;
}