From 1a32ab4009853be740f61c73a480d5617d67472c Mon Sep 17 00:00:00 2001
From: Liam Waldron <liamwaldron@everestlinux.org>
Date: Tue, 18 Mar 2025 14:59:44 -0400
Subject: [PATCH] remove queue data structure and begin to implement basic
 chroot script for testing

---
 Makefile           |   6 +-
 build_in_chroot.sh |  27 ++++++++
 include/data.h     | 151 ---------------------------------------------
 src/libglacier.c   | 140 +----------------------------------------
 tests/unit-tests.c |  44 ++++++++++---
 5 files changed, 69 insertions(+), 299 deletions(-)
 create mode 100644 build_in_chroot.sh

diff --git a/Makefile b/Makefile
index f1464e4..8e38ac2 100644
--- a/Makefile
+++ b/Makefile
@@ -35,7 +35,7 @@ lib: $(SDIR)/libglacier.c
 	$(CC) $(SDIR)/libglacier.c -c $(LIBFLAGS) -o $(BDIR)/lib/libglacier.o
 	$(AR) -rc $(BDIR)/lib/libglacier.a build/lib/libglacier.o
 
-check: $(BDIR) $(TDIR)/unit-tests.c
+check: lib $(BDIR) $(TDIR)/unit-tests.c
 	$(CC) tests/unit-tests.c -o tests/test-suite -lcunit -Wall -Wextra build/lib/libglacier.a $(LIBFLAGS)
 	tests/test-suite
 
@@ -63,7 +63,7 @@ install: install_lib install_head
 	@echo "[INFO]"
 
 clean:
-	rm -rf $(BDIR)
+	rm -rf $(BDIR) ./chroot
 
 distclean:
-	rm -rf $(BDIR) $(TDIR)/test-suite
+	rm -rf $(BDIR) $(TDIR)/test-suite ./chroot
diff --git a/build_in_chroot.sh b/build_in_chroot.sh
new file mode 100644
index 0000000..a303916
--- /dev/null
+++ b/build_in_chroot.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+#
+# 	build_in_chroot.sh - Build libglacier and install it in a simple chroot
+#
+# 	This file is part of Glacier.
+#
+# 	Glacier is free software: you can redistribute it and/or modify it under the terms of the
+# 	GNU General Public License as published by the Free Software Foundation, either
+# 	version 3 of the License, or (at your option) any later version.
+#
+# 	Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+# 	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# 	PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# 	You should have received a copy of the GNU General Public License along with Glacier. If
+# 	not, see <https://www.gnu.org/licenses/>.
+#
+
+init_chroot() {
+	mkdir -v ./chroot
+	cd ./chroot
+	git clone https://gitlab.com/buildroot.org/buildroot.git
+	cd buildroot
+}
+
+init_chroot "$@"
diff --git a/include/data.h b/include/data.h
index 55ff195..44e74f0 100644
--- a/include/data.h
+++ b/include/data.h
@@ -74,155 +74,4 @@ void add_child(struct node *parent, struct node *child);
 
 void print_tree(struct node *root, int level);
 
-/**************************************************************************************************************/
-
-/*
- * queue
- *
- * DESCRIPTION: Queue is a type definition for the queue data structure.
- */
-
-typedef struct queue;
-
-/**************************************************************************************************************/
-
-/*
- * init_queue
- *
- * DESCRIPTION: Init_queue initializes a queue data structue.
- * PARAMETERS:
- * 		queue *q -> The name of the queue to initialize
- * RETURN VALUES:
- * 		None.
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		init_queue("operation_queue");
- */
-
-void init_queue(queue *q);
-
-/**************************************************************************************************************/
-
-/*
- * queue_is_empty
- *
- * DESCRIPTION: Queue_is_empty checks if queue *q is empty.
- * PARAMETERS:
- * 		queue *q -> The queue to check
- * RETURN VALUES:
- * 		true on empty, false on not empty
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		if (queue_is_empty(q)) {
- * 			printf("Queue is empty\n");
- * 			return;
- * 		}
- */
-
-bool queue_is_empty(queue *q);
-
-/**************************************************************************************************************/
-
-/*
- * queue_is_full
- *
- * DESCRIPTION: Queue_is_full checks if queue *q is full.
- * PARAMETERS:
- * 		queue *q -> The queue to check
- * RETURN VALUES:
- * 		true on full, false on not full
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		if (queue_is_full(q)) {
- * 			printf("Queue is full\n");
- * 			return;
- * 		}
- */
-
-bool queue_is_full(queue *q);
-
-/**************************************************************************************************************/
-
-/*
- * enqueue
- *
- * DESCRIPTION: Enqueue enqueues an element at the back of the queue.
- * PARAMETERS:
- * 		queue *q -> The queue to enqueue to
- * 		int value -> The value of the element to enqueue
- * RETURN VALUES:
- * 		None.
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		if (! queue_is_full(q)) {
- * 			enqueue(q, 15);
- * 		}
- */
-
-void enqueue(queue *q, int value);
-
-/**************************************************************************************************************/
-
-/*
- * dequeue
- *
- * DESCRIPTION: Dequeue dequeues an element at the front of the queue.
- * PARAMETERS:
- * 		queue *q -> The queue to dequeue to
- * RETURN VALUES:
- * 		None.
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		if (! queue_is_empty(q)) {
- * 			dequeue(q);
- * 		}
- */
-
-void dequeue(queue *q);
-
-/**************************************************************************************************************/
-
-/*
- * peek
- *
- * DESCRIPTION: Peek displays the first element in a specified queue.
- * PARAMETERS:
- * 		queue *q -> The queue to peek at
- * RETURN VALUES:
- * 		-1 on queue is empty, element value (!= -1) on success
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		if (! queue_is_full(q)) {
- * 			peek(q);
- * 		}
- */
-
-int peek(queue *q);
-
-/**************************************************************************************************************/
-
-/*
- * print_queue
- *
- * DESCRIPTION: Print_queue displays the contents of the queue in order.
- * PARAMETERS:
- * 		queue *q -> The queue to print
- * RETURN VALUES:
- * 		None.
- * CAVEATS:
- * 		None.
- * EXAMPLE:
- * 		if (! queue_is_full(q)) {
- * 			print_queue(q);
- * 		}
- */
-
-void print_queue(queue *q);
-
 #endif
