Allocator.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2009-present, 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. // Reset
  42. {
  43. using Alloc = StackAllocator<U8>;
  44. Alloc a(allocAligned, nullptr, 64_MB);
  45. a.allocate(32_MB);
  46. a.allocate(32_MB);
  47. a.allocate(32_MB);
  48. a.getMemoryPool().reset();
  49. a.allocate(32_MB);
  50. }
  51. ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount, Foo::destructorCallCount);
  52. // End
  53. Foo::reset();
  54. }