Commit 8bf3295
Changed files (18)
src
src/01/01a/main.c
@@ -1,9 +1,8 @@
+#include "priority_queue.h"
#include <stdio.h>
#include <stdlib.h>
-#include "priority_queue.h"
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
printf("=== COMP-272 - Assignment 1 - Question 1a ===\n");
PriorityQueue *queue = initialize();
src/01/01a/priority_queue.c
@@ -33,9 +33,7 @@ static Node *create_node(int priority, int data) {
* @param self The queue to investigate
* @return The total number of items stored in the queue.
*/
-int size(PriorityQueue *self) {
- return self->size;
-}
+int size(PriorityQueue *self) { return self->size; }
/**
* Compares two integers and returns:
@@ -129,7 +127,7 @@ void inspect(PriorityQueue *self) {
Node *tmp = self->head;
printf("Items (%d): [ ", size(self));
- while(tmp) {
+ while (tmp) {
printf("(%d,%d) ", tmp->priority, tmp->data);
tmp = tmp->next;
}
@@ -146,10 +144,11 @@ void destroy(PriorityQueue *self) {
Node *current = self->head;
Node *tmp;
- while(current) {
+ while (current) {
tmp = current, current = current->next;
- if (tmp) free(tmp);
+ if (tmp)
+ free(tmp);
}
free(self);
}
src/01/01a/priority_queue_test.c
@@ -1,10 +1,10 @@
+#include "priority_queue.h"
#include <cgreen/cgreen.h>
#include <string.h>
-#include "priority_queue.h"
Describe(PriorityQueue);
-BeforeEach(PriorityQueue){ }
-AfterEach(PriorityQueue){ }
+BeforeEach(PriorityQueue) {}
+AfterEach(PriorityQueue) {}
Ensure(PriorityQueue, returns_size) {
PriorityQueue *queue = initialize();
@@ -24,7 +24,7 @@ Ensure(PriorityQueue, adds_a_node) {
destroy(queue);
}
-Ensure(PriorityQueue, removes_the_node_with_the_lowest_priority){
+Ensure(PriorityQueue, removes_the_node_with_the_lowest_priority) {
PriorityQueue *queue = initialize();
add(queue, 3, 300);
@@ -59,7 +59,8 @@ Ensure(PriorityQueue, when_removing_it_decreases_the_size) {
destroy(queue);
}
-Ensure(PriorityQueue, when_removing_the_last_node_it_decrements_the_count_correctly) {
+Ensure(PriorityQueue,
+ when_removing_the_last_node_it_decrements_the_count_correctly) {
PriorityQueue *queue = initialize();
add(queue, 2, 200);
@@ -92,7 +93,9 @@ Ensure(PriorityQueue, when_adding_data_out_of_order) {
destroy(queue);
}
-Ensure(PriorityQueue, when_adding_random_values_with_random_priority_it_returns_the_minimum_priority_value_correctly) {
+Ensure(
+ PriorityQueue,
+ when_adding_random_values_with_random_priority_it_returns_the_minimum_priority_value_correctly) {
PriorityQueue *queue = initialize();
int n = 10;
@@ -110,13 +113,20 @@ TestSuite *priority_queue_tests() {
TestSuite *suite = create_test_suite();
add_test_with_context(suite, PriorityQueue, adds_a_node);
- add_test_with_context(suite, PriorityQueue, removes_the_node_with_the_lowest_priority);
+ add_test_with_context(suite, PriorityQueue,
+ removes_the_node_with_the_lowest_priority);
add_test_with_context(suite, PriorityQueue, returns_size);
add_test_with_context(suite, PriorityQueue, when_adding_data_out_of_order);
- add_test_with_context(suite, PriorityQueue, when_adding_random_values_with_random_priority_it_returns_the_minimum_priority_value_correctly);
- add_test_with_context(suite, PriorityQueue, when_removing_it_decreases_the_size);
- add_test_with_context(suite, PriorityQueue, when_removing_node_from_empty_queue);
- add_test_with_context(suite, PriorityQueue, when_removing_the_last_node_it_decrements_the_count_correctly);
+ add_test_with_context(
+ suite, PriorityQueue,
+ when_adding_random_values_with_random_priority_it_returns_the_minimum_priority_value_correctly);
+ add_test_with_context(suite, PriorityQueue,
+ when_removing_it_decreases_the_size);
+ add_test_with_context(suite, PriorityQueue,
+ when_removing_node_from_empty_queue);
+ add_test_with_context(
+ suite, PriorityQueue,
+ when_removing_the_last_node_it_decrements_the_count_correctly);
return suite;
}
src/01/01b/main.c
@@ -2,8 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
printf("=== COMP-272 - Assignment 1 - Question 1b ===\n");
Stack *stack = initialize();
src/01/01b/stack.c
@@ -9,7 +9,7 @@ static int MAX_SIZE = 2147483647;
*
* @return A Queue struct
*/
-static Queue *newQueue(void){
+static Queue *newQueue(void) {
Queue *queue = malloc(sizeof(Queue));
queue->size = 0;
queue->head = NULL;
@@ -68,7 +68,8 @@ void enqueue(Queue *self, int data) {
* @return The data for the next it to remove from the Queue
*/
int dequeue(Queue *self) {
- if (self->head == NULL) return -1;
+ if (self->head == NULL)
+ return -1;
Node *node = self->head;
int data = node->data;
@@ -118,9 +119,7 @@ int pop(Stack *self) {
* @param self The stack to investigate
* @return The count of items on the stack.
*/
-int size(Stack *self) {
- return self->q1->size;
-}
+int size(Stack *self) { return self->q1->size; }
/**
* The equivalent of a destructor to free any memory associated with a Stack.
src/01/01b/stack_test.c
@@ -3,8 +3,8 @@
#include <math.h>
Describe(Stack);
-BeforeEach(Stack){ }
-AfterEach(Stack){ }
+BeforeEach(Stack) {}
+AfterEach(Stack) {}
Ensure(Stack, push_onto_stack) {
Stack *stack = initialize();
src/01/02a/main.c
@@ -2,12 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
-int next(void) {
- return rand() % 100;
-}
+int next(void) { return rand() % 100; }
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
printf("=== COMP-272 - Assignment 1 - Question 2a ===\n");
Node *head = initialize(next());
@@ -17,9 +14,9 @@ int main(int argc, char *argv[])
printf("\n\t");
inspect(head);
- for (int i = 0; i < 10; i+=2){
- swap(&head, i, i+1);
- printf("swap: %d,%d\n\t", i, i+1);
+ for (int i = 0; i < 10; i += 2) {
+ swap(&head, i, i + 1);
+ printf("swap: %d,%d\n\t", i, i + 1);
inspect(head);
}
src/01/02a/singly_linked_list.c
@@ -25,7 +25,7 @@ Node *add(Node *head, int data) {
Node *tail;
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
if (!tmp->next)
break;
tmp = tmp->next;
@@ -43,9 +43,10 @@ Node *add(Node *head, int data) {
* @return The node at the specific index
*/
Node *get(Node *self, int index) {
- if (!self || index < 0) return NULL;
+ if (!self || index < 0)
+ return NULL;
- while(index > 0 && self){
+ while (index > 0 && self) {
self = self->next;
index--;
}
@@ -60,7 +61,8 @@ Node *get(Node *self, int index) {
*/
static int size(Node *head) {
int i = 0;
- for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++;
+ for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next)
+ i++;
return i;
}
@@ -75,9 +77,12 @@ static int size(Node *head) {
void swap(Node **head, int x, int y) {
int count = size(*head);
- if (x == y) return;
- if (x >= count) return;
- if (y >= count) return;
+ if (x == y)
+ return;
+ if (x >= count)
+ return;
+ if (y >= count)
+ return;
Node *xp = get(*head, x - 1);
Node *yp = get(*head, y - 1);
@@ -106,10 +111,11 @@ void swap(Node **head, int x, int y) {
* @param self The head of the linked list
*/
void inspect(Node *self) {
- if (!self) return;
+ if (!self)
+ return;
printf("[");
- while(self) {
+ while (self) {
printf(" %d ", self->data);
self = self->next;
}
src/01/02a/singly_linked_list_test.c
@@ -2,8 +2,8 @@
#include <cgreen/cgreen.h>
Describe(SinglyLinkedList);
-BeforeEach(SinglyLinkedList){ }
-AfterEach(SinglyLinkedList){ }
+BeforeEach(SinglyLinkedList) {}
+AfterEach(SinglyLinkedList) {}
Ensure(SinglyLinkedList, when_getting_head) {
Node *head = initialize(100);
@@ -54,7 +54,6 @@ Ensure(SinglyLinkedList, when_getting_index_out_of_range) {
free(head);
}
-
Ensure(SinglyLinkedList, when_swapping_head) {
Node *head = initialize(100);
@@ -177,14 +176,16 @@ TestSuite *swap_singly_linked_list_tests() {
add_test_with_context(suite, SinglyLinkedList, when_getting_tail);
add_test_with_context(suite, SinglyLinkedList, when_getting_from_empty_list);
add_test_with_context(suite, SinglyLinkedList, when_getting_negative_index);
- add_test_with_context(suite, SinglyLinkedList, when_getting_index_out_of_range);
+ add_test_with_context(suite, SinglyLinkedList,
+ when_getting_index_out_of_range);
add_test_with_context(suite, SinglyLinkedList, when_swapping_head);
add_test_with_context(suite, SinglyLinkedList, when_swapping_y_head);
add_test_with_context(suite, SinglyLinkedList, when_swapping_mid);
add_test_with_context(suite, SinglyLinkedList, when_swapping_y_mid);
add_test_with_context(suite, SinglyLinkedList, when_swapping_tail);
- add_test_with_context(suite, SinglyLinkedList, when_swapping_index_out_of_range);
+ add_test_with_context(suite, SinglyLinkedList,
+ when_swapping_index_out_of_range);
add_test_with_context(suite, SinglyLinkedList, when_swapping_self);
return suite;
src/01/02b/doubly_linked_list.c
@@ -14,7 +14,7 @@ Node *add(Node *head, int data) {
Node *tail;
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
if (!tmp->next)
break;
tmp = tmp->next;
@@ -26,9 +26,10 @@ Node *add(Node *head, int data) {
}
Node *get(Node *from, int index) {
- if (!from || index < 0) return NULL;
+ if (!from || index < 0)
+ return NULL;
- while(index > 0 && from){
+ while (index > 0 && from) {
from = from->next;
index--;
}
@@ -37,7 +38,8 @@ Node *get(Node *from, int index) {
static int size(Node *head) {
int i = 0;
- for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++;
+ for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next)
+ i++;
return i;
}
@@ -58,9 +60,12 @@ static void assign_prev(Node *self, Node *other) {
}
void swap(Node *x, Node *y) {
- if (x == y) return;
- if (!x || !y) return;
- if (x->prev == y && y->next == x) return swap(y, x);
+ if (x == y)
+ return;
+ if (!x || !y)
+ return;
+ if (x->prev == y && y->next == x)
+ return swap(y, x);
Node *xp = x->prev, *xn = x->next, *yp = y->prev, *yn = y->next;
@@ -100,7 +105,8 @@ static void print(Node *node) {
}
void inspect(Node *node) {
- if (!node) return;
+ if (!node)
+ return;
printf("[ ");
while (node) {
@@ -110,4 +116,3 @@ void inspect(Node *node) {
}
printf("]\n");
}
-
src/01/02b/doubly_linked_list_test.c
@@ -1,9 +1,9 @@
-#include <cgreen/cgreen.h>
#include "doubly_linked_list.h"
+#include <cgreen/cgreen.h>
Describe(DoublyLinkedList);
-BeforeEach(DoublyLinkedList){ }
-AfterEach(DoublyLinkedList){ }
+BeforeEach(DoublyLinkedList) {}
+AfterEach(DoublyLinkedList) {}
Ensure(DoublyLinkedList, when_getting_head) {
Node *head = initialize(100);
@@ -440,20 +440,27 @@ TestSuite *swap_doubly_linked_list_tests() {
add_test_with_context(suite, DoublyLinkedList, when_getting_tail);
add_test_with_context(suite, DoublyLinkedList, when_getting_from_empty_list);
add_test_with_context(suite, DoublyLinkedList, when_getting_negative_index);
- add_test_with_context(suite, DoublyLinkedList, when_getting_index_out_of_range);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_getting_index_out_of_range);
add_test_with_context(suite, DoublyLinkedList, when_swapping_head_adjacent);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_head_adjacent_inverted);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_head_with_non_adjacent_node);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_head_with_non_adjacent_node_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_head_adjacent_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_head_with_non_adjacent_node);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_head_with_non_adjacent_node_inverted);
add_test_with_context(suite, DoublyLinkedList, when_swapping_mid);
add_test_with_context(suite, DoublyLinkedList, when_swapping_y_mid);
add_test_with_context(suite, DoublyLinkedList, when_swapping_mid_adjacent);
add_test_with_context(suite, DoublyLinkedList, when_swapping_mid_adjacent_y);
add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_adjacent);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_adjacent_inverted);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_with_non_adjacent_node);
- add_test_with_context(suite, DoublyLinkedList, when_swapping_tail_with_non_adjacent_node_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_tail_adjacent_inverted);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_tail_with_non_adjacent_node);
+ add_test_with_context(suite, DoublyLinkedList,
+ when_swapping_tail_with_non_adjacent_node_inverted);
add_test_with_context(suite, DoublyLinkedList, when_swapping_with_NULL);
add_test_with_context(suite, DoublyLinkedList, when_swapping_self);
src/01/02b/main.c
@@ -2,12 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
-int next(void) {
- return rand() % 100;
-}
+int next(void) { return rand() % 100; }
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
printf("=== COMP-272 - Assignment 1 - Question 2b ===\n");
Node *head = initialize(next());
Node *new_head = NULL;
@@ -24,7 +21,7 @@ int main(int argc, char *argv[])
printf("swap: 0,1\n\t");
inspect(head);
- for (int i = 2; i < 10; i+=2) {
+ for (int i = 2; i < 10; i += 2) {
swap(get(head, i), get(head, i + 1));
printf("swap: %d,%d\n\t", i, i + 1);
inspect(head);
src/01/05/doubly_linked_list.c
@@ -14,7 +14,7 @@ Node *add(Node *head, int data) {
Node *tail;
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
if (!tmp->next)
break;
tmp = tmp->next;
@@ -26,9 +26,10 @@ Node *add(Node *head, int data) {
}
Node *get(Node *from, int index) {
- if (!from || index < 0) return NULL;
+ if (!from || index < 0)
+ return NULL;
- while(index > 0 && from){
+ while (index > 0 && from) {
from = from->next;
index--;
}
@@ -37,7 +38,8 @@ Node *get(Node *from, int index) {
static int size(Node *head) {
int i = 0;
- for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next) i++;
+ for (Node *tmp = head; tmp && tmp != NULL; tmp = tmp->next)
+ i++;
return i;
}
@@ -80,7 +82,8 @@ static void print(Node *node) {
}
void inspect(Node *node) {
- if (!node) return;
+ if (!node)
+ return;
printf("[ ");
while (node) {
@@ -90,4 +93,3 @@ void inspect(Node *node) {
}
printf("]\n");
}
-
src/01/05/doubly_linked_list_test.c
@@ -1,9 +1,9 @@
-#include <cgreen/cgreen.h>
#include "doubly_linked_list.h"
+#include <cgreen/cgreen.h>
Describe(DoublyLinkedList);
-BeforeEach(DoublyLinkedList){ }
-AfterEach(DoublyLinkedList){ }
+BeforeEach(DoublyLinkedList) {}
+AfterEach(DoublyLinkedList) {}
Ensure(DoublyLinkedList, when_reversing_a_short_list) {
Node *head = initialize(100);
src/01/05/main.c
@@ -2,12 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
-int next(void) {
- return rand() % 100;
-}
+int next(void) { return rand() % 100; }
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
printf("=== COMP-272 - Assignment 1 - Question 5 ===\n");
Node *head = initialize(0);
src/01/06/main.c
@@ -2,8 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
-int main(int argc, char *argv[])
-{
+int main(int argc, char *argv[]) {
Stack *stack = initialize();
printf("=== COMP-272 - Assignment 1 - Question 6 ===\n");
@@ -18,7 +17,7 @@ int main(int argc, char *argv[])
printf("Popping:\n==========\n");
inspect(stack);
- while(size(stack) > 0) {
+ while (size(stack) > 0) {
printf("Pop: %d, Min: %d\n", pop(stack), min(stack));
inspect(stack);
}
src/01/06/min_stack.c
@@ -1,8 +1,8 @@
+#include "min_stack.h"
#include <stdio.h>
#include <stdlib.h>
-#include "min_stack.h"
-static Node *new(int data, Node *next) {
+static Node *new (int data, Node *next) {
Node *node = malloc(sizeof(Node));
node->next = next;
node->data = data;
@@ -17,36 +17,34 @@ Stack *initialize(void) {
return self;
}
-int size(Stack *self) {
- return self->size;
-}
+int size(Stack *self) { return self->size; }
void push(Stack *self, int data) {
if (!self->min || (data < self->min->data))
- self->min = new(data, self->min);
+ self->min = new (data, self->min);
- self->head = new(data, self->head);
+ self->head = new (data, self->head);
self->size++;
}
void each(Node *head, Visitor block) {
Node *tmp = head;
- while(tmp) {
+ while (tmp) {
(*block)(tmp);
tmp = tmp->next;
}
}
int min(Stack *self) {
- if(self->min)
+ if (self->min)
return self->min->data;
if (self->head) {
int min = self->head->data;
Node *tmp = self->head->next;
- while(tmp) {
+ while (tmp) {
if (tmp->data < min)
min = tmp->data;
tmp = tmp->next;
@@ -72,9 +70,7 @@ int pop(Stack *self) {
return data;
}
-void print_node(Node *node) {
- printf("[%d]", node->data);
-}
+void print_node(Node *node) { printf("[%d]", node->data); }
void inspect(Stack *stack) {
printf("\t");
src/01/06/min_stack_test.c
@@ -1,9 +1,9 @@
-#include <cgreen/cgreen.h>
#include "min_stack.h"
+#include <cgreen/cgreen.h>
Describe(MinStack);
-BeforeEach(MinStack){ }
-AfterEach(MinStack){ }
+BeforeEach(MinStack) {}
+AfterEach(MinStack) {}
Ensure(MinStack, when_empty) {
Stack *stack = initialize();
@@ -51,7 +51,9 @@ Ensure(MinStack, when_a_single_item_is_on_the_stack_it_is_the_min) {
free(stack);
}
-Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item) {
+Ensure(
+ MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item) {
Stack *stack = initialize();
push(stack, 200);
@@ -61,7 +63,9 @@ Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_ret
free(stack);
}
-Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero) {
+Ensure(
+ MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero) {
Stack *stack = initialize();
push(stack, 200);
@@ -72,7 +76,9 @@ Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_ret
free(stack);
}
-Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null) {
+Ensure(
+ MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null) {
Stack *stack = initialize();
push(stack, 200);
@@ -159,15 +165,24 @@ TestSuite *min_stack_tests() {
add_test_with_context(suite, MinStack, when_empty_it_has_a_size_of_zero);
add_test_with_context(suite, MinStack, when_empty_it_has_a_min_of_null);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_it_is_the_min);
-
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero);
- add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null);
+ add_test_with_context(
+ suite, MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one);
+ add_test_with_context(suite, MinStack,
+ when_a_single_item_is_on_the_stack_it_is_the_min);
+
+ add_test_with_context(
+ suite, MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item);
+ add_test_with_context(
+ suite, MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero);
+ add_test_with_context(
+ suite, MinStack,
+ when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null);
add_test_with_context(suite, MinStack, when_pushing_a_single_integer);
- add_test_with_context(suite, MinStack, when_pushing_multiple_integers_out_of_order);
+ add_test_with_context(suite, MinStack,
+ when_pushing_multiple_integers_out_of_order);
return suite;
}