Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions test2/bitComparison/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CC = gcc
CFLAGS = -Iinclude -Wall -Wextra

SRC = main.c src/bitComparison.c
TEST_SRC = tests/testBitComparison.c

OUT = program
TEST_OUT = testProgram

all: $(OUT)

$(OUT): $(SRC)
$(CC) $(CFLAGS) $(SRC) -o $(OUT)

test: $(TEST_SRC) src/bitComparison.c
$(CC) $(CFLAGS) -DTEST_MAIN src/bitComparison.c $(TEST_SRC) -o $(TEST_OUT)

clean:
rm -f $(OUT) $(TEST_OUT)

format:
find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file -i

format-check:
find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file --dry-run -Werror
5 changes: 5 additions & 0 deletions test2/bitComparison/include/bitComparison.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdbool.h>

int compareBinaryArrays(const bool* arr1, int len1, const bool* arr2, int len2);
31 changes: 31 additions & 0 deletions test2/bitComparison/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "include/bitComparison.h"
#include <stdbool.h>
#include <stdio.h>

int main()
{
bool num1[] = { 0, 1, 0, 1, 1, 0, 0 };
bool num2[] = { 1, 1, 0, 0, 1, 1 };

int len1 = sizeof(num1) / sizeof(num1[0]);
int len2 = sizeof(num2) / sizeof(num2[0]);

int result = compareBinaryArrays(num1, len1, num2, len2);

printf("Number 1: ");
for (int i = 0; i < len1; ++i)
printf("%d", num1[i]);
printf("\nNumber 2: ");
for (int i = 0; i < len2; ++i)
printf("%d", num2[i]);

printf("\n\nResult: ");
if (result == 1)
printf("Number 1 > Number 2\n");
else if (result == -1)
printf("Number 1 < Number 2\n");
else
printf("Number 1 == Number 2\n");

return 0;
}
22 changes: 22 additions & 0 deletions test2/bitComparison/src/bitComparison.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "../include/bitComparison.h"

int compareBinaryArrays(const bool* arr1, int len1, const bool* arr2, int len2)
{
int i = 0, j = 0;
while (i < len1 && !arr1[i])
++i;
while (j < len2 && !arr2[j])
++j;
int realLen1 = len1 - i;
int realLen2 = len2 - j;

if (realLen1 != realLen2)
return (realLen1 > realLen2) ? 1 : -1;

for (int k = 0; k < realLen1; ++k) {
if (arr1[i + k] ^ arr2[j + k])
return arr1[i + k] ? 1 : -1;
}

return 0;
}
71 changes: 71 additions & 0 deletions test2/bitComparison/tests/testBitComparison.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "../include/bitComparison.h"
#include <stdbool.h>
#include <stdio.h>

void testGreater()
{
bool arr1[] = { 1, 0, 1 };
bool arr2[] = { 0, 1, 1 };
int result = compareBinaryArrays(arr1, 3, arr2, 3);
if (result != 1)
printf("FAIL testGreater: expected 1, got %d\n", result);
else
printf("PASS testGreater\n");
}

void testEqualWithLeadingZeros()
{
bool arr1[] = { 0, 0, 1 };
bool arr2[] = { 1 };
int result = compareBinaryArrays(arr1, 3, arr2, 1);
if (result != 0)
printf("FAIL testEqualWithLeadingZeros: expected 0, got %d\n", result);
else
printf("PASS testEqualWithLeadingZeros\n");
}

void testDifferentLengths()
{
bool arr1[] = { 1, 0, 0, 0 };
bool arr2[] = { 1, 1, 1 };
int result = compareBinaryArrays(arr1, 4, arr2, 3);
if (result != 1)
printf("FAIL testDifferentLengths: expected 1, got %d\n", result);
else
printf("PASS testDifferentLengths\n");
}

void testZeros()
{
bool arr1[] = { 0 };
bool arr2[] = { 0, 0, 0 };
int result = compareBinaryArrays(arr1, 1, arr2, 3);
if (result != 0)
printf("FAIL testZeros: expected 0, got %d\n", result);
else
printf("PASS testZeros\n");
}

