ComponentsTests.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "tests/ComponentsTests.h"
  2. #include "data/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. c.add(20, 40);
  36. c.remove(20);
  37. int* i1 = c.search(1);
  38. int* i2 = c.search(5);
  39. int* i3 = c.search(10);
  40. test.checkEqual(true, i1 != nullptr, "remove 1");
  41. test.checkEqual(true, i2 == nullptr, "remove 2");
  42. test.checkEqual(true, i3 != nullptr, "remove 3");
  43. test.checkEqual(10, *i1, "remove 4");
  44. test.checkEqual(30, *i3, "remove 5");
  45. c.remove(10);
  46. i1 = c.search(1);
  47. i2 = c.search(5);
  48. i3 = c.search(10);
  49. test.checkEqual(true, i1 != nullptr, "remove 6");
  50. test.checkEqual(true, i2 == nullptr, "remove 7");
  51. test.checkEqual(true, i3 == nullptr, "remove 8");
  52. test.checkEqual(10, *i1, "remove 9");
  53. c.remove(1);
  54. i1 = c.search(1);
  55. i2 = c.search(5);
  56. i3 = c.search(10);
  57. test.checkEqual(true, i1 == nullptr, "remove 10");
  58. test.checkEqual(true, i2 == nullptr, "remove 11");
  59. test.checkEqual(true, i3 == nullptr, "remove 12");
  60. }
  61. void ComponentsTests::test() {
  62. Test test("Components");
  63. testAddForEach(test);
  64. testRemove(test);
  65. test.finalize();
  66. }