tensorflow/core/common_runtime/scoped_allocator_mgr_test.cc
// ScopedAllocator allocation should fail because we called more times than
// expected, or we deallocated a non-existent pointer, or we requested more
// or less than the exact size of an instance buffer.
ScopedAllocator allocation should fail because we called more times than
expected, or we deallocated a non-existent pointer, or we requested more
or less than the exact size of an instance buffer.
ScopedAllocator allocation should fail because we called more times than expected or we deallocated a non - existent pointer or we requested more or less than the exact size of an instance buffer .
TEST_F(ScopedAllocatorMgrTest, AllocatorFail) {
  backing_tensor_shape_ = TensorShape({1024});
  fields_shapes_ = std::vector<TensorShape>({{512}, {512}});
  Status s = PrepScopedAllocatorMgr(2);
  EXPECT_TRUE(s.ok());
  SaveInstances(fields_shapes_.size());
  char* ptr0 =
      static_cast<char*>(sa_instances_[0]->AllocateRaw(0, 512 * sizeof(float)));
  VLOG(2) << "Should fail because we deallocate ptr="
          << static_cast<void*>(ptr0 + 8) << " which we never allocated.";
  EXPECT_DEATH(sa_instances_[0]->DeallocateRaw(ptr0 + 8), "");
  VLOG(2) << "Should fail because we allocate smaller than the size of the "
          << "field.";
  EXPECT_EQ(nullptr, sa_instances_[1]->AllocateRaw(0, 256 * sizeof(float)));
  VLOG(2) << "Should fail because we allocate larger than the size of the "
          << "field.";
  EXPECT_EQ(nullptr, sa_instances_[1]->AllocateRaw(0, 1024 * sizeof(float)));
  void* ptr1 = sa_instances_[1]->AllocateRaw(0, 512 * sizeof(float));
  VLOG(2) << "Should fail because we exceed expected_use_count.";
  EXPECT_EQ(nullptr, sa_instances_[0]->AllocateRaw(0, 512 * sizeof(float)));
  sa_instances_[0]->DeallocateRaw(ptr0);
  sa_instances_[1]->DeallocateRaw(ptr1);
}
TEST_F ( ScopedAllocatorMgrTest , AllocatorFail ) { backing_tensor_shape_ = TensorShape ( { 1024 } ) ; fields_shapes_ = std :: vector < TensorShape > ( { { 512 } , { 512 } } ) ; Status s = PrepScopedAllocatorMgr ( 2 ) ; EXPECT_TRUE ( s . ok ( ) ) ; SaveInstances ( fields_shapes_ . size ( ) ) ; char * ptr0 = static_cast < char * > ( sa_instances_ [ 0 ] -> AllocateRaw ( 0 , 512 * sizeof ( float ) ) ) ; VLOG ( 2 ) << " " << static_cast < void * > ( ptr0 + 8 ) << " " ; EXPECT_DEATH ( sa_instances_ [ 0 ] -> DeallocateRaw ( ptr0 + 8 ) , " " ) ; VLOG ( 2 ) << " " << " " ; EXPECT_EQ ( nullptr , sa_instances_ [ 1 ] -> AllocateRaw ( 0 , 256 * sizeof ( float ) ) ) ; VLOG ( 2 ) << " " << " " ; EXPECT_EQ ( nullptr , sa_instances_ [ 1 ] -> AllocateRaw ( 0 , 1024 * sizeof ( float ) ) ) ; void * ptr1 = sa_instances_ [ 1 ] -> AllocateRaw ( 0 , 512 * sizeof ( float ) ) ; VLOG ( 2 ) << " " ; EXPECT_EQ ( nullptr , sa_instances_ [ 0 ] -> AllocateRaw ( 0 , 512 * sizeof ( float ) ) ) ; sa_instances_ [ 0 ] -> DeallocateRaw ( ptr0 ) ; sa_instances_ [ 1 ] -> DeallocateRaw ( ptr1 ) ; }
ScopedAllocator allocation should fail because we called more times than
expected, or we deallocated a non-existent pointer, or we requested more
or less than the exact size of an instance buffer.
ScopedAllocator allocation should fail because we called more times than expected or we deallocated a non - existent pointer or we requested more or less than the exact size of an instance buffer .
// Save instances so that we can explicitly delete later on.  In normal // operation the instances will be automatically deleted after single use, but // in this test we are invoking the ScopedAllocator's Alloc/Dealloc interface, // so we need to explicitly delete the instances to avoid a memleak.