Allocator.cpp 858 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "tests/framework/Framework.h"
  2. #include "tests/util/Foo.h"
  3. #include "anki/util/Allocator.h"
  4. #include <string>
  5. ANKI_TEST(Allocator, StackAllocator)
  6. {
  7. Foo::reset();
  8. // With simple string
  9. {
  10. StackAllocator<char, false> alloc(128);
  11. typedef std::basic_string<char, std::char_traits<char>,
  12. StackAllocator<char, false>> Str;
  13. Str str(alloc);
  14. str = "lalala";
  15. str = "lalalalo";
  16. }
  17. // With vector
  18. {
  19. StackAllocator<Foo> alloc(128);
  20. std::vector<Foo, StackAllocator<Foo>> vec(alloc);
  21. U sumi = 0;
  22. for(U i = 0; i < 10; i++)
  23. {
  24. std::cout << "pushing" << std::endl;
  25. vec.push_back(Foo(10 * i));
  26. sumi += 10 * i;
  27. }
  28. U sum = 0;
  29. for(const Foo& foo : vec)
  30. {
  31. sum += foo.x;
  32. }
  33. ANKI_TEST_EXPECT_EQ(sum, sumi);
  34. }
  35. ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount,
  36. Foo::destructorCallCount);
  37. // End
  38. Foo::reset();
  39. }