diff --git a/src/libglacier.c b/src/libglacier.c
index 5a01c46..a57ab4c 100644
--- a/src/libglacier.c
+++ b/src/libglacier.c
@@ -88,6 +88,8 @@ struct node {
 	char *data;
 	struct node *children[MAX_CHILDREN];
 	int numChildren;
+	struct node *left;
+	struct node *right;
 };
 
 /*
@@ -384,144 +386,6 @@ print_tree(struct node *root, int level)
 	}
 }
 
-/*
- * queue
- *
- * DESCRIPTION: Definition of queue type.
- * DEFINED IN: data.h
- *
- */
-
-typedef struct {
-	int items[MAX_SIZE];
-	int front;
-	int rear;
-} queue;
-
-/*
-* init_queue
-*
-* DESCRIPTION: Initialize a queue.
-* PARAMETERS: queue *q
-* DEFINED IN: data.h
-*
-*/
-
-void
-init_queue(queue *q)
-{
-	q -> front = -1;
-	q -> rear = 0;
-}
-
-/*
-* queue_is_empty
-*
-* DESCRIPTION: Check if queue is empty.
-* PARAMETERS: struct node *root, int level
-* DEFINED IN: data.h
-*
-*/
-
-bool queue_is_empty(queue *q) { return (q -> front == q -> rear -1); }
-
-/*
-* queue_is_full
-*
-* DESCRIPTION: Check if queue is full.
-* PARAMETERS: queue *q
-* DEFINED IN: data.h
-*
-*/
-
-bool queue_is_full(queue *q) { return (q -> rear == MAX_SIZE); }
-
-/*
-* enqueue
-*
-* DESCRIPTION: Enqueue an element at the back of the queue.
-* PARAMETERS: queue *q, int value
-* DEFINED IN: data.h
-*
-*/
-
-void
-enqueue (queue *q, int value)
-{
-	if (queue_is_full(q)) {
-		printf("Queue is full\n");
-		return;
-	}
-	
-	q -> items[q -> rear] = value;
-	q -> rear++;
-}
-
-/*
-* dequeue
-*
-* DESCRIPTION: Dequeue the element at the front of the queue.
-* PARAMETERS: queue *q, int value
-* DEFINED IN: data.h
-*
-*/
-
-void
-dequeue(queue *q)
-{
-	if (queue_is_empty(q)) {
-		printf("Queue is empty\n");
-		return;
-	}
-
-	q -> front++;
-}
-
-/*
-* peek
-*
-* DESCRIPTION: View the element at the front of the queue.
-* PARAMETERS: struct node *root, int level
-* DEFINED IN: data.h
-*
-*/
-
-int
-peek(queue *q)
-{
-	if (queue_is_empty(q)) {
-		printf("Queue is empty\n");
-		return -1;
-	}
-
-	return q -> items[q -> front + 1];
-}
-
-/*
-* print_queue
-*
-* DESCRIPTION: Print the queue.
-* PARAMETERS: queue *q
-* DEFINED IN: data.h
-*
-*/
-
-void
-print_queue(queue *q)
-{
-	if (queue_is_empty(q)) {
-		printf("Queue is empty\n");
-		return;
-	}
-
-	printf("Current Queue: ");
-	
-	for (int i = q -> front + 1; i < q -> rear; i++) {
-		printf("%d ", q -> items[i]);
-	}
-	printf("\n");
-}
-
 /*
  * mkworkspace
  *
diff --git a/tests/unit-tests.c b/tests/unit-tests.c
index c1c9b5d..ecf88c7 100644
--- a/tests/unit-tests.c
+++ b/tests/unit-tests.c
@@ -30,17 +30,22 @@
 void
 test_is_process_root(void)
 {
-	CU_ASSERT(is_process_root() == 0);
+	if (is_process_root() != 0) {
+		CU_FAIL("is_process_root() with UID != 0 failed");
+	}
+	else if (is_process_root() == 0) {
+		CU_PASS("is_process_root() with UID != 0 passed");
+	}
+}
 
-	uid_t saved_uid = getuid();
-	setuid(0);
-	CU_ASSERT(is_process_root() == 1);
-
-	setuid((int)saved_uid);
+void
+test_init_config(void)
+{
+	CU_ASSERT_TRUE(init_config());
 }
 
 int
-main()
+main(void)
 {
 	if (CUE_SUCCESS != CU_initialize_registry()) {
 		return CU_get_error();
@@ -48,6 +53,31 @@ main()
 
 	CU_basic_set_mode(CU_BRM_VERBOSE);
 
+	CU_pSuite runtime_tests = CU_add_suite("Runtime Functions Suite", NULL, NULL);
+	if (! runtime_tests) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+
+	CU_pSuite config_tests = CU_add_suite("Configuration Functions Suite", NULL, NULL);
+	if (! config_tests) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+
+	if (! CU_add_test(runtime_tests, "test of is_process_root()", test_is_process_root)) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+
+	if (! CU_add_test(config_tests, "test of init_config()", test_init_config)) {
+		CU_cleanup_registry();
+		return CU_get_error();
+	}
+
+
+
 	CU_basic_run_tests();
+	CU_cleanup_registry();
 	return CU_get_error();
 }