1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/************************************************************************
*
* Copyright (C) 2009-2025 IRCAD France
* Copyright (C) 2012-2019 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "field_helper_test.hpp"
#include <core/com/signal.hpp>
#include <core/com/signal.hxx>
#include <core/com/slot.hpp>
#include <core/com/slot.hxx>
#include <data/helper/field.hpp>
#include <data/string.hpp>
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::data::tools::ut::field_helper_test);
namespace sight::data::tools::ut
{
//------------------------------------------------------------------------------
void field_helper_test::setUp()
{
// Set up context before running a test.
}
//------------------------------------------------------------------------------
void field_helper_test::tearDown()
{
// Clean up after the test run.
}
//------------------------------------------------------------------------------
void field_helper_test::test_helper()
{
const std::string field_i_d1 = "FIELD_ID1";
const std::string field_i_d2 = "FIELD_ID2";
const std::string field_i_d3 = "FIELD_ID3";
data::object::sptr nullobj;
data::object::sptr obj = std::make_shared<data::string>();
data::object::sptr field_obj1 = std::make_shared<data::string>();
data::object::sptr field_obj2 = std::make_shared<data::string>();
data::object::sptr field_obj3 = std::make_shared<data::string>();
core::thread::worker::sptr worker = core::thread::worker::make();
// Setup to test notifications
unsigned int num_added_notif = 0;
std::mutex mutex;
std::condition_variable condition;
data::fields_container_t added_fields;
std::function<void(data::fields_container_t)> fn_add =
[&](data::fields_container_t _f)
{
{
std::unique_lock<std::mutex> lock(mutex);
++num_added_notif;
added_fields = _f;
}
condition.notify_one();
};
auto slot_added = core::com::new_slot(fn_add);
slot_added->set_worker(worker);
auto sig_added = obj->signal<data::object::added_fields_signal_t>(data::object::ADDED_FIELDS_SIG);
sig_added->connect(slot_added);
unsigned int num_removed_notif = 0;
data::fields_container_t removed_fields;
std::function<void(data::fields_container_t)> fn_remove =
[&](data::fields_container_t _f)
{
{
std::unique_lock<std::mutex> lock(mutex);
++num_removed_notif;
removed_fields = _f;
}
condition.notify_one();
};
auto slot_removed = core::com::new_slot(fn_remove);
slot_removed->set_worker(worker);
auto sig_removed = obj->signal<data::object::removed_fields_signal_t>(data::object::REMOVED_FIELDS_SIG);
sig_removed->connect(slot_removed);
unsigned int num_changed_notif = 0;
data::fields_container_t new_fields;
data::fields_container_t old_fields;
std::function<void(data::fields_container_t, data::fields_container_t)> fn_change =
[&](data::fields_container_t _new_f, data::fields_container_t _old_f)
{
{
std::unique_lock<std::mutex> lock(mutex);
++num_changed_notif;
new_fields = _new_f;
old_fields = _old_f;
}
condition.notify_one();
};
auto slot_changed = core::com::new_slot(fn_change);
slot_changed->set_worker(worker);
auto sig_changed = obj->signal<data::object::changed_fields_signal_t>(data::object::CHANGED_FIELDS_SIG);
sig_changed->connect(slot_changed);
auto clear_arrays = [&](){added_fields.clear(); removed_fields.clear(); new_fields.clear(); old_fields.clear();};
{
// Test set_field()
data::helper::field field_helper(obj);
field_helper.set_field(field_i_d1, field_obj1);
field_helper.set_field(field_i_d2, field_obj2);
CPPUNIT_ASSERT_EQUAL(std::size_t(2), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == field_obj1);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == field_obj2);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
}
{
// Check notification
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{return num_added_notif == 1;});
CPPUNIT_ASSERT_EQUAL(std::size_t(2), added_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(0), removed_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(0), new_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(0), old_fields.size());
CPPUNIT_ASSERT(added_fields[field_i_d1] == field_obj1);
CPPUNIT_ASSERT(added_fields[field_i_d2] == field_obj2);
clear_arrays();
}
{
// Test set_fields()
data::object::field_map_t fields_with_obj1 = {{field_i_d1, field_obj3}};
data::helper::field field_helper(obj);
field_helper.set_fields(fields_with_obj1);
CPPUNIT_ASSERT_EQUAL(std::size_t(1), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == field_obj3);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == nullobj);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
}
{
// Check notification
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{return num_removed_notif == 1 && num_changed_notif == 1;});
CPPUNIT_ASSERT_EQUAL(std::size_t(0), added_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), removed_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), new_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), old_fields.size());
CPPUNIT_ASSERT(removed_fields[field_i_d2] == field_obj2);
CPPUNIT_ASSERT(old_fields[field_i_d1] == field_obj1);
CPPUNIT_ASSERT(new_fields[field_i_d1] == field_obj3);
clear_arrays();
}
{
// Test replacement with set_field()
data::helper::field field_helper(obj);
field_helper.set_field(field_i_d1, field_obj2);
CPPUNIT_ASSERT_EQUAL(std::size_t(1), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == field_obj2);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == nullobj);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
// Notify explicitly, this should change nothing since the destructor skip it in this case
field_helper.notify();
}
{
// Check notification
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{return num_changed_notif == 2;});
CPPUNIT_ASSERT_EQUAL(std::size_t(0), added_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(0), removed_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), new_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), old_fields.size());
CPPUNIT_ASSERT(old_fields[field_i_d1] == field_obj3);
CPPUNIT_ASSERT(new_fields[field_i_d1] == field_obj2);
clear_arrays();
}
{
// Test add(), addOrSwap() and remove()
data::helper::field field_helper(obj);
field_helper.add(field_i_d2, field_obj1);
CPPUNIT_ASSERT_EQUAL(std::size_t(2), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == field_obj2);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == field_obj1);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
field_helper.add_or_swap(field_i_d2, field_obj3);
CPPUNIT_ASSERT_EQUAL(std::size_t(2), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == field_obj2);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == field_obj3);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
field_helper.remove(field_i_d1);
CPPUNIT_ASSERT_EQUAL(std::size_t(1), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == nullobj);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == field_obj3);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
field_helper.add(field_i_d3, field_obj1);
CPPUNIT_ASSERT_EQUAL(std::size_t(2), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == nullobj);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == field_obj3);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == field_obj1);
}
{
// Check notification
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{return num_added_notif == 2 && num_removed_notif == 2 && num_changed_notif == 3;});
CPPUNIT_ASSERT_EQUAL(std::size_t(2), added_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), removed_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), new_fields.size());
CPPUNIT_ASSERT_EQUAL(std::size_t(1), old_fields.size());
CPPUNIT_ASSERT(added_fields[field_i_d2] == field_obj1);
CPPUNIT_ASSERT(added_fields[field_i_d3] == field_obj1);
CPPUNIT_ASSERT(old_fields[field_i_d2] == field_obj1);
CPPUNIT_ASSERT(new_fields[field_i_d2] == field_obj3);
CPPUNIT_ASSERT(removed_fields[field_i_d1] == field_obj2);
clear_arrays();
}
{
// Test clear()
data::helper::field field_helper(obj);
field_helper.clear();
CPPUNIT_ASSERT_EQUAL(std::size_t(0), obj->get_fields().size());
CPPUNIT_ASSERT(obj->get_field(field_i_d1) == nullobj);
CPPUNIT_ASSERT(obj->get_field(field_i_d2) == nullobj);
CPPUNIT_ASSERT(obj->get_field(field_i_d3) == nullobj);
}
{
// Check notification
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{return num_removed_notif == 3;});
CPPUNIT_ASSERT_EQUAL(std::size_t(2), removed_fields.size());
CPPUNIT_ASSERT(removed_fields[field_i_d2] == field_obj3);
CPPUNIT_ASSERT(removed_fields[field_i_d3] == field_obj1);
clear_arrays();
}
worker->stop();
}
//------------------------------------------------------------------------------
} // namespace sight::data::tools::ut
|