You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensor_string_test.cc 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <memory>
  17. #include <string>
  18. #include "minddata/dataset/core/client.h"
  19. #include "common/common.h"
  20. #include "gtest/gtest.h"
  21. #include "securec.h"
  22. #include "minddata/dataset/core/tensor.h"
  23. #include "minddata/dataset/core/cv_tensor.h"
  24. #include "minddata/dataset/core/data_type.h"
  25. using namespace mindspore::dataset;
  26. namespace py = pybind11;
  27. class MindDataTestStringTensorDE : public UT::Common {
  28. public:
  29. MindDataTestStringTensorDE() = default;
  30. void SetUp() override { GlobalInit(); }
  31. };
  32. TEST_F(MindDataTestStringTensorDE, Basics) {
  33. std::shared_ptr<Tensor> t;
  34. Tensor::CreateScalar<std::string>("Hi", &t);
  35. ASSERT_TRUE(t->shape() == TensorShape({}));
  36. std::string_view s = "";
  37. t->GetItemAt(&s, {});
  38. ASSERT_TRUE(s == "Hi");
  39. std::shared_ptr<Tensor> t2;
  40. Tensor::CreateFromVector(std::vector<std::string>{"Hi", "Bye"}, &t2);
  41. ASSERT_TRUE(t2->shape() == TensorShape({2}));
  42. t2->GetItemAt(&s, {0});
  43. ASSERT_TRUE(s == "Hi");
  44. t2->GetItemAt(&s, {1});
  45. ASSERT_TRUE(s == "Bye");
  46. std::vector<std::string> strings{"abc", "defg", "hi", "klmno", "123", "789"};
  47. std::shared_ptr<Tensor> t3;
  48. Tensor::CreateFromVector(strings, TensorShape({2, 3}), &t3);
  49. ASSERT_TRUE(t3->shape() == TensorShape({2, 3}));
  50. uint32_t index = 0;
  51. for (uint32_t i = 0; i < 2; i++) {
  52. for (uint32_t j = 0; j < 3; j++) {
  53. std::string_view s = "";
  54. t3->GetItemAt(&s, {i, j});
  55. ASSERT_TRUE(s == strings[index++]);
  56. }
  57. }
  58. }
  59. TEST_F(MindDataTestStringTensorDE, Basics2) {
  60. std::shared_ptr<Tensor> t;
  61. Tensor::CreateFromVector(std::vector<std::string>{"abc", "defg", "hi", "klmno", "123", "789"}, TensorShape({2, 3}),
  62. &t);
  63. ASSERT_TRUE(t->SizeInBytes() == 6 * 5 + 20 + 4);
  64. std::vector<uint32_t> offsets = {0, 4, 9, 12, 18, 22, 26};
  65. uint32_t ctr = 0;
  66. for (auto i : offsets) {
  67. ASSERT_TRUE(*(reinterpret_cast<const uint32_t *>(t->GetBuffer() + ctr)) == i + 28);
  68. ctr += 4;
  69. }
  70. const char *buf = reinterpret_cast<const char *>(t->GetBuffer()) + 6 * 4 + 4;
  71. std::vector<uint32_t> starts = {0, 4, 9, 12, 18, 22};
  72. uint32_t index = 0;
  73. for (uint32_t i = 0; i < 2; i++) {
  74. for (uint32_t j = 0; j < 3; j++) {
  75. std::string_view s = "";
  76. t->GetItemAt(&s, {i, j});
  77. ASSERT_TRUE(s.data() == buf + starts[index++]);
  78. }
  79. }
  80. }
  81. TEST_F(MindDataTestStringTensorDE, Empty) {
  82. std::vector<std::string> strings{"abc", "defg", "", "", "123", ""};
  83. std::shared_ptr<Tensor> t;
  84. Tensor::CreateFromVector(strings, TensorShape({2, 3}), &t);
  85. // abc_defg___123__
  86. // 0123456789012345
  87. ASSERT_TRUE(t->SizeInBytes() == 6 * 5 + 10 + 4);
  88. std::vector<uint32_t> offsets = {0, 4, 9, 10, 11, 15, 16};
  89. uint32_t ctr = 0;
  90. for (auto i : offsets) {
  91. ASSERT_TRUE(*(reinterpret_cast<const uint32_t *>(t->GetBuffer() + ctr)) == i + 28);
  92. ctr += 4;
  93. }
  94. const char *buf = reinterpret_cast<const char *>(t->GetBuffer()) + 6 * 4 + 4;
  95. std::vector<uint32_t> starts = {0, 4, 9, 10, 11, 15};
  96. uint32_t index = 0;
  97. for (uint32_t i = 0; i < 2; i++) {
  98. for (uint32_t j = 0; j < 3; j++) {
  99. std::string_view s = "";
  100. t->GetItemAt(&s, {i, j});
  101. ASSERT_TRUE(s.data() == buf + starts[index]);
  102. ASSERT_TRUE(s == strings[index++]);
  103. }
  104. }
  105. }
  106. TEST_F(MindDataTestStringTensorDE, EmptyData) {
  107. std::shared_ptr<Tensor> t;
  108. Tensor::CreateScalar<std::string>("", &t);
  109. // empty string has 1 element
  110. ASSERT_TRUE(t->HasData());
  111. std::shared_ptr<Tensor> t1;
  112. Tensor::CreateEmpty(TensorShape({0}), DataType(DataType::DE_STRING), &t1);
  113. ASSERT_TRUE(!t1->HasData());
  114. }
  115. TEST_F(MindDataTestStringTensorDE, SetItem) {
  116. std::vector<std::string> strings{"abc", "defg", "hi", "klmno", "123", "789"};
  117. std::shared_ptr<Tensor> t3;
  118. Tensor::CreateFromVector(strings, TensorShape({2, 3}), &t3);
  119. ASSERT_TRUE(t3->shape() == TensorShape({2, 3}));
  120. t3->SetItemAt({0, 1}, std::string{"xyzz"});
  121. strings[1] = "xyzz";
  122. t3->SetItemAt({0, 2}, std::string{"07"});
  123. strings[2] = "07";
  124. t3->SetItemAt({1, 2}, std::string{"987"});
  125. strings[5] = "987";
  126. uint32_t index = 0;
  127. for (uint32_t i = 0; i < 2; i++) {
  128. for (uint32_t j = 0; j < 3; j++) {
  129. std::string_view s = "";
  130. t3->GetItemAt(&s, {i, j});
  131. ASSERT_TRUE(s == strings[index++]);
  132. }
  133. }
  134. }
  135. TEST_F(MindDataTestStringTensorDE, Iterator) {
  136. std::vector<std::string> strings{"abc", "defg", "hi", "klmno", "123", "789"};
  137. std::shared_ptr<Tensor> t;
  138. Tensor::CreateFromVector(strings, TensorShape({2, 3}), &t);
  139. uint32_t index = 0;
  140. auto itr = t->begin<std::string_view>();
  141. for (; itr != t->end<std::string_view>(); itr++) {
  142. ASSERT_TRUE(*itr == strings[index++]);
  143. }
  144. index = 0;
  145. itr = t->begin<std::string_view>();
  146. for (; itr != t->end<std::string_view>(); itr += 2) {
  147. ASSERT_TRUE(*itr == strings[index]);
  148. index += 2;
  149. }
  150. }