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