Caffe2 - C++ API
A deep learning, cross platform ML framework
memory.cpp
1 #include <gtest/gtest.h>
2 
3 #include <torch/csrc/utils/memory.h>
4 
5 #include <c10/util/Optional.h>
6 
7 struct TestValue {
8  explicit TestValue(const int& x) : lvalue_(x) {}
9  explicit TestValue(int&& x) : rvalue_(x) {}
10 
11  c10::optional<int> lvalue_;
12  c10::optional<int> rvalue_;
13 };
14 
15 TEST(MakeUniqueTest, ForwardRvaluesCorrectly) {
16  auto ptr = torch::make_unique<TestValue>(123);
17  ASSERT_FALSE(ptr->lvalue_.has_value());
18  ASSERT_TRUE(ptr->rvalue_.has_value());
19  ASSERT_EQ(*ptr->rvalue_, 123);
20 }
21 
22 TEST(MakeUniqueTest, ForwardLvaluesCorrectly) {
23  int x = 5;
24  auto ptr = torch::make_unique<TestValue>(x);
25  ASSERT_TRUE(ptr->lvalue_.has_value());
26  ASSERT_EQ(*ptr->lvalue_, 5);
27  ASSERT_FALSE(ptr->rvalue_.has_value());
28 }
29 
30 TEST(MakeUniqueTest, CanConstructUniquePtrOfArray) {
31  auto ptr = torch::make_unique<int[]>(3);
32  // Value initialization is required by the standard.
33  ASSERT_EQ(ptr[0], 0);
34  ASSERT_EQ(ptr[1], 0);
35  ASSERT_EQ(ptr[2], 0);
36 }