mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
Reworked generic structures, read more
The following structures are now more independant: - Toy_Array - Toy_Stack - Toy_Bucket - Toy_String I reworked a lot of the memory allocation, so now there are more direct calls to malloc() or realloc(), rather than relying on the macros from toy_memory.h. I've also split toy_memory into proper array and bucket files, because it makes more sense this way, rather than having them both jammed into one file. This means the eventual hashtable structure can also stand on its own. Toy_Array is a new wrapper around raw array pointers, and all of the structures have their metadata embedded into their allocated memory now, using variable length array members. A lot of 'capacity' and 'count' variables were changed to 'size_t' types, but this doesn't seem to be a problem anywhere. If the workflow fails, then I'll leave it for tonight - I'm too tired, and I don't want to overdo myself.
This commit is contained in:
52
tests/cases/test_array.c
Normal file
52
tests/cases/test_array.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "toy_array.h"
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_resizeArray() {
|
||||
//test single pointer
|
||||
{
|
||||
Toy_Array* array = TOY_ALLOCATE_ARRAY(int, 1);
|
||||
TOY_FREE_ARRAY(int, array);
|
||||
}
|
||||
|
||||
//test single pointer array
|
||||
{
|
||||
Toy_Array* array = TOY_ALLOCATE_ARRAY(int, 10);
|
||||
|
||||
//check you can access the memory
|
||||
array->data[1] = 42;
|
||||
|
||||
TOY_FREE_ARRAY(int, array);
|
||||
}
|
||||
|
||||
//test multiple pointer arrays
|
||||
{
|
||||
Toy_Array* array1 = TOY_ALLOCATE_ARRAY(int, 10);
|
||||
Toy_Array* array2 = TOY_ALLOCATE_ARRAY(int, 10);
|
||||
|
||||
array1->data[1] = 42; //access the given memory
|
||||
array2->data[1] = 42; //access the given memory
|
||||
|
||||
TOY_FREE_ARRAY(int, array1);
|
||||
TOY_FREE_ARRAY(int, array2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
{
|
||||
res = test_resizeArray();
|
||||
total += res;
|
||||
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
@@ -162,7 +162,7 @@ int test_type_emission(Toy_Bucket** bucket) {
|
||||
Toy_private_emitAstBinary(bucket, &ast, TOY_AST_FLAG_ADD, right);
|
||||
Toy_private_emitAstGroup(bucket, &ast);
|
||||
|
||||
Toy_private_appendAstBlock(bucket, &block, ast);
|
||||
Toy_private_appendAstBlock(bucket, block, ast);
|
||||
}
|
||||
|
||||
//check if it worked
|
||||
@@ -216,10 +216,9 @@ int main() {
|
||||
#endif
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_type_emission(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
@@ -1,46 +1,13 @@
|
||||
#include "toy_memory.h"
|
||||
#include "toy_bucket.h"
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_reallocate() {
|
||||
//test single pointer
|
||||
{
|
||||
int* integer = TOY_ALLOCATE(int, 1);
|
||||
TOY_FREE(int, integer);
|
||||
}
|
||||
|
||||
//test single pointer array
|
||||
{
|
||||
int* array = TOY_ALLOCATE(int, 10);
|
||||
|
||||
//check you can access the memory
|
||||
array[1] = 42;
|
||||
|
||||
TOY_FREE_ARRAY(int, array, 10);
|
||||
}
|
||||
|
||||
//test multiple pointer arrays
|
||||
{
|
||||
int* array1 = TOY_ALLOCATE(int, 10);
|
||||
int* array2 = TOY_ALLOCATE(int, 10);
|
||||
|
||||
array1[1] = 42; //access the given memory
|
||||
array2[1] = 42; //access the given memory
|
||||
|
||||
TOY_FREE_ARRAY(int, array1, 10);
|
||||
TOY_FREE_ARRAY(int, array2, 10);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_buckets() {
|
||||
//test initializing and freeing a bucket
|
||||
{
|
||||
//init
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(int, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(int) * 32);
|
||||
|
||||
//check
|
||||
if (bucket == NULL || bucket->capacity != 32 * sizeof(int)) {
|
||||
@@ -49,20 +16,19 @@ int test_buckets() {
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
}
|
||||
|
||||
//test partitioning a bucket, several times
|
||||
{
|
||||
//init
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(int, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(int) * 32);
|
||||
|
||||
//grab some memory
|
||||
int* a = TOY_BUCKET_PART(int, bucket);
|
||||
int* b = TOY_BUCKET_PART(int, bucket);
|
||||
int* c = TOY_BUCKET_PART(int, bucket);
|
||||
int* d = TOY_BUCKET_PART(int, bucket);
|
||||
int* a = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* b = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* c = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* d = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
|
||||
//check
|
||||
if (bucket == NULL || bucket->count != 4 * sizeof(int)) {
|
||||
@@ -71,22 +37,21 @@ int test_buckets() {
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
}
|
||||
|
||||
//test partitioning a bucket, several times, with an internal expansion
|
||||
{
|
||||
//init
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(int, bucket, 4);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(int) * 4);
|
||||
|
||||
//grab some memory
|
||||
int* a = TOY_BUCKET_PART(int, bucket);
|
||||
int* b = TOY_BUCKET_PART(int, bucket);
|
||||
int* c = TOY_BUCKET_PART(int, bucket);
|
||||
int* d = TOY_BUCKET_PART(int, bucket);
|
||||
int* e = TOY_BUCKET_PART(int, bucket);
|
||||
int* f = TOY_BUCKET_PART(int, bucket);
|
||||
int* a = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* b = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* c = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* d = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* e = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
int* f = Toy_partitionBucket(&bucket, sizeof(int));
|
||||
|
||||
//checks - please note that the top-most bucket is what is being filled - older buckets are further along
|
||||
if (
|
||||
@@ -101,20 +66,19 @@ int test_buckets() {
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
}
|
||||
|
||||
//test partitioning a bucket, several times, with an internal expansion, and awkward sizes
|
||||
{
|
||||
//init
|
||||
Toy_Bucket* bucket = NULL;
|
||||
Toy_initBucket(&bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(32);
|
||||
|
||||
//grab some memory
|
||||
void* a = Toy_partBucket(&bucket, 16);
|
||||
void* b = Toy_partBucket(&bucket, 10);
|
||||
void* c = Toy_partBucket(&bucket, 10);
|
||||
void* d = Toy_partBucket(&bucket, 10);
|
||||
void* a = Toy_partitionBucket(&bucket, 16);
|
||||
void* b = Toy_partitionBucket(&bucket, 10);
|
||||
void* c = Toy_partitionBucket(&bucket, 10);
|
||||
void* d = Toy_partitionBucket(&bucket, 10);
|
||||
|
||||
//checks - awkward and mismatched sizes is not officially supported, but it should work
|
||||
if (
|
||||
@@ -129,7 +93,7 @@ int test_buckets() {
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -139,18 +103,13 @@ int main() {
|
||||
//run each test set, returning the total errors given
|
||||
int total = 0, res = 0;
|
||||
|
||||
res = test_reallocate();
|
||||
total += res;
|
||||
{
|
||||
res = test_buckets();
|
||||
total += res;
|
||||
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
res = test_buckets();
|
||||
total += res;
|
||||
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
@@ -26,7 +26,7 @@ int test_bytecode_header(Toy_Bucket** bucket) {
|
||||
strcmp((char*)(bc.ptr + 3), TOY_VERSION_BUILD) != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to write the bytecode header correctly:\n" TOY_CC_RESET);
|
||||
fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, bc.ptr[0], bc.ptr[1], bc.ptr[2], (char*)(bc.ptr + 3));
|
||||
fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, (int)(bc.ptr[0]), (int)(bc.ptr[1]), (int)(bc.ptr[2]), (char*)(bc.ptr + 3));
|
||||
fprintf(stderr, TOY_CC_ERROR "\t%d.%d.%d.%s\n" TOY_CC_RESET, TOY_VERSION_MAJOR, TOY_VERSION_MINOR, TOY_VERSION_PATCH, TOY_VERSION_BUILD);
|
||||
|
||||
//cleanup and return
|
||||
@@ -35,7 +35,7 @@ int test_bytecode_header(Toy_Bucket** bucket) {
|
||||
}
|
||||
|
||||
if (bc.count % 4 != 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: bytecode size is not a multiple of 4, size is: %d\n" TOY_CC_RESET, bc.count);
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: bytecode size is not a multiple of 4, size is: %d\n" TOY_CC_RESET, (int)bc.count);
|
||||
|
||||
//cleanup and return
|
||||
Toy_freeBytecode(bc);
|
||||
@@ -65,7 +65,7 @@ int test_bytecode_from_source(Toy_Bucket** bucket) {
|
||||
|
||||
//check bytecode alignment
|
||||
if (bc.count % 4 != 0) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: bytecode alignment is not a multiple of 4 (size is %d), source: %s\n" TOY_CC_RESET, bc.count, source);
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: bytecode alignment is not a multiple of 4 (size is %d), source: %s\n" TOY_CC_RESET, (int)bc.count, source);
|
||||
|
||||
//cleanup and return
|
||||
Toy_freeBytecode(bc);
|
||||
@@ -175,10 +175,9 @@ int main() {
|
||||
int total = 0, res = 0;
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_bytecode_header(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -186,10 +185,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_bytecode_from_source(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
@@ -578,10 +578,9 @@ int main() {
|
||||
int total = 0, res = 0;
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_simple_empty_parsers(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -589,10 +588,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_values(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -600,10 +598,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_unary(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -611,10 +608,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_binary(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -622,10 +618,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_precedence(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "toy_parser.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
//tests
|
||||
@@ -32,7 +33,7 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, ast: PASS\n" TOY_CC_RESET);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -46,12 +47,12 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, ast: PASS\n" TOY_CC_RESET);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//rerun the test with a more complex ast, derived from a snippet of source
|
||||
@@ -81,7 +82,7 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -95,12 +96,12 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce a null value
|
||||
@@ -130,7 +131,7 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -148,12 +149,12 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce a boolean value
|
||||
@@ -183,7 +184,7 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -201,12 +202,12 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce an integer value
|
||||
@@ -236,7 +237,7 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -255,12 +256,12 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce a float value
|
||||
@@ -290,7 +291,7 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -309,12 +310,12 @@ int test_routine_header_and_values(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -352,7 +353,7 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -383,12 +384,12 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce a simple comparison
|
||||
@@ -418,7 +419,7 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -449,12 +450,12 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce a simple comparison
|
||||
@@ -484,7 +485,7 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -520,12 +521,12 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
//produce a more complex algorithm
|
||||
@@ -555,7 +556,7 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine header, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -612,12 +613,12 @@ int test_routine_binary(Toy_Bucket** bucket) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to produce the expected routine code, source: %s\n" TOY_CC_RESET, source);
|
||||
|
||||
//cleanup and return
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//cleanup
|
||||
TOY_FREE_ARRAY(unsigned char, buffer, len);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -628,10 +629,9 @@ int main() {
|
||||
int total = 0, res = 0;
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_routine_header_and_values(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -639,10 +639,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_routine_binary(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
@@ -3,40 +3,40 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int test_stack_with_init() {
|
||||
//init and free the stack
|
||||
int test_stack_basics() {
|
||||
//allocate and free the stack
|
||||
{
|
||||
Toy_Stack stack;
|
||||
Toy_initStack(&stack);
|
||||
Toy_Stack* stack = Toy_allocateStack();
|
||||
|
||||
//check if it worked
|
||||
if (
|
||||
stack.ptr != NULL ||
|
||||
stack.capacity != 0 ||
|
||||
stack.count != 0)
|
||||
stack == NULL ||
|
||||
stack->capacity != 64 ||
|
||||
stack->count != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to init Toy_Stack\n" TOY_CC_RESET);
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to allocate Toy_Stack\n" TOY_CC_RESET);
|
||||
Toy_freeStack(stack);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_freeStack(&stack);
|
||||
Toy_freeStack(stack);
|
||||
}
|
||||
|
||||
//push, peek and pop stack
|
||||
{
|
||||
Toy_Stack stack;
|
||||
Toy_initStack(&stack);
|
||||
Toy_Stack* stack = Toy_allocateStack();
|
||||
|
||||
//check if it worked (push)
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(42));
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(69));
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(420));
|
||||
if (
|
||||
stack.ptr == NULL ||
|
||||
stack.capacity != 64 ||
|
||||
stack.count != 3)
|
||||
stack == NULL ||
|
||||
stack->capacity != 64 ||
|
||||
stack->count != 3)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to push Toy_Stack\n" TOY_CC_RESET);
|
||||
Toy_freeStack(stack);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -47,19 +47,21 @@ int test_stack_with_init() {
|
||||
TOY_VALUE_AS_INTEGER(top1) != 420)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to peek Toy_Stack\n" TOY_CC_RESET);
|
||||
Toy_freeStack(stack);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//check if it worked (pop)
|
||||
Toy_Value top2 = Toy_popStack(&stack);
|
||||
if (
|
||||
stack.ptr == NULL ||
|
||||
stack.capacity != 64 ||
|
||||
stack.count != 2 ||
|
||||
stack == NULL ||
|
||||
stack->capacity != 64 ||
|
||||
stack->count != 2 ||
|
||||
TOY_VALUE_IS_INTEGER(top2) != true ||
|
||||
TOY_VALUE_AS_INTEGER(top2) != 420)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop Toy_Stack\n" TOY_CC_RESET);
|
||||
Toy_freeStack(stack);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -70,86 +72,38 @@ int test_stack_with_init() {
|
||||
TOY_VALUE_AS_INTEGER(top3) != 69)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop then peek Toy_Stack\n" TOY_CC_RESET);
|
||||
Toy_freeStack(stack);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_freeStack(&stack);
|
||||
Toy_freeStack(stack);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_stack_with_preallocate() {
|
||||
//preallocate and free the stack
|
||||
int test_stack_stress() {
|
||||
//stress the stack's contents
|
||||
{
|
||||
Toy_Stack stack;
|
||||
Toy_preallocateStack(&stack);
|
||||
Toy_Stack* stack = Toy_allocateStack();
|
||||
|
||||
//allocate 500 values
|
||||
for (int i = 0; i < 500; i++) {
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(i));
|
||||
}
|
||||
|
||||
//check if it worked
|
||||
if (
|
||||
stack.ptr == NULL ||
|
||||
stack.capacity != 64 ||
|
||||
stack.count != 0)
|
||||
stack == NULL ||
|
||||
stack->capacity != 512 ||
|
||||
stack->count != 500)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to preallocate Toy_Stack\n" TOY_CC_RESET);
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to stress the Toy_Stack\n" TOY_CC_RESET);
|
||||
Toy_freeStack(stack);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_freeStack(&stack);
|
||||
}
|
||||
|
||||
//push, peek and pop stack
|
||||
{
|
||||
Toy_Stack stack;
|
||||
Toy_initStack(&stack);
|
||||
|
||||
//check if it worked (push)
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(42));
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(69));
|
||||
Toy_pushStack(&stack, TOY_VALUE_TO_INTEGER(420));
|
||||
if (
|
||||
stack.ptr == NULL ||
|
||||
stack.capacity != 64 ||
|
||||
stack.count != 3)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to push Toy_Stack\n" TOY_CC_RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//check if it worked (peek)
|
||||
Toy_Value top1 = Toy_peekStack(&stack);
|
||||
if (
|
||||
TOY_VALUE_IS_INTEGER(top1) != true ||
|
||||
TOY_VALUE_AS_INTEGER(top1) != 420)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to peek Toy_Stack\n" TOY_CC_RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//check if it worked (pop)
|
||||
Toy_Value top2 = Toy_popStack(&stack);
|
||||
if (
|
||||
stack.ptr == NULL ||
|
||||
stack.capacity != 64 ||
|
||||
stack.count != 2 ||
|
||||
TOY_VALUE_IS_INTEGER(top2) != true ||
|
||||
TOY_VALUE_AS_INTEGER(top2) != 420)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop Toy_Stack\n" TOY_CC_RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//check if it worked (post-pop peek)
|
||||
Toy_Value top3 = Toy_peekStack(&stack);
|
||||
if (
|
||||
TOY_VALUE_IS_INTEGER(top3) != true ||
|
||||
TOY_VALUE_AS_INTEGER(top3) != 69)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: failed to pop then peek Toy_Stack\n" TOY_CC_RESET);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Toy_freeStack(&stack);
|
||||
Toy_freeStack(stack);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -160,7 +114,7 @@ int main() {
|
||||
int total = 0, res = 0;
|
||||
|
||||
{
|
||||
res = test_stack_with_init();
|
||||
res = test_stack_basics();
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -168,7 +122,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
res = test_stack_with_preallocate();
|
||||
res = test_stack_stress();
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "toy_string.h"
|
||||
#include "toy_console_colors.h"
|
||||
|
||||
#include "toy_memory.h"
|
||||
#include "toy_bucket.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int test_sizeof_string_64bit() {
|
||||
@@ -34,8 +35,7 @@ int test_string_allocation() {
|
||||
//allocate a single string from a c-string
|
||||
{
|
||||
//setup
|
||||
Toy_Bucket* bucket = NULL;
|
||||
Toy_initBucket(&bucket, 1024);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(1024);
|
||||
|
||||
const char* cstring = "Hello world";
|
||||
Toy_String* str = Toy_createString(&bucket, cstring);
|
||||
@@ -77,8 +77,7 @@ int test_string_allocation() {
|
||||
//copy and deep copy a string
|
||||
{
|
||||
//setup
|
||||
Toy_Bucket* bucket = NULL;
|
||||
Toy_initBucket(&bucket, 1024);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(1024);
|
||||
|
||||
const char* cstring = "Hello world";
|
||||
Toy_String* str = Toy_createString(&bucket, cstring);
|
||||
@@ -104,8 +103,7 @@ int test_string_allocation() {
|
||||
//allocate a zero-length string
|
||||
{
|
||||
//setup
|
||||
Toy_Bucket* bucket = NULL;
|
||||
Toy_initBucket(&bucket, 1024);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(1024);
|
||||
|
||||
const char* cstring = "";
|
||||
Toy_String* str = Toy_createString(&bucket, cstring);
|
||||
@@ -133,8 +131,7 @@ int test_string_allocation() {
|
||||
|
||||
int test_string_concatenation() {
|
||||
//one big bucket o' fun
|
||||
Toy_Bucket* bucket = NULL;
|
||||
Toy_initBucket(&bucket, 1024);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(1024);
|
||||
|
||||
//concatenate two strings, and check the refcounts
|
||||
{
|
||||
@@ -191,12 +188,12 @@ int test_string_concatenation() {
|
||||
strcmp(buffer, "Hello world") != 0)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to get the raw buffer from concatenated string\n" TOY_CC_RESET);
|
||||
TOY_FREE_ARRAY(char, buffer, result->length + 1);
|
||||
free(buffer);
|
||||
Toy_freeBucket(&bucket);
|
||||
return -1;
|
||||
}
|
||||
|
||||
TOY_FREE_ARRAY(char, buffer, result->length + 1);
|
||||
free(buffer);
|
||||
Toy_freeString(result);
|
||||
Toy_freeString(first);
|
||||
Toy_freeString(second);
|
||||
@@ -224,8 +221,7 @@ int test_string_with_stressed_bucket() {
|
||||
};
|
||||
|
||||
//setup
|
||||
Toy_Bucket* bucket = NULL;
|
||||
Toy_initBucket(&bucket, 128); //deliberately too short for one bucket
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(128);//deliberately too much data for one bucket
|
||||
|
||||
//stress
|
||||
Toy_String* str = Toy_createString(&bucket, testData[0]);
|
||||
@@ -250,7 +246,7 @@ int test_string_with_stressed_bucket() {
|
||||
strlen(buffer) != 36)
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected state of the raw buffer after string stress test: '%s'\n" TOY_CC_RESET, buffer);
|
||||
Toy_reallocate(buffer, 0, 0); //direct call to free, regardless of size
|
||||
free(buffer);
|
||||
Toy_freeBucket(&bucket);
|
||||
return -1;
|
||||
}
|
||||
@@ -258,13 +254,13 @@ int test_string_with_stressed_bucket() {
|
||||
if (bucket->next == NULL) //just to make sure
|
||||
{
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected state of the bucket after string stress test\n" TOY_CC_RESET);
|
||||
Toy_reallocate(buffer, 0, 0); //direct call to free, regardless of size
|
||||
free(buffer);
|
||||
Toy_freeBucket(&bucket);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//clean up
|
||||
TOY_FREE_ARRAY(char, buffer, str->length);
|
||||
free(buffer);
|
||||
Toy_freeBucket(&bucket);
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,6 @@ int test_setup_and_teardown(Toy_Bucket** bucket) {
|
||||
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bc.ptr, bc.capacity);
|
||||
|
||||
//check the header size
|
||||
@@ -93,14 +92,14 @@ int test_simple_execution(Toy_Bucket** bucket) {
|
||||
|
||||
//run the setup
|
||||
Toy_VM vm;
|
||||
Toy_initVM(&vm);
|
||||
Toy_bindVM(&vm, bc.ptr, bc.capacity);
|
||||
|
||||
//run
|
||||
Toy_runVM(&vm);
|
||||
|
||||
//check the final state of the stack
|
||||
if (vm.stack.count != 1 ||
|
||||
if (vm.stack == NULL ||
|
||||
vm.stack->count != 1 ||
|
||||
TOY_VALUE_IS_INTEGER( Toy_peekStack(&vm.stack) ) != true ||
|
||||
TOY_VALUE_AS_INTEGER( Toy_peekStack(&vm.stack) ) != 21
|
||||
)
|
||||
@@ -124,10 +123,9 @@ int main() {
|
||||
int total = 0, res = 0;
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_setup_and_teardown(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
@@ -135,10 +133,9 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
Toy_Bucket* bucket = NULL;
|
||||
TOY_BUCKET_INIT(Toy_Ast, bucket, 32);
|
||||
Toy_Bucket* bucket = Toy_allocateBucket(sizeof(Toy_Ast) * 32);
|
||||
res = test_simple_execution(&bucket);
|
||||
TOY_BUCKET_FREE(bucket);
|
||||
Toy_freeBucket(&bucket);
|
||||
if (res == 0) {
|
||||
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user