ComponentsTests.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "tests/ComponentsTests.h"
  2. #include "ecs/Components.h"
  3. #include "tests/Test.h"
  4. typedef Components<int> IntComponent;
  5. static void testAddForEach(Test& test) {
  6. IntComponent c;
  7. c.add(1, 10);
  8. c.add(5, 20);
  9. c.add(10, 30);
  10. auto iter = c.entities();
  11. auto pos = iter.begin();
  12. auto end = iter.end();
  13. test.checkEqual(1u, (*pos).entity, "contains added entity 1");
  14. test.checkEqual(10, (*pos).component, "contains added component 1");
  15. test.checkEqual(true, pos != end, "not at end 1");
  16. ++pos;
  17. test.checkEqual(5u, (*pos).entity, "contains added entity 2");
  18. test.checkEqual(20, (*pos).component, "contains added component 2");
  19. test.checkEqual(true, pos != end, "not at end 2");
  20. ++pos;
  21. test.checkEqual(10u, (*pos).entity, "contains added entity 3");
  22. test.checkEqual(30, (*pos).component, "contains added component 3");
  23. test.checkEqual(true, pos != end, "not at end 3");
  24. ++pos;
  25. test.checkEqual(false, pos != end, "at end");
  26. }
  27. static void testRemove(Test& test) {
  28. IntComponent c;
  29. c.add(1, 10);
  30. c.add(5, 20);
  31. c.add(10, 30);
  32. c.remove(20);
  33. c.remove(5);
  34. c.remove(30);
  35. int* i1 = c.search(1);
  36. int* i2 = c.search(5);
  37. int* i3 = c.search(10);
  38. test.checkEqual(true, i1 != nullptr, "remove 1");
  39. test.checkEqual(true, i2 == nullptr, "remove 2");
  40. test.checkEqual(true, i3 != nullptr, "remove 3");
  41. test.checkEqual(10, *i1, "remove 4");
  42. test.checkEqual(30, *i3, "remove 5");
  43. c.remove(10);
  44. i1 = c.search(1);
  45. i2 = c.search(5);
  46. i3 = c.search(10);
  47. test.checkEqual(true, i1 != nullptr, "remove 6");
  48. test.checkEqual(true, i2 == nullptr, "remove 7");
  49. test.checkEqual(true, i3 == nullptr, "remove 8");
  50. test.checkEqual(10, *i1, "remove 9");
  51. c.remove(1);
  52. i1 = c.search(1);
  53. i2 = c.search(5);
  54. i3 = c.search(10);
  55. test.checkEqual(true, i1 == nullptr, "remove 10");
  56. test.checkEqual(true, i2 == nullptr, "remove 11");
  57. test.checkEqual(true, i3 == nullptr, "remove 12");
  58. }
  59. void ComponentsTests::test() {
  60. Test test("Components");
  61. testAddForEach(test);
  62. testRemove(test);
  63. test.finalize();
  64. }