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

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