diff --git a/source/bubel/ecs/hash_map.d b/source/bubel/ecs/hash_map.d index 6ae6a79..764374c 100755 --- a/source/bubel/ecs/hash_map.d +++ b/source/bubel/ecs/hash_map.d @@ -19,10 +19,21 @@ export ulong defaultHashFunc(T)(auto ref T t) nothrow @nogc } else { - return 0; //hashInt(t.hashOf); // hashOf is not giving proper distribution between H1 and H2 hash parts + static if(isArray!T)return hashInt(hash((cast(byte*)t.ptr)[0 .. t.length * ForeachType!(T).sizeof])); + else return hashInt(hash((cast(byte*)&t)[0 .. T.sizeof])); //hashInt(t.hashOf); // hashOf is not giving proper distribution between H1 and H2 hash parts } } +ulong hash(byte[] array) nothrow @nogc +{ + ulong hash = 0; + + foreach(c;array) + hash = c + (hash << 6) + (hash << 16) - hash; + + return hash; +} + // Can turn bad hash function to good one export ulong hashInt(ulong x) nothrow @nogc @safe { diff --git a/source/bubel/ecs/vector.d b/source/bubel/ecs/vector.d index ca6cd6c..019673b 100644 --- a/source/bubel/ecs/vector.d +++ b/source/bubel/ecs/vector.d @@ -165,10 +165,10 @@ public: return duplicate; } - export bool canAddWithoutRealloc(uint elemNum = 1) + /*export bool canAddWithoutRealloc(uint elemNum = 1) { return used + elemNum <= array.length; - } + }*/ export void add()(T t) { diff --git a/tests/basic.d b/tests/basic.d index aefdc7e..5046dff 100644 --- a/tests/basic.d +++ b/tests/basic.d @@ -135,6 +135,24 @@ void afterEveryTest() gEM.destroy(); } +@("EntityMeta") +unittest +{ + EntityTemplate* tmpl_ = gEM.allocateTemplate([CInt.component_id, CFloat.component_id, CFlag.component_id].staticArray); + Entity* entity = gEM.addEntity(tmpl_); + EntityMeta meta = entity.getMeta(); + assert(meta.hasComponent(CInt.component_id)); + assert(meta.getComponent!CInt); + assert(meta.hasComponent(CFloat.component_id)); + assert(meta.getComponent!CFloat); + assert(!meta.getComponent!CLong); + assert(!meta.hasComponent(CLong.component_id)); + assert(!meta.getComponent!CUnregistered); + assert(!meta.hasComponent(CUnregistered.component_id)); + assert(*meta.getComponent!CInt == 1); + assert(*meta.getComponent!CFloat == 2.0); +} + @("AddEntity") unittest { @@ -167,8 +185,8 @@ unittest //Entity* entity3 = gEM.addEntity(tmpl_, [cint.ref_, clong.ref_].staticArray); Entity* entity3 = gEM.addEntity(tmpl_, [CInt(10).ref_, CLong().ref_, CFlag().ref_].staticArray); EntityID id = entity3.id; - assert(entity3.getComponent!CInt); - assert(entity3.getComponent!CFloat); + assert(entity3.hasComponent(CInt.component_id)); + assert(entity3.hasComponent(CFloat.component_id)); assert(*entity3.getComponent!CInt == 10); assert(*entity3.getComponent!CFloat == 2.0); diff --git a/tests/map.d b/tests/map.d new file mode 100644 index 0000000..a82985b --- /dev/null +++ b/tests/map.d @@ -0,0 +1,53 @@ +module tests.map; + +import bubel.ecs.hash_map; + +version(GNU) +{ + pragma(inline, true) T[n] staticArray(T, size_t n)(auto ref T[n] a) + { + return a; + } +} +else import std.array : staticArray; + +@("HashMap") +unittest +{ + HashMap!(string, int) map; + + assert(map.length == 0); + map.add("asd",1); + assert(map.length == 1); + map.clear(); + assert(map.length == 0); + map.add("asd",1); + assert(map.length == 1); + map.reset(); + assert(map.length == 0); + + map.add("asd",1); + string asd = "asd"; + assert(map.isIn("asd")); + assert(map.isIn(asd)); + assert(!map.isIn("asdf")); + map.tryRemove("asdf"); + map.tryRemove("asd"); + assert(map.length == 0); + map.add("asdf",1); + map.add("asd",2); + assert(map["asd"] == 2); + assert(map["asdf"] == 1); + assert(map.length == 2); + map.tryRemove("asdf"); + assert(map.length == 1); + map.remove("asd"); + assert(map.length == 0); + + map.add("asd",1); + map.add("asdwwghe",6); + foreach(k,v;&map.byKeyValue) + { + assert(map[k] == v); + } +} \ No newline at end of file diff --git a/tests/runner.d b/tests/runner.d index b14dafe..7e3dd07 100644 --- a/tests/runner.d +++ b/tests/runner.d @@ -420,7 +420,7 @@ extern (C) int main(int argc, char** args) } } - TestRunner!(tests.id_manager, tests.vector, tests.basic, tests.perf, tests.access_perf, tests.bugs) runner; + TestRunner!(tests.id_manager, tests.vector, tests.basic, tests.perf, tests.access_perf, tests.bugs, tests.map) runner; runner.runTests(include[], exclude[]); diff --git a/tests/vector.d b/tests/vector.d index d733f0f..1756518 100644 --- a/tests/vector.d +++ b/tests/vector.d @@ -1,7 +1,16 @@ module tests.vector; import bubel.ecs.simple_vector; -//import bubel.ecs.vector; +import bubel.ecs.vector; + +version(GNU) +{ + pragma(inline, true) T[n] staticArray(T, size_t n)(auto ref T[n] a) + { + return a; + } +} +else import std.array : staticArray; @("simple-vector") unittest @@ -33,3 +42,38 @@ unittest assert(vector2[1023] == 'a'); assert(vector2[1024] == 'b'); } + +@("Vector") +unittest +{ + struct G + { + int a; + } + + Vector!G vector; + assert(vector.empty()); + vector.add(G(1)); + assert(!vector.empty()); + vector.clear(); + assert(vector.empty()); + vector.add(G(1)); + assert(!vector.empty()); + vector.reset(); + assert(vector.empty()); + + vector.add(G(1)); + vector.add([G(2),G(5)].staticArray); + assert(vector.length == 3); + assert(vector.capacity == 1); + + Vector!G vector2; + vector2.add([G(1),G(2),G(5)].staticArray); + assert(vector == vector2); + vector2.remove(1); + assert(vector != vector2); + assert(vector2.length == 2); + assert(vector2[1] == G(5)); + vector2.add(G(2),1); + assert(vector == vector2); +}