Added simple testing capabilities

This commit is contained in:
kilroy
2025-11-28 19:38:18 -05:00
parent b7ce065002
commit a7a2759053
3 changed files with 52 additions and 35 deletions

View File

@@ -30,8 +30,13 @@ $(OBJS): dir
@$(CC) $(CFLAGS) -o $(BUILD_DIR)/$@ -c $*.c @$(CC) $(CFLAGS) -o $(BUILD_DIR)/$@ -c $*.c
# Runs CUnit tests # Runs CUnit tests
# test: dir
# @$(CC) $(CFLAGS) -lcunit -o $(BIN_DIR)/$(NAME)_test $(TESTS_DIR)/*.c
# @$(BIN_DIR)/$(NAME)_test
# Runs tests
test: dir test: dir
@$(CC) $(CFLAGS) -lcunit -o $(BIN_DIR)/$(NAME)_test $(TESTS_DIR)/*.c @$(CC) $(CFLAGS) -o $(BIN_DIR)/$(NAME)_test $(TESTS_DIR)/*.c
@$(BIN_DIR)/$(NAME)_test @$(BIN_DIR)/$(NAME)_test
# Run valgrind memory checker on executable # Run valgrind memory checker on executable

View File

@@ -5,41 +5,8 @@
int main(void) int main(void)
{ {
// C-style to string string a = StrLit("Hello, world!");
string a;
a.chars = "Manual transformation of C-style to String";
a.length = strlen(a.chars);
PrintStr(a); PrintStr(a);
// Automatic c-style to string
string b = CStyleToLengthString_Assign("Function transformation of C-style to String");
PrintStr(b);
// String assigning (shallow copy) and copying (deep copy)
char xx[] = "String Assign Copy";
char yy[] = "String Deep Copy";
string x = CStyleToLengthString_Assign(xx);
string y = CStyleToLengthString_Assign(yy);
string c = x;
PrintStr(c);
string d = StringCopy(y);
PrintStr(d);
x.chars[3] = '!';
y.chars[3] = '!';
PrintStr(c);
PrintStr(d);
// Manual string slicing
string e;
e.chars = &a.chars[7];
e.length = 14;
PrintStr(e);
// Automatic string slicing (by length or by index range)
string f = StringSlice_Length(a, 7, 14);
string g = StringSlice_Index(a, 7, 21);
PrintStr(f);
PrintStr(g);
return 0; return 0;
} }

45
tests/main.c Normal file
View File

@@ -0,0 +1,45 @@
#include "../include/lenstr.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
// C-style to string
string a;
a.chars = "Manual transformation of C-style to String";
a.length = strlen(a.chars);
PrintStr(a);
// Automatic c-style to string
string b = CStyleToLengthString_Assign("Function transformation of C-style to String");
PrintStr(b);
// String assigning (shallow copy) and copying (deep copy)
char xx[] = "String Assign Copy";
char yy[] = "String Deep Copy";
string x = CStyleToLengthString_Assign(xx);
string y = CStyleToLengthString_Assign(yy);
string c = x;
PrintStr(c);
string d = StringCopy(y);
PrintStr(d);
x.chars[3] = '!';
y.chars[3] = '!';
PrintStr(c);
PrintStr(d);
// Manual string slicing
string e;
e.chars = &a.chars[7];
e.length = 14;
PrintStr(e);
// Automatic string slicing (by length or by index range)
string f = StringSlice_Length(a, 7, 14);
string g = StringSlice_Index(a, 7, 21);
PrintStr(f);
PrintStr(g);
return 0;
}