guesscontainer_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file guesscontainer_test.cpp
  3. /// \brief Unit tests GuessContainer
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2014-03-19 09:46:54 +0100 (Wed, 19 Mar 2014) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "catch.hpp"
  11. #include "guesscontainer.h"
  12. TEST_CASE("indexing", "Tests random access") {
  13. GuessContainer<int> container;
  14. container.push_back(new int(1));
  15. container.push_back(new int(2));
  16. REQUIRE(container[0] == 1);
  17. REQUIRE(container[1] == 2);
  18. REQUIRE(container.size() == 2);
  19. }
  20. class InstanceCountingClass {
  21. public:
  22. static int living_instances;
  23. InstanceCountingClass() {
  24. ++living_instances;
  25. }
  26. ~InstanceCountingClass() {
  27. --living_instances;
  28. }
  29. };
  30. int InstanceCountingClass::living_instances = 0;
  31. TEST_CASE("memory", "Tests memory management in GuessContainer") {
  32. GuessContainer<InstanceCountingClass> container;
  33. container.push_back(new InstanceCountingClass());
  34. container.push_back(new InstanceCountingClass());
  35. container.push_back(new InstanceCountingClass());
  36. REQUIRE(InstanceCountingClass::living_instances == 3);
  37. container.erase(container.begin());
  38. REQUIRE(InstanceCountingClass::living_instances == 2);
  39. container.clear();
  40. REQUIRE(InstanceCountingClass::living_instances == 0);
  41. }
  42. TEST_CASE("iteration", "Tests iteration in GuessContainer") {
  43. GuessContainer<int> container;
  44. container.push_back(new int(1));
  45. container.push_back(new int(2));
  46. GuessContainer<int>::iterator itr = container.begin();
  47. REQUIRE(itr == container.begin());
  48. REQUIRE(itr++ == container.begin());
  49. REQUIRE(*itr == 2);
  50. itr = container.begin();
  51. REQUIRE(++itr != container.begin());
  52. REQUIRE(*itr == 2);
  53. REQUIRE(++itr == container.end());
  54. }
  55. TEST_CASE("erase", "Tests GuessContainer<T>::erase()") {
  56. GuessContainer<int> container;
  57. container.push_back(new int(1));
  58. container.push_back(new int(2));
  59. container.push_back(new int(3));
  60. GuessContainer<int>::iterator itr = container.erase(container.begin());
  61. REQUIRE(itr == container.begin());
  62. ++itr;
  63. itr = container.erase(itr);
  64. REQUIRE(itr == container.end());
  65. itr = container.begin();
  66. itr = container.erase(itr);
  67. REQUIRE(itr == container.end());
  68. REQUIRE(container.size() == 0);
  69. }