|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/devtools/etdump/data_sinks/file_data_sink.h> |
| 10 | +#include <executorch/runtime/core/exec_aten/exec_aten.h> |
| 11 | +#include <executorch/runtime/platform/runtime.h> |
| 12 | +#include <gtest/gtest.h> |
| 13 | +#include <stdio.h> // tmpnam(), remove() |
| 14 | +#include <fstream> |
| 15 | + |
| 16 | +using namespace ::testing; |
| 17 | +using ::executorch::etdump::FileDataSink; |
| 18 | +using ::executorch::runtime::Error; |
| 19 | +using ::executorch::runtime::Result; |
| 20 | + |
| 21 | +class FileDataSinkTest : public ::testing::Test { |
| 22 | + protected: |
| 23 | + void SetUp() override { |
| 24 | + // Initialize the runtime environment |
| 25 | + torch::executor::runtime_init(); |
| 26 | + |
| 27 | + // Define the file path for testing |
| 28 | + std::array<char, L_tmpnam> buf; |
| 29 | + const char* ret = std::tmpnam(buf.data()); |
| 30 | + ASSERT_NE(ret, nullptr) << "Could not generate temp file"; |
| 31 | + buf[L_tmpnam - 1] = '\0'; |
| 32 | + file_path_ = std::string(buf.data()) + "-executorch-testing"; |
| 33 | + } |
| 34 | + |
| 35 | + void TearDown() override { |
| 36 | + // Remove the test file |
| 37 | + std::remove(file_path_.c_str()); |
| 38 | + } |
| 39 | + |
| 40 | + std::string file_path_; |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(FileDataSinkTest, CreationExpectFail) { |
| 44 | + // Create a FileDataSink instance with a valid file path |
| 45 | + Result<FileDataSink> success = FileDataSink::create(file_path_.c_str()); |
| 46 | + ASSERT_TRUE(success.ok()); |
| 47 | + |
| 48 | + // Try to create another FileDataSink instance with an invalid file path |
| 49 | + Result<FileDataSink> fail_with_invalid_file_path = FileDataSink::create(""); |
| 50 | + ASSERT_EQ(fail_with_invalid_file_path.error(), Error::AccessFailed); |
| 51 | +} |
| 52 | + |
| 53 | +TEST_F(FileDataSinkTest, WriteDataToFile) { |
| 54 | + const char* data = "Hello, World!"; |
| 55 | + size_t data_size = strlen(data); |
| 56 | + |
| 57 | + // Create a FileDataSink instance |
| 58 | + Result<FileDataSink> result = FileDataSink::create(file_path_.c_str()); |
| 59 | + ASSERT_TRUE(result.ok()); |
| 60 | + |
| 61 | + FileDataSink* data_sink = &result.get(); |
| 62 | + |
| 63 | + // Write data to the file |
| 64 | + Result<size_t> write_result = data_sink->write(data, data_size); |
| 65 | + ASSERT_TRUE(write_result.ok()); |
| 66 | + |
| 67 | + size_t used_bytes = data_sink->get_used_bytes(); |
| 68 | + EXPECT_EQ(used_bytes, data_size); |
| 69 | + |
| 70 | + data_sink->close(); |
| 71 | + |
| 72 | + // Expect fail if write again after close |
| 73 | + Result<size_t> write_result_after_close = data_sink->write(data, data_size); |
| 74 | + ASSERT_EQ(write_result_after_close.error(), Error::AccessFailed); |
| 75 | + |
| 76 | + // Verify the file contents |
| 77 | + std::ifstream file(file_path_, std::ios::binary); |
| 78 | + file.seekg(0, std::ios::end); |
| 79 | + size_t file_size = file.tellg(); |
| 80 | + file.seekg(0, std::ios::beg); |
| 81 | + EXPECT_EQ(file_size, used_bytes); |
| 82 | + |
| 83 | + // Read the file content and verify it matches the original data |
| 84 | + std::vector<char> file_content(file_size); |
| 85 | + file.read(file_content.data(), file_size); |
| 86 | + file.close(); |
| 87 | + |
| 88 | + EXPECT_EQ(std::memcmp(file_content.data(), data, data_size), 0); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(FileDataSinkTest, WriteMultipleDataAndCheckOffsets) { |
| 92 | + const char* data1 = "Accelerate"; |
| 93 | + const char* data2 = "Core"; |
| 94 | + const char* data3 = "Experience"; |
| 95 | + size_t data1_size = strlen(data1); |
| 96 | + size_t data2_size = strlen(data2); |
| 97 | + size_t data3_size = strlen(data3); |
| 98 | + |
| 99 | + // Create a FileDataSink instance |
| 100 | + Result<FileDataSink> result = FileDataSink::create(file_path_.c_str()); |
| 101 | + ASSERT_TRUE(result.ok()); |
| 102 | + FileDataSink* data_sink = &result.get(); |
| 103 | + |
| 104 | + // Write multiple data chunks and check offsets |
| 105 | + Result<size_t> offset1 = data_sink->write(data1, data1_size); |
| 106 | + ASSERT_TRUE(offset1.ok()); |
| 107 | + EXPECT_EQ(offset1.get(), 0); |
| 108 | + |
| 109 | + Result<size_t> offset2 = data_sink->write(data2, data2_size); |
| 110 | + ASSERT_TRUE(offset2.ok()); |
| 111 | + EXPECT_EQ(offset2.get(), data1_size); |
| 112 | + |
| 113 | + Result<size_t> offset3 = data_sink->write(data3, data3_size); |
| 114 | + ASSERT_TRUE(offset3.ok()); |
| 115 | + EXPECT_EQ(offset3.get(), data1_size + data2_size + data3_size); |
| 116 | + size_t used_bytes = data_sink->get_used_bytes(); |
| 117 | + EXPECT_EQ(used_bytes, data1_size + data2_size); |
| 118 | + |
| 119 | + data_sink->close(); |
| 120 | + |
| 121 | + // Verify the file contents |
| 122 | + std::ifstream file(file_path_, std::ios::binary); |
| 123 | + file.seekg(0, std::ios::end); |
| 124 | + size_t file_size = file.tellg(); |
| 125 | + file.seekg(0, std::ios::beg); |
| 126 | + EXPECT_EQ(file_size, used_bytes); |
| 127 | + |
| 128 | + // Read the file content |
| 129 | + std::vector<char> file_content(file_size); |
| 130 | + file.read(file_content.data(), file_size); |
| 131 | + file.close(); |
| 132 | + |
| 133 | + // Verify each data chunk in the file using offsets |
| 134 | + EXPECT_EQ( |
| 135 | + std::memcmp(file_content.data() + offset1.get(), data1, data1_size), 0); |
| 136 | + EXPECT_EQ( |
| 137 | + std::memcmp(file_content.data() + offset2.get(), data2, data2_size), 0); |
| 138 | + EXPECT_EQ( |
| 139 | + std::memcmp(file_content.data() + offset3.get(), data3, data3_size), 0); |
| 140 | +} |
0 commit comments