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:
@@ -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;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "toy_array.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int test_value_creation() {
|
||||
@@ -166,6 +167,8 @@ int test_value_copying() {
|
||||
Toy_freeValue(result);
|
||||
}
|
||||
|
||||
//arrays can't be compared
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -253,6 +256,37 @@ int test_value_equality() {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -314,6 +348,8 @@ int test_value_comparison() {
|
||||
Toy_freeBucket(&bucket);
|
||||
}
|
||||
|
||||
//arrays can't be compared
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -448,7 +484,57 @@ int test_value_stringify() {
|
||||
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;
|
||||
}
|
||||
@@ -505,7 +591,7 @@ int main() {
|
||||
total += res;
|
||||
}
|
||||
|
||||
//URGENT: arrays
|
||||
//TODO: references
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
17
tests/integrations/test_arrays.toy
Normal file
17
tests/integrations/test_arrays.toy
Normal file
@@ -0,0 +1,17 @@
|
||||
//1-D array
|
||||
var arr = [1, 2, 3];
|
||||
arr[1] = 6;
|
||||
|
||||
assert arr == [1, 6, 3], "1-D array failed";
|
||||
|
||||
//we need to go deeper
|
||||
var barr = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9]
|
||||
];
|
||||
|
||||
barr[1][1] = 99;
|
||||
|
||||
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