mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
To help with storing strings within tables, I've replaced the unused '_padding' member of 'Toy_String' with 'cachedHash', which is set to zero on string allocation. The hash of a string isn't generated and stored until it's actually needed, as the rope pattern means not every string needs a hash - hopefully this will save unnecessarily wasted time. When a hash of a string is needed, the hashing function first checks to see if that string already has one, and if so, returns it. Again, less time wasted. When generating a new string hash, the hashing function takes the string's type into account, as node-based strings first need their contents assembled into a simple char buffer. Other changes include: * Changed 'TOY_VALUE_TO_*' to 'TOY_VALUE_FROM_*' * Changed 'TOY_VALUE_IS_EQUAL' to 'TOY_VALUES_ARE_EQUAL' * Added a missing '#pragma once' to 'toy_print.h'
89 lines
2.1 KiB
C
89 lines
2.1 KiB
C
#include "toy_value.h"
|
|
#include "toy_console_colors.h"
|
|
|
|
#include "toy_bucket.h"
|
|
#include "toy_string.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
int main() {
|
|
//test for the correct size
|
|
{
|
|
if (sizeof(Toy_Value) != 8) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: 'Toy_Value' is an unexpected size in memory\n" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
//test creating a null
|
|
{
|
|
Toy_Value v = TOY_VALUE_FROM_NULL();
|
|
|
|
if (!TOY_VALUE_IS_NULL(v)) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: creating a 'null' value failed\n" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
//test creating values
|
|
{
|
|
Toy_Value t = TOY_VALUE_FROM_BOOLEAN(true);
|
|
Toy_Value f = TOY_VALUE_FROM_BOOLEAN(false);
|
|
|
|
if (!TOY_VALUE_IS_TRUTHY(t) || TOY_VALUE_IS_TRUTHY(f)) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: 'boolean' value failed\n" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
//test value equality
|
|
{
|
|
Toy_Value answer = TOY_VALUE_FROM_INTEGER(42);
|
|
Toy_Value question = TOY_VALUE_FROM_INTEGER(42);
|
|
Toy_Value nice = TOY_VALUE_FROM_INTEGER(69);
|
|
|
|
if (!TOY_VALUES_ARE_EQUAL(answer, question)) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: equality check failed, expected true\n" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
|
|
if (TOY_VALUES_ARE_EQUAL(answer, nice)) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: equality check failed, expected false\n" TOY_CC_RESET);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
//test value hashing
|
|
{
|
|
//setup
|
|
Toy_Bucket* bucket = Toy_allocateBucket(512);
|
|
|
|
//values
|
|
Toy_Value n = TOY_VALUE_FROM_NULL();
|
|
Toy_Value t = TOY_VALUE_FROM_BOOLEAN(true);
|
|
Toy_Value f = TOY_VALUE_FROM_BOOLEAN(false);
|
|
Toy_Value i = TOY_VALUE_FROM_INTEGER(42);
|
|
//skip float
|
|
Toy_Value s = TOY_VALUE_FROM_STRING(Toy_createString(&bucket, "Hello world"));
|
|
|
|
if (Toy_hashValue(n) != 0 ||
|
|
Toy_hashValue(t) != 1 ||
|
|
Toy_hashValue(f) != 0 ||
|
|
Toy_hashValue(i) != 4147366645 ||
|
|
Toy_hashValue(s) != 994097935 ||
|
|
TOY_VALUE_AS_STRING(s)->cachedHash == 0
|
|
)
|
|
{
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Unexpected hash of a value\n" TOY_CC_RESET);
|
|
Toy_freeBucket(&bucket);
|
|
return -1;
|
|
}
|
|
|
|
//cleanup
|
|
Toy_freeBucket(&bucket);
|
|
}
|
|
|
|
printf(TOY_CC_NOTICE "All good\n" TOY_CC_RESET);
|
|
return 0;
|
|
}
|