|
| 1 | +#include "dlt_common.h" |
| 2 | + |
| 3 | +#include <boost/interprocess/file_mapping.hpp> |
| 4 | +#include <boost/interprocess/mapped_region.hpp> |
| 5 | +#include <gtest/gtest.h> |
| 6 | + |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdlib> |
| 9 | +#include <cstring> |
| 10 | +#include <filesystem> |
| 11 | +#include <fstream> |
| 12 | +#include <iostream> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +#define DLT_DAEMON_TEXTSIZE 10024 |
| 16 | + |
| 17 | +// TEST(ParseDltFile, initialize_open_free) { |
| 18 | +// DltFile file; |
| 19 | +// static char text[DLT_DAEMON_TEXTSIZE]; |
| 20 | +// /* Get PWD so file can be used*/ |
| 21 | +// std::filesystem::path openfile{std::filesystem::current_path() / "testfile_10.dlt"}; |
| 22 | +// std::filesystem::path output{"/tmp/output_testfile_10.txt"}; |
| 23 | +// |
| 24 | +// /* Normal Use-Case, expected 0 */ |
| 25 | +// EXPECT_LE(0, dlt_file_init(&file, 0)); |
| 26 | +// EXPECT_LE(0, dlt_file_open(&file, openfile.c_str(), 0)); |
| 27 | +// |
| 28 | +// while (dlt_file_read(&file, 0) >= 0) { |
| 29 | +// // throw std::exception{}; |
| 30 | +// } |
| 31 | +// |
| 32 | +// for (int i = 0; i < file.counter; i++) { |
| 33 | +// EXPECT_LE(0, dlt_file_message(&file, i, 0)); |
| 34 | +// EXPECT_LE(0, dlt_message_print_ascii(&file.msg, text, DLT_DAEMON_TEXTSIZE, 0)); |
| 35 | +// } |
| 36 | +// |
| 37 | +// // for (int i = 0; i < file.counter; i++) { |
| 38 | +// // EXPECT_LE(0, dlt_file_message(&file, i, 0)); |
| 39 | +// // EXPECT_LE(0, dlt_message_print_ascii(&file.msg, text, DLT_DAEMON_TEXTSIZE, 1)); |
| 40 | +// // } |
| 41 | +// |
| 42 | +// EXPECT_LE(0, dlt_file_free(&file, 0)); |
| 43 | +// } |
| 44 | + |
| 45 | +TEST(ParseDltFile, memory_mapped_file) { |
| 46 | + using namespace boost::interprocess; |
| 47 | + |
| 48 | + // Define file names |
| 49 | + const char* FileName = "testfile_10.dlt"; |
| 50 | + |
| 51 | + // Open the file mapping and map it as read-only |
| 52 | + boost::interprocess::file_mapping m_file(FileName, read_only); |
| 53 | + |
| 54 | + mapped_region region(m_file, read_only); |
| 55 | + |
| 56 | + // Get the address of the mapped region |
| 57 | + void* addr = region.get_address(); |
| 58 | + std::size_t size = region.get_size(); |
| 59 | + |
| 60 | + // Check that memory was initialized to 1 |
| 61 | + const char* mem = static_cast<char*>(addr); |
| 62 | + for (std::size_t i = 0; i < size; ++i) |
| 63 | + if (*mem++ != 1) |
| 64 | + std::cout << *mem; |
| 65 | +} |
| 66 | + |
| 67 | +// TEST(ParseDltFile, filebuffer) { |
| 68 | +// // Define file names |
| 69 | +// const char* FileName = "testfile_10.dlt"; |
| 70 | +// const std::size_t FileSize = 10000; |
| 71 | +// |
| 72 | +// // Now test it reading the file |
| 73 | +// std::filebuf fbuf; |
| 74 | +// fbuf.open(FileName, std::ios_base::in | std::ios_base::binary); |
| 75 | +// |
| 76 | +// // Read it to memory |
| 77 | +// std::vector<char> vect(FileSize, 0); |
| 78 | +// fbuf.sgetn(&vect[0], std::streamsize(vect.size())); |
| 79 | +// |
| 80 | +// // Check that memory was initialized to 1 |
| 81 | +// const char* mem = static_cast<char*>(&vect[0]); |
| 82 | +// for (std::size_t i = 0; i < FileSize; ++i) |
| 83 | +// if (*mem++ != 1) |
| 84 | +// std::cout << *mem; |
| 85 | +// } |
0 commit comments