void testLargeNumbers()
{
bool arr1[] = { 1, 1, 0, 1, 1, 0, 1 };
bool arr2[] = { 1, 1, 0, 1, 1, 0, 0 };
int result = compareBinaryArrays(arr1, 7, arr2, 7);
if (result != 1)
printf("FAIL testLargeNumbers: expected 1, got %d\n", result);
else
printf("PASS testLargeNumbers\n");
}

#ifdef TEST_MAIN
int main()
{
testGreater();
testEqualWithLeadingZeros();
testDifferentLengths();
testZeros();
testLargeNumbers();
printf("All tests passed\n");
return 0;
}
#endif
25 changes: 25 additions & 0 deletions test2/queue/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CC = gcc
CFLAGS = -Iinclude -Wall -Wextra

SRC = main.c src/queue.c
TEST_SRC = tests/testQueue.c

OUT = program
TEST_OUT = testProgram

all: $(OUT)

$(OUT): $(SRC)
$(CC) $(CFLAGS) $(SRC) -o $(OUT)

test: $(TEST_SRC) src/queue.c
$(CC) $(CFLAGS) -DTEST_MAIN src/queue.c $(TEST_SRC) -o $(TEST_OUT)

clean:
rm -f $(OUT) $(TEST_OUT)

format:
find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file -i

format-check:
find . -path ./build -prune -o -type f \( -path "./src/*" -o -path "./include/*" -o -path "./tests/*" -o -name "main.c" \) \( -name '*.c' -o -name '*.h' \) -print | xargs clang-format --style=file --dry-run -Werror
21 changes: 21 additions & 0 deletions test2/queue/include/queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

typedef struct List {
int value;
struct List* next;
} List;

List* listPush(int value, List* list);
List* listReverse(List* list);
void listFree(List* list);
int listIsEmpty(List* list);

typedef struct Queue {
List* front;
List* rear;
} Queue;

int queueCheck(Queue* q);
int enqueue(Queue* q, int value);
int dequeue(Queue* q, int* value);
void queueClear(Queue* q);
24 changes: 24 additions & 0 deletions test2/queue/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "include/queue.h"
#include <stdio.h>

int main()
{
Queue q = { NULL, NULL };

int values[] = { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < (int)(sizeof(values) / sizeof(values[0])); ++i) {
if (enqueue(&q, values[i]) == -1) {
printf("enqueue error\n");
queueClear(&q);
return 1;
}
}

int value;
while (dequeue(&q, &value) == 0)
printf("%d\n", value);

queueClear(&q);

return 0;
}
95 changes: 95 additions & 0 deletions test2/queue/src/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "../include/queue.h"
#include <stdlib.h>

List* listPush(int value, List* list)
{
List* newNode = (List*)malloc(sizeof(List));
if (!newNode)
return NULL;

newNode->value = value;
newNode->next = list;
return newNode;
}

List* listReverse(List* list)
{
List* result = NULL;
List* current = list;

while (current) {
List* newResult = listPush(current->value, result);
if (!newResult) {
listFree(result);
return NULL;
}
result = newResult;
current = current->next;
}
return result;
}

void listFree(List* list)
{
while (list) {
List* next = list->next;
free(list);
list = next;
}
}

int listIsEmpty(List* list)
{
return list == NULL;
}

int queueCheck(Queue* q)
{
if (listIsEmpty(q->front) && !listIsEmpty(q->rear)) {
List* newFront = listReverse(q->rear);
if (!newFront)
return -1;

listFree(q->rear);
q->rear = NULL;
q->front = newFront;
}
return 0;
}

int enqueue(Queue* q, int value)
{
List* newRear = listPush(value, q->rear);
if (!newRear)
return -1;

q->rear = newRear;

return queueCheck(q);
}

int dequeue(Queue* q, int* value)
{
if (listIsEmpty(q->front)) {
if (listIsEmpty(q->rear))
return -1;

if (queueCheck(q) == -1)
return -1;
}

*value = q->front->value;
List* oldFront = q->front;
q->front = q->front->next;
free(oldFront);

return 0;
}

void queueClear(Queue* q)
{
listFree(q->front);
listFree(q->rear);
q->front = NULL;
q->rear = NULL;
}
Loading