Better assertion infos and formatted code

This commit is contained in:
Mergul 2020-04-14 17:39:52 +02:00
parent 0670aed506
commit 14839b3765
6 changed files with 163 additions and 109 deletions

View file

@ -351,8 +351,10 @@ export struct EntityManager
void registerSystem(Sys)(int priority, const(char)[] pass_name)
{
ushort pass = passes_map.get(pass_name, ushort.max);
version(D_BetterC)assert(pass != ushort.max, "Update pass doesn't exist.");
else assert(pass != ushort.max, "Update pass (Name " ~ pass_name ~ ") doesn't exist.");
version (D_BetterC)
assert(pass != ushort.max, "Update pass doesn't exist.");
else
assert(pass != ushort.max, "Update pass (Name " ~ pass_name ~ ") doesn't exist.");
registerSystem!(Sys)(priority, pass);
}
@ -371,8 +373,10 @@ export struct EntityManager
assert(register_state,
"registerSystem must be called between beginRegister() and endRegister().");
version(D_BetterC)assert(pass < passes.length, "Update pass doesn't exist.");
else assert(pass < passes.length, "Update pass (ID " ~ pass.to!string ~ ") doesn't exist.");
version (D_BetterC)
assert(pass < passes.length, "Update pass doesn't exist.");
else
assert(pass < passes.length, "Update pass (ID " ~ pass.to!string ~ ") doesn't exist.");
System system;
system.m_pass = pass;
@ -599,7 +603,8 @@ export struct EntityManager
string entites_array;
}
static void allocateSystemComponents(ComponentsIndices!component_counts components_info)(ref System system)
static void allocateSystemComponents(ComponentsIndices!component_counts components_info)(
ref System system)
{
size_t req = components_info.req.length;
size_t opt = components_info.optional.length;
@ -744,7 +749,8 @@ export struct EntityManager
ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max);
version (D_BetterC)
assert(comp != ushort.max,
"Can't register system due to non existing component.");
"Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component.");
else
assert(comp != ushort.max, "Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component \"" ~ comp_info.type ~ "\".");
@ -753,29 +759,49 @@ export struct EntityManager
foreach (iii, comp_info; components_info.excluded)
{
ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max);
version (D_BetterC)assert(comp != ushort.max, "Can't register system due to non existing component.");
else assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\".");
version (D_BetterC)
assert(comp != ushort.max,
"Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component.");
else
assert(comp != ushort.max, "Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component \"" ~ comp_info.type ~ "\".");
system.m_excluded_components[iii] = comp;
}
foreach (iii, comp_info; components_info.optional)
{
ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max);
version (D_BetterC)assert(comp != ushort.max, "Can't register system due to non existing component.");
else assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\".");
version (D_BetterC)
assert(comp != ushort.max,
"Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component.");
else
assert(comp != ushort.max, "Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component \"" ~ comp_info.type ~ "\".");
system.m_optional_components[iii] = comp;
}
foreach (iii, comp_info; components_info.readonly)
{
ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max);
version (D_BetterC)assert(comp != ushort.max, "Can't register system due to non existing component.");
else assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\".");
version (D_BetterC)
assert(comp != ushort.max,
"Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component.");
else
assert(comp != ushort.max, "Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component \"" ~ comp_info.type ~ "\".");
system.m_read_only_components[iii] = comp;
}
foreach (iii, comp_info; components_info.mutable)
{
ushort comp = components_map.get(cast(char[]) comp_info.type, ushort.max);
version (D_BetterC)assert(comp != ushort.max, "Can't register system due to non existing component.");
else assert(comp != ushort.max, "Can't register system \""~Sys.stringof~"\" due to non existing component \""~comp_info.type~"\".");
version (D_BetterC)
assert(comp != ushort.max,
"Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component.");
else
assert(comp != ushort.max, "Can't register system \"" ~ Sys.stringof
~ "\" due to non existing component \"" ~ comp_info.type ~ "\".");
system.m_modified_components[iii] = comp;
}
@ -1531,31 +1557,35 @@ export struct EntityManager
*Params:
*components_ids = array of components allocated with template
*/
export EntityTemplate* allocateTemplate(EntityTemplate* base_tmpl, ushort[] components_ids, ushort[] remove_components_ids = null)
export EntityTemplate* allocateTemplate(EntityTemplate* base_tmpl,
ushort[] components_ids, ushort[] remove_components_ids = null)
{
size_t len = base_tmpl.info.components.length + components_ids.length;
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * len))[0
.. len];
memcpy(ids.ptr, base_tmpl.info.components.ptr, ushort.sizeof * base_tmpl.info.components.length);
memcpy(ids.ptr + base_tmpl.info.components.length, components_ids.ptr, ushort.sizeof * components_ids.length);
ushort[] ids = (cast(ushort*) alloca(ushort.sizeof * len))[0 .. len];
memcpy(ids.ptr, base_tmpl.info.components.ptr,
ushort.sizeof * base_tmpl.info.components.length);
memcpy(ids.ptr + base_tmpl.info.components.length, components_ids.ptr,
ushort.sizeof * components_ids.length);
qsort(ids.ptr, ids.length, ushort.sizeof, &compareUShorts);
qsort(remove_components_ids.ptr, remove_components_ids.length, ushort.sizeof, &compareUShorts);
qsort(remove_components_ids.ptr, remove_components_ids.length,
ushort.sizeof, &compareUShorts);
{
uint k = 0;
uint j = 1;
foreach (i; 1 .. ids.length)
{
assert(ids[i] != ushort.max);
if(k < remove_components_ids.length)
if (k < remove_components_ids.length)
{
while(k < remove_components_ids.length && remove_components_ids[k] < ids[i])
while (k < remove_components_ids.length && remove_components_ids[k] < ids[i])
{
k++;
}
if(k < remove_components_ids.length)
if (k < remove_components_ids.length)
{
if(remove_components_ids[k] == ids[i])continue;
if (remove_components_ids[k] == ids[i])
continue;
}
}
if (ids[i] != ids[j - 1])
@ -1578,10 +1608,11 @@ export struct EntityManager
//fill components with default data and copy from base template
foreach (comp; info.components)
{
if(comp < base_tmpl.info.deltas.length && base_tmpl.info.deltas[comp] != ushort.max) //copy data from base component
if (comp < base_tmpl.info.deltas.length && base_tmpl.info.deltas[comp] != ushort.max) //copy data from base component
{
memcpy(temp.entity_data.ptr + info.tmpl_deltas[comp],
base_tmpl.entity_data.ptr + base_tmpl.info.tmpl_deltas[comp], components[comp].size);
base_tmpl.entity_data.ptr + base_tmpl.info.tmpl_deltas[comp],
components[comp].size);
}
else //fill with default data
{

View file

@ -85,7 +85,7 @@ unittest
assert(!tmpl_.getComponent!CUnregistered);
assert(*tmpl_.getComponent!CInt == 1);
assert(*tmpl_.getComponent!CFloat == 2.0);
Entity* entity = gEM.addEntity(tmpl_);
assert(entity.getComponent!CInt);
assert(entity.getComponent!CFloat);
@ -182,4 +182,4 @@ unittest
gEM.freeTemplate(tmpl_5);
gEM.freeTemplate(tmpl_6);
gEM.freeTemplate(tmpl_7);
}
}

View file

@ -33,4 +33,4 @@ unittest
assert(!manager.isExist(EntityID(1, 0)));
assert(!manager.isExist(EntityID(0, 0)));
manager.deinitialize();
}
}

