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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "dataset/core/client.h"
  19. #include "common/common.h"
  20. #include "gtest/gtest.h"
  21. #include "securec.h"
  22. #include "dataset/core/tensor.h"
  23. #include "dataset/core/cv_tensor.h"
  24. #include "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 = std::make_shared<Tensor>("Hi");
  34. ASSERT_TRUE(t->shape() == TensorShape({}));
  35. std::string_view s = "";
  36. t->GetItemAt(&s, {});
  37. ASSERT_TRUE(s == "Hi");
  38. std::shared_ptr<Tensor> t2 = std::make_shared<Tensor>(std::vector<std::string>{"Hi", "Bye"});
  39. ASSERT_TRUE(t2->shape() == TensorShape({2}));
  40. t2->GetItemAt(&s, {0});
  41. ASSERT_TRUE(s == "Hi");
  42. t2->GetItemAt(&s, {1});
  43. ASSERT_TRUE(s == "Bye");
  44. std::vector<std::string> strings{"abc", "defg", "hi", "klmno", "123", "789"};
  45. std::shared_ptr<Tensor> t3 = std::make_shared<Tensor>(strings, TensorShape({2, 3}));
  46. ASSERT_TRUE(t3->shape() == TensorShape({2, 3}));
  47. uint32_t index = 0;
  48. for (uint32_t i = 0; i < 2; i++) {
  49. for (uint32_t j = 0; j < 3; j++) {
  50. std::string_view s = "";
  51. t3->GetItemAt(&s, {i, j});
  52. ASSERT_TRUE(s == strings[index++]);
  53. }
  54. }
  55. }
  56. TEST_F(MindDataTestStringTensorDE, Basics2) {
  57. std::shared_ptr<Tensor> t =
  58. std::make_shared<Tensor>(std::vector<std::string>{"abc", "defg", "hi", "klmno", "123", "789"}, TensorShape({2, 3}));
  59. ASSERT_TRUE(t->SizeInBytes() == 6 * 5 + 20 + 4);
  60. std::vector<uint32_t> offsets = {0, 4, 9, 12, 18, 22, 26};
  61. uint32_t ctr = 0;
  62. for (auto i : offsets) {
  63. ASSERT_TRUE(*(reinterpret_cast<const uint32_t *>(t->GetBuffer() + ctr)) == i + 28);
  64. ctr += 4;
  65. }
  66. const char *buf = reinterpret_cast<const char *>(t->GetBuffer()) + 6 * 4 + 4;
  67. std::vector<uint32_t> starts = {0, 4, 9, 12, 18, 22};
  68. uint32_t index = 0;
  69. for (uint32_t i = 0; i < 2; i++) {
  70. for (uint32_t j = 0; j < 3; j++) {
  71. std::string_view s = "";
  72. t->GetItemAt(&s, {i, j});
  73. ASSERT_TRUE(s.data() == buf + starts[index++]);
  74. }
  75. }
  76. }
  77. TEST_F(MindDataTestStringTensorDE, Empty) {
  78. std::vector<std::string> strings{"abc", "defg", "", "", "123", ""};
  79. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(strings, TensorShape({2, 3}));
  80. // abc_defg___123__
  81. // 0123456789012345
  82. ASSERT_TRUE(t->SizeInBytes() == 6 * 5 + 10 + 4);
  83. std::vector<uint32_t> offsets = {0, 4, 9, 10, 11, 15, 16};
  84. uint32_t ctr = 0;
  85. for (auto i : offsets) {
  86. ASSERT_TRUE(*(reinterpret_cast<const uint32_t *>(t->GetBuffer() + ctr)) == i + 28);
  87. ctr += 4;
  88. }
  89. const char *buf = reinterpret_cast<const char *>(t->GetBuffer()) + 6 * 4 + 4;
  90. std::vector<uint32_t> starts = {0, 4, 9, 10, 11, 15};
  91. uint32_t index = 0;
  92. for (uint32_t i = 0; i < 2; i++) {
  93. for (uint32_t j = 0; j < 3; j++) {
  94. std::string_view s = "";
  95. t->GetItemAt(&s, {i, j});
  96. ASSERT_TRUE(s.data() == buf + starts[index]);
  97. ASSERT_TRUE(s == strings[index++]);
  98. }
  99. }
  100. }
  101. TEST_F(MindDataTestStringTensorDE, SetItem) {
  102. std::vector<std::string> strings{"abc", "defg", "hi", "klmno", "123", "789"};
  103. std::shared_ptr<Tensor> t3 = std::make_shared<Tensor>(strings, TensorShape({2, 3}));
  104. ASSERT_TRUE(t3->shape() == TensorShape({2, 3}));
  105. t3->SetItemAt({0, 1}, std::string{"xyzz"});
  106. strings[1] = "xyzz";
  107. t3->SetItemAt({0, 2}, std::string{"07"});
  108. strings[2] = "07";
  109. t3->SetItemAt({1, 2}, std::string{"987"});
  110. strings[5] = "987";
  111. uint32_t index = 0;
  112. for (uint32_t i = 0; i < 2; i++) {
  113. for (uint32_t j = 0; j < 3; j++) {
  114. std::string_view s = "";
  115. t3->GetItemAt(&s, {i, j});
  116. ASSERT_TRUE(s == strings[index++]);
  117. }
  118. }
  119. }
  120. TEST_F(MindDataTestStringTensorDE, Iterator) {
  121. std::vector<std::string> strings{"abc", "defg", "hi", "klmno", "123", "789"};
  122. std::shared_ptr<Tensor> t = std::make_shared<Tensor>(strings, TensorShape({2, 3}));
  123. uint32_t index = 0;
  124. auto itr = t->begin<std::string_view>();
  125. for (; itr != t->end<std::string_view>(); itr++) {
  126. ASSERT_TRUE(*itr == strings[index++]);
  127. }
  128. index = 0;
  129. itr = t->begin<std::string_view>();
  130. for (; itr != t->end<std::string_view>(); itr += 2) {
  131. ASSERT_TRUE(*itr == strings[index]);
  132. index += 2;
  133. }
  134. }