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

Function Test

sdk/tests/test_feature/source/test_addon_scriptarray.cpp:328–493  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

326};
327
328bool Test()
329{
330 bool fail = false;
331 fail = Test2() || fail;
332 int r;
333 COutStream out;
334 CBufferedOutStream bout;
335 asIScriptContext *ctx;
336 asIScriptEngine *engine;
337
338 // Test foreach with array when the array is modified in the foreach loop
339 {
340 engine = asCreateScriptEngine();
341 engine->SetMessageCallback(asMETHOD(CBufferedOutStream, Callback), &bout, asCALL_THISCALL);
342 bout.buffer = "";
343 engine->RegisterGlobalFunction("void assert(bool)", asFUNCTION(Assert), asCALL_GENERIC);
344 RegisterScriptArray(engine, false);
345
346 asIScriptModule* mod = engine->GetModule("test", asGM_ALWAYS_CREATE);
347 mod->AddScriptSection("test",
348 "void main() { \n"
349 " array<int> arr = {1,2,3,4,5,7}; \n"
350 " int sum = 0; \n"
351 " foreach( auto v, auto i : arr ) \n"
352 " if( v & 1 == 1 ) arr.removeAt(i); \n" // attempt to remove every impair number.
353 " assert( arr == {2,4,7} ); \n" // the algorithm doesn't work because when the element is removed, the iterator skips the next element
354 "} \n");
355
356 r = mod->Build();
357 if (r < 0)
358 TEST_FAILED;
359
360 r = ExecuteString(engine, "main()", mod);
361 if (r != asEXECUTION_FINISHED)
362 TEST_FAILED;
363
364 if (bout.buffer != "")
365 {
366 PRINTF("%s", bout.buffer.c_str());
367 TEST_FAILED;
368 }
369
370 engine->ShutDownAndRelease();
371 }
372
373 // Test foreach with array
374 {
375 engine = asCreateScriptEngine();
376
377 engine->SetMessageCallback(asMETHOD(CBufferedOutStream, Callback), &bout, asCALL_THISCALL);
378 bout.buffer = "";
379 engine->RegisterGlobalFunction("void assert(bool)", asFUNCTION(Assert), asCALL_GENERIC);
380
381 RegisterScriptArray(engine, false);
382 RegisterStdString(engine);
383
384 asIScriptModule* mod = engine->GetModule("test", asGM_ALWAYS_CREATE);
385 mod->AddScriptSection("test",

Callers

nothing calls this directly

Calls 10

RegisterScriptArrayFunction · 0.85
ExecuteStringFunction · 0.85
RegisterStdStringFunction · 0.85
SetMessageCallbackMethod · 0.80
AddScriptSectionMethod · 0.80
ShutDownAndReleaseMethod · 0.80
Test2Function · 0.70
GetModuleMethod · 0.45
BuildMethod · 0.45

Tested by

no test coverage detected