#include "tensorflow/core/framework/allocator.h" #include #include "tensorflow/core/platform/logging.h" #include namespace tensorflow { TEST(CPUAllocatorTest, Simple) { Allocator* a = cpu_allocator(); std::vector ptrs; for (int s = 1; s < 1024; s++) { void* raw = a->AllocateRaw(1, s); ptrs.push_back(raw); } std::sort(ptrs.begin(), ptrs.end()); for (size_t i = 0; i < ptrs.size(); i++) { if (i > 0) { CHECK_NE(ptrs[i], ptrs[i - 1]); // No dups } a->DeallocateRaw(ptrs[i]); } float* t1 = a->Allocate(1024); double* t2 = a->Allocate(1048576); a->Deallocate(t1); a->Deallocate(t2); } // Define a struct that we will use to observe behavior in the unit tests struct TestStruct { int x; // not used just want to make sure sizeof(TestStruct) > 1 }; TEST(CPUAllocatorTest, CheckStructSize) { CHECK_GT(sizeof(TestStruct), 1); } TEST(CPUAllocatorTest, AllocateOverflowMaxSizeT) { Allocator* a = cpu_allocator(); // The maximum size_t value will definitely overflow. size_t count_to_allocate = std::numeric_limits::max(); TestStruct* const test_pointer = a->Allocate(count_to_allocate); CHECK_EQ(test_pointer, reinterpret_cast(NULL)); } TEST(CPUAllocatorTest, AllocateOverflowSmallest) { Allocator* a = cpu_allocator(); // count_to_allocate is the smallest count that will cause overflow. const size_t count_to_allocate = (std::numeric_limits::max() / sizeof(TestStruct)) + 1; TestStruct* const test_pointer = a->Allocate(count_to_allocate); CHECK_EQ(test_pointer, reinterpret_cast(NULL)); } TEST(CPUAllocatorTest, Sizes) { Allocator* a = cpu_allocator(); EXPECT_EQ(false, a->TracksAllocationSizes()); } } // namespace tensorflow