View file

@ -15,7 +15,9 @@ enum string OUT_FILE = "test_report.xml";
static jmp_buf gEnvBuffer;
static AssertInfo gAssertInfo;
version(D_BetterC){}
version (D_BetterC)
{
}
else version = notBetterC;
struct AssertInfo
@ -70,7 +72,7 @@ struct TestRunner(Args...)
write(failed);
write("\">\n");
foreach(ref TestSuite suite; suites)
foreach (ref TestSuite suite; suites)
{
write("\t<testsuite name=\"");
write(suite.name);
@ -82,14 +84,14 @@ struct TestRunner(Args...)
write(suite.skipped);
write("\">\n");
foreach(ref Test test; suite.tests)
foreach (ref Test test; suite.tests)
{
write("\t\t<testcase name=\"");
write(test.name);
write("\" classname=\"");
write(suite.name);
write("\">\n");
if(test.msg)
if (test.msg)
{
write("\t\t\t<failure type=\"Fail\" message=\"");
write(test.msg[0 .. $ - 1]);
@ -106,14 +108,14 @@ struct TestRunner(Args...)
}
write("\t</testsuite>\n");
}
}
write("</testsuites>");
}
void runTests(string[] include = null, string[] exclude = null)
{
foreach(i, module_; Args)
foreach (i, module_; Args)
{
TestSuite* suite = &suites[i];
suite.name = module_.stringof;
@ -122,35 +124,35 @@ struct TestRunner(Args...)
enum attributes = __traits(getAttributes, unittest_);
static if (attributes.length != 0)
{
if(include.length > 0)
if (include.length > 0)
{
bool matched = false;
foreach(str; include)
foreach (str; include)
{
if(match(str, attributes[0]))
if (match(str, attributes[0]))
{
matched = true;
break;
}
}
foreach(str; exclude)
foreach (str; exclude)
{
if(match(str, attributes[0]))
if (match(str, attributes[0]))
{
matched = false;
break;
}
}
if(!matched)
if (!matched)
{
suite.skipped++;
continue;
}
}
}
else if(include.length > 0)
else if (include.length > 0)
{
suite.skipped++;
continue;
@ -158,11 +160,13 @@ struct TestRunner(Args...)
Test test;
static if (attributes.length == 0)test.name = "None";
else test.name = attributes[0];
static if (attributes.length == 0)
test.name = "None";
else
test.name = attributes[0];
static if(__traits(hasMember, module_, "beforeEveryTest"))module_.beforeEveryTest();
static if (__traits(hasMember, module_, "beforeEveryTest"))
module_.beforeEveryTest();
// Save calling environment for longjmp
int jmp_ret = setjmp(gEnvBuffer);
@ -181,10 +185,13 @@ struct TestRunner(Args...)
test.passed = true;
}
if(test.passed)suite.passed++;
else suite.failed++;
if (test.passed)
suite.passed++;
else
suite.failed++;
suite.tests ~= test;
static if(__traits(hasMember, module_, "afterEveryTest"))module_.afterEveryTest();
static if (__traits(hasMember, module_, "afterEveryTest"))
module_.afterEveryTest();
}
passed += suite.passed;
failed += suite.failed;
@ -194,43 +201,51 @@ struct TestRunner(Args...)
void writeFile()
{
if(junit.length == 0)generateJUnit();
if (junit.length == 0)
generateJUnit();
auto file = fopen(OUT_FILE, "w");
fwrite(junit.data.ptr,junit.length,1,file);
fwrite(junit.data.ptr, junit.length, 1, file);
fclose(file);
}
void writeOutput()
{
foreach(ref TestSuite suite; suites)
foreach (ref TestSuite suite; suites)
{
printf("Suite: \"%s\" Passed: %u/%u Skipped: %u\n", suite.name.ptr, suite.passed, suite.passed + suite.failed, suite.skipped);
foreach(ref Test test;suite.tests)
printf("Suite: \"%s\" Passed: %u/%u Skipped: %u\n", suite.name.ptr,
suite.passed, suite.passed + suite.failed, suite.skipped);
foreach (ref Test test; suite.tests)
{
if(!test.passed)
if (!test.passed)
{
printf("\tTest: \"%s\" Failed! Line: %u Message: %s\n",test.name.ptr, test.file_line, test.msg.ptr);
printf("\tTest: \"%s\" Failed! Line: %u Message: %s\n",
test.name.ptr, test.file_line, test.msg.ptr);
}
}
}
printf("Passed %u/%u tests. Skipped: %u.\n", passed, passed+failed, skipped);
printf("Passed %u/%u tests. Skipped: %u.\n", passed, passed + failed, skipped);
}
bool match(string a, string b)
{
uint i = 0;
foreach(char c; b)
foreach (char c; b)
{
if(i > a.length)return false;
if(a[i] == c)i++;
else
if (i > a.length)
return false;
if (a[i] == c)
i++;
else
{
if(a[i] == '*')
if (a[i] == '*')
{
if(i+1 >= a.length)return true;
else if(a[i + 1] == c)i += 2;
if (i + 1 >= a.length)
return true;
else if (a[i + 1] == c)
i += 2;
}
else return false;
else
return false;
}
}
return i == a.length;
@ -245,8 +260,8 @@ struct TestRunner(Args...)
{
ubyte[20] buffer;
int len;
len = sprintf(cast(char*)buffer.ptr, "%u", num);
junit.add(buffer[0..len]);
len = sprintf(cast(char*) buffer.ptr, "%u", num);
junit.add(buffer[0 .. len]);
}
TestSuite[Args.length] suites;
@ -257,70 +272,75 @@ struct TestRunner(Args...)
SimpleVector junit;
}
version(notBetterC)
version (notBetterC)
{
extern(C) int rt_init();
extern(C) int rt_term();
version(D_Coverage) extern (C) void dmd_coverDestPath(string path);
extern (C) int rt_init();
extern (C) int rt_term();
version (D_Coverage) extern (C) void dmd_coverDestPath(string path);
}
void extractStrings(ref Vector!string container, string str)
{
uint s = 0;
foreach(j, char c; str)
foreach (j, char c; str)
{
if(c == ',')
if (c == ',')
{
if(s < j)
if (s < j)
{
container.add(str[s .. j]);
}
s = cast(uint)j+1;
s = cast(uint) j + 1;
}
}
if(s < str.length)
if (s < str.length)
{
container.add(str[s .. $]);
}
}
extern(C) int main(int argc, char** args)
extern (C) int main(int argc, char** args)
{
Vector!string include;
Vector!string exclude;
version(notBetterC)
version (notBetterC)
{
rt_init();
version(D_Coverage) dmd_coverDestPath("reports");
version (D_Coverage)
dmd_coverDestPath("reports");
}
for(int i=1;i<argc;i++)
for (int i = 1; i < argc; i++)
{
string arg = cast(string)args[i][0..strlen(args[i])];
if(arg.length < 2)continue;
else if(arg == "-i")
string arg = cast(string) args[i][0 .. strlen(args[i])];
if (arg.length < 2)
continue;
else if (arg == "-i")
{
if(i + 1 >= argc)break;
if (i + 1 >= argc)
break;
i++;
arg = cast(string)args[i][0..strlen(args[i])];
arg = cast(string) args[i][0 .. strlen(args[i])];
extractStrings(include, arg);
}
else if(arg =="-e")
else if (arg == "-e")
{
if(i + 1 >= argc)break;
if (i + 1 >= argc)
break;
i++;
arg = cast(string)args[i][0..strlen(args[i])];
arg = cast(string) args[i][0 .. strlen(args[i])];
extractStrings(exclude, arg);
}
else if(arg.length < 10)continue;
else if(arg[0..10] == "--include=")
else if (arg.length < 10)
continue;
else if (arg[0 .. 10] == "--include=")
{
extractStrings(include, arg[10..$]);
extractStrings(include, arg[10 .. $]);
}
else if(arg[0..10] == "--exclude=")
else if (arg[0 .. 10] == "--exclude=")
{
extractStrings(exclude, arg[10..$]);
extractStrings(exclude, arg[10 .. $]);
}
}
@ -332,8 +352,11 @@ extern(C) int main(int argc, char** args)
runner.writeOutput();
version(notBetterC)rt_term();
if(!runner.failed)return 0;
else return 1;
}
version (notBetterC)
rt_term();
if (!runner.failed)
return 0;
else
return 1;
}

