Allocator.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  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. ANKI_TEST(Util, StackAllocator)
  10. {
  11. Foo::reset();
  12. // With simple string
  13. {
  14. StackAllocator<char> alloc(allocAligned, nullptr, 128);
  15. using Str = std::basic_string<char, std::char_traits<char>,
  16. 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,
  25. (sizeof(Foo) + 1) * 10, alignof(Foo));
  26. std::vector<Foo, All> vec(alloc);
  27. vec.reserve(10);
  28. U sumi = 0;
  29. for(U i = 0; i < 10; i++)
  30. {
  31. std::cout << "pushing" << std::endl;
  32. vec.push_back(Foo(10 * i));
  33. sumi += 10 * i;
  34. }
  35. U sum = 0;
  36. for(const Foo& foo : vec)
  37. {
  38. sum += foo.x;
  39. }
  40. ANKI_TEST_EXPECT_EQ(sum, sumi);
  41. }
  42. // Copy around
  43. {
  44. typedef StackAllocator<Foo> Alloc;
  45. Alloc a(allocAligned, nullptr,
  46. (sizeof(Foo) + 1) * 10, alignof(Foo));
  47. Alloc b(allocAligned, nullptr,
  48. (sizeof(Foo) + 1) * 10, alignof(Foo));
  49. a = b;
  50. ANKI_TEST_EXPECT_EQ(a.getMemoryPool().getUsersCount(), 2);
  51. b = a;
  52. ANKI_TEST_EXPECT_EQ(&a.getMemoryPool(), &b.getMemoryPool());
  53. ANKI_TEST_EXPECT_EQ(a.getMemoryPool().getUsersCount(), 2);
  54. }
  55. ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount, Foo::destructorCallCount);
  56. // End
  57. Foo::reset();
  58. }