MCPcopy Create free account
hub / github.com/anjo76/angelscript / Test2

Function Test2

sdk/tests/test_feature/source/test_scriptstruct.cpp:2548–2600  ·  view source on GitHub ↗

-------------------------------- Test reported by SiCrane Doing an assignment of a temporary object would give an incorrect result, even crashing the application

Source from the content-addressed store, hash-verified

2546//
2547// Doing an assignment of a temporary object would give an incorrect result, even crashing the application
2548bool Test2()
2549{
2550 bool fail = false;
2551 COutStream out;
2552 int r;
2553
2554
2555 const char *script1 =
2556 "class MyClass { \n"
2557 " int a; \n"
2558 " MyClass(int a) { this.a = a; } \n"
2559 " MyClass &opAssign(const MyClass &in o) { a = o.a; return this; } \n"
2560 " int foo() { return a; } \n"
2561 "} \n"
2562 " \n"
2563 "void main() { \n"
2564 " int i; \n"
2565 " MyClass m(5); \n"
2566 " MyClass t(10); \n"
2567 " i = (m = t).a; \n"
2568 " assert(i == 10); \n"
2569 " i = (m = MyClass(10)).a; \n"
2570 " assert(i == 10); \n"
2571 " MyClass n(10); \n"
2572 " MyClass o(15); \n"
2573 " m = n = o; \n"
2574 " m = n = MyClass(20); \n"
2575 " (m = n).foo(); \n"
2576 " (m = MyClass(20)).foo(); \n"
2577 "} \n";
2578
2579 asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
2580 engine->SetMessageCallback(asMETHOD(COutStream,Callback), &out, asCALL_THISCALL);
2581 engine->RegisterGlobalFunction("void assert(bool)", asFUNCTION(Assert), asCALL_GENERIC);
2582
2583 asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE);
2584 mod->AddScriptSection("script", script1, strlen(script1), 0);
2585 r = mod->Build();
2586 if( r < 0 )
2587 {
2588 TEST_FAILED;
2589 }
2590
2591 r = ExecuteString(engine, "main()", mod);
2592 if( r != asEXECUTION_FINISHED )
2593 {
2594 TEST_FAILED;
2595 }
2596
2597 engine->Release();
2598
2599 return fail;
2600}
2601
2602} // namespace
2603

Callers 1

TestFunction · 0.70

Calls 7

ExecuteStringFunction · 0.85
SetMessageCallbackMethod · 0.80
AddScriptSectionMethod · 0.80
GetModuleMethod · 0.45
BuildMethod · 0.45
ReleaseMethod · 0.45

Tested by

no test coverage detected