PointersTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include "Pointers.h"
  18. TEST(PointersTest, smoke) {
  19. Pointers pointers(1);
  20. pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd));
  21. void* memory_pointer = pointers.Remove(0x1234);
  22. ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer);
  23. }
  24. TEST(PointersTest, readd_pointer) {
  25. Pointers pointers(1);
  26. pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd));
  27. void* memory_pointer = pointers.Remove(0x1234);
  28. ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer);
  29. pointers.Add(0x1234, reinterpret_cast<void*>(0x5555));
  30. memory_pointer = pointers.Remove(0x1234);
  31. ASSERT_EQ(reinterpret_cast<void*>(0x5555), memory_pointer);
  32. }
  33. TEST(PointersTest, expect_collision) {
  34. Pointers pointers(2);
  35. // This assumes the simple hash being used will result in a collision
  36. // hitting the same entry.
  37. pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd));
  38. pointers.Add(0x11234, reinterpret_cast<void*>(0xabcf));
  39. void* memory_pointer = pointers.Remove(0x11234);
  40. ASSERT_EQ(reinterpret_cast<void*>(0xabcf), memory_pointer);
  41. memory_pointer = pointers.Remove(0x1234);
  42. ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer);
  43. }
  44. TEST(PointersTest, multiple_add_removes) {
  45. Pointers pointers(4);
  46. pointers.Add(0x1234, reinterpret_cast<void*>(0xabcd));
  47. pointers.Add(0x1235, reinterpret_cast<void*>(0xabcf));
  48. pointers.Add(0x1236, reinterpret_cast<void*>(0xabc1));
  49. pointers.Add(0x1237, reinterpret_cast<void*>(0xabc2));
  50. void* memory_pointer = pointers.Remove(0x1236);
  51. ASSERT_EQ(reinterpret_cast<void*>(0xabc1), memory_pointer);
  52. pointers.Add(0x2349, reinterpret_cast<void*>(0x2abcd));
  53. memory_pointer = pointers.Remove(0x1234);
  54. ASSERT_EQ(reinterpret_cast<void*>(0xabcd), memory_pointer);
  55. memory_pointer = pointers.Remove(0x1237);
  56. ASSERT_EQ(reinterpret_cast<void*>(0xabc2), memory_pointer);
  57. pointers.Add(0x3500, reinterpret_cast<void*>(0x3abcd));
  58. memory_pointer = pointers.Remove(0x3500);
  59. ASSERT_EQ(reinterpret_cast<void*>(0x3abcd), memory_pointer);
  60. memory_pointer = pointers.Remove(0x2349);
  61. ASSERT_EQ(reinterpret_cast<void*>(0x2abcd), memory_pointer);
  62. }
  63. static void TestNoEntriesLeft() {
  64. Pointers pointers(1);
  65. // Even though we've requested only one pointer, we get more due
  66. // to the way the data is allocated.
  67. for (size_t i = 0; i <= pointers.max_pointers(); i++) {
  68. pointers.Add(0x1234 + i, reinterpret_cast<void*>(0xabcd + i));
  69. }
  70. }
  71. TEST(PointersTest_DeathTest, no_entries_left) {
  72. ASSERT_EXIT(TestNoEntriesLeft(), ::testing::ExitedWithCode(1), "");
  73. }
  74. static void TestFindNoPointer() {
  75. Pointers pointers(1);
  76. pointers.Remove(0x1234);
  77. }
  78. TEST(PointersTest_DeathTest, find_no_pointer) {
  79. ASSERT_EXIT(TestFindNoPointer(), ::testing::ExitedWithCode(1), "");
  80. }
  81. static void TestRemoveZeroValue() {
  82. Pointers pointers(1);
  83. void* memory = pointers.Remove(0);
  84. if (memory) {}
  85. }
  86. TEST(PointersTest_DeathTest, remove_zero_value) {
  87. ASSERT_EXIT(TestRemoveZeroValue(), ::testing::ExitedWithCode(1), "");
  88. }