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

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