Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 746b9b7219 | |||
| 1f3fba80ec | |||
| 1d363980f6 | |||
| 4ce3a6aac0 |
Binary file not shown.
100
bintree.c
100
bintree.c
@ -1,128 +1,36 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
// TODO: binären Suchbaum implementieren
|
//TODO: binären Suchbaum implementieren
|
||||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||||
{
|
{
|
||||||
if (isDuplicate)
|
|
||||||
*isDuplicate = 0;
|
|
||||||
|
|
||||||
if (root == NULL)
|
|
||||||
{
|
|
||||||
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
|
|
||||||
if (!newNode)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
newNode->data = malloc(dataSize);
|
|
||||||
if (!newNode->data)
|
|
||||||
{
|
|
||||||
free(newNode);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(newNode->data, data, dataSize);
|
|
||||||
newNode->left = NULL;
|
|
||||||
newNode->right = NULL;
|
|
||||||
return newNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
int cmp = compareFct(data, root->data);
|
|
||||||
|
|
||||||
if (cmp < 0)
|
|
||||||
{
|
|
||||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
|
||||||
}
|
|
||||||
else if (cmp > 0)
|
|
||||||
{
|
|
||||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
if (isDuplicate)
|
|
||||||
{
|
|
||||||
*isDuplicate = 1; // ignorieren
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Duplikate erlaubt -> nach rechts
|
|
||||||
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return root;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
||||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
||||||
// push the top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
static StackNode *iterator = NULL;
|
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
if (root != NULL)
|
|
||||||
{
|
|
||||||
clearStack(iterator);
|
|
||||||
iterator = NULL;
|
|
||||||
|
|
||||||
// Root + alle linken Nachfolger pushen
|
|
||||||
TreeNode *current = root;
|
|
||||||
while (current != NULL)
|
|
||||||
{
|
|
||||||
iterator = push(iterator, current);
|
|
||||||
current = current->left;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wenn Stack leer -> fertig
|
|
||||||
if (iterator == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Top-Node holen
|
|
||||||
TreeNode *node = (TreeNode *)top(iterator);
|
|
||||||
iterator = pop(iterator);
|
|
||||||
|
|
||||||
// rechten Teilbaum + linke Kette pushen
|
|
||||||
TreeNode *right = node->right;
|
|
||||||
while (right != NULL)
|
|
||||||
{
|
|
||||||
iterator = push(iterator, right);
|
|
||||||
right = right->left;
|
|
||||||
}
|
|
||||||
|
|
||||||
return node->data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
if (root != NULL)
|
|
||||||
{
|
|
||||||
clearTree(root->left);
|
|
||||||
clearTree(root->right);
|
|
||||||
|
|
||||||
free(root->data);
|
|
||||||
free(root);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *root)
|
||||||
{
|
{
|
||||||
if (root == NULL)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return 1 + treeSize(root->left) + treeSize(root->right);
|
|
||||||
}
|
}
|
||||||
Binary file not shown.
@ -1,4 +1 @@
|
|||||||
player_name;5967
|
player1;3999
|
||||||
player_name;4698
|
|
||||||
player_name;2994
|
|
||||||
jan;2993
|
|
||||||
|
|||||||
36
makefile
36
makefile
@ -29,47 +29,21 @@ program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
|
|||||||
doble : main.o $(program_obj_files)
|
doble : main.o $(program_obj_files)
|
||||||
$(CC) $(FLAGS) $^ -o doble
|
$(CC) $(FLAGS) $^ -o doble
|
||||||
|
|
||||||
$(program_obj_filesobj_files): %.o: %.c
|
$(program_obj_files): %.o: %.c
|
||||||
$(CC) -c $(FLAGS) $^ -o $@
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests: test_stack test_tree test_numbers
|
unitTests:
|
||||||
test_stack: test_stack.o stack.o unity/unity.c
|
echo "needs to be implemented"
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) $^ -o test_stack
|
|
||||||
|
|
||||||
test_tree: test_tree.o bintree.o stack.o unity/unity.c
|
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) $^ -o test_tree
|
|
||||||
|
|
||||||
test_numbers: test_numbers.o numbers.o bintree.o stack.o unity/unity.c
|
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) $^ -o test_numbers
|
|
||||||
|
|
||||||
test_stack.o: test_stack.c
|
|
||||||
$(CC) -c $(FLAGS) -I$(unityfolder) $< -o $@
|
|
||||||
|
|
||||||
stack.o: stack.c
|
|
||||||
$(CC) -c $(FLAGS) $< -o $@
|
|
||||||
|
|
||||||
test_tree.o: test_tree.c
|
|
||||||
$(CC) -c $(FLAGS) -I$(unityfolder) $< -o $@
|
|
||||||
|
|
||||||
test_numbers.o: test_numbers.c
|
|
||||||
$(CC) -c $(FLAGS) -I$(unityfolder) $< -o $@
|
|
||||||
|
|
||||||
bintree.o: bintree.c
|
|
||||||
$(CC) -c $(FLAGS) $< -o $@
|
|
||||||
|
|
||||||
numbers.o: numbers.c
|
|
||||||
$(CC) -c $(FLAGS) $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
del /f *.o doble $(TEST_BIN).exe
|
del /f *.o doble
|
||||||
else
|
else
|
||||||
rm -f *.o doble $(TEST_BIN)
|
rm -f *.o doble
|
||||||
endif
|
endif
|
||||||
96
numbers.c
96
numbers.c
@ -5,108 +5,22 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
// TODO: getDuplicate und createNumbers implementieren
|
//TODO: getDuplicate und createNumbers implementieren
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
||||||
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
||||||
* Duplizieren eines zufälligen Eintrags im Array.
|
* Duplizieren eines zufälligen Eintrags im Array.
|
||||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
||||||
|
|
||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
|
|
||||||
static void duplicateRandomEntry(unsigned int *numbers, unsigned int len)
|
|
||||||
{
|
|
||||||
if (!numbers || len < 2)
|
|
||||||
return;
|
|
||||||
|
|
||||||
unsigned int src = rand() % len;
|
|
||||||
unsigned int dst = rand() % len;
|
|
||||||
|
|
||||||
while (dst == src)
|
|
||||||
dst = rand() % len;
|
|
||||||
|
|
||||||
numbers[dst] = numbers[src];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int compare(const void *a, const void *b)
|
|
||||||
{
|
|
||||||
unsigned int x = *(const unsigned int *)a;
|
|
||||||
unsigned int y = *(const unsigned int *)b;
|
|
||||||
return (x > y) - (x < y);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
if (len < 2)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
|
|
||||||
if (!numbers)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
static int seeded = 0;
|
|
||||||
if (!seeded) {
|
|
||||||
srand((unsigned)time(NULL));
|
|
||||||
seeded = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
TreeNode *root = NULL;
|
|
||||||
unsigned int i = 0;
|
|
||||||
|
|
||||||
while (i < len)
|
|
||||||
{
|
|
||||||
unsigned int val = (rand() % (2 * len)) + 1;
|
|
||||||
int isDup = 0;
|
|
||||||
|
|
||||||
root = addToTree(root, &val, sizeof(unsigned int), compare, &isDup);
|
|
||||||
if (!root)
|
|
||||||
{
|
|
||||||
free(numbers); // malloc-Fehler
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isDup)
|
|
||||||
numbers[i++] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
duplicateRandomEntry(numbers, len);
|
|
||||||
clearTree(root);
|
|
||||||
return numbers;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
if (!numbers || len < 2)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *copy = (unsigned int *)malloc(len * sizeof(unsigned int));
|
}
|
||||||
if (!copy)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(copy, numbers, len * sizeof(unsigned int));
|
|
||||||
qsort(copy, len, sizeof(unsigned int), compare);
|
|
||||||
|
|
||||||
unsigned int dup = 0;
|
|
||||||
for (unsigned int i = 1; i < len; i++)
|
|
||||||
{
|
|
||||||
if (copy[i] == copy[i - 1])
|
|
||||||
{
|
|
||||||
dup = copy[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(copy);
|
|
||||||
return dup;
|
|
||||||
}
|
|
||||||
29
stack.c
29
stack.c
@ -10,53 +10,24 @@
|
|||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
StackNode *newNode = malloc(sizeof(StackNode));
|
|
||||||
if(newNode == NULL)
|
|
||||||
{
|
|
||||||
return stack; //Speicher konnte nicht allokiert werden.
|
|
||||||
}
|
|
||||||
|
|
||||||
newNode->data = data;
|
|
||||||
newNode->next = stack;
|
|
||||||
|
|
||||||
return newNode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if (stack == NULL)
|
|
||||||
{
|
|
||||||
return NULL; // stack leer nichts zu löschen
|
|
||||||
}
|
|
||||||
|
|
||||||
StackNode *next = stack->next;
|
|
||||||
|
|
||||||
free(stack);
|
|
||||||
return next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
if(stack == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return stack->data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
while (stack != NULL)
|
|
||||||
{
|
|
||||||
StackNode *next = stack->next;
|
|
||||||
free(stack);
|
|
||||||
|
|
||||||
stack = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
5
stack.h
5
stack.h
@ -9,11 +9,6 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
|
|
||||||
typedef struct StackNode {
|
|
||||||
void *data;
|
|
||||||
struct StackNode* next;
|
|
||||||
} StackNode;
|
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode *push(StackNode *stack, void *data);
|
||||||
|
|
||||||
|
|||||||
@ -1,72 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include "numbers.h"
|
|
||||||
#include "unity.h"
|
|
||||||
#include "unity_internals.h"
|
|
||||||
|
|
||||||
void test_createNumbers_no_null(void)
|
|
||||||
{
|
|
||||||
unsigned int *arr = createNumbers(10);
|
|
||||||
TEST_ASSERT_NOT_NULL(arr);
|
|
||||||
free(arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_createNumbers_has_exactly_one_duplicate(void)
|
|
||||||
{
|
|
||||||
unsigned int len = 100;
|
|
||||||
unsigned int *arr = createNumbers(len);
|
|
||||||
TEST_ASSERT_NOT_NULL(arr);
|
|
||||||
|
|
||||||
unsigned int dup = getDuplicate(arr, len);
|
|
||||||
TEST_ASSERT_NOT_EQUAL(0, dup);
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
for (unsigned int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (arr[i] == dup)
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL(2, count);
|
|
||||||
|
|
||||||
free(arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_createNumbers_has_correct_value_range(void)
|
|
||||||
{
|
|
||||||
unsigned int len = 10;
|
|
||||||
unsigned int *arr = createNumbers(len);
|
|
||||||
TEST_ASSERT_NOT_NULL(arr);
|
|
||||||
|
|
||||||
// passendes Interval
|
|
||||||
for (unsigned int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
TEST_ASSERT_TRUE(arr[i] >= 1);
|
|
||||||
TEST_ASSERT_TRUE(arr[i] <= 2 * len);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_getDuplicate_returns_correct_value(void)
|
|
||||||
{
|
|
||||||
unsigned int arr[6] = {4, 1, 7, 7, 3, 2};
|
|
||||||
unsigned int d = getDuplicate(arr, 6);
|
|
||||||
TEST_ASSERT_EQUAL_UINT(7, d);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUp(void) { }
|
|
||||||
void tearDown(void) { }
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
|
|
||||||
printf("\n============================\n Numbers tests\n============================\n");
|
|
||||||
|
|
||||||
RUN_TEST(test_createNumbers_no_null);
|
|
||||||
RUN_TEST(test_createNumbers_has_exactly_one_duplicate);
|
|
||||||
RUN_TEST(test_createNumbers_has_correct_value_range);
|
|
||||||
RUN_TEST(test_getDuplicate_returns_correct_value);
|
|
||||||
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
BIN
test_numbers.exe
BIN
test_numbers.exe
Binary file not shown.
155
test_stack.c
155
test_stack.c
@ -1,155 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "unity.h"
|
|
||||||
#include "stack.h"
|
|
||||||
|
|
||||||
// Hilfsfunktion: int dynamisch anlegen
|
|
||||||
static int *makeInt(int value)
|
|
||||||
{
|
|
||||||
int *p = (int *)malloc(sizeof(int));
|
|
||||||
if (p != NULL)
|
|
||||||
{
|
|
||||||
*p = value;
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_pushAndTopReturnLastPushed(void)
|
|
||||||
{
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
int *v1 = makeInt(10);
|
|
||||||
int *v2 = makeInt(20);
|
|
||||||
int *v3 = makeInt(30);
|
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(v1);
|
|
||||||
TEST_ASSERT_NOT_NULL(v2);
|
|
||||||
TEST_ASSERT_NOT_NULL(v3);
|
|
||||||
|
|
||||||
stack = push(stack, v1);
|
|
||||||
TEST_ASSERT_NOT_NULL(stack);
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v1, top(stack));
|
|
||||||
TEST_ASSERT_EQUAL_INT(10, *(int *)top(stack));
|
|
||||||
|
|
||||||
stack = push(stack, v2);
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v2, top(stack));
|
|
||||||
TEST_ASSERT_EQUAL_INT(20, *(int *)top(stack));
|
|
||||||
|
|
||||||
stack = push(stack, v3);
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v3, top(stack));
|
|
||||||
TEST_ASSERT_EQUAL_INT(30, *(int *)top(stack));
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v3, top(stack));
|
|
||||||
free(v3);
|
|
||||||
stack = pop(stack);
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v2, top(stack));
|
|
||||||
free(v2);
|
|
||||||
stack = pop(stack);
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v1, top(stack));
|
|
||||||
free(v1);
|
|
||||||
stack = pop(stack);
|
|
||||||
|
|
||||||
TEST_ASSERT_NULL(stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_popOnEmptyStackReturnsNull(void)
|
|
||||||
{
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
StackNode *newStack = pop(stack);
|
|
||||||
TEST_ASSERT_NULL(newStack);
|
|
||||||
|
|
||||||
TEST_ASSERT_NULL(top(newStack));
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_pushAndPopLifoOrder(void)
|
|
||||||
{
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
int *v1 = makeInt(1);
|
|
||||||
int *v2 = makeInt(2);
|
|
||||||
int *v3 = makeInt(3);
|
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(v1);
|
|
||||||
TEST_ASSERT_NOT_NULL(v2);
|
|
||||||
TEST_ASSERT_NOT_NULL(v3);
|
|
||||||
|
|
||||||
stack = push(stack, v1);
|
|
||||||
stack = push(stack, v2);
|
|
||||||
stack = push(stack, v3);
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v3, top(stack));
|
|
||||||
TEST_ASSERT_EQUAL_INT(3, *(int *)top(stack));
|
|
||||||
free(v3);
|
|
||||||
stack = pop(stack);
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v2, top(stack));
|
|
||||||
TEST_ASSERT_EQUAL_INT(2, *(int *)top(stack));
|
|
||||||
free(v2);
|
|
||||||
stack = pop(stack);
|
|
||||||
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_PTR(v1, top(stack));
|
|
||||||
TEST_ASSERT_EQUAL_INT(1, *(int *)top(stack));
|
|
||||||
free(v1);
|
|
||||||
stack = pop(stack);
|
|
||||||
|
|
||||||
TEST_ASSERT_NULL(stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_topOnEmptyStackReturnsNull(void)
|
|
||||||
{
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
TEST_ASSERT_NULL(top(stack));
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_clearStackFreesAllNodes(void)
|
|
||||||
{
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
int a = 11;
|
|
||||||
int b = 22;
|
|
||||||
int c = 33;
|
|
||||||
|
|
||||||
stack = push(stack, &a);
|
|
||||||
stack = push(stack, &b);
|
|
||||||
stack = push(stack, &c);
|
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(stack);
|
|
||||||
TEST_ASSERT_EQUAL_INT(33, *(int *)top(stack));
|
|
||||||
|
|
||||||
clearStack(stack);
|
|
||||||
|
|
||||||
stack = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void setUp(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void tearDown(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
|
|
||||||
printf("\n============================\nStack tests\n============================\n");
|
|
||||||
|
|
||||||
RUN_TEST(test_pushAndTopReturnLastPushed);
|
|
||||||
RUN_TEST(test_popOnEmptyStackReturnsNull);
|
|
||||||
RUN_TEST(test_pushAndPopLifoOrder);
|
|
||||||
RUN_TEST(test_topOnEmptyStackReturnsNull);
|
|
||||||
RUN_TEST(test_clearStackFreesAllNodes);
|
|
||||||
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
BIN
test_stack.exe
BIN
test_stack.exe
Binary file not shown.
BIN
test_tree.exe
BIN
test_tree.exe
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user