Skip to content

Commit 71107a8

Browse files
add unit tests for data sharing mechanisms
1 parent a9832ed commit 71107a8

File tree

6 files changed

+132
-132
lines changed

6 files changed

+132
-132
lines changed

libraries/utils/include/utils/FileStructures.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace curan
1717
Eigen::Matrix<double, 4, 4> f_homogeneous_transformation;
1818
double f_optimization_error;
1919

20+
void decode(std::istream &instream);
21+
2022
public:
2123
UltrasoundCalibrationData(const std::string &in_timestamp,
2224
const Eigen::Matrix<double, 4, 4> &in_homogeneous_transformation,
@@ -28,6 +30,8 @@ namespace curan
2830

2931
UltrasoundCalibrationData(const std::string &path);
3032

33+
UltrasoundCalibrationData(std::istream &instream);
34+
3135
std::string timestamp() const;
3236

3337
Eigen::Matrix<double, 4, 4> homogeneous_transformation() const;
@@ -52,6 +56,8 @@ namespace curan
5256
double f_registration_error;
5357
Type f_registration_type = Type::LANDMARK;
5458

59+
void decode(std::istream &instream);
60+
5561
public:
5662
RegistrationData(const std::string &in_timestamp,
5763
const Eigen::Matrix<double, 4, 4> &in_moving_to_fixed_transform,
@@ -65,6 +71,8 @@ namespace curan
6571

6672
RegistrationData(const std::string &path);
6773

74+
RegistrationData(std::istream &instream);
75+
6876
std::string timestamp() const;
6977

7078
Eigen::Matrix<double, 4, 4> moving_to_fixed_transform() const;
@@ -82,6 +90,8 @@ namespace curan
8290
Eigen::Matrix<double, 4, 4> f_needle_homogeneous_transformation;
8391
double f_optimization_error;
8492

93+
void decode(std::istream &instream);
94+
8595
public:
8696
NeedleCalibrationData(const std::string &in_timestamp,
8797
const Eigen::Matrix<double, 4, 4> &in_needle_homogeneous_transformation,
@@ -93,6 +103,8 @@ namespace curan
93103

94104
NeedleCalibrationData(const std::string &path);
95105

106+
NeedleCalibrationData(std::istream &instream);
107+
96108
std::string timestamp() const;
97109

98110
Eigen::Matrix<double, 4, 4> needle_calibration() const;
@@ -110,6 +122,8 @@ namespace curan
110122
Eigen::Matrix<double, 3, 3> f_desired_orientation;
111123
std::string f_path_to_image;
112124

125+
void decode(std::istream &instream);
126+
113127
public:
114128
TrajectorySpecificationData(const std::string &in_timestamp,
115129
const Eigen::Matrix<double, 3, 1> &in_target,
@@ -125,6 +139,8 @@ namespace curan
125139

126140
TrajectorySpecificationData(const std::string &path);
127141

142+
TrajectorySpecificationData(std::istream &instream);
143+
128144
std::string timestamp() const;
129145

130146
Eigen::Matrix<double, 3, 1> target() const;

libraries/utils/src/FileStructures.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ namespace curan
1515
{
1616
nlohmann::json calibration_data;
1717
std::ifstream in(path);
18-
1918
if (!in.is_open())
2019
throw std::runtime_error("failure to open configuration file");
20+
decode(in);
21+
}
22+
23+
UltrasoundCalibrationData::UltrasoundCalibrationData(std::istream &instream){
24+
decode(instream);
25+
}
2126

22-
in >> calibration_data;
27+
void UltrasoundCalibrationData::decode(std::istream &instream){
28+
nlohmann::json calibration_data;
29+
instream >> calibration_data;
2330
f_timestamp = calibration_data["timestamp"];
2431
std::string homogenenous_transformation = calibration_data["homogeneous_transformation"];
2532
f_optimization_error = calibration_data["optimization_error"];
@@ -36,6 +43,8 @@ namespace curan
3643
f_homogeneous_transformation(row, col) = calibration_matrix(row, col);
3744
}
3845

46+
47+
3948
std::string UltrasoundCalibrationData::timestamp() const
4049
{
4150
return f_timestamp;
@@ -78,7 +87,16 @@ namespace curan
7887
if (!in.is_open())
7988
throw std::runtime_error("failure to open configuration file");
8089

81-
in >> registration_data;
90+
decode(in);
91+
}
92+
93+
RegistrationData::RegistrationData(std::istream &instream){
94+
decode(instream);
95+
}
96+
97+
void RegistrationData::decode(std::istream &instream){
98+
nlohmann::json registration_data;
99+
instream >> registration_data;
82100

83101
if (auto search = conversion_to_type.find(registration_data["type"]); search != conversion_to_type.end())
84102
f_registration_type = search->second;
@@ -144,7 +162,17 @@ namespace curan
144162
if (!in.is_open())
145163
throw std::runtime_error("failure to open configuration file");
146164

147-
in >> needle_calibration_data;
165+
decode(in);
166+
}
167+
168+
NeedleCalibrationData::NeedleCalibrationData(std::istream &instream)
169+
{
170+
decode(instream);
171+
}
172+
173+
void NeedleCalibrationData::decode(std::istream &instream){
174+
nlohmann::json needle_calibration_data;
175+
instream >> needle_calibration_data;
148176
f_timestamp = needle_calibration_data["timestamp"];
149177
std::string homogenenous_transformation = needle_calibration_data["needle_homogeneous_transformation"];
150178
f_optimization_error = needle_calibration_data["optimization_error"];
@@ -196,8 +224,17 @@ namespace curan
196224
std::ifstream in(path);
197225
if (!in.is_open())
198226
throw std::runtime_error("failure to open configuration file");
227+
decode(in);
228+
}
199229

200-
in >> trajectory_data;
230+
TrajectorySpecificationData::TrajectorySpecificationData(std::istream &instream)
231+
{
232+
decode(instream);
233+
}
234+
235+
void TrajectorySpecificationData::decode(std::istream &instream){
236+
nlohmann::json trajectory_data;
237+
instream >> trajectory_data;
201238

202239
std::stringstream ss;
203240
std::string target = trajectory_data["target"];

tests/utilities/testing_cpp_mechanics.cpp

Lines changed: 6 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -2,134 +2,16 @@
22
#include <string>
33
#include <chrono>
44
#include <thread>
5+
#include "utils/DateManipulation.h"
56

6-
class MemoryOwner // this is a naive implementation of a memory owner, simillar to a shared pointer
7-
{
8-
struct ControlBlock
9-
{
10-
std::atomic<size_t> counter = 1;
11-
12-
void increment()
13-
{
14-
counter.fetch_add(1);
15-
}
16-
17-
bool decrement()
18-
{
19-
return counter.fetch_sub(1)-1 == 0;
20-
}
21-
};
22-
public:
23-
void *pointer_to_data;
24-
size_t block_of_memory;
25-
26-
MemoryOwner() : pointer_to_data{nullptr}, control_block{nullptr}
27-
{
28-
std::cout << "alocated MemoryOwner()" << std::endl;
29-
control_block = new ControlBlock{};
30-
print_addresses();
31-
}
32-
33-
MemoryOwner(size_t block_size) : pointer_to_data{nullptr}, control_block{nullptr}
34-
{
35-
std::cout << "alocated MemoryOwner(size_t block_size)" << std::endl;
36-
control_block = new ControlBlock{};
37-
pointer_to_data = new unsigned char[block_size]{};
38-
block_of_memory = block_size;
39-
print_addresses();
40-
}
41-
42-
MemoryOwner(const MemoryOwner &other) : pointer_to_data{nullptr}, control_block{nullptr}
43-
{
44-
std::cout << "alocated MemoryOwner(const MemoryOwner &other)" << std::endl;
45-
control_block = other.control_block;
46-
control_block->increment();
47-
pointer_to_data = other.pointer_to_data;
48-
print_addresses();
49-
}
50-
51-
MemoryOwner &operator=(const MemoryOwner &other)
52-
{
53-
std::cout << "copied MemoryOwner &operator=(const MemoryOwner &other)" << std::endl;
54-
decrement();
55-
control_block = other.control_block;
56-
control_block->increment();
57-
pointer_to_data = other.pointer_to_data;
58-
print_addresses();
59-
}
60-
61-
MemoryOwner(MemoryOwner &&other) : pointer_to_data{nullptr}, control_block{nullptr}
62-
{
63-
std::cout << "alocated MemoryOwner(MemoryOwner &&other)" << std::endl;
64-
control_block = other.control_block;
65-
control_block->increment();
66-
pointer_to_data = other.pointer_to_data;
67-
print_addresses();
68-
}
69-
70-
MemoryOwner &operator=(MemoryOwner &&other)
71-
{
72-
std::cout << "copied MemoryOwner &operator=(MemoryOwner &&other)" << std::endl;
73-
decrement();
74-
control_block = other.control_block;
75-
control_block->increment();
76-
pointer_to_data = other.pointer_to_data;
77-
print_addresses();
78-
}
79-
80-
~MemoryOwner()
81-
{
82-
decrement();
83-
std::cout << "destructor ~MemoryOwner()" << std::endl;
84-
}
85-
86-
void Alloc(size_t size)
87-
{
88-
decrement();
89-
control_block = new ControlBlock{};
90-
pointer_to_data = new unsigned char[size]{};
91-
block_of_memory = size;
92-
std::cout << "raw Alloc(size_t size)" << std::endl;
93-
print_addresses();
94-
}
95-
96-
void* data(){
97-
return pointer_to_data;
98-
}
99-
100-
size_t size(){
101-
return block_of_memory;
102-
}
7+
int main(){
8+
try{
9+
std::cout << curan::utilities::formated_date<std::chrono::system_clock>(std::chrono::system_clock::time_point(std::chrono::system_clock::duration(0))) << std::endl;
10310

104-
private:
105-
void decrement()
106-
{
107-
if (control_block->decrement())
108-
{
109-
print_addresses();
110-
if (pointer_to_data){
111-
std::cout << "decrement(): deleting block of memory" << std::endl;
112-
delete[] pointer_to_data;
113-
}
114-
std::cout << "decrement(): deleting control block" << std::endl;
115-
delete control_block;
116-
}
117-
pointer_to_data = nullptr;
118-
control_block = nullptr;
119-
}
120-
ControlBlock *control_block;
11+
std::cout << curan::utilities::formated_date<std::chrono::system_clock>(std::chrono::system_clock::time_point(std::chrono::system_clock::duration(std::chrono::seconds(1000000000)))) << std::endl;
12112

122-
void print_addresses(){
123-
std::printf("(this: %p) (pointer_to_data:%p) (control_block:%p)\n",this,pointer_to_data,control_block);
124-
}
125-
};
13+
std::cout << curan::utilities::formated_date<std::chrono::system_clock>(std::chrono::system_clock::time_point(std::chrono::system_clock::duration(1000000000))) << std::endl;
12614

127-
int main(){
128-
try{
129-
MemoryOwner owner;
130-
MemoryOwner owner2{10};
131-
MemoryOwner copied_owner{owner};
132-
MemoryOwner copied_owner2{owner2};
13315
} catch (std::runtime_error &e){
13416
std::cout << "exception: " << e.what() << std::endl;
13517
return 2;

unittests/utils/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ gtest_discover_tests(MemoryUtils)
1818

1919
add_executable(DateFormater date_formater.cpp)
2020
target_link_libraries(DateFormater GTest::gtest_main utils)
21-
gtest_discover_tests(DateFormater)
21+
gtest_discover_tests(DateFormater)
22+
23+
add_executable(FileSharing file_data.cpp)
24+
target_link_libraries(FileSharing GTest::gtest_main utils)
25+
gtest_discover_tests(FileSharing)

unittests/utils/date_formater.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <gtest/gtest.h>
33

44
TEST(UnitTestDateFormater, DateFormater){
5-
auto current_date = curan::utilities::formated_date<std::chrono::system_clock>(std::chrono::system_clock::now());
6-
5+
const std::string expected{"1970-01-01 00:00:00"};
6+
EXPECT_EQ(curan::utilities::formated_date<std::chrono::system_clock>(std::chrono::system_clock::time_point(std::chrono::system_clock::duration(0))), expected);
7+
const std::string expected2{"2001-09-09 02:46:40"};
8+
EXPECT_EQ(curan::utilities::formated_date<std::chrono::system_clock>(std::chrono::system_clock::time_point(std::chrono::system_clock::duration(std::chrono::seconds(1000000000)))), expected2);
79
}

unittests/utils/file_data.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "utils/FileStructures.h"
2+
#include <gtest/gtest.h>
3+
4+
const std::string date{"1970-01-01 00:00:00"};
5+
6+
TEST(UnitTestFilePropagator, NeedleCalibrationData)
7+
{
8+
for (size_t run_n_times = 0; run_n_times < 20; ++run_n_times)
9+
{
10+
Eigen::Matrix<double, 4, 4> transform = Eigen::Matrix<double, 4, 4>::Identity();
11+
curan::utilities::NeedleCalibrationData source_needle_calib(date, transform, 1.0);
12+
std::stringstream mock_file;
13+
mock_file << source_needle_calib;
14+
curan::utilities::NeedleCalibrationData target_needle_calib{mock_file};
15+
ASSERT_EQ(source_needle_calib, target_needle_calib) << "the serialization deserealization process loses data";
16+
}
17+
}
18+
19+
TEST(UnitTestFilePropagator, RegistrationData)
20+
{
21+
for (size_t run_n_times = 0; run_n_times < 20; ++run_n_times)
22+
{
23+
Eigen::Matrix<double, 4, 4> transform = Eigen::Matrix<double, 4, 4>::Random();
24+
curan::utilities::RegistrationData source_registration_data(date, transform, 1.0, curan::utilities::SURFACE);
25+
std::stringstream mock_file;
26+
mock_file << source_registration_data;
27+
curan::utilities::RegistrationData target_registration_data{mock_file};
28+
ASSERT_EQ(source_registration_data, target_registration_data) << "the serialization deserealization process loses data";
29+
}
30+
}
31+
32+
TEST(UnitTestFilePropagator, TrajectorySpecificationData)
33+
{
34+
for (size_t run_n_times = 0; run_n_times < 20; ++run_n_times)
35+
{
36+
Eigen::Matrix<double, 3, 1> target = Eigen::Matrix<double, 3, 1>::Random();
37+
Eigen::Matrix<double, 3, 1> entrypoint = Eigen::Matrix<double, 3, 1>::Random();
38+
Eigen::Matrix<double, 3, 3> orientation = Eigen::Matrix<double, 3, 3>::Random();
39+
const std::string path_to_fixed_image{"blub/mock/not/real/path"};
40+
curan::utilities::TrajectorySpecificationData source_trajectory_specification_data(date, target, entrypoint, orientation, path_to_fixed_image);
41+
std::stringstream mock_file;
42+
mock_file << source_trajectory_specification_data;
43+
curan::utilities::TrajectorySpecificationData target_trajectory_specification_data{mock_file};
44+
ASSERT_EQ(source_trajectory_specification_data, target_trajectory_specification_data) << "the serialization deserealization process loses data";
45+
}
46+
}
47+
48+
TEST(UnitTestFilePropagator, UltrasoundCalibrationData)
49+
{
50+
for (size_t run_n_times = 0; run_n_times < 20; ++run_n_times)
51+
{
52+
Eigen::Matrix<double, 4, 4> homogeneous_transformation = Eigen::Matrix<double, 4, 4>::Random();
53+
curan::utilities::UltrasoundCalibrationData source_ultrasound_data(date, homogeneous_transformation, 1.0);
54+
std::stringstream mock_file;
55+
mock_file << source_ultrasound_data;
56+
curan::utilities::UltrasoundCalibrationData target_ultrasound_data{mock_file};
57+
ASSERT_EQ(source_ultrasound_data, target_ultrasound_data) << "the serialization deserealization process loses data";
58+
}
59+
}

0 commit comments

Comments
 (0)