resource_container_test.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // =============================================================================
  15. // Tests the methods of ResourceContainer.
  16. //
  17. // NOTE(danielandor): For all tests: ResourceContainer is derived from
  18. // RefCounted, which requires the use of Unref to reduce the ref count
  19. // to zero and automatically delete the pointer.
  20. #include "dragnn/core/resource_container.h"
  21. #include <gmock/gmock.h>
  22. #include "tensorflow/core/platform/test.h"
  23. namespace syntaxnet {
  24. namespace dragnn {
  25. class MockDatatype {};
  26. TEST(ResourceContainerTest, Get) {
  27. std::unique_ptr<MockDatatype> data(new MockDatatype());
  28. MockDatatype *data_ptr = data.get();
  29. auto *container = new ResourceContainer<MockDatatype>(std::move(data));
  30. EXPECT_EQ(data_ptr, container->get());
  31. container->Unref();
  32. }
  33. TEST(ResourceContainerTest, Release) {
  34. std::unique_ptr<MockDatatype> data(new MockDatatype());
  35. MockDatatype *data_ptr = data.get();
  36. auto *container = new ResourceContainer<MockDatatype>(std::move(data));
  37. std::unique_ptr<MockDatatype> data_again = container->release();
  38. container->Unref();
  39. EXPECT_EQ(data_ptr, data_again.get());
  40. }
  41. TEST(ResourceContainerTest, NullptrOnGetAfterRelease) {
  42. std::unique_ptr<MockDatatype> data(new MockDatatype());
  43. auto *container = new ResourceContainer<MockDatatype>(std::move(data));
  44. container->release();
  45. EXPECT_EQ(nullptr, container->get());
  46. container->Unref();
  47. }
  48. TEST(ResourceContainerTest, DebugString) {
  49. std::unique_ptr<MockDatatype> data(new MockDatatype());
  50. auto *container = new ResourceContainer<MockDatatype>(std::move(data));
  51. EXPECT_EQ("ResourceContainer", container->DebugString());
  52. container->Unref();
  53. }
  54. } // namespace dragnn
  55. } // namespace syntaxnet