2010-01-09 20:20:14 +01:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib/self_test.h"
|
|
|
|
|
|
|
|
#include "simulation2/serialization/DebugSerializer.h"
|
|
|
|
#include "simulation2/serialization/HashSerializer.h"
|
|
|
|
#include "simulation2/serialization/StdSerializer.h"
|
|
|
|
#include "simulation2/serialization/StdDeserializer.h"
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
|
2010-05-25 19:28:26 +02:00
|
|
|
#include "graphics/MapReader.h"
|
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
#include "lib/timer.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "ps/CLogger.h"
|
2010-05-25 19:28:26 +02:00
|
|
|
#include "ps/Filesystem.h"
|
|
|
|
#include "ps/Loader.h"
|
|
|
|
#include "ps/XML/Xeromyces.h"
|
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
|
|
|
|
#include "callgrind.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-10 12:31:31 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
#define TS_ASSERT_STREAM(stream, len, buffer) \
|
|
|
|
TS_ASSERT_EQUALS(stream.str().length(), (size_t)len); \
|
|
|
|
TS_ASSERT_SAME_DATA(stream.str().data(), buffer, len)
|
|
|
|
#define TSM_ASSERT_STREAM(m, stream, len, buffer) \
|
|
|
|
TSM_ASSERT_EQUALS(m, stream.str().length(), (size_t)len); \
|
|
|
|
TSM_ASSERT_SAME_DATA(m, stream.str().data(), buffer, len)
|
|
|
|
|
|
|
|
class TestSerializer : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void serialize_types(ISerializer& serialize)
|
|
|
|
{
|
|
|
|
serialize.NumberU8_Unbounded("u8", 255);
|
|
|
|
serialize.NumberI32_Unbounded("i32", -123);
|
|
|
|
serialize.NumberU32_Unbounded("u32", (unsigned)-123);
|
|
|
|
serialize.NumberFloat_Unbounded("float", 1e+30f);
|
|
|
|
serialize.NumberDouble_Unbounded("double", 1e+300);
|
2010-05-02 22:32:37 +02:00
|
|
|
serialize.NumberFixed_Unbounded("fixed", fixed::FromFloat(1234.5f));
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
serialize.Bool("t", true);
|
|
|
|
serialize.Bool("f", false);
|
|
|
|
|
|
|
|
serialize.StringASCII("string", "example", 0, 255);
|
|
|
|
serialize.StringASCII("string 2", "example\"\\\"", 0, 255);
|
|
|
|
serialize.StringASCII("string 3", "example\n\ntest", 0, 255);
|
|
|
|
|
|
|
|
wchar_t testw[] = { 't', 0xEA, 's', 't', 0 };
|
|
|
|
serialize.String("string 4", testw, 0, 255);
|
|
|
|
|
|
|
|
serialize.RawBytes("raw bytes", (const u8*)"\0\1\2\3\x0f\x10", 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Debug_basic()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
std::stringstream stream;
|
|
|
|
CDebugSerializer serialize(script, stream);
|
|
|
|
serialize.NumberI32_Unbounded("x", -123);
|
|
|
|
serialize.NumberU32_Unbounded("y", 1234);
|
|
|
|
serialize.NumberI32("z", 12345, 0, 65535);
|
|
|
|
TS_ASSERT_STR_EQUALS(stream.str(), "x: -123\ny: 1234\nz: 12345\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Debug_floats()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
std::stringstream stream;
|
|
|
|
CDebugSerializer serialize(script, stream);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e4f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e-4f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e5f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e-5f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e6f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e-6f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e10f);
|
|
|
|
serialize.NumberFloat_Unbounded("x", 1e-10f);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e4);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e-4);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e5);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e-5);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e6);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e-6);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e10);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e-10);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e100);
|
|
|
|
serialize.NumberDouble_Unbounded("x", 1e-100);
|
2010-05-02 22:32:37 +02:00
|
|
|
serialize.NumberFixed_Unbounded("x", fixed::FromDouble(1e4));
|
2010-01-09 20:20:14 +01:00
|
|
|
TS_ASSERT_STR_EQUALS(stream.str(),
|
|
|
|
"x: 10000\nx: 0.0001\nx: 100000\nx: 1e-05\nx: 1e+06\nx: 1e-06\nx: 1e+10\nx: 1e-10\n"
|
|
|
|
"x: 10000\nx: 0.0001\nx: 100000\nx: 1e-05\nx: 1e+06\nx: 1e-06\nx: 1e+10\nx: 1e-10\nx: 1e+100\nx: 1e-100\n"
|
2010-05-02 22:32:37 +02:00
|
|
|
"x: 10000\n"
|
2010-01-09 20:20:14 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Debug_types()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
std::stringstream stream;
|
|
|
|
CDebugSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
serialize.Comment("comment");
|
|
|
|
|
|
|
|
serialize_types(serialize);
|
|
|
|
|
|
|
|
TS_ASSERT_STR_EQUALS(stream.str(),
|
|
|
|
"# comment\n"
|
|
|
|
"u8: 255\n"
|
|
|
|
"i32: -123\n"
|
|
|
|
"u32: 4294967173\n"
|
|
|
|
"float: 1e+30\n"
|
|
|
|
"double: 1e+300\n"
|
|
|
|
"fixed: 1234.5\n"
|
|
|
|
"t: true\n"
|
|
|
|
"f: false\n"
|
|
|
|
"string: \"example\"\n"
|
|
|
|
"string 2: \"example\\\"\\\\\\\"\"\n" // C-escaped form of: "example\"\\\""
|
|
|
|
"string 3: \"example\\n\\ntest\"\n"
|
|
|
|
"string 4: \"t\xC3\xAAst\"\n"
|
|
|
|
"raw bytes: (6 bytes) 00 01 02 03 0f 10\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Std_basic()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
std::stringstream stream;
|
|
|
|
CStdSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
serialize.NumberI32_Unbounded("x", -123);
|
|
|
|
serialize.NumberU32_Unbounded("y", 1234);
|
|
|
|
serialize.NumberI32("z", 12345, 0, 65535);
|
|
|
|
|
|
|
|
TS_ASSERT_STREAM(stream, 12, "\x85\xff\xff\xff" "\xd2\x04\x00\x00" "\x39\x30\x00\x00");
|
|
|
|
|
|
|
|
CStdDeserializer deserialize(script, stream);
|
|
|
|
int32_t n;
|
|
|
|
|
|
|
|
deserialize.NumberI32_Unbounded(n);
|
|
|
|
TS_ASSERT_EQUALS(n, -123);
|
|
|
|
deserialize.NumberI32_Unbounded(n);
|
|
|
|
TS_ASSERT_EQUALS(n, 1234);
|
|
|
|
deserialize.NumberI32(n, 0, 65535);
|
|
|
|
TS_ASSERT_EQUALS(n, 12345);
|
|
|
|
|
|
|
|
TS_ASSERT(stream.good());
|
|
|
|
TS_ASSERT_EQUALS(stream.peek(), EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Std_types()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
std::stringstream stream;
|
|
|
|
CStdSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
serialize_types(serialize);
|
|
|
|
|
|
|
|
CStdDeserializer deserialize(script, stream);
|
|
|
|
uint8_t u8v;
|
|
|
|
int32_t i32v;
|
|
|
|
uint32_t u32v;
|
|
|
|
float flt;
|
|
|
|
double dbl;
|
2010-05-02 22:32:37 +02:00
|
|
|
fixed fxd;
|
2010-01-09 20:20:14 +01:00
|
|
|
bool bl;
|
|
|
|
std::string str;
|
|
|
|
std::wstring wstr;
|
|
|
|
u8 cbuf[256];
|
|
|
|
|
|
|
|
deserialize.NumberU8_Unbounded(u8v);
|
|
|
|
TS_ASSERT_EQUALS(u8v, 255);
|
|
|
|
deserialize.NumberI32_Unbounded(i32v);
|
|
|
|
TS_ASSERT_EQUALS(i32v, -123);
|
|
|
|
deserialize.NumberU32_Unbounded(u32v);
|
2010-03-31 22:33:42 +02:00
|
|
|
TS_ASSERT_EQUALS(u32v, 4294967173u);
|
2010-01-09 20:20:14 +01:00
|
|
|
deserialize.NumberFloat_Unbounded(flt);
|
|
|
|
TS_ASSERT_EQUALS(flt, 1e+30f);
|
|
|
|
deserialize.NumberDouble_Unbounded(dbl);
|
|
|
|
TS_ASSERT_EQUALS(dbl, 1e+300);
|
2010-05-02 22:32:37 +02:00
|
|
|
deserialize.NumberFixed_Unbounded(fxd);
|
|
|
|
TS_ASSERT_EQUALS(fxd.ToDouble(), 1234.5);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
deserialize.Bool(bl);
|
|
|
|
TS_ASSERT_EQUALS(bl, true);
|
|
|
|
deserialize.Bool(bl);
|
|
|
|
TS_ASSERT_EQUALS(bl, false);
|
|
|
|
|
|
|
|
deserialize.StringASCII(str, 0, 255);
|
|
|
|
TS_ASSERT_STR_EQUALS(str, "example");
|
|
|
|
deserialize.StringASCII(str, 0, 255);
|
|
|
|
TS_ASSERT_STR_EQUALS(str, "example\"\\\"");
|
|
|
|
deserialize.StringASCII(str, 0, 255);
|
|
|
|
TS_ASSERT_STR_EQUALS(str, "example\n\ntest");
|
|
|
|
|
|
|
|
wchar_t testw[] = { 't', 0xEA, 's', 't', 0 };
|
|
|
|
deserialize.String(wstr, 0, 255);
|
|
|
|
TS_ASSERT_WSTR_EQUALS(wstr, testw);
|
|
|
|
|
|
|
|
cbuf[6] = 0x42; // sentinel
|
|
|
|
deserialize.RawBytes(cbuf, 6);
|
|
|
|
TS_ASSERT_SAME_DATA(cbuf, (const u8*)"\0\1\2\3\x0f\x10\x42", 7);
|
|
|
|
|
|
|
|
TS_ASSERT(stream.good());
|
|
|
|
TS_ASSERT_EQUALS(stream.peek(), EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Hash_basic()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
CHashSerializer serialize(script);
|
|
|
|
|
|
|
|
serialize.NumberI32_Unbounded("x", -123);
|
|
|
|
serialize.NumberU32_Unbounded("y", 1234);
|
|
|
|
serialize.NumberI32("z", 12345, 0, 65535);
|
|
|
|
|
2010-01-10 20:29:27 +01:00
|
|
|
TS_ASSERT_EQUALS(serialize.GetHashLength(), (size_t)16);
|
|
|
|
TS_ASSERT_SAME_DATA(serialize.ComputeHash(), "\xa0\x3a\xe5\x3e\x9b\xd7\xfb\x11\x88\x35\xc6\xfb\xb9\x94\xa9\x72", 16);
|
|
|
|
// echo -en "\x85\xff\xff\xff\xd2\x04\x00\x00\x39\x30\x00\x00" | openssl md5 | perl -pe 's/(..)/\\x$1/g'
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_bounds()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
std::stringstream stream;
|
|
|
|
CDebugSerializer serialize(script, stream);
|
|
|
|
serialize.NumberI32("x", 16, -16, 16);
|
|
|
|
serialize.NumberI32("x", -16, -16, 16);
|
|
|
|
TS_ASSERT_THROWS(serialize.NumberI32("x", 17, -16, 16), PSERROR_Serialize_OutOfBounds);
|
|
|
|
TS_ASSERT_THROWS(serialize.NumberI32("x", -17, -16, 16), PSERROR_Serialize_OutOfBounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test exceptions more thoroughly
|
|
|
|
|
|
|
|
void test_script_basic()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
CScriptVal obj;
|
|
|
|
TS_ASSERT(script.Eval("({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})", obj));
|
|
|
|
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
CDebugSerializer serialize(script, stream);
|
|
|
|
serialize.ScriptVal("script", obj);
|
2010-03-19 21:54:44 +01:00
|
|
|
TS_ASSERT_STR_EQUALS(stream.str(), "script: ({x:123, y:[1, 1.5, \"2\", \"test\", (void 0), null, true, false]})\n");
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
CStdSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
serialize.ScriptVal("script", obj);
|
|
|
|
|
2010-05-25 20:07:41 +02:00
|
|
|
TS_ASSERT_STREAM(stream, 115,
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x03" // SCRIPT_TYPE_OBJECT
|
|
|
|
"\x02\0\0\0" // num props
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "x\0" // "x"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x05" // SCRIPT_TYPE_INT
|
|
|
|
"\x7b\0\0\0" // 123
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "y\0" // "y"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x02" // SCRIPT_TYPE_ARRAY
|
|
|
|
"\x08\0\0\0" // num props
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "0\0" // "0"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x05" "\x01\0\0\0" // SCRIPT_TYPE_INT 1
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "1\0" // "1"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x06" "\0\0\0\0\0\0\xf8\x3f" // SCRIPT_TYPE_DOUBLE 1.5
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "2\0" // "2"
|
|
|
|
"\x04" "\x01\0\0\0" "2\0" // SCRIPT_TYPE_STRING "2"
|
|
|
|
"\x01\0\0\0" "3\0" // "3"
|
|
|
|
"\x04" "\x04\0\0\0" "t\0e\0s\0t\0" // SCRIPT_TYPE_STRING "test"
|
|
|
|
"\x01\0\0\0" "4\0" // "4"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x00" // SCRIPT_TYPE_VOID
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "5\0" // "5"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x01" // SCRIPT_TYPE_NULL
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "6\0" // "6"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x07" "\x01" // SCRIPT_TYPE_BOOLEAN true
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "7\0" // "7"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x07" "\x00" // SCRIPT_TYPE_BOOLEAN false
|
|
|
|
);
|
|
|
|
|
|
|
|
CStdDeserializer deserialize(script, stream);
|
|
|
|
|
|
|
|
jsval newobj;
|
|
|
|
deserialize.ScriptVal(newobj);
|
|
|
|
TS_ASSERT(stream.good());
|
|
|
|
TS_ASSERT_EQUALS(stream.peek(), EOF);
|
|
|
|
|
|
|
|
std::string source;
|
|
|
|
TS_ASSERT(script.CallFunction(newobj, "toSource", source));
|
2010-03-19 21:54:44 +01:00
|
|
|
TS_ASSERT_STR_EQUALS(source, "({x:123, y:[1, 1.5, \"2\", \"test\", (void 0), null, true, false]})");
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL)
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
CScriptVal obj;
|
|
|
|
TSM_ASSERT(msg, script.Eval(input, obj));
|
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
CStdSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
serialize.ScriptVal("script", obj);
|
|
|
|
|
|
|
|
if (expstream)
|
|
|
|
{
|
|
|
|
TSM_ASSERT_STREAM(msg, stream, expstreamlen, expstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
CStdDeserializer deserialize(script, stream);
|
|
|
|
|
|
|
|
jsval newobj;
|
|
|
|
deserialize.ScriptVal(newobj);
|
|
|
|
TSM_ASSERT(msg, stream.good());
|
|
|
|
TSM_ASSERT_EQUALS(msg, stream.peek(), EOF);
|
|
|
|
|
|
|
|
std::string source;
|
|
|
|
TSM_ASSERT(msg, script.CallFunction(newobj, "toSource", source));
|
|
|
|
TS_ASSERT_STR_EQUALS(source, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_script_unicode()
|
|
|
|
{
|
|
|
|
helper_script_roundtrip("unicode", "({"
|
|
|
|
"'x': \"\\x01\\x80\\xff\\u0100\\ud7ff\", "
|
|
|
|
"'y': \"\\ue000\\ufffd\""
|
|
|
|
"})",
|
|
|
|
/* expected: */
|
|
|
|
"({"
|
|
|
|
"x:\"\\x01\\x80\\xFF\\u0100\\uD7FF\", "
|
|
|
|
"y:\"\\uE000\\uFFFD\""
|
|
|
|
"})");
|
|
|
|
|
2010-05-25 20:07:41 +02:00
|
|
|
// Disabled since we no longer do the UTF-8 conversion that rejects invalid characters
|
|
|
|
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 1", "(\"\\ud7ff\\ud800\")", "..."), PSERROR_Serialize_InvalidCharInString);
|
|
|
|
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 2", "(\"\\udfff\")", "..."), PSERROR_Serialize_InvalidCharInString);
|
|
|
|
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 3", "(\"\\uffff\")", "..."), PSERROR_Serialize_InvalidCharInString);
|
|
|
|
// TS_ASSERT_THROWS(helper_script_roundtrip("invalid chars 4", "(\"\\ud800\\udc00\")" /* U+10000 */, "..."), PSERROR_Serialize_InvalidCharInString);
|
|
|
|
helper_script_roundtrip("unicode", "\"\\ud800\\uffff\"", "(new String(\"\\uD800\\uFFFF\"))");
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TODO_test_script_objects()
|
|
|
|
{
|
|
|
|
helper_script_roundtrip("Number", "([1, new Number('2.0'), 3])", "([1, new Number(2), 3])");
|
|
|
|
helper_script_roundtrip("Number with props", "var n=new Number('2.0'); n.foo='bar'; n", "(new Number(2))");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_script_property_order()
|
|
|
|
{
|
|
|
|
helper_script_roundtrip("prop order 1", "var x={}; x.a=1; x.f=2; x.b=7; x.d=3; x", "({a:1, f:2, b:7, d:3})");
|
|
|
|
helper_script_roundtrip("prop order 2", "var x={}; x.d=3; x.a=1; x.f=2; x.b=7; x", "({d:3, a:1, f:2, b:7})");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_script_numbers()
|
|
|
|
{
|
|
|
|
const char stream[] = "\x02" // SCRIPT_TYPE_ARRAY
|
|
|
|
"\x04\0\0\0" // num props
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "0\0" // "0"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x05" "\x00\0\0\xC0" // SCRIPT_TYPE_INT -1073741824 (JS_INT_MIN)
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "1\0" // "1"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x06" "\0\0\x40\0\0\0\xD0\xC1" // SCRIPT_TYPE_DOUBLE -1073741825 (JS_INT_MIN-1)
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "2\0" // "2"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x05" "\xFF\xFF\xFF\x3F" // SCRIPT_TYPE_INT 1073741823
|
2010-05-25 20:07:41 +02:00
|
|
|
"\x01\0\0\0" "3\0" // "3"
|
2010-01-09 20:20:14 +01:00
|
|
|
"\x06" "\0\0\0\0\0\0\xD0\x41" // SCRIPT_TYPE_DOUBLE 1073741824
|
|
|
|
;
|
|
|
|
|
|
|
|
helper_script_roundtrip("numbers", "[-1073741824, -1073741825, 1.073741823e+9, 1073741824]",
|
|
|
|
"[-1073741824, -1073741825, 1073741823, 1073741824]", sizeof(stream) - 1, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_script_exceptions()
|
|
|
|
{
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
CScriptVal obj;
|
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
CStdSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
TestLogger logger;
|
|
|
|
|
|
|
|
TS_ASSERT(script.Eval("({x:1, y:<x/>})", obj));
|
|
|
|
TS_ASSERT_THROWS(serialize.ScriptVal("script", obj), PSERROR_Serialize_InvalidScriptValue);
|
|
|
|
|
|
|
|
TS_ASSERT(script.Eval("([1, 2, function () { }])", obj));
|
|
|
|
TS_ASSERT_THROWS(serialize.ScriptVal("script", obj), PSERROR_Serialize_InvalidScriptValue);
|
|
|
|
}
|
|
|
|
|
2010-06-27 13:57:00 +02:00
|
|
|
void test_script_splice()
|
|
|
|
{
|
|
|
|
helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 1); a", "[20]");
|
|
|
|
helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 2); a", "[]");
|
|
|
|
helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 0, 5); a", "[5, 10, 20]");
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// TODO: test deserializing invalid streams
|
|
|
|
|
|
|
|
// TODO: test non-tree script structures
|
|
|
|
// (not critical since TestComponentManager::test_script_serialization indirectly tests that already)
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestSerializerPerf : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
2010-05-25 19:28:26 +02:00
|
|
|
void test_script_props_DISABLED()
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x";
|
|
|
|
|
|
|
|
ScriptInterface script("Test");
|
|
|
|
CScriptVal obj;
|
|
|
|
TS_ASSERT(script.Eval(input, obj));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 256; ++i)
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
CStdSerializer serialize(script, stream);
|
|
|
|
|
|
|
|
serialize.ScriptVal("script", obj);
|
|
|
|
|
|
|
|
CStdDeserializer deserialize(script, stream);
|
|
|
|
|
|
|
|
jsval newobj;
|
|
|
|
deserialize.ScriptVal(newobj);
|
|
|
|
TS_ASSERT(stream.good());
|
|
|
|
TS_ASSERT_EQUALS(stream.peek(), EOF);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
std::string source;
|
|
|
|
TS_ASSERT(script.CallFunction(newobj, "toSource", source));
|
|
|
|
std::cout << source << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 19:28:26 +02:00
|
|
|
|
|
|
|
void test_hash_DISABLED()
|
|
|
|
{
|
|
|
|
CXeromyces::Startup();
|
|
|
|
|
|
|
|
g_VFS = CreateVfs(20 * MiB);
|
|
|
|
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/L"mods/public", VFS_MOUNT_MUST_EXIST));
|
|
|
|
TS_ASSERT_OK(g_VFS->Mount(L"cache/", DataDir()/L"cache"));
|
|
|
|
|
|
|
|
CTerrain terrain;
|
|
|
|
|
|
|
|
CSimulation2 sim2(NULL, &terrain);
|
|
|
|
sim2.LoadDefaultScripts();
|
|
|
|
sim2.ResetState();
|
|
|
|
|
|
|
|
CMapReader* mapReader = new CMapReader(); // it'll call "delete this" itself
|
|
|
|
|
|
|
|
LDR_BeginRegistering();
|
|
|
|
mapReader->LoadMap(L"maps/scenarios/Latium.pmp", &terrain, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &sim2, NULL);
|
|
|
|
LDR_EndRegistering();
|
|
|
|
TS_ASSERT_OK(LDR_NonprogressiveLoad());
|
|
|
|
|
|
|
|
sim2.Update(0);
|
|
|
|
|
|
|
|
{
|
|
|
|
std::stringstream str;
|
|
|
|
std::string hash;
|
|
|
|
sim2.SerializeState(str);
|
|
|
|
sim2.ComputeStateHash(hash);
|
2010-05-25 19:55:41 +02:00
|
|
|
debug_printf(L"\n");
|
|
|
|
debug_printf(L"# size = %d\n", (int)str.str().length());
|
|
|
|
debug_printf(L"# hash = ");
|
2010-05-25 19:28:26 +02:00
|
|
|
for (size_t i = 0; i < hash.size(); ++i)
|
2010-05-25 19:55:41 +02:00
|
|
|
debug_printf(L"%02x", (u8)hash[i]);
|
|
|
|
debug_printf(L"\n");
|
2010-05-25 19:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double t = timer_Time();
|
|
|
|
CALLGRIND_START_INSTRUMENTATION
|
|
|
|
size_t reps = 128;
|
|
|
|
for (size_t i = 0; i < reps; ++i)
|
|
|
|
{
|
|
|
|
std::string hash;
|
|
|
|
sim2.ComputeStateHash(hash);
|
|
|
|
}
|
|
|
|
CALLGRIND_STOP_INSTRUMENTATION
|
|
|
|
t = timer_Time() - t;
|
2010-05-25 19:55:41 +02:00
|
|
|
debug_printf(L"# time = %f (%f/%d)\n", t/reps, t, (int)reps);
|
2010-05-25 19:28:26 +02:00
|
|
|
|
|
|
|
// Shut down the world
|
|
|
|
g_VFS.reset();
|
|
|
|
CXeromyces::Terminate();
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|