mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Expanded array tests, read more
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'.
This commit is contained in:
17
repl/main.c
17
repl/main.c
@@ -111,12 +111,21 @@ static void printCallback(const char* msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void errorAndExitCallback(const char* msg) {
|
static void errorAndExitCallback(const char* msg) {
|
||||||
fprintf(stderr, "%s\n", msg);
|
fprintf(stderr, "Error: %s\n", msg);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void errorAndContinueCallback(const char* msg) {
|
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) {
|
static void noOpCallback(const char* msg) {
|
||||||
@@ -263,7 +272,7 @@ int repl(const char* filepath) {
|
|||||||
//output options
|
//output options
|
||||||
Toy_setPrintCallback(printCallback);
|
Toy_setPrintCallback(printCallback);
|
||||||
Toy_setErrorCallback(errorAndContinueCallback);
|
Toy_setErrorCallback(errorAndContinueCallback);
|
||||||
Toy_setAssertFailureCallback(errorAndContinueCallback);
|
Toy_setAssertFailureCallback(assertFailureAndContinueCallback);
|
||||||
|
|
||||||
//vars to use
|
//vars to use
|
||||||
char prompt[256];
|
char prompt[256];
|
||||||
@@ -472,7 +481,7 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
|
|||||||
int main(int argc, const char* argv[]) {
|
int main(int argc, const char* argv[]) {
|
||||||
Toy_setPrintCallback(printCallback);
|
Toy_setPrintCallback(printCallback);
|
||||||
Toy_setErrorCallback(errorAndExitCallback);
|
Toy_setErrorCallback(errorAndExitCallback);
|
||||||
Toy_setAssertFailureCallback(errorAndExitCallback);
|
Toy_setAssertFailureCallback(assertFailureAndExitCallback);
|
||||||
|
|
||||||
//if there's args, process them
|
//if there's args, process them
|
||||||
CmdLine cmd = parseCmdLine(argc, argv);
|
CmdLine cmd = parseCmdLine(argc, argv);
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
var a = 0;
|
|
||||||
var b = a = a + 1, 6;
|
|
||||||
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
if (true) print "Correct"; else print "Error";
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (false) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
if (true) { print "Correct"; } else { print "Error"; }
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
print "foo" .. "bar";
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
|
|
||||||
//literals
|
|
||||||
if (true) {
|
|
||||||
print "Success 1";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Failure 1";
|
|
||||||
}
|
|
||||||
|
|
||||||
//false literals
|
|
||||||
if (false) {
|
|
||||||
print "Failure 2";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Success 2";
|
|
||||||
}
|
|
||||||
|
|
||||||
//conditionals
|
|
||||||
if (1 < 2) {
|
|
||||||
print "Success 3";
|
|
||||||
}
|
|
||||||
if (1 > 2) {
|
|
||||||
print "Failure 3";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//variables
|
|
||||||
var a = 42;
|
|
||||||
|
|
||||||
if (a) {
|
|
||||||
print "Success 4";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Failure 4";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (a == 42) {
|
|
||||||
print "Success 5";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Failure 5";
|
|
||||||
}
|
|
||||||
|
|
||||||
//concatenated strings
|
|
||||||
if ("foo" .. "bar" == "foobar") {
|
|
||||||
print "Success 6";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Failure 6";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if ("foobar" == "foo" .. "bar") {
|
|
||||||
print "Success 7";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Failure 7";
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("fizz" .. "le" == "fi" .. "zzle") {
|
|
||||||
print "Success 8";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "Failure 8";
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
//if and while works
|
|
||||||
var count = 1;
|
|
||||||
while (count <= 10) {
|
|
||||||
if (count % 2 == 0) {
|
|
||||||
print "even";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
print "odd";
|
|
||||||
}
|
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
|
//standard example
|
||||||
|
|
||||||
//moment of truth
|
|
||||||
var counter: int = 1;
|
var counter: int = 1;
|
||||||
|
|
||||||
while (counter <= 100) {
|
while (counter <= 100) {
|
||||||
@@ -15,11 +13,11 @@ while (counter <= 100) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//finally
|
//finally
|
||||||
if (result == "") {
|
if (result != "") {
|
||||||
print counter;
|
print result;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
print result;
|
print counter;
|
||||||
}
|
}
|
||||||
|
|
||||||
counter += 1;
|
counter += 1;
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
var a: int = 0;
|
|
||||||
|
|
||||||
while(a < 10) {
|
|
||||||
print a;
|
|
||||||
a += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
print "Finished";
|
|
||||||
|
|
||||||
13
scripts/odd_and_even.toy
Normal file
13
scripts/odd_and_even.toy
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,7 +28,7 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);
|
|||||||
|
|
||||||
//quick free
|
//quick free
|
||||||
#ifndef TOY_ARRAY_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
|
#endif
|
||||||
|
|
||||||
//one line to expand the array
|
//one line to expand the array
|
||||||
@@ -38,7 +38,7 @@ TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity);
|
|||||||
|
|
||||||
//quick push back
|
//quick push back
|
||||||
#ifndef TOY_ARRAY_PUSHBACK
|
#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
|
#endif
|
||||||
|
|
||||||
//URGENT: get array length in scripts (dot operator?)
|
//URGENT: get array length in scripts (dot operator?)
|
||||||
|
|||||||
@@ -10,9 +10,13 @@ static void errDefault(const char* msg) {
|
|||||||
fprintf(stderr, "%s", msg);
|
fprintf(stderr, "%s", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void assertDefault(const char* msg) {
|
||||||
|
fprintf(stderr, "%s", msg);
|
||||||
|
}
|
||||||
|
|
||||||
static Toy_callbackType printCallback = outDefault;
|
static Toy_callbackType printCallback = outDefault;
|
||||||
static Toy_callbackType errorCallback = errDefault;
|
static Toy_callbackType errorCallback = errDefault;
|
||||||
static Toy_callbackType assertCallback = errDefault;
|
static Toy_callbackType assertCallback = assertDefault;
|
||||||
|
|
||||||
void Toy_print(const char* msg) {
|
void Toy_print(const char* msg) {
|
||||||
printCallback(msg);
|
printCallback(msg);
|
||||||
@@ -47,5 +51,5 @@ void Toy_resetErrorCallback() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Toy_resetAssertFailureCallback() {
|
void Toy_resetAssertFailureCallback() {
|
||||||
assertCallback = errDefault;
|
assertCallback = assertDefault;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -226,7 +226,7 @@ bool Toy_checkValuesAreEqual(Toy_Value left, Toy_Value right) {
|
|||||||
|
|
||||||
for (unsigned int i = 0; i < leftArray->count; i++) {
|
for (unsigned int i = 0; i < leftArray->count; i++) {
|
||||||
//any mismatch is an easy difference
|
//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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,30 +6,30 @@
|
|||||||
int test_array() {
|
int test_array() {
|
||||||
//test allocation and free
|
//test allocation and free
|
||||||
{
|
{
|
||||||
Toy_Array* array = Toy_resizeArray(NULL, 1);
|
Toy_Array* array = TOY_ARRAY_ALLOCATE();
|
||||||
array = Toy_resizeArray(array, 0);
|
TOY_ARRAY_FREE(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
//test initial data
|
//test initial data
|
||||||
{
|
{
|
||||||
Toy_Array* array = Toy_resizeArray(NULL, 10);
|
Toy_Array* array = TOY_ARRAY_ALLOCATE();
|
||||||
|
|
||||||
//check you can access the memory
|
//check you can access the memory
|
||||||
array->data[1] = TOY_VALUE_FROM_INTEGER(42);
|
array->data[1] = TOY_VALUE_FROM_INTEGER(42);
|
||||||
|
|
||||||
Toy_resizeArray(array, 0);
|
TOY_ARRAY_FREE(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
//test multiple arrays (no overlaps or conflicts)
|
//test multiple arrays (no overlaps or conflicts)
|
||||||
{
|
{
|
||||||
Toy_Array* array1 = Toy_resizeArray(NULL, 10);
|
Toy_Array* array1 = TOY_ARRAY_ALLOCATE();
|
||||||
Toy_Array* array2 = Toy_resizeArray(NULL, 10);
|
Toy_Array* array2 = TOY_ARRAY_ALLOCATE();
|
||||||
|
|
||||||
array1->data[1] = TOY_VALUE_FROM_INTEGER(42);
|
array1->data[1] = TOY_VALUE_FROM_INTEGER(42);
|
||||||
array2->data[1] = TOY_VALUE_FROM_INTEGER(42);
|
array2->data[1] = TOY_VALUE_FROM_INTEGER(42);
|
||||||
|
|
||||||
Toy_resizeArray(array1, 0);
|
TOY_ARRAY_FREE(array1);
|
||||||
Toy_resizeArray(array2, 0);
|
TOY_ARRAY_FREE(array2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "toy_array.h"
|
#include "toy_array.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int test_value_creation() {
|
int test_value_creation() {
|
||||||
@@ -166,6 +167,8 @@ int test_value_copying() {
|
|||||||
Toy_freeValue(result);
|
Toy_freeValue(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//arrays can't be compared
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,6 +256,37 @@ int test_value_equality() {
|
|||||||
Toy_freeBucket(&bucket);
|
Toy_freeBucket(&bucket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//again with arrays
|
||||||
|
{
|
||||||
|
//setup
|
||||||
|
Toy_Array* array1 = TOY_ARRAY_ALLOCATE();
|
||||||
|
TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(42));
|
||||||
|
TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(69));
|
||||||
|
TOY_ARRAY_PUSHBACK(array1, TOY_VALUE_FROM_INTEGER(8891));
|
||||||
|
|
||||||
|
Toy_Value value1 = TOY_VALUE_FROM_ARRAY(array1);
|
||||||
|
|
||||||
|
Toy_Array* array2 = TOY_ARRAY_ALLOCATE();
|
||||||
|
TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(42));
|
||||||
|
TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(69));
|
||||||
|
TOY_ARRAY_PUSHBACK(array2, TOY_VALUE_FROM_INTEGER(8891));
|
||||||
|
|
||||||
|
Toy_Value value2 = TOY_VALUE_FROM_ARRAY(array2);
|
||||||
|
|
||||||
|
|
||||||
|
if (Toy_checkValuesAreEqual(value1, value2) != true)
|
||||||
|
{
|
||||||
|
fprintf(stderr, TOY_CC_ERROR "ERROR: array values are not equal\n" TOY_CC_RESET);
|
||||||
|
Toy_freeValue(value1);
|
||||||
|
Toy_freeValue(value2);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
Toy_freeValue(value1);
|
||||||
|
Toy_freeValue(value2);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,6 +348,8 @@ int test_value_comparison() {
|
|||||||
Toy_freeBucket(&bucket);
|
Toy_freeBucket(&bucket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//arrays can't be compared
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -448,7 +484,57 @@ int test_value_stringify() {
|
|||||||
Toy_freeBucket(&bucket);
|
Toy_freeBucket(&bucket);
|
||||||
}
|
}
|
||||||
|
|
||||||
//URGENT: test stringify and string & array
|
//stringify strings
|
||||||
|
{
|
||||||
|
//setup
|
||||||
|
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
|
||||||
|
|
||||||
|
Toy_Value value = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world!"));
|
||||||
|
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||||
|
char* buffer = Toy_getStringRawBuffer(string);
|
||||||
|
|
||||||
|
if (buffer == NULL || strcmp(buffer, "Hello world!") != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify string 'Hello world!' failed\n" TOY_CC_RESET);
|
||||||
|
free(buffer);
|
||||||
|
Toy_freeBucket(&bucket);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
free(buffer);
|
||||||
|
Toy_freeBucket(&bucket);
|
||||||
|
}
|
||||||
|
|
||||||
|
//stringify array
|
||||||
|
{
|
||||||
|
//setup
|
||||||
|
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_SMALL);
|
||||||
|
|
||||||
|
//setup
|
||||||
|
Toy_Array* array = TOY_ARRAY_ALLOCATE();
|
||||||
|
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(42));
|
||||||
|
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(69));
|
||||||
|
TOY_ARRAY_PUSHBACK(array, TOY_VALUE_FROM_INTEGER(8891));
|
||||||
|
|
||||||
|
Toy_Value value = TOY_VALUE_FROM_ARRAY(array);
|
||||||
|
Toy_String* string = Toy_stringifyValue(&bucket, value);
|
||||||
|
char* buffer = Toy_getStringRawBuffer(string);
|
||||||
|
|
||||||
|
if (buffer == NULL || strcmp(buffer, "[42,69,8891]") != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, TOY_CC_ERROR "ERROR: stringify array '[42,69,8891]' failed\n" TOY_CC_RESET);
|
||||||
|
free(buffer);
|
||||||
|
TOY_ARRAY_FREE(array);
|
||||||
|
Toy_freeBucket(&bucket);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//cleanup
|
||||||
|
free(buffer);
|
||||||
|
TOY_ARRAY_FREE(array);
|
||||||
|
Toy_freeBucket(&bucket);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -505,7 +591,7 @@ int main() {
|
|||||||
total += res;
|
total += res;
|
||||||
}
|
}
|
||||||
|
|
||||||
//URGENT: arrays
|
//TODO: references
|
||||||
|
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
//1-D array
|
//1-D array
|
||||||
var arr = [1, 2, 3];
|
var arr = [1, 2, 3];
|
||||||
arr[1] = 6;
|
arr[1] = 6;
|
||||||
print arr;
|
|
||||||
|
|
||||||
|
|
||||||
|
assert arr == [1, 6, 3], "1-D array failed";
|
||||||
|
|
||||||
//we need to go deeper
|
//we need to go deeper
|
||||||
var barr = [
|
var barr = [
|
||||||
@@ -16,4 +13,5 @@ var barr = [
|
|||||||
|
|
||||||
barr[1][1] = 99;
|
barr[1][1] = 99;
|
||||||
|
|
||||||
print barr;
|
assert barr == [[1, 2, 3],[4,99,6],[7,8,9]], "2-D array failed";
|
||||||
|
|
||||||
25
tests/integrations/test_keyword_while.toy
Normal file
25
tests/integrations/test_keyword_while.toy
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
//TODO: test keyword 'while', 'break', 'continue'
|
||||||
|
|
||||||
|
{
|
||||||
|
//iteration
|
||||||
|
var iteration = 0;
|
||||||
|
while(iteration < 10) {
|
||||||
|
print iteration;
|
||||||
|
iteration += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
//if and while work together
|
||||||
|
var count = 1;
|
||||||
|
while (count <= 10) {
|
||||||
|
if (count % 2 == 0) {
|
||||||
|
print "even";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print "odd";
|
||||||
|
}
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user