From 3c0a50c4cd750fd16ba03682f7012f56bfc8c58b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 1 Feb 2025 10:04:19 +1100 Subject: [PATCH] Tweaked AST bitness test --- source/toy_ast.h | 50 ++++++++-------- tests/cases/test_ast.c | 127 +++++++++++++++-------------------------- 2 files changed, 71 insertions(+), 106 deletions(-) diff --git a/source/toy_ast.h b/source/toy_ast.h index 560da5a..f9d1c88 100644 --- a/source/toy_ast.h +++ b/source/toy_ast.h @@ -88,7 +88,7 @@ typedef struct Toy_AstBlock { bool innerScope; Toy_Ast* child; //begin encoding the line Toy_Ast* next; //'next' is either an AstBlock or null - Toy_Ast* tail; //'tail' - either points to the tail of the current list, or null; only used by the head of a list as an optimisation + Toy_Ast* tail; //'tail' - either points to the tail of the current list, or null; only used as an optimisation in toy_ast.c } Toy_AstBlock; typedef struct Toy_AstValue { @@ -203,30 +203,30 @@ typedef struct Toy_AstEnd { Toy_AstType type; } Toy_AstEnd; -union Toy_Ast { //32 | 64 BITNESS - Toy_AstType type; //4 | 4 - Toy_AstBlock block; //16 | 32 - Toy_AstValue value; //12 | 24 - Toy_AstUnary unary; //12 | 16 - Toy_AstBinary binary; //16 | 24 - Toy_AstBinaryShortCircuit binaryShortCircuit; //16 | 24 - Toy_AstCompare compare; //16 | 24 - Toy_AstGroup group; //8 | 16 - Toy_AstCompound compound; //12 | 16 - Toy_AstAggregate aggregate; //16 | 24 - Toy_AstAssert assert; //16 | 24 - Toy_AstIfThenElse ifThenElse; //16 | 32 - Toy_AstWhileThen whileThen; //16 | 24 - Toy_AstBreak breakPoint; //4 | 4 - Toy_AstContinue continuePoint; //4 | 4 - Toy_AstPrint print; //8 | 16 - Toy_AstVarDeclare varDeclare; //16 | 24 - Toy_AstVarAssign varAssign; //16 | 24 - Toy_AstVarAccess varAccess; //8 | 16 - Toy_AstPass pass; //4 | 4 - Toy_AstError error; //4 | 4 - Toy_AstEnd end; //4 | 4 -}; //16 | 32 +union Toy_Ast { //see 'test_ast.c' for bitness tests + Toy_AstType type; + Toy_AstBlock block; + Toy_AstValue value; + Toy_AstUnary unary; + Toy_AstBinary binary; + Toy_AstBinaryShortCircuit binaryShortCircuit; + Toy_AstCompare compare; + Toy_AstGroup group; + Toy_AstCompound compound; + Toy_AstAggregate aggregate; + Toy_AstAssert assert; + Toy_AstIfThenElse ifThenElse; + Toy_AstWhileThen whileThen; + Toy_AstBreak breakPoint; + Toy_AstContinue continuePoint; + Toy_AstPrint print; + Toy_AstVarDeclare varDeclare; + Toy_AstVarAssign varAssign; + Toy_AstVarAccess varAccess; + Toy_AstPass pass; + Toy_AstError error; + Toy_AstEnd end; +}; void Toy_private_initAstBlock(Toy_Bucket** bucketHandle, Toy_Ast** astHandle); void Toy_private_appendAstBlock(Toy_Bucket** bucketHandle, Toy_Ast* block, Toy_Ast* child); diff --git a/tests/cases/test_ast.c b/tests/cases/test_ast.c index 84bb20f..4c23cab 100644 --- a/tests/cases/test_ast.c +++ b/tests/cases/test_ast.c @@ -4,87 +4,62 @@ #include #include -int test_sizeof_ast_64bit(void) { - //NOTE: This could've covered both bitness sizes as TEST_SIZEOF(type, bit32, bit32) -#define TEST_SIZEOF(type, size) \ - if (sizeof(type) != size) { \ - fprintf(stderr, TOY_CC_ERROR "ERROR: sizeof(" #type ") is %d, expected %d\n" TOY_CC_RESET, (int)sizeof(type), size); \ +#if TOY_BITNESS == 32 + +#define TEST_SIZEOF(type, bit32, bit64) \ + if (sizeof(type) != bit32) { \ + fprintf(stderr, TOY_CC_ERROR "ERROR: sizeof(" #type ") is %d, expected %d (bitness %d)\n" TOY_CC_RESET, (int)sizeof(type), bit32, TOY_BITNESS); \ ++err; \ } - //count errors +#elif TOY_BITNESS == 64 + +#define TEST_SIZEOF(type, bit32, bit64) \ + if (sizeof(type) != bit64) { \ + fprintf(stderr, TOY_CC_ERROR "ERROR: sizeof(" #type ") is %d, expected %d (bitness %d)\n" TOY_CC_RESET, (int)sizeof(type), bit64, TOY_BITNESS); \ + ++err; \ + } + +#else + +#pragma message("Unable to test the size of Toy_Ast members, as TOY_BITNESS is not recognized") +#define TEST_SIZEOF(type, bit32, bit64) + +#endif + +int test_sizeof_ast(void) { int err = 0; //run for each type - TEST_SIZEOF(Toy_AstType, 4); - TEST_SIZEOF(Toy_AstBlock, 32); - TEST_SIZEOF(Toy_AstValue, 24); - TEST_SIZEOF(Toy_AstUnary, 16); - TEST_SIZEOF(Toy_AstBinary, 24); - TEST_SIZEOF(Toy_AstBinaryShortCircuit, 24); - TEST_SIZEOF(Toy_AstCompare, 24); - TEST_SIZEOF(Toy_AstGroup, 16); - TEST_SIZEOF(Toy_AstCompound, 16); - TEST_SIZEOF(Toy_AstAggregate, 24); - TEST_SIZEOF(Toy_AstAssert, 24); - TEST_SIZEOF(Toy_AstIfThenElse, 32); - TEST_SIZEOF(Toy_AstWhileThen, 24); - TEST_SIZEOF(Toy_AstBreak, 4); - TEST_SIZEOF(Toy_AstContinue, 4); - TEST_SIZEOF(Toy_AstPrint, 16); - TEST_SIZEOF(Toy_AstVarDeclare, 24); - TEST_SIZEOF(Toy_AstVarAssign, 24); - TEST_SIZEOF(Toy_AstVarAccess, 16); - TEST_SIZEOF(Toy_AstPass, 4); - TEST_SIZEOF(Toy_AstError, 4); - TEST_SIZEOF(Toy_AstEnd, 4); - TEST_SIZEOF(Toy_Ast, 32); - -#undef TEST_SIZEOF + TEST_SIZEOF(Toy_AstType, 4 , 4); + TEST_SIZEOF(Toy_AstBlock, 20 , 32); + TEST_SIZEOF(Toy_AstValue, 12 , 24); + TEST_SIZEOF(Toy_AstUnary, 12 , 16); + TEST_SIZEOF(Toy_AstBinary, 16 , 24); + TEST_SIZEOF(Toy_AstBinaryShortCircuit, 16 , 24); + TEST_SIZEOF(Toy_AstCompare, 16 , 24); + TEST_SIZEOF(Toy_AstGroup, 8 , 16); + TEST_SIZEOF(Toy_AstCompound, 12 , 16); + TEST_SIZEOF(Toy_AstAggregate, 16 , 24); + TEST_SIZEOF(Toy_AstAssert, 12 , 24); + TEST_SIZEOF(Toy_AstIfThenElse, 16 , 32); + TEST_SIZEOF(Toy_AstWhileThen, 12 , 24); + TEST_SIZEOF(Toy_AstBreak, 4 , 4); + TEST_SIZEOF(Toy_AstContinue, 4 , 4); + TEST_SIZEOF(Toy_AstPrint, 8 , 16); + TEST_SIZEOF(Toy_AstVarDeclare, 12 , 24); + TEST_SIZEOF(Toy_AstVarAssign, 16 , 24); + TEST_SIZEOF(Toy_AstVarAccess, 8 , 16); + TEST_SIZEOF(Toy_AstPass, 4 , 4); + TEST_SIZEOF(Toy_AstError, 4 , 4); + TEST_SIZEOF(Toy_AstEnd, 4 , 4); + TEST_SIZEOF(Toy_Ast, 20 , 32); return -err; } -int test_sizeof_ast_32bit(void) { -#define TEST_SIZEOF(type, size) \ - if (sizeof(type) != size) { \ - fprintf(stderr, TOY_CC_ERROR "ERROR: sizeof(" #type ") is %d, expected %d\n" TOY_CC_RESET, (int)sizeof(type), size); \ - ++err; \ - } - - //count errors - int err = 0; - - //run for each type - TEST_SIZEOF(Toy_AstType, 4); - TEST_SIZEOF(Toy_AstBlock, 20); - TEST_SIZEOF(Toy_AstValue, 12); - TEST_SIZEOF(Toy_AstUnary, 12); - TEST_SIZEOF(Toy_AstBinary, 16); - TEST_SIZEOF(Toy_AstBinaryShortCircuit, 16); - TEST_SIZEOF(Toy_AstCompare, 16); - TEST_SIZEOF(Toy_AstGroup, 8); - TEST_SIZEOF(Toy_AstCompound, 12); - TEST_SIZEOF(Toy_AstAggregate, 16); - TEST_SIZEOF(Toy_AstAssert, 12); - TEST_SIZEOF(Toy_AstIfThenElse, 16); - TEST_SIZEOF(Toy_AstWhileThen, 12); - TEST_SIZEOF(Toy_AstBreak, 4); - TEST_SIZEOF(Toy_AstContinue, 4); - TEST_SIZEOF(Toy_AstPrint, 8); - TEST_SIZEOF(Toy_AstVarDeclare, 12); - TEST_SIZEOF(Toy_AstVarAssign, 16); - TEST_SIZEOF(Toy_AstVarAccess, 8); - TEST_SIZEOF(Toy_AstPass, 4); - TEST_SIZEOF(Toy_AstError, 4); - TEST_SIZEOF(Toy_AstEnd, 4); - TEST_SIZEOF(Toy_Ast, 20); - #undef TEST_SIZEOF - return -err; -} - int test_type_emission(Toy_Bucket** bucketHandle) { //emit value { @@ -498,21 +473,11 @@ int main(void) { int total = 0, res = 0; { -#if TOY_BITNESS == 64 - res = test_sizeof_ast_64bit(); -#elif TOY_BITNESS == 32 - res = test_sizeof_ast_32bit(); -#else - res = -1; - fprintf(stderr, TOY_CC_WARN "WARNING: Skipping test_sizeof_ast_**bit(); Can't determine the 'bitness' of this platform (seems to be %d)\n" TOY_CC_RESET, TOY_BITNESS); -#endif - + res = test_sizeof_ast(); if (res == 0) { printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET); } - else if (res > 0) { - total += res; - } + total += res; } {