Compare commits

..

7 Commits

Author SHA1 Message Date
baa683d970 use assert instead of ambiuous return code 2026-07-12 18:09:46 -05:00
96e1a8e0d6 add string type 2026-07-10 04:19:48 -05:00
34b49d6090 delete array after test completion 2026-07-10 04:17:51 -05:00
da792dda8a add array swap 2026-06-14 07:41:14 -05:00
a09faf4d2d rename array to larray 2026-06-14 07:28:55 -05:00
5b46fc25a1 I need to start being patient 2026-06-14 07:28:40 -05:00
c1303e4ed7 remove version file 2026-06-14 07:27:17 -05:00
9 changed files with 121 additions and 30 deletions

View File

@@ -1,2 +1,2 @@
CompileFlags:
Add: [-xc, -DL_ARRAY_IMPLEMENTATION]
Add: [-xc, -DL_ARRAY_IMPLEMENTATION, -DL_STRING_IMPLEMENTATION]

View File

@@ -8,6 +8,7 @@ BUILD_DIR := build
TEST_BUILD_DIR := $(BUILD_DIR)/test
TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \
$(TEST_BUILD_DIR)/array.o \
$(TEST_BUILD_DIR)/string.o \
INCLUDE_INSTALL_PATH := /usr/local/include/LTypes

View File

@@ -1 +0,0 @@
To use any single header file, either include `version.h` or remove the include from the top.

View File

@@ -1,7 +1,6 @@
#ifndef LTYPES_ARRAY_H
#define LTYPES_ARRAY_H
#include "version.h"
#include <stddef.h>
typedef struct {
@@ -13,6 +12,7 @@ typedef struct {
LArray* l_array_create();
void l_array_delete(LArray* array);
int l_array_append(LArray* array, void* item);
int l_array_swap(LArray* array, int indexA, int indexB);
void l_array_clear(LArray* array);
#ifdef L_ARRAY_IMPLEMENTATION
@@ -39,6 +39,16 @@ int l_array_append(LArray* array, void* item) {
return 0;
}
int l_array_swap(LArray* array, int indexA, int indexB) {
if (array->count < (size_t)indexA || array->count < (size_t)indexB || indexA == indexB) return 1;
void* itemA = array->items[indexA];
array->items[indexA] = array->items[indexB];
array->items[indexB] = itemA;
return 0;
}
void l_array_clear(LArray* array) {
free(array->items);
array->items = NULL;

60
include/lstring.h Normal file
View File

@@ -0,0 +1,60 @@
#ifndef LTYPES_STRING_H
#define LTYPES_STRING_H
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STRING_LEN 1024
typedef struct {
char* buf;
size_t len;
} LString;
LString* l_string_new(const char* fmt, ...);
void l_string_delete(LString* str);
int l_string_cmp(LString* strA, LString* strB);
LString* l_string_concat(LString* strA, LString* strB);
#ifdef L_STRING_IMPLEMENTATION
#include <stdarg.h>
LString* l_string_new(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char temp_string[MAX_STRING_LEN];
vsnprintf(temp_string, MAX_STRING_LEN, fmt, args);
va_end(args);
LString* str = calloc(1, sizeof(LString));
str->len = strlen(temp_string);
str->buf = malloc(str->len + 1);
if (str->buf != NULL) {
strcpy(str->buf, temp_string);
}
return str;
}
void l_string_delete(LString* str) {
free(str);
}
int l_string_cmp(LString* strA, LString* strB) {
int num_chars = (strA->len <= strB->len) ? strB->len : strA->len;
return strncmp(strA->buf, strB->buf, num_chars);
}
LString* l_string_concat(LString* strA, LString* strB) {
return l_string_new("%.*s%.*s", strA->len, strA->buf, strB->len, strB->buf);
}
#endif
#endif

View File

@@ -1,9 +0,0 @@
#ifndef LTYPES_VERSION_H
#define LTYPES_VERSION_H
#define L_TYPES_AUTHOR "Lucielle <luci@git.lunarware.tech"
#define L_TYPES_REPO "https://git.lunarware.tech/lucielle/ltypes.git"
#define L_TYPES_VERSION "0.1.0"
#define L_TYPES_LICENSE "GNU GPL v3"
#endif

View File

@@ -1,11 +1,8 @@
#define L_ARRAY_IMPLEMENTATION
#include "../include/array.h"
#include <stdio.h>
#include "../include/larray.h"
#include <assert.h>
int test_l_array() {
int RETURN_CODE = 0;
printf("test_l_array() : ");
LArray* array = l_array_create();
int number_1 = 5;
@@ -14,14 +11,18 @@ int test_l_array() {
l_array_append(array, &number_2);
l_array_append(array, &number_1);
if (*(int*)array->items[0] != 15) RETURN_CODE++;
if (*(int*)array->items[1] != 5) RETURN_CODE++;
if (array->count != 2) RETURN_CODE++;
if (array->capacity != 8) RETURN_CODE++;
assert(*(int*)array->items[0] == 15);
assert(*(int*)array->items[1] == 5);
assert(array->count == 2);
assert(array->capacity == 8);
l_array_swap(array, 0, 1);
assert(*(int*)array->items[0] == 5);
assert(*(int*)array->items[1] == 15);
l_array_clear(array);
if (array->items != NULL) RETURN_CODE++;
assert(array->items == NULL);
printf("%d\n", RETURN_CODE);
return RETURN_CODE;
l_array_delete(array);
return 0;
}

25
test/string.c Normal file
View File

@@ -0,0 +1,25 @@
#define L_STRING_IMPLEMENTATION
#include "../include/lstring.h"
#include <assert.h>
int test_l_string() {
int integer_0 = 25;
int integer_1 = 15;
LString* test_string_0 = l_string_new("I would like to purchase item id `%d`", integer_0);
LString* test_string_1 = l_string_new(" for `%d` coins.", integer_1);
assert(!l_string_cmp(test_string_0, l_string_new("I would like to purchase item id `25`")));
assert(!l_string_cmp(test_string_1, l_string_new(" for `15` coins.")));
LString* test_string_2 = l_string_new("I would like to purchase item id `25` for `15` coins.");
LString* test_string_3 = l_string_concat(test_string_0, test_string_1);
assert(!l_string_cmp(test_string_2, test_string_3));
l_string_delete(test_string_0);
l_string_delete(test_string_1);
l_string_delete(test_string_2);
l_string_delete(test_string_3);
return 0;
}

View File

@@ -1,18 +1,22 @@
#include <stdio.h>
#include "../include/version.h"
#define L_TYPES_AUTHOR "Lucielle <luci@git.lunarware.tech"
#define L_TYPES_REPO "https://git.lunarware.tech/lucielle/ltypes.git"
#define L_TYPES_VERSION "0.2.0"
#define L_TYPES_LICENSE "GNU GPL v3"
int test_l_array();
int test_l_string();
int main(void) {
printf("AUTHOR: %s\n", L_TYPES_AUTHOR);
printf("REPO: %s\n", L_TYPES_REPO);
printf("VERSION: %s\n", L_TYPES_VERSION);
printf("LICENSE: %s\n\n", L_TYPES_LICENSE);
int RETURN_CODE = 0;
RETURN_CODE += test_l_array();
test_l_array();
test_l_string();
printf("\nError Total: %d\n", RETURN_CODE);
return RETURN_CODE;
printf("Test Completed Succefully.\n");
return 0;
}