Allocator.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <Tests/Util/Foo.h>
  7. #include <AnKi/Util/Allocator.h>
  8. #include <string>
  9. #include <iostream>
  10. ANKI_TEST(Util, StackAllocator)
  11. {
  12. Foo::reset();
  13. // With simple string
  14. {
  15. StackAllocator<char> alloc(allocAligned, nullptr, 128);
  16. using Str = std::basic_string<char, std::char_traits<char>, StackAllocator<char>>;
  17. Str str(alloc);
  18. str = "lalala";
  19. str = "lalalalo";
  20. }
  21. // With vector
  22. {
  23. using All = StackAllocator<Foo>;
  24. All alloc(allocAligned, nullptr, (sizeof(Foo) + 1) * 10);
  25. std::vector<Foo, All> vec(alloc);
  26. vec.reserve(10);
  27. U sumi = 0;
  28. for(U i = 0; i < 10; i++)
  29. {
  30. std::cout << "pushing" << std::endl;
  31. vec.push_back(Foo(10 * I32(i)));
  32. sumi += 10 * i;
  33. }
  34. U sum = 0;
  35. for(const Foo& foo : vec)
  36. {
  37. sum += foo.x;
  38. }
  39. ANKI_TEST_EXPECT_EQ(sum, sumi);
  40. }
  41. // Copy around
  42. {
  43. using Alloc = StackAllocator<Foo>;
  44. Alloc a(allocAligned, nullptr, (sizeof(Foo) + 1) * 10);
  45. Alloc b(allocAligned, nullptr, (sizeof(Foo) + 1) * 10);
  46. a = b;
  47. ANKI_TEST_EXPECT_EQ(a.getMemoryPool().getUsersCount(), 2);
  48. b = a;
  49. ANKI_TEST_EXPECT_EQ(&a.getMemoryPool(), &b.getMemoryPool());
  50. ANKI_TEST_EXPECT_EQ(a.getMemoryPool().getUsersCount(), 2);
  51. }
  52. ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount, Foo::destructorCallCount);
  53. // End
  54. Foo::reset();
  55. }