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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, false);
  71. }
  72. TEST_F(TestMemoryDumper, test_DumpToFileNotExistDir) {
  73. int len = 1;
  74. int data[1] = {0};
  75. for (uint32_t i = 0; i < len; i++) {
  76. data[i] = i % 10;
  77. }
  78. const std::string filename = "./tmp/dumpToFileTestFile";
  79. int ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int), ShapeVector {1,}, kNumberTypeInt32);
  80. ASSERT_EQ(ret, true);
  81. int fd = open((filename + ".npy").c_str(), O_RDONLY);
  82. int readBack[1000] = {0};
  83. int readSize = read(fd, readBack, len * sizeof(int));
  84. (void)close(fd);
  85. ASSERT_EQ(readSize, len * sizeof(int));
  86. ret = true;
  87. for (uint32_t i = 0; i < len; i++) {
  88. // Skip the size of npy header.
  89. if (data[i] != readBack[i+1]) {
  90. ret = false;
  91. break;
  92. }
  93. }
  94. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  95. if (fs->FileExist(filename)) {
  96. fs->DeleteFile(filename);
  97. }
  98. ASSERT_EQ(ret, true);
  99. }
  100. } // namespace mindspore