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.

memory_dumper_test.cc 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright 2020 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 <fcntl.h>
  17. #include <iostream>
  18. #include <memory>
  19. #include "common/common_test.h"
  20. #include "utils/system/file_system.h"
  21. #include "utils/system/env.h"
  22. #define private public
  23. #include "debug/data_dump/dump_json_parser.h"
  24. #undef private
  25. namespace mindspore {
  26. class TestMemoryDumper : public UT::Common {
  27. public:
  28. TestMemoryDumper() {}
  29. };
  30. TEST_F(TestMemoryDumper, test_DumpToFileAbsPath) {
  31. int len = 1000;
  32. int data[len] = {0};
  33. for (uint32_t i = 0; i < len; i++) {
  34. data[i] = i % 10;
  35. }
  36. int ret;
  37. const std::string filename = "/tmp/dumpToFileTestFile";
  38. ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int), ShapeVector {10, 100}, kNumberTypeInt32);
  39. ASSERT_EQ(ret, true);
  40. int fd = open((filename + ".npy").c_str(), O_RDONLY);
  41. int header_size = 32;
  42. int npylen = len + header_size;
  43. int readBack[npylen] = {0};
  44. int readSize = read(fd, readBack, npylen * sizeof(int));
  45. (void)close(fd);
  46. ASSERT_EQ(readSize, npylen * sizeof(int));
  47. ret = true;
  48. for (uint32_t i = 0; i < len; i++) {
  49. // Skip the size of npy header.
  50. if (data[i] != readBack[i+header_size]) {
  51. ret = false;
  52. break;
  53. }
  54. }
  55. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  56. if (fs->FileExist(filename)) {
  57. fs->DeleteFile(filename);
  58. }
  59. ASSERT_EQ(ret, true);
  60. }
  61. TEST_F(TestMemoryDumper, test_DumpToFileRelativePath) {
  62. int len = 1000;
  63. int data[len] = {0};
  64. for (uint32_t i = 0; i < len; i++) {
  65. data[i] = i % 10;
  66. }
  67. int ret;
  68. const std::string filename = "../../dumpToFileTestFile";
  69. ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int), ShapeVector{100, 10}, kNumberTypeInt32);
  70. ASSERT_EQ(ret, true);
  71. int fd = open((filename + ".npy").c_str(), O_RDONLY);
  72. int header_size = 32;
  73. int npylen = len + header_size;
  74. int readBack[npylen] = {0};
  75. int readSize = read(fd, readBack, npylen * sizeof(int));
  76. (void)close(fd);
  77. ASSERT_EQ(readSize, npylen * sizeof(int));
  78. ret = true;
  79. for (uint32_t i = 0; i < len; i++) {
  80. // Skip the size of npy header.
  81. if (data[i] != readBack[i+header_size]) {
  82. ret = false;
  83. break;
  84. }
  85. }
  86. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  87. if (fs->FileExist(filename)) {
  88. fs->DeleteFile(filename);
  89. }
  90. ASSERT_EQ(ret, true);
  91. }
  92. TEST_F(TestMemoryDumper, test_DumpToFileNotExistDir) {
  93. int len = 1;
  94. int data[1] = {0};
  95. for (uint32_t i = 0; i < len; i++) {
  96. data[i] = i % 10;
  97. }
  98. const std::string filename = "./tmp/dumpToFileTestFile";
  99. int ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int), ShapeVector {1,}, kNumberTypeInt32);
  100. ASSERT_EQ(ret, true);
  101. int fd = open((filename + ".npy").c_str(), O_RDONLY);
  102. int readBack[1000] = {0};
  103. int readSize = read(fd, readBack, len * sizeof(int));
  104. (void)close(fd);
  105. ASSERT_EQ(readSize, len * sizeof(int));
  106. ret = true;
  107. for (uint32_t i = 0; i < len; i++) {
  108. // Skip the size of npy header.
  109. if (data[i] != readBack[i+1]) {
  110. ret = false;
  111. break;
  112. }
  113. }
  114. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  115. if (fs->FileExist(filename)) {
  116. fs->DeleteFile(filename);
  117. }
  118. ASSERT_EQ(ret, true);
  119. }
  120. } // namespace mindspore