Skip to content

Commit

Permalink
Expanded array tests, read more
Browse files Browse the repository at this point in the history
Getting the array's length is still not available yet, so I'm not
marking arrays as done - but everything that is there is tested.

I've also tweaked the assert output callbacks to also print 'assert failure'.
  • Loading branch information
Ratstail91 committed Dec 9, 2024
1 parent 61a105d commit 1a36c14
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 153 deletions.
17 changes: 13 additions & 4 deletions repl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,21 @@ static void printCallback(const char* msg) {
}

static void errorAndExitCallback(const char* msg) {
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "Error: %s\n", msg);
exit(-1);
}

static void errorAndContinueCallback(const char* msg) {
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "Error: %s\n", msg);
}

static void assertFailureAndExitCallback(const char* msg) {
fprintf(stderr, "Assert Failure: %s\n", msg);
exit(-1);
}

static void assertFailureAndContinueCallback(const char* msg) {
fprintf(stderr, "Assert Failure: %s\n", msg);
}

static void noOpCallback(const char* msg) {
Expand Down Expand Up @@ -263,7 +272,7 @@ int repl(const char* filepath) {
//output options
Toy_setPrintCallback(printCallback);
Toy_setErrorCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(assertFailureAndContinueCallback);

//vars to use
char prompt[256];
Expand Down Expand Up @@ -472,7 +481,7 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
int main(int argc, const char* argv[]) {
Toy_setPrintCallback(printCallback);
Toy_setErrorCallback(errorAndExitCallback);
Toy_setAssertFailureCallback(errorAndExitCallback);
Toy_setAssertFailureCallback(assertFailureAndExitCallback);

//if there's args, process them
CmdLine cmd = parseCmdLine(argc, argv);
Expand Down
5 changes: 0 additions & 5 deletions scripts/a.toy

This file was deleted.

10 changes: 0 additions & 10 deletions scripts/big_if_true.toy

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/brace_yourself.toy

This file was deleted.

10 changes: 0 additions & 10 deletions scripts/concat.toy

This file was deleted.

67 changes: 0 additions & 67 deletions scripts/cond.toy

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/count.toy

This file was deleted.

10 changes: 4 additions & 6 deletions scripts/fizzbuzz.toy
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


//moment of truth
//standard example
var counter: int = 1;

while (counter <= 100) {
Expand All @@ -15,11 +13,11 @@ while (counter <= 100) {
}

//finally
if (result == "") {
print counter;
if (result != "") {
print result;
}
else {
print result;
print counter;
}

counter += 1;
Expand Down
9 changes: 0 additions & 9 deletions scripts/looping.toy

This file was deleted.

13 changes: 13 additions & 0 deletions scripts/odd_and_even.toy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
//if and while work together
var count = 1;
while (count <= 10) {
if (count % 2 == 0) {
print "even";
}
else {
print "odd";
}
count += 1;
}
}
4 changes: 2 additions & 2 deletions source/toy_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);

//quick free
#ifndef TOY_ARRAY_FREE
#define TOY_ARRAY_FREE(array) Toy_resizeArray(array, 0)
#define TOY_ARRAY_FREE(array) (array = Toy_resizeArray(array, 0))
#endif

//one line to expand the array
Expand All @@ -38,7 +38,7 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);

//quick push back
#ifndef TOY_ARRAY_PUSHBACK
#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array),(array)->data[(array)->count++] = (value))
#define TOY_ARRAY_PUSHBACK(array, value) (TOY_ARRAY_EXPAND(array), (array)->data[(array)->count++] = (value))
#endif

//URGENT: get array length in scripts (dot operator?)
Expand Down
8 changes: 6 additions & 2 deletions source/toy_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ static void errDefault(const char* msg) {
fprintf(stderr, "%s", msg);
}

static void assertDefault(const char* msg) {
fprintf(stderr, "%s", msg);
}

static Toy_callbackType printCallback = outDefault;
static Toy_callbackType errorCallback = errDefault;
static Toy_callbackType assertCallback = errDefault;
static Toy_callbackType assertCallback = assertDefault;

void Toy_print(const char* msg) {
printCallback(msg);
Expand Down Expand Up @@ -47,5 +51,5 @@ void Toy_resetErrorCallback() {
}

void Toy_resetAssertFailureCallback() {
assertCallback = errDefault;
assertCallback = assertDefault;
}
2 changes: 1 addition & 1 deletion source/toy_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ bool Toy_checkValuesAreEqual(Toy_Value left, Toy_Value right) {

for (unsigned int i = 0; i < leftArray->count; i++) {
//any mismatch is an easy difference
if (Toy_checkValuesAreEqual(leftArray->data[i], rightArray->data[i])) {
if (Toy_checkValuesAreEqual(leftArray->data[i], rightArray->data[i]) != true) {
return false;
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/cases/test_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
int test_array() {
//test allocation and free
{
Toy_Array* array = Toy_resizeArray(NULL, 1);
array = Toy_resizeArray(array, 0);
Toy_Array* array = TOY_ARRAY_ALLOCATE();
TOY_ARRAY_FREE(array);
}

//test initial data
{
Toy_Array* array = Toy_resizeArray(NULL, 10);
Toy_Array* array = TOY_ARRAY_ALLOCATE();

//check you can access the memory
array->data[1] = TOY_VALUE_FROM_INTEGER(42);

Toy_resizeArray(array, 0);
TOY_ARRAY_FREE(array);
}

//test multiple arrays (no overlaps or conflicts)
{
Toy_Array* array1 = Toy_resizeArray(NULL, 10);
Toy_Array* array2 = Toy_resizeArray(NULL, 10);
Toy_Array* array1 = TOY_ARRAY_ALLOCATE();
Toy_Array* array2 = TOY_ARRAY_ALLOCATE();

array1->data[1] = TOY_VALUE_FROM_INTEGER(42);
array2->data[1] = TOY_VALUE_FROM_INTEGER(42);

Toy_resizeArray(array1, 0);
Toy_resizeArray(array2, 0);
TOY_ARRAY_FREE(array1);
TOY_ARRAY_FREE(array2);
}

return 0;
Expand Down
Loading

0 comments on commit 1a36c14

Please sign in to comment.