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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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[1000] = {0};
  33. for (uint32_t i = 0; i < len; i++) {
  34. data[i] = i % 10;
  35. }
  36. int ret;
  37. char filename[] = "/tmp/dumpToFileTestFile";
  38. ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int));
  39. ASSERT_EQ(ret, true);
  40. int fd = open(filename, O_RDONLY);
  41. int readBack[1000] = {0};
  42. int readSize = read(fd, readBack, len * sizeof(int));
  43. (void)close(fd);
  44. ASSERT_EQ(readSize, len * sizeof(int));
  45. ret = true;
  46. for (uint32_t i = 0; i < len; i++) {
  47. if (data[i] != readBack[i]) {
  48. ret = false;
  49. break;
  50. }
  51. }
  52. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  53. if (fs->FileExist(filename)) {
  54. fs->DeleteFile(filename);
  55. }
  56. ASSERT_EQ(ret, true);
  57. }
  58. TEST_F(TestMemoryDumper, test_DumpToFileRelativePath) {
  59. int len = 1000;
  60. int data[1000] = {0};
  61. for (uint32_t i = 0; i < len; i++) {
  62. data[i] = i % 10;
  63. }
  64. int ret;
  65. char filename[] = "../../dumpToFileTestFile";
  66. ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int));
  67. ASSERT_EQ(ret, true);
  68. int fd = open(filename, O_RDONLY);
  69. int readBack[1000] = {0};
  70. int readSize = read(fd, readBack, len * sizeof(int));
  71. (void)close(fd);
  72. ASSERT_EQ(readSize, len * sizeof(int));
  73. ret = true;
  74. for (uint32_t i = 0; i < len; i++) {
  75. if (data[i] != readBack[i]) {
  76. ret = false;
  77. break;
  78. }
  79. }
  80. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  81. if (fs->FileExist(filename)) {
  82. fs->DeleteFile(filename);
  83. }
  84. ASSERT_EQ(ret, true);
  85. }
  86. TEST_F(TestMemoryDumper, test_DumpToFileNotExistDir) {
  87. int len = 1;
  88. int data[1] = {0};
  89. for (uint32_t i = 0; i < len; i++) {
  90. data[i] = i % 10;
  91. }
  92. char filename[] = "./tmp/dumpToFileTestFile";
  93. int ret = DumpJsonParser::DumpToFile(filename, data, len * sizeof(int));
  94. ASSERT_EQ(ret, true);
  95. int fd = open(filename, O_RDONLY);
  96. int readBack[1000] = {0};
  97. int readSize = read(fd, readBack, len * sizeof(int));
  98. (void)close(fd);
  99. ASSERT_EQ(readSize, len * sizeof(int));
  100. ret = true;
  101. for (uint32_t i = 0; i < len; i++) {
  102. if (data[i] != readBack[i]) {
  103. ret = false;
  104. break;
  105. }
  106. }
  107. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  108. if (fs->FileExist(filename)) {
  109. fs->DeleteFile(filename);
  110. }
  111. ASSERT_EQ(ret, true);
  112. }
  113. } // namespace mindspore