-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Queue: Complere the refined implementation, unit test, and demo program.
- Loading branch information
Showing
4 changed files
with
471 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,94 @@ | ||
#include "cds.h" | ||
|
||
|
||
typedef struct Employ_ { | ||
int8_t cYear; | ||
int8_t cLevel; | ||
int32_t iId; | ||
} Employ; | ||
typedef struct Tuple_ { | ||
int first; | ||
int second; | ||
} Tuple; | ||
|
||
|
||
void DestroyObject(Item item) | ||
void CleanObject(void* element) | ||
{ | ||
free((Employ*)item); | ||
free(element); | ||
} | ||
|
||
|
||
void ManipulateNumerics() | ||
{ | ||
/* We should initialize the container before any operations. */ | ||
Queue* queue = QueueInit(); | ||
|
||
/* Push numeric elements to the queue. */ | ||
QueuePush(queue, (void*)(intptr_t)1); | ||
QueuePush(queue, (void*)(intptr_t)2); | ||
QueuePush(queue, (void*)(intptr_t)3); | ||
QueuePush(queue, (void*)(intptr_t)4); | ||
|
||
/* Check the element order. */ | ||
void* element; | ||
QueueFront(queue, &element); | ||
assert((int)(intptr_t)element == 1); | ||
|
||
QueueBack(queue, &element); | ||
assert((int)(intptr_t)element == 4); | ||
|
||
/* Pop elements from the queue*/ | ||
QueuePop(queue); | ||
QueuePop(queue); | ||
|
||
/* Check the number of stored elements. */ | ||
assert(QueueSize(queue) == 2); | ||
|
||
QueueDeinit(queue); | ||
} | ||
|
||
void ManipulateObjects() | ||
{ | ||
/* We should initialize the container before any operations. */ | ||
Queue* queue = QueueInit(); | ||
QueueSetClean(queue, CleanObject); | ||
|
||
/* Push numeric elements to the queue. */ | ||
Tuple* tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 1; | ||
tuple->second = -1; | ||
QueuePush(queue, tuple); | ||
|
||
tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 2; | ||
tuple->second = -2; | ||
QueuePush(queue, tuple); | ||
|
||
tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 3; | ||
tuple->second = -3; | ||
QueuePush(queue, tuple); | ||
|
||
tuple = (Tuple*)malloc(sizeof(Tuple)); | ||
tuple->first = 4; | ||
tuple->second = -4; | ||
QueuePush(queue, tuple); | ||
|
||
/* Check the element order. */ | ||
void* element; | ||
QueueFront(queue, &element); | ||
assert(((Tuple*)element)->first == 1); | ||
QueueBack(queue, &element); | ||
assert(((Tuple*)element)->first == 4); | ||
|
||
/* Pop elements from the queue*/ | ||
QueuePop(queue); | ||
QueuePop(queue); | ||
|
||
/* Check the number of stored elements. */ | ||
assert(QueueSize(queue) == 2); | ||
|
||
QueueDeinit(queue); | ||
} | ||
|
||
int main() | ||
{ | ||
Queue *pQueue; | ||
|
||
/* You should initialize the DS before any operations. */ | ||
int32_t iRtnCode = QueueInit(&pQueue); | ||
if (iRtnCode != SUCC) | ||
return iRtnCode; | ||
|
||
/* If you plan to delegate the resource clean task to the DS, please set the | ||
custom clean method. */ | ||
pQueue->set_destroy(pQueue, DestroyObject); | ||
|
||
/* Push items onto the queue. */ | ||
Employ *pEmploy = (Employ*)malloc(sizeof(Employ)); | ||
pEmploy->iId = 4; | ||
pEmploy->cLevel = 4; | ||
pEmploy->cYear = 4; | ||
pQueue->push(pQueue, (Item)pEmploy); | ||
|
||
pEmploy = (Employ*)malloc(sizeof(Employ)); | ||
pEmploy->iId = 3; | ||
pEmploy->cLevel = 3; | ||
pEmploy->cYear = 3; | ||
pQueue->push(pQueue, (Item)pEmploy); | ||
|
||
pEmploy = (Employ*)malloc(sizeof(Employ)); | ||
pEmploy->iId = 2; | ||
pEmploy->cLevel = 2; | ||
pEmploy->cYear = 2; | ||
pQueue->push(pQueue, (Item)pEmploy); | ||
|
||
pEmploy = (Employ*)malloc(sizeof(Employ)); | ||
pEmploy->iId = 1; | ||
pEmploy->cLevel = 1; | ||
pEmploy->cYear = 1; | ||
pQueue->push(pQueue, (Item)pEmploy); | ||
|
||
/* Check the queue order. */ | ||
Item item; | ||
pQueue->front(pQueue, &item); | ||
assert(((Employ*)item)->cLevel == 4); | ||
pQueue->back(pQueue, &item); | ||
assert(((Employ*)item)->cLevel == 1); | ||
|
||
/* Pop items from the queue */ | ||
pQueue->pop(pQueue); | ||
pQueue->pop(pQueue); | ||
|
||
/* Check the number of stored items. */ | ||
int32_t iSize = pQueue->size(pQueue); | ||
assert(iSize == 2); | ||
|
||
/* You should deinitialize the DS after all the relevant tasks. */ | ||
QueueDeinit(&pQueue); | ||
|
||
return SUCC; | ||
ManipulateNumerics(); | ||
ManipulateObjects(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.