GrAllocatorTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/gpu/GrAllocator.h"
  8. #include "tests/Test.h"
  9. namespace {
  10. struct C {
  11. C() : fID(-1) { ++gInstCnt; }
  12. C(int id) : fID(id) { ++gInstCnt; }
  13. ~C() { --gInstCnt; }
  14. int fID;
  15. static int gInstCnt;
  16. };
  17. int C::gInstCnt = 0;
  18. }
  19. static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
  20. skiatest::Reporter* reporter);
  21. // Adds cnt items to the allocator, tests the cnts and iterators, pops popCnt items and checks
  22. // again. Finally it resets the allocator and checks again.
  23. static void check_allocator(GrTAllocator<C>* allocator, int cnt, int popCnt,
  24. skiatest::Reporter* reporter) {
  25. SkASSERT(allocator);
  26. SkASSERT(allocator->empty());
  27. for (int i = 0; i < cnt; ++i) {
  28. // Try both variations of push_back().
  29. if (i % 1) {
  30. allocator->push_back(C(i));
  31. } else {
  32. allocator->push_back() = C(i);
  33. }
  34. }
  35. check_allocator_helper(allocator, cnt, popCnt, reporter);
  36. allocator->reset();
  37. check_allocator_helper(allocator, 0, 0, reporter);
  38. }
  39. // Checks that the allocator has the correct count, etc and that the element IDs are correct.
  40. // Then pops popCnt items and checks again.
  41. static void check_allocator_helper(GrTAllocator<C>* allocator, int cnt, int popCnt,
  42. skiatest::Reporter* reporter) {
  43. REPORTER_ASSERT(reporter, (0 == cnt) == allocator->empty());
  44. REPORTER_ASSERT(reporter, cnt == allocator->count());
  45. REPORTER_ASSERT(reporter, cnt == C::gInstCnt);
  46. GrTAllocator<C>::Iter iter(allocator);
  47. for (int i = 0; i < cnt; ++i) {
  48. REPORTER_ASSERT(reporter, iter.next() && i == iter.get()->fID);
  49. }
  50. REPORTER_ASSERT(reporter, !iter.next());
  51. if (cnt > 0) {
  52. REPORTER_ASSERT(reporter, cnt-1 == allocator->back().fID);
  53. }
  54. if (popCnt > 0) {
  55. for (int i = 0; i < popCnt; ++i) {
  56. allocator->pop_back();
  57. }
  58. check_allocator_helper(allocator, cnt - popCnt, 0, reporter);
  59. }
  60. }
  61. DEF_TEST(GrAllocator, reporter) {
  62. // Test combinations of allocators with and without stack storage and with different block
  63. // sizes.
  64. SkTArray<GrTAllocator<C>*> allocators;
  65. GrTAllocator<C> a1(1);
  66. allocators.push_back(&a1);
  67. GrTAllocator<C> a2(2);
  68. allocators.push_back(&a2);
  69. GrTAllocator<C> a5(5);
  70. allocators.push_back(&a5);
  71. GrSTAllocator<1, C> sa1;
  72. allocators.push_back(&a1);
  73. GrSTAllocator<3, C> sa3;
  74. allocators.push_back(&sa3);
  75. GrSTAllocator<4, C> sa4;
  76. allocators.push_back(&sa4);
  77. for (int i = 0; i < allocators.count(); ++i) {
  78. check_allocator(allocators[i], 0, 0, reporter);
  79. check_allocator(allocators[i], 1, 1, reporter);
  80. check_allocator(allocators[i], 2, 2, reporter);
  81. check_allocator(allocators[i], 10, 1, reporter);
  82. check_allocator(allocators[i], 10, 5, reporter);
  83. check_allocator(allocators[i], 10, 10, reporter);
  84. check_allocator(allocators[i], 100, 10, reporter);
  85. }
  86. }