Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/tests/test_ScriptConversions.h
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 13 Lines | |||||
#include "maths/FixedVector2D.h" | #include "maths/FixedVector2D.h" | ||||
#include "maths/FixedVector3D.h" | #include "maths/FixedVector3D.h" | ||||
#include "maths/MathUtil.h" | #include "maths/MathUtil.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "jsapi.h" | #include "jsapi.h" | ||||
cassert(JSVAL_INT_MIN == -2147483648); | |||||
cassert(JSVAL_INT_MAX == 2147483647); | |||||
class TestScriptConversions : public CxxTest::TestSuite | class TestScriptConversions : public CxxTest::TestSuite | ||||
{ | { | ||||
template <typename T> | template <typename T> | ||||
void convert_to(const T& value, const std::string& expected) | void convert_to(const T& value, const std::string& expected) | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptRuntime); | ||||
TS_ASSERT(script.LoadGlobalScripts()); | TS_ASSERT(script.LoadGlobalScripts()); | ||||
JSContext* cx = script.GetContext(); | JSContext* cx = script.GetContext(); | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | void test_roundtrip() | ||||
roundtrip<float>(0, "0"); | roundtrip<float>(0, "0"); | ||||
roundtrip<float>(0.5, "0.5"); | roundtrip<float>(0.5, "0.5"); | ||||
roundtrip<float>(1e9f, "1000000000"); | roundtrip<float>(1e9f, "1000000000"); | ||||
roundtrip<float>(1e30f, "1.0000000150474662e+30"); | roundtrip<float>(1e30f, "1.0000000150474662e+30"); | ||||
roundtrip<i32>(0, "0"); | roundtrip<i32>(0, "0"); | ||||
roundtrip<i32>(123, "123"); | roundtrip<i32>(123, "123"); | ||||
roundtrip<i32>(-123, "-123"); | roundtrip<i32>(-123, "-123"); | ||||
roundtrip<i32>(1073741822, "1073741822"); // JSVAL_INT_MAX-1 | roundtrip<i32>(2147483646, "2147483646"); // JSVAL_INT_MAX-1 | ||||
roundtrip<i32>(1073741823, "1073741823"); // JSVAL_INT_MAX | roundtrip<i32>(2147483647, "2147483647"); // JSVAL_INT_MAX | ||||
roundtrip<i32>(-1073741823, "-1073741823"); // JSVAL_INT_MIN+1 | roundtrip<i32>(-2147483647, "-2147483647"); // JSVAL_INT_MIN+1 | ||||
roundtrip<i32>(-1073741824, "-1073741824"); // JSVAL_INT_MIN | roundtrip<i32>(-2147483648, "-2147483648"); // JSVAL_INT_MIN | ||||
wraitii: If this is valid for SM38, why is this changing? | |||||
ItmsAuthorUnsubmitted Done Inline ActionsThis should have been changed in a previous SM upgrade, yes. I don't know which one bumped jsval limits to the current values, but the new cassert will allow us to not miss future changes. Note that this is valid because the old value is now completely irrelevant and is a i32 value like any other. Itms: This should have been changed in a previous SM upgrade, yes. I don't know which one bumped… | |||||
wraitiiUnsubmitted Not Done Inline ActionsAh, right. Any reason yo not use JSVALA_INT_MAX directly here? wraitii: Ah, right. Any reason yo not use JSVALA_INT_MAX directly here? | |||||
ItmsAuthorUnsubmitted Done Inline ActionsAh, it could be used for the first argument yes indeed, and would remove the need for the cassert and all those comments. I basically kept things as they were, and it's quite readable this way, but doing what you suggest is probably better. Itms: Ah, it could be used for the first argument yes indeed, and would remove the need for the… | |||||
roundtrip<u32>(0, "0"); | roundtrip<u32>(0, "0"); | ||||
roundtrip<u32>(123, "123"); | roundtrip<u32>(123, "123"); | ||||
roundtrip<u32>(1073741822, "1073741822"); // JSVAL_INT_MAX-1 | roundtrip<u32>(2147483646, "2147483646"); // JSVAL_INT_MAX-1 | ||||
roundtrip<u32>(1073741823, "1073741823"); // JSVAL_INT_MAX | roundtrip<u32>(2147483647, "2147483647"); // JSVAL_INT_MAX | ||||
{ | roundtrip<u32>(2147483648, "2147483648"); // JSVAL_INT_MAX+1 | ||||
TestLogger log; // swallow warnings about values not being stored as integer JS::Values | |||||
roundtrip<i32>(1073741824, "1073741824"); // JSVAL_INT_MAX+1 | |||||
roundtrip<i32>(-1073741825, "-1073741825"); // JSVAL_INT_MIN-1 | |||||
roundtrip<u32>(1073741824, "1073741824"); // JSVAL_INT_MAX+1 | |||||
} | |||||
std::string s1 = "test"; | std::string s1 = "test"; | ||||
s1[1] = '\0'; | s1[1] = '\0'; | ||||
std::string s2 = "тест"; | std::string s2 = "тест"; | ||||
s2[2] = s2[3] = '\0'; | s2[2] = s2[3] = '\0'; | ||||
std::wstring w1 = L"test"; | std::wstring w1 = L"test"; | ||||
w1[1] = '\0'; | w1[1] = '\0'; | ||||
▲ Show 20 Lines • Show All 110 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
If this is valid for SM38, why is this changing?