MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / TEST

Function TEST

test/cpp/phi/core/test_string_tensor.cc:32–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30using pstring = ::phi::dtype::pstring;
31
32TEST(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,

Callers

nothing calls this directly

Calls 15

InstanceFunction · 0.85
metaMethod · 0.80
CPUPlaceClass · 0.50
productFunction · 0.50
pstringFunction · 0.50
getMethod · 0.45
numelMethod · 0.45
dimsMethod · 0.45
placeMethod · 0.45
initializedMethod · 0.45
IsSharedWithMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected