TemplatesTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2015 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 "include/private/SkTemplates.h"
  8. #include "tests/Test.h"
  9. // Tests for some of the helpers in SkTemplates.h
  10. static void test_automalloc_realloc(skiatest::Reporter* reporter) {
  11. SkAutoSTMalloc<1, int> array;
  12. // test we have a valid pointer, should not crash
  13. array[0] = 1;
  14. REPORTER_ASSERT(reporter, array[0] == 1);
  15. // using realloc for init
  16. array.realloc(1);
  17. array[0] = 1;
  18. REPORTER_ASSERT(reporter, array[0] == 1);
  19. // verify realloc can grow
  20. array.realloc(2);
  21. REPORTER_ASSERT(reporter, array[0] == 1);
  22. // realloc can shrink
  23. array.realloc(1);
  24. REPORTER_ASSERT(reporter, array[0] == 1);
  25. // should not crash
  26. array.realloc(0);
  27. // grow and shrink again
  28. array.realloc(10);
  29. for (int i = 0; i < 10; i++) {
  30. array[i] = 10 - i;
  31. }
  32. array.realloc(20);
  33. for (int i = 0; i < 10; i++) {
  34. REPORTER_ASSERT(reporter, array[i] == 10 - i);
  35. }
  36. array.realloc(10);
  37. for (int i = 0; i < 10; i++) {
  38. REPORTER_ASSERT(reporter, array[i] == 10 - i);
  39. }
  40. array.realloc(1);
  41. REPORTER_ASSERT(reporter, array[0] = 10);
  42. // resets mixed with realloc, below stack alloc size
  43. array.reset(0);
  44. array.realloc(1);
  45. array.reset(1);
  46. array[0] = 1;
  47. REPORTER_ASSERT(reporter, array[0] == 1);
  48. // reset and realloc > stack size
  49. array.reset(2);
  50. array.realloc(3);
  51. array[0] = 1;
  52. REPORTER_ASSERT(reporter, array[0] == 1);
  53. array.realloc(1);
  54. REPORTER_ASSERT(reporter, array[0] == 1);
  55. }
  56. DEF_TEST(Templates, reporter) {
  57. test_automalloc_realloc(reporter);
  58. }
  59. constexpr int static kStackPreallocCount = 10;
  60. // Ensures the containers in SkTemplates.h all have a consistent api.
  61. template<typename TContainer, typename TCount>
  62. static void test_container_apis(skiatest::Reporter* reporter) {
  63. REPORTER_ASSERT(reporter, !TContainer((TCount)0).get());
  64. REPORTER_ASSERT(reporter, TContainer((TCount)1).get());
  65. REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount).get());
  66. REPORTER_ASSERT(reporter, TContainer((TCount)kStackPreallocCount + 1).get());
  67. TContainer container;
  68. // The default constructor may or may not init to empty, depending on the type of container.
  69. container.reset((TCount)1);
  70. REPORTER_ASSERT(reporter, container.get());
  71. container.reset((TCount)kStackPreallocCount);
  72. REPORTER_ASSERT(reporter, container.get());
  73. container.reset((TCount)kStackPreallocCount + 1);
  74. REPORTER_ASSERT(reporter, container.get());
  75. container.reset((TCount)0);
  76. REPORTER_ASSERT(reporter, !container.get());
  77. }
  78. DEF_TEST(TemplateContainerAPIs, reporter) {
  79. test_container_apis<SkAutoTArray<int>, int>(reporter);
  80. test_container_apis<SkAutoSTArray<kStackPreallocCount, int>, int>(reporter);
  81. test_container_apis<SkAutoTMalloc<int>, size_t>(reporter);
  82. test_container_apis<SkAutoSTMalloc<kStackPreallocCount, int>, size_t>(reporter);
  83. }
  84. // Ensures that realloc(0) results in a null pointer.
  85. template<typename TAutoMalloc> static void test_realloc_to_zero(skiatest::Reporter* reporter) {
  86. TAutoMalloc autoMalloc(kStackPreallocCount);
  87. REPORTER_ASSERT(reporter, autoMalloc.get());
  88. autoMalloc.realloc(0);
  89. REPORTER_ASSERT(reporter, !autoMalloc.get());
  90. autoMalloc.realloc(kStackPreallocCount + 1);
  91. REPORTER_ASSERT(reporter, autoMalloc.get());
  92. autoMalloc.realloc(0);
  93. REPORTER_ASSERT(reporter, !autoMalloc.get());
  94. autoMalloc.realloc(kStackPreallocCount);
  95. REPORTER_ASSERT(reporter, autoMalloc.get());
  96. }
  97. DEF_TEST(AutoReallocToZero, reporter) {
  98. test_realloc_to_zero<SkAutoTMalloc<int> >(reporter);
  99. test_realloc_to_zero<SkAutoSTMalloc<kStackPreallocCount, int> >(reporter);
  100. }
  101. DEF_TEST(SkAutoTMallocSelfMove, r) {
  102. #if defined(__clang__)
  103. #pragma clang diagnostic push
  104. #pragma clang diagnostic ignored "-Wself-move"
  105. #endif
  106. SkAutoTMalloc<int> foo(20);
  107. REPORTER_ASSERT(r, foo.get());
  108. foo = std::move(foo);
  109. REPORTER_ASSERT(r, foo.get()); // NOLINT(bugprone-use-after-move)
  110. #if defined(__clang__)
  111. #pragma clang diagnostic pop
  112. #endif
  113. }