1
0
forked from Mirror/wren
Files
wren/test/api/slots.c

194 lines
4.8 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include "slots.h"
static void noSet(WrenVM* vm)
{
// Do nothing.
}
static void getSlots(WrenVM* vm)
{
bool result = true;
if (wrenGetSlotBool(vm, 1) != true) result = false;
int length;
const char* bytes = wrenGetSlotBytes(vm, 2, &length);
if (length != 5) result = false;
if (memcmp(bytes, "by\0te", length) != 0) result = false;
Improve Travis Build & Test Coverage Build Wren for more targets, and run the test suite on both 32 and 64 bit builds. * Update the build config to test both with and without NAN_TAGGING defined. * Updatest `util/test.py` to take the executable suffix as a parameter. This allows the makefile to control which binaries will be tested. Adds a new target to the makefile to be run by travis, this runs the test suite against all of the configurations it builds. * Gcc on some 32 bit platforms was complaining about numeric overflows when -INFINITY was used. Update the logic for converting a double to a string to not explicitly check against the literal values. * Make CI builds run the tests on both 64 _and_ 32 bit builds. * If I limit the number of CPUs on my MBP I can get some of the tests to time out, I'm imagining that the specs of the Travis Macs means that the same is happening there too. Updated the test script to allow an extra few seconds for the test to complete successfully before killing it. * Due to slight differences in accuracy in some computations tests were failing on 32 bit builds. Stop comparing things quite as exactly in the cases where it is causing issues. For some reason 12.34 was refusing to compare equal to itself. Bad show 12.34 :-/. I've also updated the test so it doesn't leak handles even if the assertions fail. * Double-cast from `double` to `uint32_t` to prevent undefined behaviour on overflow of basic integers. This should hopefully prevent 32 bit test failures on Linux. * Move to a version of LibUV with a fix for the 32 bit build error on Travis.
2016-10-09 09:18:27 +01:00
if (wrenGetSlotDouble(vm, 3) != 1.5) result = false;
if (strcmp(wrenGetSlotString(vm, 4), "str") != 0) result = false;
2016-05-20 20:55:28 -07:00
WrenHandle* handle = wrenGetSlotHandle(vm, 5);
if (result)
{
// Otherwise, return the value so we can tell if we captured it correctly.
2016-05-20 20:55:28 -07:00
wrenSetSlotHandle(vm, 0, handle);
}
else
{
// If anything failed, return false.
wrenSetSlotBool(vm, 0, false);
}
Improve Travis Build & Test Coverage Build Wren for more targets, and run the test suite on both 32 and 64 bit builds. * Update the build config to test both with and without NAN_TAGGING defined. * Updatest `util/test.py` to take the executable suffix as a parameter. This allows the makefile to control which binaries will be tested. Adds a new target to the makefile to be run by travis, this runs the test suite against all of the configurations it builds. * Gcc on some 32 bit platforms was complaining about numeric overflows when -INFINITY was used. Update the logic for converting a double to a string to not explicitly check against the literal values. * Make CI builds run the tests on both 64 _and_ 32 bit builds. * If I limit the number of CPUs on my MBP I can get some of the tests to time out, I'm imagining that the specs of the Travis Macs means that the same is happening there too. Updated the test script to allow an extra few seconds for the test to complete successfully before killing it. * Due to slight differences in accuracy in some computations tests were failing on 32 bit builds. Stop comparing things quite as exactly in the cases where it is causing issues. For some reason 12.34 was refusing to compare equal to itself. Bad show 12.34 :-/. I've also updated the test so it doesn't leak handles even if the assertions fail. * Double-cast from `double` to `uint32_t` to prevent undefined behaviour on overflow of basic integers. This should hopefully prevent 32 bit test failures on Linux. * Move to a version of LibUV with a fix for the 32 bit build error on Travis.
2016-10-09 09:18:27 +01:00
wrenReleaseHandle(vm, handle);
}
static void setSlots(WrenVM* vm)
{
2016-05-20 20:55:28 -07:00
WrenHandle* handle = wrenGetSlotHandle(vm, 1);
wrenSetSlotBool(vm, 1, true);
wrenSetSlotBytes(vm, 2, "by\0te", 5);
Improve Travis Build & Test Coverage Build Wren for more targets, and run the test suite on both 32 and 64 bit builds. * Update the build config to test both with and without NAN_TAGGING defined. * Updatest `util/test.py` to take the executable suffix as a parameter. This allows the makefile to control which binaries will be tested. Adds a new target to the makefile to be run by travis, this runs the test suite against all of the configurations it builds. * Gcc on some 32 bit platforms was complaining about numeric overflows when -INFINITY was used. Update the logic for converting a double to a string to not explicitly check against the literal values. * Make CI builds run the tests on both 64 _and_ 32 bit builds. * If I limit the number of CPUs on my MBP I can get some of the tests to time out, I'm imagining that the specs of the Travis Macs means that the same is happening there too. Updated the test script to allow an extra few seconds for the test to complete successfully before killing it. * Due to slight differences in accuracy in some computations tests were failing on 32 bit builds. Stop comparing things quite as exactly in the cases where it is causing issues. For some reason 12.34 was refusing to compare equal to itself. Bad show 12.34 :-/. I've also updated the test so it doesn't leak handles even if the assertions fail. * Double-cast from `double` to `uint32_t` to prevent undefined behaviour on overflow of basic integers. This should hopefully prevent 32 bit test failures on Linux. * Move to a version of LibUV with a fix for the 32 bit build error on Travis.
2016-10-09 09:18:27 +01:00
wrenSetSlotDouble(vm, 3, 1.5);
wrenSetSlotString(vm, 4, "str");
2017-03-03 07:57:50 -08:00
wrenSetSlotNull(vm, 5);
// Read the slots back to make sure they were set correctly.
bool result = true;
if (wrenGetSlotBool(vm, 1) != true) result = false;
int length;
const char* bytes = wrenGetSlotBytes(vm, 2, &length);
if (length != 5) result = false;
if (memcmp(bytes, "by\0te", length) != 0) result = false;
Improve Travis Build & Test Coverage Build Wren for more targets, and run the test suite on both 32 and 64 bit builds. * Update the build config to test both with and without NAN_TAGGING defined. * Updatest `util/test.py` to take the executable suffix as a parameter. This allows the makefile to control which binaries will be tested. Adds a new target to the makefile to be run by travis, this runs the test suite against all of the configurations it builds. * Gcc on some 32 bit platforms was complaining about numeric overflows when -INFINITY was used. Update the logic for converting a double to a string to not explicitly check against the literal values. * Make CI builds run the tests on both 64 _and_ 32 bit builds. * If I limit the number of CPUs on my MBP I can get some of the tests to time out, I'm imagining that the specs of the Travis Macs means that the same is happening there too. Updated the test script to allow an extra few seconds for the test to complete successfully before killing it. * Due to slight differences in accuracy in some computations tests were failing on 32 bit builds. Stop comparing things quite as exactly in the cases where it is causing issues. For some reason 12.34 was refusing to compare equal to itself. Bad show 12.34 :-/. I've also updated the test so it doesn't leak handles even if the assertions fail. * Double-cast from `double` to `uint32_t` to prevent undefined behaviour on overflow of basic integers. This should hopefully prevent 32 bit test failures on Linux. * Move to a version of LibUV with a fix for the 32 bit build error on Travis.
2016-10-09 09:18:27 +01:00
if (wrenGetSlotDouble(vm, 3) != 1.5) result = false;
if (strcmp(wrenGetSlotString(vm, 4), "str") != 0) result = false;
2017-03-03 07:57:50 -08:00
if (wrenGetSlotType(vm, 5) != WREN_TYPE_NULL) result = false;
if (result)
{
// Move the value into the return position.
2016-05-20 20:55:28 -07:00
wrenSetSlotHandle(vm, 0, handle);
}
else
{
// If anything failed, return false.
wrenSetSlotBool(vm, 0, false);
}
Improve Travis Build & Test Coverage Build Wren for more targets, and run the test suite on both 32 and 64 bit builds. * Update the build config to test both with and without NAN_TAGGING defined. * Updatest `util/test.py` to take the executable suffix as a parameter. This allows the makefile to control which binaries will be tested. Adds a new target to the makefile to be run by travis, this runs the test suite against all of the configurations it builds. * Gcc on some 32 bit platforms was complaining about numeric overflows when -INFINITY was used. Update the logic for converting a double to a string to not explicitly check against the literal values. * Make CI builds run the tests on both 64 _and_ 32 bit builds. * If I limit the number of CPUs on my MBP I can get some of the tests to time out, I'm imagining that the specs of the Travis Macs means that the same is happening there too. Updated the test script to allow an extra few seconds for the test to complete successfully before killing it. * Due to slight differences in accuracy in some computations tests were failing on 32 bit builds. Stop comparing things quite as exactly in the cases where it is causing issues. For some reason 12.34 was refusing to compare equal to itself. Bad show 12.34 :-/. I've also updated the test so it doesn't leak handles even if the assertions fail. * Double-cast from `double` to `uint32_t` to prevent undefined behaviour on overflow of basic integers. This should hopefully prevent 32 bit test failures on Linux. * Move to a version of LibUV with a fix for the 32 bit build error on Travis.
2016-10-09 09:18:27 +01:00
wrenReleaseHandle(vm, handle);
}
static void slotTypes(WrenVM* vm)
{
bool result =
wrenGetSlotType(vm, 1) == WREN_TYPE_BOOL &&
wrenGetSlotType(vm, 2) == WREN_TYPE_FOREIGN &&
wrenGetSlotType(vm, 3) == WREN_TYPE_LIST &&
wrenGetSlotType(vm, 4) == WREN_TYPE_MAP &&
wrenGetSlotType(vm, 5) == WREN_TYPE_NULL &&
wrenGetSlotType(vm, 6) == WREN_TYPE_NUM &&
wrenGetSlotType(vm, 7) == WREN_TYPE_STRING &&
wrenGetSlotType(vm, 8) == WREN_TYPE_UNKNOWN;
wrenSetSlotBool(vm, 0, result);
}
static void ensure(WrenVM* vm)
{
int before = wrenGetSlotCount(vm);
wrenEnsureSlots(vm, 20);
int after = wrenGetSlotCount(vm);
// Use the slots to make sure they're available.
for (int i = 0; i < 20; i++)
{
wrenSetSlotDouble(vm, i, i);
}
int sum = 0;
for (int i = 0; i < 20; i++)
{
sum += (int)wrenGetSlotDouble(vm, i);
}
char result[100];
sprintf(result, "%d -> %d (%d)", before, after, sum);
wrenSetSlotString(vm, 0, result);
}
static void ensureOutsideForeign(WrenVM* vm)
{
// To test the behavior outside of a foreign method (which we're currently
// in), create a new separate VM.
WrenConfiguration config;
wrenInitConfiguration(&config);
WrenVM* otherVM = wrenNewVM(&config);
int before = wrenGetSlotCount(otherVM);
wrenEnsureSlots(otherVM, 20);
int after = wrenGetSlotCount(otherVM);
// Use the slots to make sure they're available.
for (int i = 0; i < 20; i++)
{
wrenSetSlotDouble(otherVM, i, i);
}
int sum = 0;
for (int i = 0; i < 20; i++)
{
sum += (int)wrenGetSlotDouble(otherVM, i);
}
wrenFreeVM(otherVM);
char result[100];
sprintf(result, "%d -> %d (%d)", before, after, sum);
wrenSetSlotString(vm, 0, result);
}
static void foreignClassAllocate(WrenVM* vm)
{
wrenSetSlotNewForeign(vm, 0, 0, 4);
}
static void getListCount(WrenVM* vm)
{
wrenSetSlotDouble(vm, 0, wrenGetListCount(vm, 1));
}
static void getListElement(WrenVM* vm)
{
int index = (int)wrenGetSlotDouble(vm, 2);
wrenGetListElement(vm, 1, index, 0);
}
static void getMapValue(WrenVM* vm)
{
wrenGetMapValue(vm, 1, 2, 0);
}
WrenForeignMethodFn slotsBindMethod(const char* signature)
{
if (strcmp(signature, "static Slots.noSet") == 0) return noSet;
if (strcmp(signature, "static Slots.getSlots(_,_,_,_,_)") == 0) return getSlots;
2017-03-03 07:57:50 -08:00
if (strcmp(signature, "static Slots.setSlots(_,_,_,_,_)") == 0) return setSlots;
if (strcmp(signature, "static Slots.slotTypes(_,_,_,_,_,_,_,_)") == 0) return slotTypes;
if (strcmp(signature, "static Slots.ensure()") == 0) return ensure;
if (strcmp(signature, "static Slots.ensureOutsideForeign()") == 0) return ensureOutsideForeign;
if (strcmp(signature, "static Slots.getListCount(_)") == 0) return getListCount;
if (strcmp(signature, "static Slots.getListElement(_,_)") == 0) return getListElement;
if (strcmp(signature, "static Slots.getMapValue(_,_)") == 0) return getMapValue;
return NULL;
}
void slotsBindClass(const char* className, WrenForeignClassMethods* methods)
{
methods->allocate = foreignClassAllocate;
}