Allocator.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2009-2016, 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. #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,
  17. std::char_traits<char>,
  18. StackAllocator<char>>;
  19. Str str(alloc);
  20. str = "lalala";
  21. str = "lalalalo";
  22. }
  23. // With vector
  24. {
  25. using All = StackAllocator<Foo>;
  26. All alloc(allocAligned, nullptr, (sizeof(Foo) + 1) * 10, alignof(Foo));
  27. std::vector<Foo, All> vec(alloc);
  28. vec.reserve(10);
  29. U sumi = 0;
  30. for(U i = 0; i < 10; i++)
  31. {
  32. std::cout << "pushing" << std::endl;
  33. vec.push_back(Foo(10 * i));
  34. sumi += 10 * i;
  35. }
  36. U sum = 0;
  37. for(const Foo& foo : vec)
  38. {
  39. sum += foo.x;
  40. }
  41. ANKI_TEST_EXPECT_EQ(sum, sumi);
  42. }
  43. // Copy around
  44. {
  45. typedef StackAllocator<Foo> Alloc;
  46. Alloc a(allocAligned, nullptr, (sizeof(Foo) + 1) * 10, alignof(Foo));
  47. Alloc b(allocAligned, nullptr, (sizeof(Foo) + 1) * 10, alignof(Foo));
  48. a = b;
  49. ANKI_TEST_EXPECT_EQ(a.getMemoryPool().getUsersCount(), 2);
  50. b = a;
  51. ANKI_TEST_EXPECT_EQ(&a.getMemoryPool(), &b.getMemoryPool());
  52. ANKI_TEST_EXPECT_EQ(a.getMemoryPool().getUsersCount(), 2);
  53. }
  54. ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount, Foo::destructorCallCount);
  55. // End
  56. Foo::reset();
  57. }