Compare commits
11 Commits
40bab5490e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
baa683d970
|
|||
|
96e1a8e0d6
|
|||
|
34b49d6090
|
|||
|
da792dda8a
|
|||
|
a09faf4d2d
|
|||
|
5b46fc25a1
|
|||
|
c1303e4ed7
|
|||
|
5f93db596e
|
|||
|
7350a8cdfc
|
|||
|
84025f52cc
|
|||
|
d523097f2e
|
2
.clangd
2
.clangd
@@ -1,2 +1,2 @@
|
||||
CompileFlags:
|
||||
Add: [-xc]
|
||||
Add: [-xc, -DL_ARRAY_IMPLEMENTATION, -DL_STRING_IMPLEMENTATION]
|
||||
35
Makefile
Normal file
35
Makefile
Normal file
@@ -0,0 +1,35 @@
|
||||
CC = gcc
|
||||
SHELL := /bin/bash
|
||||
|
||||
INCLUDE := include
|
||||
CFLAGS = -Wall -Wextra -I$(INCLUDE) -ggdb
|
||||
|
||||
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
|
||||
|
||||
all: test
|
||||
|
||||
test: $(TEST_BUILD_FILES)
|
||||
$(CC) $(CFLAGS) -I./test/include -o $(TEST_BUILD_DIR)/test $(TEST_BUILD_FILES)
|
||||
@echo ""
|
||||
$(TEST_BUILD_DIR)/test
|
||||
|
||||
$(TEST_BUILD_DIR)/%.o: test/%.c | $(TEST_BUILD_DIR)
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(TEST_BUILD_DIR):
|
||||
mkdir -p $(TEST_BUILD_DIR)
|
||||
|
||||
$(INCLUDE_INSTALL_PATH):
|
||||
mkdir -p $(INCLUDE_INSTALL_PATH)
|
||||
|
||||
install: $(INCLUDE_INSTALL_PATH)
|
||||
cp $(INCLUDE)/* $(INCLUDE_INSTALL_PATH)
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)
|
||||
56
array.h
56
array.h
@@ -1,56 +0,0 @@
|
||||
#ifndef LTYPES_ARRAY_H
|
||||
#define LTYPES_ARRAY_H
|
||||
#define L_ARRAY_IMPLEMENTATION // for development to fix clangd
|
||||
|
||||
#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"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
void** items;
|
||||
} l_array;
|
||||
|
||||
l_array* l_array_create();
|
||||
void l_array_delete(l_array* array);
|
||||
int l_array_append(l_array* array, void* item);
|
||||
void l_array_clear(l_array* array);
|
||||
|
||||
#ifdef L_ARRAY_IMPLEMENTATION
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
l_array* l_array_create() {
|
||||
return (l_array*) calloc(1, sizeof(l_array));
|
||||
}
|
||||
|
||||
void l_array_delete(l_array* array) {
|
||||
free(array);
|
||||
}
|
||||
|
||||
int l_array_append(l_array* array, void* item) {
|
||||
if (array->count >= array->capacity) {
|
||||
if (array->capacity == 0) array->capacity = 8;
|
||||
else array->capacity *= 2;
|
||||
|
||||
array->items = realloc(array->items, array->capacity * sizeof(*array->items));
|
||||
}
|
||||
|
||||
array->items[array->count++] = item;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void l_array_clear(l_array* array) {
|
||||
free(array->items);
|
||||
array->items = NULL;
|
||||
array->capacity = 0;
|
||||
array->count = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
61
include/larray.h
Normal file
61
include/larray.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef LTYPES_ARRAY_H
|
||||
#define LTYPES_ARRAY_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
void** items;
|
||||
} LArray;
|
||||
|
||||
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
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
LArray* l_array_create() {
|
||||
return (LArray*) calloc(1, sizeof(LArray));
|
||||
}
|
||||
|
||||
void l_array_delete(LArray* array) {
|
||||
free(array);
|
||||
}
|
||||
|
||||
int l_array_append(LArray* array, void* item) {
|
||||
if (array->count >= array->capacity) {
|
||||
if (array->capacity == 0) array->capacity = 8;
|
||||
else array->capacity *= 2;
|
||||
|
||||
array->items = realloc(array->items, array->capacity * sizeof(*array->items));
|
||||
}
|
||||
|
||||
array->items[array->count++] = 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;
|
||||
array->capacity = 0;
|
||||
array->count = 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
60
include/lstring.h
Normal file
60
include/lstring.h
Normal 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
|
||||
41
test.c
41
test.c
@@ -1,41 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#define L_ARRAY_IMPLEMENTATION
|
||||
#include "array.h"
|
||||
#include "assert.h"
|
||||
|
||||
#define TEST_FUNCTION
|
||||
|
||||
static int test_l_array() {
|
||||
int RETURN_CODE = 0;
|
||||
|
||||
l_array* array = l_array_create();
|
||||
|
||||
int number_1 = 5;
|
||||
int number_2 = 15;
|
||||
|
||||
l_array_append(array, &number_2);
|
||||
l_array_append(array, &number_1);
|
||||
|
||||
assert(*(int*)array->items[0] == 15);
|
||||
assert(*(int*)array->items[1] == 5);
|
||||
assert(array->count == 2);
|
||||
assert(array->capacity == 8);
|
||||
|
||||
l_array_clear(array);
|
||||
assert(array->items == NULL);
|
||||
|
||||
return RETURN_CODE;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
if (
|
||||
test_l_array()
|
||||
) return 1;
|
||||
return 0;
|
||||
}
|
||||
28
test/array.c
Normal file
28
test/array.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#define L_ARRAY_IMPLEMENTATION
|
||||
#include "../include/larray.h"
|
||||
#include <assert.h>
|
||||
|
||||
int test_l_array() {
|
||||
LArray* array = l_array_create();
|
||||
|
||||
int number_1 = 5;
|
||||
int number_2 = 15;
|
||||
|
||||
l_array_append(array, &number_2);
|
||||
l_array_append(array, &number_1);
|
||||
|
||||
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);
|
||||
assert(array->items == NULL);
|
||||
|
||||
l_array_delete(array);
|
||||
return 0;
|
||||
}
|
||||
25
test/string.c
Normal file
25
test/string.c
Normal 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;
|
||||
}
|
||||
22
test/test.c
Normal file
22
test/test.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.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);
|
||||
|
||||
test_l_array();
|
||||
test_l_string();
|
||||
|
||||
printf("Test Completed Succefully.\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user