Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/tests/test_Serializer.h
Show First 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | void serialize_types(ISerializer& serialize) | ||||
wchar_t testw[] = { 't', 0xEA, 's', 't', 0 }; | wchar_t testw[] = { 't', 0xEA, 's', 't', 0 }; | ||||
serialize.String("string 4", testw, 0, 255); | serialize.String("string 4", testw, 0, 255); | ||||
serialize.RawBytes("raw bytes", (const u8*)"\0\1\2\3\x0f\x10", 6); | serialize.RawBytes("raw bytes", (const u8*)"\0\1\2\3\x0f\x10", 6); | ||||
} | } | ||||
void test_Debug_basic() | void test_Debug_basic() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CDebugSerializer serialize(script, stream); | CDebugSerializer serialize(script, stream); | ||||
serialize.NumberI32_Unbounded("x", -123); | serialize.NumberI32_Unbounded("x", -123); | ||||
serialize.NumberU32_Unbounded("y", 1234); | serialize.NumberU32_Unbounded("y", 1234); | ||||
serialize.NumberI32("z", 12345, 0, 65535); | serialize.NumberI32("z", 12345, 0, 65535); | ||||
TS_ASSERT_STR_EQUALS(stream.str(), "x: -123\ny: 1234\nz: 12345\n"); | TS_ASSERT_STR_EQUALS(stream.str(), "x: -123\ny: 1234\nz: 12345\n"); | ||||
} | } | ||||
void test_Debug_floats() | void test_Debug_floats() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CDebugSerializer serialize(script, stream); | CDebugSerializer serialize(script, stream); | ||||
serialize.NumberFloat_Unbounded("x", 1e4f); | serialize.NumberFloat_Unbounded("x", 1e4f); | ||||
serialize.NumberFloat_Unbounded("x", 1e-4f); | serialize.NumberFloat_Unbounded("x", 1e-4f); | ||||
serialize.NumberFloat_Unbounded("x", 1e5f); | serialize.NumberFloat_Unbounded("x", 1e5f); | ||||
serialize.NumberFloat_Unbounded("x", 1e-5f); | serialize.NumberFloat_Unbounded("x", 1e-5f); | ||||
serialize.NumberFloat_Unbounded("x", 1e6f); | serialize.NumberFloat_Unbounded("x", 1e6f); | ||||
serialize.NumberFloat_Unbounded("x", 1e-6f); | serialize.NumberFloat_Unbounded("x", 1e-6f); | ||||
Show All 14 Lines | TS_ASSERT_STR_EQUALS(stream.str(), | ||||
"x: 10000\nx: 9.9999997e-05\nx: 100000\nx: 9.9999997e-06\nx: 1000000\nx: 1e-06\nx: 1e+10\nx: 1e-10\n" | "x: 10000\nx: 9.9999997e-05\nx: 100000\nx: 9.9999997e-06\nx: 1000000\nx: 1e-06\nx: 1e+10\nx: 1e-10\n" | ||||
"x: 10000\nx: 0.0001\nx: 100000\nx: 1.0000000000000001e-05\nx: 1000000\nx: 9.9999999999999995e-07\nx: 10000000000\nx: 1e-10\nx: 1e+100\nx: 1e-100\n" | "x: 10000\nx: 0.0001\nx: 100000\nx: 1.0000000000000001e-05\nx: 1000000\nx: 9.9999999999999995e-07\nx: 10000000000\nx: 1e-10\nx: 1e+100\nx: 1e-100\n" | ||||
"x: 10000\n" | "x: 10000\n" | ||||
); | ); | ||||
} | } | ||||
void test_Debug_types() | void test_Debug_types() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CDebugSerializer serialize(script, stream); | CDebugSerializer serialize(script, stream); | ||||
serialize.Comment("comment"); | serialize.Comment("comment"); | ||||
serialize_types(serialize); | serialize_types(serialize); | ||||
TS_ASSERT_STR_EQUALS(stream.str(), | TS_ASSERT_STR_EQUALS(stream.str(), | ||||
Show All 14 Lines | TS_ASSERT_STR_EQUALS(stream.str(), | ||||
"string 3: \"example\\n\\ntest\"\n" | "string 3: \"example\\n\\ntest\"\n" | ||||
"string 4: \"t\xC3\xAAst\"\n" | "string 4: \"t\xC3\xAAst\"\n" | ||||
"raw bytes: (6 bytes) 00 01 02 03 0f 10\n" | "raw bytes: (6 bytes) 00 01 02 03 0f 10\n" | ||||
); | ); | ||||
} | } | ||||
void test_Std_basic() | void test_Std_basic() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CStdSerializer serialize(script, stream); | CStdSerializer serialize(script, stream); | ||||
serialize.NumberI32_Unbounded("x", -123); | serialize.NumberI32_Unbounded("x", -123); | ||||
serialize.NumberU32_Unbounded("y", 1234); | serialize.NumberU32_Unbounded("y", 1234); | ||||
serialize.NumberI32("z", 12345, 0, 65535); | serialize.NumberI32("z", 12345, 0, 65535); | ||||
TS_ASSERT_STREAM(stream, 12, "\x85\xff\xff\xff" "\xd2\x04\x00\x00" "\x39\x30\x00\x00"); | TS_ASSERT_STREAM(stream, 12, "\x85\xff\xff\xff" "\xd2\x04\x00\x00" "\x39\x30\x00\x00"); | ||||
Show All 10 Lines | void test_Std_basic() | ||||
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions | // NOTE: Don't use good() here - it fails due to a bug in older libc++ versions | ||||
TS_ASSERT(!stream.bad() && !stream.fail()); | TS_ASSERT(!stream.bad() && !stream.fail()); | ||||
TS_ASSERT_EQUALS(stream.peek(), EOF); | TS_ASSERT_EQUALS(stream.peek(), EOF); | ||||
} | } | ||||
void test_Std_types() | void test_Std_types() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CStdSerializer serialize(script, stream); | CStdSerializer serialize(script, stream); | ||||
serialize_types(serialize); | serialize_types(serialize); | ||||
CStdDeserializer deserialize(script, stream); | CStdDeserializer deserialize(script, stream); | ||||
int8_t i8v; | int8_t i8v; | ||||
uint8_t u8v; | uint8_t u8v; | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | void test_Std_types() | ||||
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions | // NOTE: Don't use good() here - it fails due to a bug in older libc++ versions | ||||
TS_ASSERT(!stream.bad() && !stream.fail()); | TS_ASSERT(!stream.bad() && !stream.fail()); | ||||
TS_ASSERT_EQUALS(stream.peek(), EOF); | TS_ASSERT_EQUALS(stream.peek(), EOF); | ||||
} | } | ||||
void test_Hash_basic() | void test_Hash_basic() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
CHashSerializer serialize(script); | CHashSerializer serialize(script); | ||||
serialize.NumberI32_Unbounded("x", -123); | serialize.NumberI32_Unbounded("x", -123); | ||||
serialize.NumberU32_Unbounded("y", 1234); | serialize.NumberU32_Unbounded("y", 1234); | ||||
serialize.NumberI32("z", 12345, 0, 65535); | serialize.NumberI32("z", 12345, 0, 65535); | ||||
TS_ASSERT_EQUALS(serialize.GetHashLength(), (size_t)16); | 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); | 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 -binary | xxd -p | perl -pe 's/(..)/\\x$1/g' | // echo -en "\x85\xff\xff\xff\xd2\x04\x00\x00\x39\x30\x00\x00" | openssl md5 -binary | xxd -p | perl -pe 's/(..)/\\x$1/g' | ||||
} | } | ||||
void test_Hash_stream() | void test_Hash_stream() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
CHashSerializer hashSerialize(script); | CHashSerializer hashSerialize(script); | ||||
hashSerialize.NumberI32_Unbounded("x", -123); | hashSerialize.NumberI32_Unbounded("x", -123); | ||||
hashSerialize.NumberU32_Unbounded("y", 1234); | hashSerialize.NumberU32_Unbounded("y", 1234); | ||||
hashSerialize.NumberI32("z", 12345, 0, 65535); | hashSerialize.NumberI32("z", 12345, 0, 65535); | ||||
ISerializer& serialize = hashSerialize; | ISerializer& serialize = hashSerialize; | ||||
{ | { | ||||
CStdSerializer streamSerialize(script, serialize.GetStream()); | CStdSerializer streamSerialize(script, serialize.GetStream()); | ||||
streamSerialize.NumberI32_Unbounded("x2", -456); | streamSerialize.NumberI32_Unbounded("x2", -456); | ||||
streamSerialize.NumberU32_Unbounded("y2", 5678); | streamSerialize.NumberU32_Unbounded("y2", 5678); | ||||
streamSerialize.NumberI32("z2", 45678, 0, 65535); | streamSerialize.NumberI32("z2", 45678, 0, 65535); | ||||
} | } | ||||
TS_ASSERT_EQUALS(hashSerialize.GetHashLength(), (size_t)16); | TS_ASSERT_EQUALS(hashSerialize.GetHashLength(), (size_t)16); | ||||
TS_ASSERT_SAME_DATA(hashSerialize.ComputeHash(), "\x5c\xff\x33\xd1\x72\xdd\x6d\x77\xa8\xd4\xa1\xf6\x84\xcc\xaa\x10", 16); | TS_ASSERT_SAME_DATA(hashSerialize.ComputeHash(), "\x5c\xff\x33\xd1\x72\xdd\x6d\x77\xa8\xd4\xa1\xf6\x84\xcc\xaa\x10", 16); | ||||
// echo -en "\x85\xff\xff\xff\xd2\x04\x00\x00\x39\x30\x00\x00\x38\xfe\xff\xff\x2e\x16\x00\x00\x6e\xb2\x00\x00" | openssl md5 -binary | xxd -p | perl -pe 's/(..)/\\x$1/g' | // echo -en "\x85\xff\xff\xff\xd2\x04\x00\x00\x39\x30\x00\x00\x38\xfe\xff\xff\x2e\x16\x00\x00\x6e\xb2\x00\x00" | openssl md5 -binary | xxd -p | perl -pe 's/(..)/\\x$1/g' | ||||
} | } | ||||
void test_bounds() | void test_bounds() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CDebugSerializer serialize(script, stream); | CDebugSerializer serialize(script, stream); | ||||
serialize.NumberI32("x", 16, -16, 16); | serialize.NumberI32("x", 16, -16, 16); | ||||
serialize.NumberI32("x", -16, -16, 16); | serialize.NumberI32("x", -16, -16, 16); | ||||
TS_ASSERT_THROWS(serialize.NumberI32("x", 99, -16, 16), const PSERROR_Serialize_OutOfBounds&); | TS_ASSERT_THROWS(serialize.NumberI32("x", 99, -16, 16), const PSERROR_Serialize_OutOfBounds&); | ||||
TS_ASSERT_THROWS(serialize.NumberI32("x", -17, -16, 16), const PSERROR_Serialize_OutOfBounds&); | TS_ASSERT_THROWS(serialize.NumberI32("x", -17, -16, 16), const PSERROR_Serialize_OutOfBounds&); | ||||
} | } | ||||
// TODO: test exceptions more thoroughly | // TODO: test exceptions more thoroughly | ||||
void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL) | void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL) | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptInterface::Request rq(script); | ||||
JS::RootedValue obj(rq.cx); | JS::RootedValue obj(rq.cx); | ||||
TSM_ASSERT(msg, script.Eval(input, &obj)); | TSM_ASSERT(msg, script.Eval(input, &obj)); | ||||
if (debug) | if (debug) | ||||
{ | { | ||||
std::stringstream dbgstream; | std::stringstream dbgstream; | ||||
▲ Show 20 Lines • Show All 445 Lines • ▼ Show 20 Lines | void test_script_numbers() | ||||
; | ; | ||||
helper_script_roundtrip("numbers", "[-2147483648, -2147483649, 2.147483647e+9, 2147483648]", | helper_script_roundtrip("numbers", "[-2147483648, -2147483649, 2.147483647e+9, 2147483648]", | ||||
"[-2147483648, -2147483649, 2147483647, 2147483648]", sizeof(stream) - 1, stream); | "[-2147483648, -2147483649, 2147483647, 2147483648]", sizeof(stream) - 1, stream); | ||||
} | } | ||||
void test_script_exceptions() | void test_script_exceptions() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptInterface::Request rq(script); | ||||
JS::RootedValue obj(rq.cx); | JS::RootedValue obj(rq.cx); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CStdSerializer serialize(script, stream); | CStdSerializer serialize(script, stream); | ||||
TestLogger logger; | TestLogger logger; | ||||
Show All 17 Lines | |||||
class TestSerializerPerf : public CxxTest::TestSuite | class TestSerializerPerf : public CxxTest::TestSuite | ||||
{ | { | ||||
public: | public: | ||||
void test_script_props_DISABLED() | void test_script_props_DISABLED() | ||||
{ | { | ||||
const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x"; | const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x"; | ||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptInterface::Request rq(script); | ||||
JS::RootedValue obj(rq.cx); | JS::RootedValue obj(rq.cx); | ||||
TS_ASSERT(script.Eval(input, &obj)); | TS_ASSERT(script.Eval(input, &obj)); | ||||
for (size_t i = 0; i < 256; ++i) | for (size_t i = 0; i < 256; ++i) | ||||
{ | { | ||||
std::stringstream stream; | std::stringstream stream; | ||||
Show All 28 Lines | void test_hash_DISABLED() | ||||
// Need some stuff for terrain movement costs: | // Need some stuff for terrain movement costs: | ||||
// (TODO: this ought to be independent of any graphics code) | // (TODO: this ought to be independent of any graphics code) | ||||
new CTerrainTextureManager; | new CTerrainTextureManager; | ||||
g_TexMan.LoadTerrainTextures(); | g_TexMan.LoadTerrainTextures(); | ||||
CTerrain terrain; | CTerrain terrain; | ||||
CSimulation2 sim2(NULL, g_ScriptRuntime, &terrain); | CSimulation2 sim2(NULL, g_ScriptContext, &terrain); | ||||
sim2.LoadDefaultScripts(); | sim2.LoadDefaultScripts(); | ||||
sim2.ResetState(); | sim2.ResetState(); | ||||
std::unique_ptr<CMapReader> mapReader(new CMapReader()); | std::unique_ptr<CMapReader> mapReader(new CMapReader()); | ||||
LDR_BeginRegistering(); | LDR_BeginRegistering(); | ||||
mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp", | mapReader->LoadMap(L"maps/skirmishes/Greek Acropolis (2).pmp", | ||||
sim2.GetScriptInterface().GetJSRuntime(), JS::UndefinedHandleValue, | sim2.GetScriptInterface().GetJSRuntime(), JS::UndefinedHandleValue, | ||||
Show All 40 Lines |
Wildfire Games · Phabricator