Demos #10
6 changed files with 133 additions and 7 deletions
|
|
@ -19,10 +19,21 @@ export ulong defaultHashFunc(T)(auto ref T t) nothrow @nogc
|
||||||
}
|
}
|
||||||
else
|
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
|
// Can turn bad hash function to good one
|
||||||
export ulong hashInt(ulong x) nothrow @nogc @safe
|
export ulong hashInt(ulong x) nothrow @nogc @safe
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -165,10 +165,10 @@ public:
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
export bool canAddWithoutRealloc(uint elemNum = 1)
|
/*export bool canAddWithoutRealloc(uint elemNum = 1)
|
||||||
{
|
{
|
||||||
return used + elemNum <= array.length;
|
return used + elemNum <= array.length;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
export void add()(T t)
|
export void add()(T t)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,24 @@ void afterEveryTest()
|
||||||
gEM.destroy();
|
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")
|
@("AddEntity")
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
|
|
@ -167,8 +185,8 @@ unittest
|
||||||
//Entity* entity3 = gEM.addEntity(tmpl_, [cint.ref_, clong.ref_].staticArray);
|
//Entity* entity3 = gEM.addEntity(tmpl_, [cint.ref_, clong.ref_].staticArray);
|
||||||
Entity* entity3 = gEM.addEntity(tmpl_, [CInt(10).ref_, CLong().ref_, CFlag().ref_].staticArray);
|
Entity* entity3 = gEM.addEntity(tmpl_, [CInt(10).ref_, CLong().ref_, CFlag().ref_].staticArray);
|
||||||
EntityID id = entity3.id;
|
EntityID id = entity3.id;
|
||||||
assert(entity3.getComponent!CInt);
|
assert(entity3.hasComponent(CInt.component_id));
|
||||||
assert(entity3.getComponent!CFloat);
|
assert(entity3.hasComponent(CFloat.component_id));
|
||||||
assert(*entity3.getComponent!CInt == 10);
|
assert(*entity3.getComponent!CInt == 10);
|
||||||
assert(*entity3.getComponent!CFloat == 2.0);
|
assert(*entity3.getComponent!CFloat == 2.0);
|
||||||
|
|
||||||
|
|
|
||||||
53
tests/map.d
Normal file
53
tests/map.d
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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[]);
|
runner.runTests(include[], exclude[]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,16 @@
|
||||||
module tests.vector;
|
module tests.vector;
|
||||||
|
|
||||||
import bubel.ecs.simple_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")
|
@("simple-vector")
|
||||||
unittest
|
unittest
|
||||||
|
|
@ -33,3 +42,38 @@ unittest
|
||||||
assert(vector2[1023] == 'a');
|
assert(vector2[1023] == 'a');
|
||||||
assert(vector2[1024] == 'b');
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue