mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 00:46:52 +08:00
Compare commits
10 Commits
142a248d34
...
e3f547875f
Author | SHA1 | Date | |
---|---|---|---|
e3f547875f | |||
03f4396227 | |||
87ff4b4127 | |||
e8c5a06fa4 | |||
d8012720b6 | |||
43cfbfcda6 | |||
a48a285cc5 | |||
f3559d516a | |||
9c0b75e718 | |||
6ef644b07d |
1
.vscode/launch.json
vendored
1
.vscode/launch.json
vendored
@ -9,6 +9,7 @@
|
|||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/build/release/bin/test.exe",
|
"program": "${workspaceFolder}/build/release/bin/test.exe",
|
||||||
|
// "program": "${workspaceFolder}/build/release/bin/demo.exe",
|
||||||
"args": [],
|
"args": [],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
|
2
3rdparty/unicstl-unity/src/unity_config.h
vendored
2
3rdparty/unicstl-unity/src/unity_config.h
vendored
@ -2,7 +2,7 @@
|
|||||||
#ifndef _UNITY_CONFIG_H_
|
#ifndef _UNITY_CONFIG_H_
|
||||||
#define _UNITY_CONFIG_H_
|
#define _UNITY_CONFIG_H_
|
||||||
|
|
||||||
// #define UNITY_OUTPUT_COLOR 1
|
#define UNITY_OUTPUT_COLOR 1
|
||||||
#define UNITY_USE_FLUSH_STDOUT 1
|
#define UNITY_USE_FLUSH_STDOUT 1
|
||||||
#define UNITY_INCLUDE_PRINT_FORMATTED 1 // support TEST_PRINTF
|
#define UNITY_INCLUDE_PRINT_FORMATTED 1 // support TEST_PRINTF
|
||||||
|
|
||||||
|
@ -58,10 +58,10 @@ int main()
|
|||||||
printf("----- unicstl demo -----\n");
|
printf("----- unicstl demo -----\n");
|
||||||
// while (1)
|
// while (1)
|
||||||
{
|
{
|
||||||
demo_list();
|
// demo_list();
|
||||||
// demo_stack();
|
// demo_stack();
|
||||||
// demo_deque();
|
// demo_deque();
|
||||||
// demo_queue();
|
demo_queue();
|
||||||
// demo_tree();
|
// demo_tree();
|
||||||
// demo_heap();
|
// demo_heap();
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,14 @@
|
|||||||
#ifndef _COMMON_H_
|
#ifndef _COMMON_H_
|
||||||
#define _COMMON_H_
|
#define _COMMON_H_
|
||||||
|
|
||||||
#define NDEBUG 1
|
// #define NDEBUG 1
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#endif // _COMMON_H_
|
#endif // _COMMON_H_
|
||||||
|
@ -39,13 +39,14 @@ struct _queue
|
|||||||
bool (*back)(struct _queue* self, void* obj);
|
bool (*back)(struct _queue* self, void* obj);
|
||||||
bool (*front)(struct _queue* self, void* obj);
|
bool (*front)(struct _queue* self, void* obj);
|
||||||
|
|
||||||
bool (*clear)(struct _queue* self);
|
// base
|
||||||
bool (*empty)(struct _queue* self);
|
bool (*empty)(struct _queue* self);
|
||||||
bool (*full)(struct _queue* self); // only for queue2
|
bool (*full)(struct _queue* self);
|
||||||
uint32_t (*size)(struct _queue* self);
|
uint32_t (*size)(struct _queue* self);
|
||||||
uint32_t (*capacity)(struct _queue* self);
|
uint32_t (*capacity)(struct _queue* self);
|
||||||
|
|
||||||
// free
|
// clear and free node
|
||||||
|
bool (*clear)(struct _queue* self);
|
||||||
void (*destory)(struct _queue* self);
|
void (*destory)(struct _queue* self);
|
||||||
|
|
||||||
// print
|
// print
|
||||||
@ -54,8 +55,8 @@ struct _queue
|
|||||||
};
|
};
|
||||||
typedef struct _queue* queue_t;
|
typedef struct _queue* queue_t;
|
||||||
|
|
||||||
bool queue_init(struct _queue * queue, uint32_t obj_size);
|
bool queue_init(struct _queue* self, uint32_t obj_size);
|
||||||
bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity);
|
bool queue_init2(struct _queue* self, uint32_t obj_size, uint32_t capacity);
|
||||||
|
|
||||||
queue_t queue_new(void);
|
queue_t queue_new(void);
|
||||||
void queue_free(queue_t* queue);
|
void queue_free(queue_t* queue);
|
||||||
|
@ -34,10 +34,11 @@ struct _stack
|
|||||||
bool (*pop)(struct _stack* self, void* obj);
|
bool (*pop)(struct _stack* self, void* obj);
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t(*size)(struct _stack* self);
|
|
||||||
bool (*empty)(struct _stack* self);
|
bool (*empty)(struct _stack* self);
|
||||||
|
uint32_t (*size)(struct _stack* self);
|
||||||
|
uint32_t (*capacity)(struct _stack* self);
|
||||||
|
|
||||||
// others
|
// clear and free node
|
||||||
bool (*clear)(struct _stack* self);
|
bool (*clear)(struct _stack* self);
|
||||||
void (*destory)(struct _stack* self);
|
void (*destory)(struct _stack* self);
|
||||||
|
|
||||||
|
270
src/queue.c
270
src/queue.c
@ -10,26 +10,54 @@
|
|||||||
*/
|
*/
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
bool queue_push(struct _queue* self, void* obj)
|
static struct _queue_node * queue_node_new(void* obj, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
void * obj_new = malloc(obj_size);
|
||||||
assert(obj != NULL);
|
|
||||||
|
|
||||||
void * obj_new = malloc(self->_obj_size);
|
|
||||||
if (obj_new == NULL)
|
if (obj_new == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
goto done;
|
||||||
}
|
}
|
||||||
memmove(obj_new, obj, self->_obj_size);
|
memmove(obj_new, obj, obj_size);
|
||||||
|
|
||||||
struct _queue_node* node_new = (struct _queue_node*)malloc(sizeof(struct _queue_node));
|
struct _queue_node* node_new = (struct _queue_node*)malloc(sizeof(struct _queue_node));
|
||||||
if(node_new == NULL)
|
if(node_new == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
goto done1;
|
||||||
}
|
}
|
||||||
node_new->obj = obj_new;
|
node_new->obj = obj_new;
|
||||||
node_new->next = NULL;
|
node_new->next = NULL;
|
||||||
|
|
||||||
|
return node_new;
|
||||||
|
done1:
|
||||||
|
free(obj_new);
|
||||||
|
done:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void queue_node_free(struct _queue_node** node)
|
||||||
|
{
|
||||||
|
if(node != NULL && *node != NULL)
|
||||||
|
{
|
||||||
|
if((*node)->obj != NULL)
|
||||||
|
{
|
||||||
|
free((*node)->obj);
|
||||||
|
}
|
||||||
|
free(*node);
|
||||||
|
*node = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool queue_push(struct _queue* self, void* obj)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(obj != NULL);
|
||||||
|
|
||||||
|
struct _queue_node* node_new = queue_node_new(obj, self->_obj_size);
|
||||||
|
if(node_new == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(self->empty(self))
|
if(self->empty(self))
|
||||||
{
|
{
|
||||||
self->_front = node_new;
|
self->_front = node_new;
|
||||||
@ -41,10 +69,11 @@ bool queue_push(struct _queue* self, void* obj)
|
|||||||
self->_back = node_new;
|
self->_back = node_new;
|
||||||
}
|
}
|
||||||
self->_size++;
|
self->_size++;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_pop(struct _queue* self, void* obj)
|
static bool queue_pop(struct _queue* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -58,12 +87,12 @@ bool queue_pop(struct _queue* self, void* obj)
|
|||||||
}
|
}
|
||||||
self->_front = node->next;
|
self->_front = node->next;
|
||||||
self->_size--;
|
self->_size--;
|
||||||
free(node->obj);
|
|
||||||
free(node);
|
queue_node_free(&node);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_back(struct _queue* self, void* obj)
|
static bool queue_back(struct _queue* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -74,7 +103,7 @@ bool queue_back(struct _queue* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_front(struct _queue* self, void* obj)
|
static bool queue_front(struct _queue* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -85,16 +114,20 @@ bool queue_front(struct _queue* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_clear(struct _queue* self)
|
static bool queue_clear(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
|
if(self->empty(self))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
struct _queue_node* node = self->_front;
|
struct _queue_node* node = self->_front;
|
||||||
struct _queue_node* next = NULL;
|
struct _queue_node* next = NULL;
|
||||||
while (node)
|
while (node)
|
||||||
{
|
{
|
||||||
next = node->next;
|
next = node->next;
|
||||||
free(node->obj);
|
queue_node_free(&node);
|
||||||
free(node);
|
|
||||||
node = next;
|
node = next;
|
||||||
}
|
}
|
||||||
self->_front = NULL;
|
self->_front = NULL;
|
||||||
@ -103,25 +136,40 @@ bool queue_clear(struct _queue* self)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_empty(struct _queue* self)
|
static bool queue_empty(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
return !self->size(self);
|
assert(self->size != NULL);
|
||||||
|
return self->size(self) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t queue_size(struct _queue* self)
|
static bool queue_full(struct _queue* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(self->size != NULL);
|
||||||
|
assert(self->capacity != NULL);
|
||||||
|
return self->size(self) == self->capacity(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t queue_size(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
return self->_size;
|
return self->_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_destory(struct _queue* self)
|
static uint32_t queue_capacity(struct _queue* self)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
return self->_capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void queue_destory(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
self->clear(self);
|
self->clear(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue_print(struct _queue* self)
|
static void queue_print(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
|
|
||||||
@ -133,40 +181,7 @@ void queue_print(struct _queue* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_init(struct _queue * queue, uint32_t obj_size)
|
static bool queue2_push(struct _queue* self, void* obj)
|
||||||
{
|
|
||||||
assert(queue != NULL);
|
|
||||||
assert(obj_size > 0);
|
|
||||||
if(queue == NULL || obj_size == 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// attribute init
|
|
||||||
queue->_size = 0;
|
|
||||||
queue->_obj_size = obj_size;
|
|
||||||
// queue->_capacity = 0;
|
|
||||||
// queue->_ratio = 0;
|
|
||||||
|
|
||||||
// function init
|
|
||||||
queue->push = queue_push;
|
|
||||||
queue->pop = queue_pop;
|
|
||||||
queue->back = queue_back;
|
|
||||||
queue->front = queue_front;
|
|
||||||
queue->clear = queue_clear;
|
|
||||||
queue->empty = queue_empty;
|
|
||||||
queue->size = queue_size;
|
|
||||||
queue->destory = queue_destory;
|
|
||||||
queue->print = queue_print;
|
|
||||||
|
|
||||||
// init front & back
|
|
||||||
queue->_front = NULL;
|
|
||||||
queue->_back = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool queue2_push(struct _queue* self, void* obj)
|
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
assert(obj != NULL);
|
assert(obj != NULL);
|
||||||
@ -192,7 +207,7 @@ bool queue2_push(struct _queue* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue2_pop(struct _queue* self, void* obj)
|
static bool queue2_pop(struct _queue* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -218,7 +233,7 @@ bool queue2_pop(struct _queue* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue2_back(struct _queue* self, void* obj)
|
static bool queue2_back(struct _queue* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -239,7 +254,7 @@ bool queue2_back(struct _queue* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue2_front(struct _queue* self, void* obj)
|
static bool queue2_front(struct _queue* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
@ -252,7 +267,7 @@ bool queue2_front(struct _queue* self, void* obj)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue2_clear(struct _queue* self)
|
static bool queue2_clear(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
self->_index_front = 0;
|
self->_index_front = 0;
|
||||||
@ -261,25 +276,19 @@ bool queue2_clear(struct _queue* self)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue2_full(struct _queue* self)
|
static void queue2_destory(struct _queue* self)
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
return self->size(self) == self->capacity(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t queue2_capacity(struct _queue* self)
|
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
return self->_capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
void queue2_destory(struct _queue* self)
|
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
self->clear(self);
|
self->clear(self);
|
||||||
|
if(self->_front != NULL)
|
||||||
|
{
|
||||||
|
free(self->_front->obj);
|
||||||
|
free(self->_front);
|
||||||
|
self->_front = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void queue2_print(struct _queue* self)
|
static void queue2_print(struct _queue* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
uint32_t index = 0;
|
uint32_t index = 0;
|
||||||
@ -297,58 +306,97 @@ void queue2_print(struct _queue* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool queue_init2(struct _queue * queue, uint32_t obj_size, uint32_t capacity)
|
bool queue_init(struct _queue * self, uint32_t obj_size)
|
||||||
{
|
{
|
||||||
assert(queue != NULL);
|
assert(self != NULL);
|
||||||
assert(obj_size > 0);
|
assert(obj_size > 0);
|
||||||
assert(capacity > 0);
|
if(self == NULL || obj_size == 0)
|
||||||
if(queue == NULL || obj_size == 0 || capacity == 0)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attribute init
|
// attribute init
|
||||||
queue->_size = 0;
|
self->_size = 0;
|
||||||
queue->_obj_size = obj_size;
|
self->_obj_size = obj_size;
|
||||||
queue->_capacity = capacity;
|
self->_capacity = UINT32_MAX;
|
||||||
queue->_ratio = 2;
|
self->_ratio = 1;
|
||||||
|
|
||||||
// function init
|
// function init
|
||||||
queue->push = queue2_push;
|
self->push = queue_push;
|
||||||
queue->pop = queue2_pop;
|
self->pop = queue_pop;
|
||||||
queue->back = queue2_back;
|
|
||||||
queue->front = queue2_front;
|
self->back = queue_back;
|
||||||
queue->clear = queue2_clear;
|
self->front = queue_front;
|
||||||
queue->empty = queue_empty;
|
|
||||||
queue->full = queue2_full;
|
self->clear = queue_clear;
|
||||||
queue->size = queue_size;
|
self->empty = queue_empty;
|
||||||
queue->capacity = queue2_capacity;
|
self->full = queue_full;
|
||||||
queue->destory = queue2_destory;
|
self->size = queue_size;
|
||||||
queue->print = queue2_print;
|
self->capacity = queue_capacity;
|
||||||
|
|
||||||
|
self->destory = queue_destory;
|
||||||
|
self->print = queue_print;
|
||||||
|
|
||||||
// init front & back
|
// init front & back
|
||||||
queue->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
|
self->_front = NULL;
|
||||||
if(queue->_front == NULL)
|
self->_back = NULL;
|
||||||
{
|
|
||||||
goto done;
|
|
||||||
// return false;
|
|
||||||
}
|
|
||||||
queue->_back = queue->_front;
|
|
||||||
|
|
||||||
// queue->_front->obj = calloc(queue->_capacity + 1, queue->_obj_size);
|
|
||||||
queue->_front->obj = calloc(queue->_capacity, queue->_obj_size);
|
|
||||||
if(queue->_front->obj == NULL)
|
|
||||||
{
|
|
||||||
goto done1;
|
|
||||||
// return false;
|
|
||||||
}
|
|
||||||
queue->_index_front = 0;
|
|
||||||
queue->_index_back = 0;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queue_init2(struct _queue * self, uint32_t obj_size, uint32_t capacity)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(obj_size > 0);
|
||||||
|
assert(capacity > 0);
|
||||||
|
if(self == NULL || obj_size == 0 || capacity == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// attribute init
|
||||||
|
self->_size = 0;
|
||||||
|
self->_obj_size = obj_size;
|
||||||
|
self->_capacity = capacity;
|
||||||
|
self->_ratio = 2;
|
||||||
|
|
||||||
|
// function init
|
||||||
|
self->push = queue2_push;
|
||||||
|
self->pop = queue2_pop;
|
||||||
|
|
||||||
|
self->back = queue2_back;
|
||||||
|
self->front = queue2_front;
|
||||||
|
|
||||||
|
self->clear = queue2_clear;
|
||||||
|
self->empty = queue_empty;
|
||||||
|
self->full = queue_full;
|
||||||
|
self->size = queue_size;
|
||||||
|
self->capacity = queue_capacity;
|
||||||
|
|
||||||
|
self->destory = queue2_destory;
|
||||||
|
self->print = queue2_print;
|
||||||
|
|
||||||
|
// init front & back
|
||||||
|
self->_front = (struct _queue_node *)malloc(sizeof(struct _queue_node));
|
||||||
|
if(self->_front == NULL)
|
||||||
|
{
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
self->_back = self->_front;
|
||||||
|
|
||||||
|
// use self->_front->obj as obj_array
|
||||||
|
// self->_front->obj = calloc(self->_capacity, self->_obj_size);
|
||||||
|
self->_front->obj = malloc(self->_capacity * self->_obj_size);
|
||||||
|
if(self->_front->obj == NULL)
|
||||||
|
{
|
||||||
|
goto done1;
|
||||||
|
}
|
||||||
|
self->_index_front = 0;
|
||||||
|
self->_index_back = 0;
|
||||||
|
|
||||||
|
return true;
|
||||||
done1:
|
done1:
|
||||||
free(queue->_front);
|
free(self->_front);
|
||||||
done:
|
done:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -360,7 +408,7 @@ queue_t queue_new(void)
|
|||||||
|
|
||||||
void queue_free(queue_t* queue)
|
void queue_free(queue_t* queue)
|
||||||
{
|
{
|
||||||
// assert(queue != NULL);
|
assert(queue != NULL);
|
||||||
if(queue != NULL && *queue != NULL)
|
if(queue != NULL && *queue != NULL)
|
||||||
{
|
{
|
||||||
if((*queue)->destory != NULL)
|
if((*queue)->destory != NULL)
|
||||||
|
108
src/stack.c
108
src/stack.c
@ -25,9 +25,7 @@ static uint32_t stack_size(struct _stack* self)
|
|||||||
static bool stack_empty(struct _stack* self)
|
static bool stack_empty(struct _stack* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
// assert(self->_head != NULL);
|
return stack_size(self) == 0;
|
||||||
// return self->_head->next == NULL ? true : false;
|
|
||||||
return !stack_size(self);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool stack_peek(struct _stack* self, void* obj)
|
static bool stack_peek(struct _stack* self, void* obj)
|
||||||
@ -105,10 +103,9 @@ static bool stack_pop(struct _stack* self, void* obj)
|
|||||||
static bool stack_clear(struct _stack* self)
|
static bool stack_clear(struct _stack* self)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
|
|
||||||
if (self->empty(self))
|
if (self->empty(self))
|
||||||
{
|
{
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _stack_node* node = self->_head->next;
|
struct _stack_node* node = self->_head->next;
|
||||||
@ -149,42 +146,6 @@ static void stack_print(struct _stack* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool stack_init(struct _stack* self, uint32_t obj_size)
|
|
||||||
{
|
|
||||||
assert(self != NULL);
|
|
||||||
|
|
||||||
// 1. set attr
|
|
||||||
self->_obj_size = obj_size;
|
|
||||||
self->_size = 0;
|
|
||||||
// self->_capacity = 64; // ÎÞЧ
|
|
||||||
// self->_ratio = 2; // ÎÞЧ
|
|
||||||
|
|
||||||
// 2. set function
|
|
||||||
// kernel
|
|
||||||
self->peek = stack_peek;
|
|
||||||
self->pop = stack_pop;
|
|
||||||
self->push = stack_push;
|
|
||||||
|
|
||||||
// others
|
|
||||||
self->clear = stack_clear;
|
|
||||||
self->empty = stack_empty;
|
|
||||||
self->size = stack_size;
|
|
||||||
self->destory = stack_destory;
|
|
||||||
self->print = stack_print;
|
|
||||||
|
|
||||||
// 3. set node
|
|
||||||
self->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node));
|
|
||||||
if (self->_head == NULL)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
self->_head->obj = NULL;
|
|
||||||
self->_head->next = NULL;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static bool stack2_peek(struct _stack* self, void* obj)
|
static bool stack2_peek(struct _stack* self, void* obj)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -208,9 +169,9 @@ static bool stack2_push(struct _stack* self, void* obj)
|
|||||||
assert(self->_head != NULL);
|
assert(self->_head != NULL);
|
||||||
assert(obj != NULL);
|
assert(obj != NULL);
|
||||||
|
|
||||||
if (self->size(self) == self->_capacity)
|
if (self->size(self) == self->capacity(self))
|
||||||
{
|
{
|
||||||
void* obj_new = (void*)realloc(self->_head->obj, self->_capacity * self->_obj_size * self->_ratio);
|
void* obj_new = (void*)realloc(self->_head->obj, self->capacity(self) * self->_obj_size * self->_ratio);
|
||||||
if (obj_new == NULL)
|
if (obj_new == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -281,6 +242,51 @@ static void stack2_print(struct _stack* self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool stack_init(struct _stack* self, uint32_t obj_size)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
assert(obj_size != 0);
|
||||||
|
if(self == NULL || obj_size == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. set attr
|
||||||
|
self->_obj_size = obj_size;
|
||||||
|
self->_size = 0;
|
||||||
|
self->_capacity = UINT32_MAX;
|
||||||
|
self->_ratio = 1;
|
||||||
|
|
||||||
|
// 2. set function
|
||||||
|
// kernel
|
||||||
|
self->peek = stack_peek;
|
||||||
|
self->pop = stack_pop;
|
||||||
|
self->push = stack_push;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->empty = stack_empty;
|
||||||
|
self->size = stack_size;
|
||||||
|
self->capacity = stack_capacity;
|
||||||
|
|
||||||
|
// clear and free node
|
||||||
|
self->clear = stack_clear;
|
||||||
|
self->destory = stack_destory;
|
||||||
|
// print
|
||||||
|
self->print = stack_print;
|
||||||
|
|
||||||
|
// 3. set node
|
||||||
|
self->_head = (struct _stack_node *)malloc(sizeof(struct _stack_node));
|
||||||
|
if (self->_head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
self->_head->obj = NULL;
|
||||||
|
self->_head->next = NULL;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
assert(self != NULL);
|
assert(self != NULL);
|
||||||
@ -298,10 +304,14 @@ bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
|||||||
self->push = stack2_push;
|
self->push = stack2_push;
|
||||||
|
|
||||||
// others
|
// others
|
||||||
self->clear = stack_clear;
|
|
||||||
self->empty = stack_empty;
|
self->empty = stack_empty;
|
||||||
self->size = stack_size;
|
self->size = stack_size;
|
||||||
|
self->capacity = stack_capacity;
|
||||||
|
|
||||||
|
// clear and free node
|
||||||
|
self->clear = stack_clear;
|
||||||
self->destory = stack2_destory;
|
self->destory = stack2_destory;
|
||||||
|
// print
|
||||||
self->print = stack2_print;
|
self->print = stack2_print;
|
||||||
|
|
||||||
// 3. set node
|
// 3. set node
|
||||||
@ -324,15 +334,19 @@ bool stack_init2(struct _stack* self, uint32_t obj_size, uint32_t capacity)
|
|||||||
|
|
||||||
stack_t stack_new(void)
|
stack_t stack_new(void)
|
||||||
{
|
{
|
||||||
return (struct _stack*)malloc(sizeof(struct _stack));
|
return (struct _stack*)calloc(1, sizeof(struct _stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
void stack_free(stack_t *stack)
|
void stack_free(stack_t *stack)
|
||||||
{
|
{
|
||||||
if(*stack)
|
assert(stack != NULL);
|
||||||
|
if(stack != NULL && *stack != NULL)
|
||||||
|
{
|
||||||
|
if((*stack)->destory != NULL)
|
||||||
{
|
{
|
||||||
(*stack)->destory(*stack);
|
(*stack)->destory(*stack);
|
||||||
free(*stack);
|
|
||||||
}
|
}
|
||||||
|
free(*stack);
|
||||||
*stack = NULL;
|
*stack = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -60,7 +60,9 @@ void print_str(void* obj)
|
|||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
void setUp(void)
|
void setUp(void)
|
||||||
{
|
{
|
||||||
// before each test
|
static uint32_t item_cnt = 1;
|
||||||
|
printf("[%4d] ", item_cnt);
|
||||||
|
item_cnt+=1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tearDown(void)
|
void tearDown(void)
|
||||||
@ -73,8 +75,8 @@ int main(int argc, char const *argv[])
|
|||||||
printf("----- Unicstl Unit Test -----\n");
|
printf("----- Unicstl Unit Test -----\n");
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
test_stack();
|
|
||||||
test_queue();
|
test_queue();
|
||||||
|
test_stack();
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
@ -10,40 +10,70 @@
|
|||||||
*/
|
*/
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* 每成功init一次,就需要对应的destory一次。否则可能存在内存泄漏
|
||||||
|
*/
|
||||||
|
static void test_queue_init(void)
|
||||||
|
{
|
||||||
|
struct _queue queue;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
||||||
|
TEST_ASSERT_FALSE(queue_init(&queue, 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(queue_init(&queue, sizeof(int)));
|
||||||
|
queue.destory(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
||||||
|
TEST_ASSERT_FALSE(queue_init2(&queue, 0, 1));
|
||||||
|
TEST_ASSERT_FALSE(queue_init2(&queue, sizeof(int), 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(queue_init2(&queue, sizeof(int), 1));
|
||||||
|
queue.destory(&queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
* 每成功init一次,就需要对应的free一次。否则可能存在内存泄漏
|
||||||
|
*/
|
||||||
static void test_queue_new(void)
|
static void test_queue_new(void)
|
||||||
{
|
{
|
||||||
queue_t queue = NULL;
|
queue_t queue = NULL;
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
|
queue_free(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
queue = queue_new();
|
||||||
TEST_ASSERT_NOT_NULL(queue);
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
|
||||||
queue_free(&queue);
|
#ifdef NDEBUG
|
||||||
TEST_ASSERT_NULL(queue);
|
|
||||||
|
|
||||||
queue_free(NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_queue_init(void)
|
|
||||||
{
|
|
||||||
queue_t queue = NULL;
|
|
||||||
|
|
||||||
// ------------------------------
|
|
||||||
queue = queue_new();
|
|
||||||
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
|
|
||||||
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
TEST_ASSERT_FALSE(queue_init(NULL, sizeof(int)));
|
||||||
TEST_ASSERT_FALSE(queue_init(queue, 0));
|
TEST_ASSERT_FALSE(queue_init(queue, 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(queue_init(queue, sizeof(int)));
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1));
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
TEST_ASSERT_FALSE(queue_init2(NULL, sizeof(int), 1));
|
||||||
TEST_ASSERT_FALSE(queue_init2(queue, 0, 1));
|
TEST_ASSERT_FALSE(queue_init2(queue, 0, 1));
|
||||||
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0));
|
TEST_ASSERT_FALSE(queue_init2(queue, sizeof(int), 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), 1));
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
|
|
||||||
queue_init2(queue, sizeof(int), 1);
|
TEST_ASSERT_NULL(queue);
|
||||||
|
queue_free(&queue); // queue_free(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void test_queue_push(void)
|
static void test_queue_push(void)
|
||||||
{
|
{
|
||||||
int temp = 0;
|
int temp = 0;
|
||||||
@ -56,19 +86,246 @@ static void test_queue_push(void)
|
|||||||
// ------------------------------
|
// ------------------------------
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
queue_init(queue, sizeof(int));
|
queue_init(queue, sizeof(int));
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
for(i = 0; i < len; i++)
|
for(i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->empty(queue));
|
||||||
}
|
}
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
|
|
||||||
// ------------------------------
|
// ------------------------------
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
queue_init2(queue, sizeof(int), 10);
|
queue_init2(queue, sizeof(int), len);
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->empty(queue));
|
||||||
|
}
|
||||||
|
queue_free(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
// if capacity is less than data len
|
||||||
|
queue = queue_new();
|
||||||
|
queue_init2(queue, sizeof(int), len - 2);
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(i < queue->capacity(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->full(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(queue->capacity(queue), queue->size(queue));
|
||||||
|
}
|
||||||
|
}
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_queue_pop(void)
|
||||||
|
{
|
||||||
|
int temp = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
queue_t queue = NULL;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
queue = queue_new();
|
||||||
|
queue_init(queue, sizeof(int));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
queue->push(queue, &data[i]);
|
||||||
|
}
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[len - 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->pop(queue, &temp));
|
||||||
|
|
||||||
|
if (!queue->empty(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i + 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[len - 1], temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_FALSE(queue->back(queue, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_FALSE(queue->pop(queue, &temp));
|
||||||
|
queue_free(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
queue = queue_new();
|
||||||
|
queue_init2(queue, sizeof(int), len);
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
queue->push(queue, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(queue->full(queue));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[len - 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->pop(queue, &temp));
|
||||||
|
|
||||||
|
if (!queue->empty(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i + 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[len - 1], temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_FALSE(queue->back(queue, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_FALSE(queue->pop(queue, &temp));
|
||||||
|
queue_free(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
// if capacity is less than data len
|
||||||
|
queue = queue_new();
|
||||||
|
queue_init2(queue, sizeof(int), len - 2);
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(i < queue->capacity(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->full(queue));
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(queue->capacity(queue), queue->size(queue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(queue->full(queue));
|
||||||
|
uint32_t capacity = queue->capacity(queue);
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (!queue->empty(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->pop(queue, &temp));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->pop(queue, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!queue->empty(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i + 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[capacity - 1], temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->pop(queue, &temp));
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_FALSE(queue->back(queue, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
|
}
|
||||||
|
queue_free(&queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_queue_clear(void)
|
||||||
|
{
|
||||||
|
int temp = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
queue_t queue = NULL;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
queue = queue_new();
|
||||||
|
queue_init(queue, sizeof(int));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
queue->push(queue, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
queue->push(queue, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_FALSE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
queue_free(&queue);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
queue = queue_new();
|
||||||
|
queue_init2(queue, sizeof(int), len);
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
queue->push(queue, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_FALSE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
queue_free(&queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static void test_queue_num(void)
|
static void test_queue_num(void)
|
||||||
@ -277,32 +534,98 @@ static void test_queue2_num(void)
|
|||||||
TEST_ASSERT_NULL(queue);
|
TEST_ASSERT_NULL(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_queue2_fifo_num(void)
|
static void test_queue2_struct(void)
|
||||||
{
|
{
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
struct _student data[] = {
|
||||||
int temp = 0;
|
{"zhao", 1001}, {"qian", 1002}, {"sun", 1003}, {"li", 1004},
|
||||||
uint32_t len = sizeof(data) / sizeof(data[0]);
|
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
||||||
uint32_t capacity = len - 1;
|
};
|
||||||
|
struct _student temp;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
|
||||||
|
uint32_t capacity = len - 2;
|
||||||
|
|
||||||
queue_t queue = NULL;
|
queue_t queue = NULL;
|
||||||
queue = queue_new();
|
queue = queue_new();
|
||||||
TEST_ASSERT_NOT_NULL(queue);
|
TEST_ASSERT_NOT_NULL(queue);
|
||||||
|
|
||||||
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(int), capacity));
|
TEST_ASSERT_TRUE(queue_init2(queue, sizeof(struct _student), capacity));
|
||||||
|
queue->print_obj = print_struct;
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
|
TEST_ASSERT_FALSE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_FALSE(queue->back(queue, &temp));
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
if (queue->size(queue) == capacity)
|
if(i < queue->capacity(queue))
|
||||||
{
|
{
|
||||||
TEST_ASSERT_TRUE(queue->full(queue));
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
TEST_ASSERT_FALSE(queue->push(queue, &data[i]));
|
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
TEST_ASSERT_FALSE(queue->full(queue));
|
TEST_ASSERT_TRUE(queue->full(queue));
|
||||||
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
|
||||||
|
TEST_ASSERT_FALSE(queue->push(queue, &data[i]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[0].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[0].name, temp.name);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[queue->size(queue) - 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[queue->size(queue) - 1].name, temp.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->empty(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->clear(queue));
|
||||||
|
TEST_ASSERT_TRUE(queue->empty(queue));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if(i < queue->capacity(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->push(queue, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, queue->size(queue));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->push(queue, &data[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (!queue->empty(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->pop(queue, &temp));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->pop(queue, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!queue->empty(queue))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i + 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[i + 1].name, temp.name);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(queue->back(queue, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[queue->capacity(queue) - 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[queue->capacity(queue) - 1].name, temp.name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(queue->front(queue, &temp));
|
||||||
|
TEST_ASSERT_FALSE(queue->back(queue, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(queue->full(queue));
|
||||||
}
|
}
|
||||||
queue_free(&queue);
|
queue_free(&queue);
|
||||||
TEST_ASSERT_NULL(queue);
|
TEST_ASSERT_NULL(queue);
|
||||||
@ -311,12 +634,15 @@ static void test_queue2_fifo_num(void)
|
|||||||
void test_queue(void)
|
void test_queue(void)
|
||||||
{
|
{
|
||||||
// TEST_MESSAGE("----- test_queue -----");
|
// TEST_MESSAGE("----- test_queue -----");
|
||||||
RUN_TEST(test_queue_new);
|
|
||||||
RUN_TEST(test_queue_init);
|
RUN_TEST(test_queue_init);
|
||||||
|
RUN_TEST(test_queue_new);
|
||||||
|
RUN_TEST(test_queue_push);
|
||||||
|
RUN_TEST(test_queue_pop);
|
||||||
|
RUN_TEST(test_queue_clear);
|
||||||
|
|
||||||
RUN_TEST(test_queue_num);
|
RUN_TEST(test_queue_num);
|
||||||
RUN_TEST(test_queue_struct);
|
RUN_TEST(test_queue_struct);
|
||||||
|
|
||||||
RUN_TEST(test_queue2_num);
|
RUN_TEST(test_queue2_num);
|
||||||
RUN_TEST(test_queue2_fifo_num);
|
RUN_TEST(test_queue2_struct);
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,257 @@
|
|||||||
*/
|
*/
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
|
|
||||||
|
static void test_stack_init(void)
|
||||||
|
{
|
||||||
|
struct _stack stack;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(stack_init(NULL, sizeof(int)));
|
||||||
|
TEST_ASSERT_FALSE(stack_init(&stack, 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(stack_init(&stack, sizeof(int)));
|
||||||
|
stack.destory(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(stack_init2(NULL, sizeof(int), 1));
|
||||||
|
TEST_ASSERT_FALSE(stack_init2(&stack, 0, 1));
|
||||||
|
TEST_ASSERT_FALSE(stack_init2(&stack, sizeof(int), 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(stack_init2(&stack, sizeof(int), 1));
|
||||||
|
stack.destory(&stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_stack_new(void)
|
||||||
|
{
|
||||||
|
stack_t stack = NULL;
|
||||||
|
stack = stack_new();
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(stack_init(NULL, sizeof(int)));
|
||||||
|
TEST_ASSERT_FALSE(stack_init(stack, 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(stack_init(stack, sizeof(int)));
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
|
#ifdef NDEBUG
|
||||||
|
TEST_ASSERT_FALSE(stack_init2(NULL, sizeof(int), 1));
|
||||||
|
TEST_ASSERT_FALSE(stack_init2(stack, 0, 1));
|
||||||
|
TEST_ASSERT_FALSE(stack_init2(stack, sizeof(int), 0));
|
||||||
|
#endif
|
||||||
|
TEST_ASSERT_TRUE(stack_init2(stack, sizeof(int), 1));
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
stack_free(&stack); // stack_free(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_stack_push(void)
|
||||||
|
{
|
||||||
|
int temp = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init(stack, sizeof(int));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
}
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init2(stack, sizeof(int), len);
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
}
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
// if capacity is less than data len
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init2(stack, sizeof(int), len - 2);
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
}
|
||||||
|
stack_free(&stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_stack_pop(void)
|
||||||
|
{
|
||||||
|
int temp = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init(stack, sizeof(int));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
stack->push(stack, &data[i]);
|
||||||
|
}
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init2(stack, sizeof(int), len);
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
stack->push(stack, &data[i]);
|
||||||
|
}
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
// if capacity is less than data len
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init2(stack, sizeof(int), len - 2);
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
}
|
||||||
|
uint32_t capacity = stack->capacity(stack);
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stack_free(&stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_stack_clear(void)
|
||||||
|
{
|
||||||
|
int temp = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init(stack, sizeof(int));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
stack->push(stack, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
stack->push(stack, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
stack_free(&stack);
|
||||||
|
|
||||||
|
// ------------------------------
|
||||||
|
stack = stack_new();
|
||||||
|
stack_init2(stack, sizeof(int), len);
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
stack->push(stack, &data[i]);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
stack_free(&stack);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_stack_num(void)
|
static void test_stack_num(void)
|
||||||
{
|
{
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
@ -19,34 +270,240 @@ static void test_stack_num(void)
|
|||||||
|
|
||||||
stack_t stack = NULL;
|
stack_t stack = NULL;
|
||||||
stack = stack_new();
|
stack = stack_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
TEST_ASSERT_TRUE(stack_init(stack, sizeof(int)));
|
TEST_ASSERT_TRUE(stack_init(stack, sizeof(int)));
|
||||||
stack->print_obj = print_num;
|
stack->print_obj = print_num;
|
||||||
|
|
||||||
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
}
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
stack->push(stack, &data[i]);
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
stack->peek(stack, &temp);
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < len + 1; i++)
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
{
|
{
|
||||||
if (true == stack->pop(stack, &temp))
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
{
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
if(false != stack->peek(stack, &temp))
|
|
||||||
{
|
|
||||||
TEST_ASSERT_EQUAL(data[len - 2 - i], temp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TEST_ASSERT_TRUE(stack->empty(stack));
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
stack_free(&stack);
|
stack_free(&stack);
|
||||||
TEST_ASSERT_NULL(stack);
|
TEST_ASSERT_NULL(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_stack_struct(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
struct _student data[] = {
|
||||||
|
{"zhao", 1001}, {"qian", 1002}, {"sun", 1003}, {"li", 1004},
|
||||||
|
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
||||||
|
};
|
||||||
|
struct _student temp;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
|
||||||
|
|
||||||
|
stack_t stack = stack_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
|
stack_init(stack, sizeof(struct _student));
|
||||||
|
stack->print_obj = print_struct;
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[i].name, temp.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[stack->size(stack) - 1].name, temp.name);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[stack->size(stack) - 1].name, temp.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
stack_free(&stack);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_stack2_num(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
int data[] = { 1,2,3,4,5,6,7,8,9,10 };
|
||||||
|
int temp = 0;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]);
|
||||||
|
uint32_t capacity = len;
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
stack = stack_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack_init2(stack, sizeof(int), capacity));
|
||||||
|
stack->print_obj = print_num;
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[i], temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1], temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
|
||||||
|
stack_free(&stack);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_stack2_struct(void)
|
||||||
|
{
|
||||||
|
uint32_t i = 0;
|
||||||
|
struct _student data[] = {
|
||||||
|
{"zhao", 1001}, {"qian", 1002}, {"sun", 1003}, {"li", 1004},
|
||||||
|
"zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008,
|
||||||
|
};
|
||||||
|
struct _student temp;
|
||||||
|
uint32_t len = sizeof(data) / sizeof(data[0]) - 1;
|
||||||
|
uint32_t capacity = len - 2;
|
||||||
|
|
||||||
|
stack_t stack = NULL;
|
||||||
|
stack = stack_new();
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack_init2(stack, sizeof(struct _student), capacity));
|
||||||
|
stack->print_obj = print_struct;
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[stack->size(stack) - 1].name, temp.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(stack->empty(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->clear(stack));
|
||||||
|
TEST_ASSERT_TRUE(stack->empty(stack));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->push(stack, &data[i]));
|
||||||
|
TEST_ASSERT_EQUAL_INT(i + 1, stack->size(stack));
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->pop(stack, &temp));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(stack->pop(stack, &temp));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stack->empty(stack))
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(stack->peek(stack, &temp));
|
||||||
|
TEST_ASSERT_EQUAL_INT(data[stack->size(stack) - 1].id, temp.id);
|
||||||
|
TEST_ASSERT_EQUAL_STRING(data[stack->size(stack) - 1].name, temp.name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TEST_ASSERT_FALSE(stack->peek(stack, &temp));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stack_free(&stack);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
void test_stack(void)
|
void test_stack(void)
|
||||||
{
|
{
|
||||||
RUN_TEST(test_stack_num);
|
RUN_TEST(test_stack_num);
|
||||||
|
|
||||||
|
RUN_TEST(test_stack_init);
|
||||||
|
RUN_TEST(test_stack_new);
|
||||||
|
RUN_TEST(test_stack_push);
|
||||||
|
RUN_TEST(test_stack_pop);
|
||||||
|
RUN_TEST(test_stack_clear);
|
||||||
|
|
||||||
|
RUN_TEST(test_stack_num);
|
||||||
|
RUN_TEST(test_stack_struct);
|
||||||
|
|
||||||
|
RUN_TEST(test_stack2_num);
|
||||||
|
RUN_TEST(test_stack2_struct);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user