MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
test_sharedmem.cpp File Reference
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <catch2/catch_test_macros.hpp>
#include "mxl-internal/SharedMemory.hpp"
Include dependency graph for test_sharedmem.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("Create", "[shared mem]")

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "Create" ,
"" [shared mem] )

Definition at line 26 of file test_sharedmem.cpp.

27{
28 auto const p = std::filesystem::path{"./mxl_shmtest_file.bin"};
29 remove(p);
30
32
34 REQUIRE(!reader.isValid());
35 REQUIRE(reader.get() == nullptr);
36 REQUIRE(reader.mappedSize() == 0U);
37 REQUIRE(!reader.created());
38
39 // Try to open a non-existing file for reading without automatically creating it.
40 REQUIRE_THROWS(
41 [&]()
42 {
44 }());
45
47 REQUIRE(!reader.isValid());
48 REQUIRE(reader.get() == nullptr);
49 REQUIRE(reader.mappedSize() == 0U);
50 REQUIRE(!reader.created());
51
53
55 REQUIRE(!writer.isValid());
56 REQUIRE(writer.get() == nullptr);
57 REQUIRE(writer.mappedSize() == 0U);
58 REQUIRE(!writer.created());
59
60 // Try to open a non-existing file for wrting without automatically creating it.
61 REQUIRE_THROWS(
62 [&]()
63 {
65 }());
66
68 REQUIRE(!writer.isValid());
69 REQUIRE(writer.get() == nullptr);
70 REQUIRE(writer.mappedSize() == 0U);
71 REQUIRE(!writer.created());
72
73 // Now try to open it with the create mode set to true
74 REQUIRE_NOTHROW(
75 [&]()
76 {
79 }());
80
81 // Confirm that the mapping is valid
82 REQUIRE(!!writer);
83 REQUIRE(writer.isValid());
84 REQUIRE(writer.get() != nullptr);
85 REQUIRE(writer.mappedSize() == TOTAL_SIZE);
86 REQUIRE(writer.created());
87
88 // Confirm correct creatiion via default constructor of payload
89 REQUIRE(writer.get()->testData == 123456789ULL);
90
91 // Confirm that it exists.
92 REQUIRE(exists(p));
93 // Confirm that the file is the right size.
94 REQUIRE(file_size(p) == TOTAL_SIZE);
95
96 // Try to open again the existing file.
97 REQUIRE_NOTHROW(
98 [&]()
99 {
101 }());
102
103 // Confirm that the mapping is valid
104 REQUIRE(!!reader);
105 REQUIRE(reader.isValid());
106 REQUIRE(reader.get() != nullptr);
107 REQUIRE(reader.mappedSize() == TOTAL_SIZE);
108 REQUIRE(!reader.created());
109
110 auto const writer_data = writer.get();
111 auto const reader_data = reader.get();
112
113 writer_data->testData = 99;
114
115 // Confirm that the data is shared between the two objects.
116 REQUIRE(reader_data->testData == 99);
117
118 REQUIRE_NOTHROW(
119 [&]()
120 {
121 reader = {};
122 writer = {};
123 }());
124
125 REQUIRE(!reader);
126 REQUIRE(!reader.isValid());
127 REQUIRE(reader.get() == nullptr);
128 REQUIRE(reader.mappedSize() == 0U);
129 REQUIRE(!reader.created());
130
131 REQUIRE(!writer);
132 REQUIRE(!writer.isValid());
133 REQUIRE(writer.get() == nullptr);
134 REQUIRE(writer.mappedSize() == 0U);
135 REQUIRE(!writer.created());
136
137 // Final cleanup
138 if (exists(p))
139 {
140 remove(p);
141 }
142}
mxlFlowReader reader
mxlFlowWriter writer
REQUIRE(timeDelta< 33 '366 '667LL)