| 30 | using pstring = ::phi::dtype::pstring; |
| 31 | |
| 32 | TEST(string_tensor, ctor) { |
| 33 | const DDim dims({1, 2}); |
| 34 | StringTensorMeta meta(dims); |
| 35 | const auto string_allocator = |
| 36 | std::make_unique<paddle::experimental::DefaultAllocator>(phi::CPUPlace()); |
| 37 | const auto alloc = string_allocator.get(); |
| 38 | auto check_string_tensor = [](const StringTensor& t, |
| 39 | const StringTensorMeta& m) -> bool { |
| 40 | bool r{true}; |
| 41 | r = r && (t.numel() == product(m.dims)); |
| 42 | r = r && (t.dims() == m.dims); |
| 43 | r = r && (t.place() == phi::CPUPlace()); |
| 44 | r = r && t.initialized(); |
| 45 | r = r && t.IsSharedWith(t); |
| 46 | r = r && (t.meta() == m); |
| 47 | return r; |
| 48 | }; |
| 49 | auto cpu = CPUPlace(); |
| 50 | |
| 51 | phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance(); |
| 52 | CPUContext* cpu_ctx = reinterpret_cast<CPUContext*>(pool.Get(cpu)); |
| 53 | |
| 54 | StringTensor tensor_0(alloc, meta); |
| 55 | check_string_tensor(tensor_0, meta); |
| 56 | |
| 57 | pstring pshort_str = pstring("A short pstring."); |
| 58 | pstring plong_str = |
| 59 | pstring("A large pstring whose length is longer than 22."); |
| 60 | |
| 61 | pstring* data = cpu_ctx->template Alloc<pstring>(&tensor_0); |
| 62 | data[0] = plong_str; |
| 63 | data[1] = pshort_str; |
| 64 | PADDLE_ENFORCE_EQ(tensor_0.data()[0], |
| 65 | plong_str, |
| 66 | common::errors::InvalidArgument( |
| 67 | "The tensor_0 should be equal to '%s', but got '%s'.", |
| 68 | plong_str, |
| 69 | tensor_0.data()[0])); |
| 70 | PADDLE_ENFORCE_EQ(tensor_0.data()[1], |
| 71 | pshort_str, |
| 72 | common::errors::InvalidArgument( |
| 73 | "The tensor_0 should be equal to '%s', but got '%s'.", |
| 74 | pshort_str, |
| 75 | tensor_0.data()[1])); |
| 76 | |
| 77 | // Test Copy Constructor |
| 78 | StringTensor tensor_1(tensor_0); |
| 79 | PADDLE_ENFORCE_EQ(tensor_1.data()[0], |
| 80 | plong_str, |
| 81 | common::errors::InvalidArgument( |
| 82 | "The tensor_1 should be equal to '%s', but got '%s'.", |
| 83 | plong_str, |
| 84 | tensor_1.data()[0])); |
| 85 | PADDLE_ENFORCE_EQ(tensor_1.data()[1], |
| 86 | pshort_str, |
| 87 | common::errors::InvalidArgument( |
| 88 | "The tensor_1 should be equal to '%s', but got '%s'.", |
| 89 | pshort_str, |
nothing calls this directly
no test coverage detected