View file

@ -425,19 +425,19 @@ struct EmptyEventSystem
void handleEvent(Entity* entity, TestEvent event)
{
if(!handled)
if (!handled)
{
printf("EmptyEventSystem.handleEvent() called!\n");
handled = true;
}
assert(0,"this shouldn't be called!");
assert(0, "this shouldn't be called!");
}
}
struct EventSystem
{
mixin ECS.System;
bool handled = false;
struct EntitiesData
@ -448,7 +448,7 @@ struct EventSystem
void handleEvent(Entity* entity, TestEvent event)
{
if(!handled)
if (!handled)
{
printf("EventSystem.handleEvent() called!\n");
handled = true;

View file

@ -7,13 +7,13 @@ import ecs.simple_vector;
unittest
{
SimpleVector vector;
vector.add(cast(ubyte[])"a");
vector.add(cast(ubyte[])"bsdf");
assert(vector[0..5] == cast(ubyte[])"absdf");
vector.add(cast(ubyte[]) "a");
vector.add(cast(ubyte[]) "bsdf");
assert(vector[0 .. 5] == cast(ubyte[]) "absdf");
assert(vector[4] == 'f');
assert(vector[] == cast(ubyte[])"absdf");
assert(vector[] == cast(ubyte[]) "absdf");
assert(vector[$ - 1] == 'f');
vector.clear();
assert(vector.length == 0);
}
}