|
| 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 | + size_t used_bytes; |
| 55 | + const char* data = "Hello, World!"; |
| 56 | + size_t data_size = strlen(data); |
| 57 | + |
| 58 | + { |
| 59 | + // Create a FileDataSink instance |
| 60 | + Result<FileDataSink> result = FileDataSink::create(file_path_.c_str()); |
| 61 | + ASSERT_TRUE(result.ok()); |
| 62 | + |
| 63 | + FileDataSink* data_sink = &result.get(); |
| 64 | + |
| 65 | + // Write data to the file |
| 66 | + Result<size_t> write_result = data_sink->write(data, data_size); |
| 67 | + ASSERT_TRUE(write_result.ok()); |
| 68 | + |
| 69 | + used_bytes = data_sink->get_used_bytes(); |
| 70 | + EXPECT_EQ(used_bytes, data_size); |
| 71 | + } |
| 72 | + |
| 73 | + // Verify the file contents |
| 74 | + std::ifstream file(file_path_, std::ios::binary); |
| 75 | + file.seekg(0, std::ios::end); |
| 76 | + size_t file_size = file.tellg(); |
| 77 | + file.seekg(0, std::ios::beg); |
| 78 | + EXPECT_EQ(file_size, used_bytes); |
| 79 | + |
| 80 | + // Read the file content and verify it matches the original data |
| 81 | + std::vector<char> file_content(file_size); |
| 82 | + file.read(file_content.data(), file_size); |
| 83 | + file.close(); |
| 84 | + |
| 85 | + EXPECT_EQ(std::memcmp(file_content.data(), data, data_size), 0); |
| 86 | +} |
0 commit comments