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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2014 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#include "UnitTest++/CheckMacros.h"
#include "unit_test_framework.h"
#include <sstream>
#include <string>
#include "etl/signal.h"
namespace
{
using callback_type = void(std::ostream&);
using signal_type = etl::signal<callback_type, 5U>;
using slot_type = signal_type::slot_type;
using span_type = signal_type::span_type;
//*************************************************************************
///\brief Generic output free function
///
///\param out: output stream
///\param str: input string
//*************************************************************************
void output(std::ostream& out, const std::string& str)
{
out << str;
}
//*************************************************************************
///\brief Free function that outputs "free"
///
///\param out: output
//*************************************************************************
void output_free(std::ostream& out)
{
output(out, "free");
}
//*************************************************************************
///\brief Free function that outputs "free"
///
///\param out: output
//*************************************************************************
void output_extra(std::ostream& out)
{
output(out, "extra");
}
//*************************************************************************
///\brief Lambda that outputs "lambda"
///
///\param out: output
//*************************************************************************
auto output_lambda = [](std::ostream& out) { output(out, "lambda"); };
class example_class
{
public:
//*************************************************************************
///\brief Method that outputs "static"
///
///\param out: output
//*************************************************************************
static void static_method(std::ostream& out)
{
output(out, "static");
}
//*************************************************************************
///\brief Method that outputs "method"
///
///\param out: output
//*************************************************************************
void method(std::ostream& out)
{
output(out, "method");
}
//*************************************************************************
///\brief Functor method that outputs "functor"
///
///\param out: output
//*************************************************************************
void operator()(std::ostream& out)
{
output(out, "functor");
}
};
example_class example;
//*************************************************************************
///\brief Makes an empty slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_empty_slot()
{
return slot_type();
}
//*************************************************************************
///\brief Makes the free function slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_free_slot()
{
return slot_type::create<output_free>();
}
//*************************************************************************
///\brief Makes the lambda slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_lambda_slot()
{
return slot_type{output_lambda};
}
//*************************************************************************
///\brief Makes the static method slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_static_slot()
{
return slot_type::create<&example_class::static_method>();
}
//*************************************************************************
///\brief Makes the class method slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_instance_slot()
{
return slot_type::create<example_class, example, &example_class::method>();
}
//*************************************************************************
///\brief Makes the functor slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_functor_slot()
{
return slot_type::create<example_class, example>();
}
//*************************************************************************
///\brief Makes the free function slot
///
///\return constexpr slot_type
//*************************************************************************
ETL_CONSTEXPR14 slot_type make_extra_slot()
{
return slot_type::create<output_extra>();
}
//*************************************************************************
///\brief Makes the incorrect slot type
/// Uncomment for test_construct_with_incorrect_slot_type
///\return An incorrect slot
//*************************************************************************
//ETL_CONSTEXPR14 etl::delegate<int(std::ostream&)> make_incorrect_slot()
//{
// return etl::delegate<int(std::ostream&)>();
//}
//***************************************************************************
SUITE(signal_test)
{
//***************************************************************************
TEST(test_default_construct)
{
const signal_type test_object;
CHECK_EQUAL(0U, test_object.size());
CHECK_EQUAL(5U, test_object.max_size());
CHECK_TRUE(test_object.empty());
CHECK_FALSE(test_object.full());
}
#if ETL_USING_CPP14
//***************************************************************************
TEST(test_constexpr_construct)
{
static constexpr signal_type signal
{
make_free_slot(),
make_lambda_slot(),
make_static_slot(),
make_instance_slot(),
make_functor_slot()
};
CHECK_EQUAL(5U, signal.size());
CHECK_EQUAL(0, signal.available());
CHECK_EQUAL(5U, signal.max_size());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.full());
}
#endif
//***************************************************************************
TEST(test_construct_from_slot_list)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
const signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
CHECK_EQUAL(5, signal.size());
CHECK_EQUAL(0, signal.available());
CHECK_EQUAL(5, signal.max_size());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.full());
}
//***************************************************************************
TEST(test_construct_from_too_many_slots)
{
// Uncomment to trigger static_assert "Number of slots exceeds capacity"
//const auto free_slot = make_free_slot();
//const auto lambda_slot = make_lambda_slot();
//const auto static_slot = make_static_slot();
//const auto instance_slot = make_instance_slot();
//const auto functor_slot = make_functor_slot();
//const auto extra_slot = make_extra_slot();
//signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot, extra_slot };
}
#if ETL_USING_CPP14
//***************************************************************************
TEST(test_constexpr_construct_from_slot_list)
{
static constexpr auto free_slot = make_free_slot();
static constexpr auto lambda_slot = make_lambda_slot();
static constexpr auto static_slot = make_static_slot();
static constexpr auto instance_slot = make_instance_slot();
static constexpr auto functor_slot = make_functor_slot();
static constexpr signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
CHECK_EQUAL(5, signal.size());
CHECK_EQUAL(0, signal.available());
CHECK_EQUAL(5, signal.max_size());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.full());
}
#endif
//***************************************************************************
TEST(test_construct_with_incorrect_slot_type)
{
// Uncomment to trigger static_assert "All slots must be slot_type"
//const auto free_slot = make_free_slot();
//const auto lambda_slot = make_lambda_slot();
//const auto incorrect_slot = make_incorrect_slot(); // Incorrect type
//const auto instance_slot = make_instance_slot();
//const auto functor_slot = make_functor_slot();
//signal_type signal{ free_slot, lambda_slot, incorrect_slot, instance_slot, functor_slot };
}
//***************************************************************************
TEST(test_connect)
{
signal_type signal;
const auto free_slot = make_free_slot();
CHECK_TRUE(signal.connect(free_slot));
CHECK_EQUAL(1U, signal.size());
CHECK_EQUAL(4U, signal.available());
signal.connect(free_slot); // Try to connect it again - nothing should change.
CHECK_EQUAL(1U, signal.size());
CHECK_EQUAL(4U, signal.available());
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
CHECK_TRUE(signal.connected(free_slot));
const auto lambda_slot = make_lambda_slot();
CHECK_TRUE(signal.connect(lambda_slot));
CHECK_EQUAL(2U, signal.size());
CHECK_EQUAL(3U, signal.available());
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
CHECK_TRUE(signal.connected(lambda_slot));
const auto static_slot = make_static_slot();
CHECK_TRUE(signal.connect(static_slot));
CHECK_EQUAL(3U, signal.size());
CHECK_EQUAL(2U, signal.available());
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
CHECK_TRUE(signal.connected(static_slot));
const auto instance_slot = make_instance_slot();
CHECK_TRUE(signal.connect(instance_slot));
CHECK_EQUAL(4U, signal.size());
CHECK_EQUAL(1U, signal.available());
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
CHECK_TRUE(signal.connected(instance_slot));
const auto functor_slot = make_functor_slot();
CHECK_TRUE(signal.connect(functor_slot));
CHECK_EQUAL(5U, signal.size());
CHECK_EQUAL(0U, signal.available());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.full());
CHECK_TRUE(signal.connected(functor_slot));
}
#if ETL_USING_CPP17
//***************************************************************************
TEST(test_connect_from_initializer_list)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
signal_type signal;
CHECK_TRUE(signal.connect({ free_slot, lambda_slot, static_slot, instance_slot, functor_slot }));
CHECK_EQUAL(5U, signal.size());
CHECK_TRUE(signal.connected(free_slot));
CHECK_TRUE(signal.connected(lambda_slot));
CHECK_TRUE(signal.connected(static_slot));
CHECK_TRUE(signal.connected(instance_slot));
CHECK_TRUE(signal.connected(functor_slot));
CHECK_EQUAL(0U, signal.available());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.full());
}
#endif
#if ETL_USING_CPP17
//***************************************************************************
TEST(test_connect_from_initializer_list_too_many_slots)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
const auto extra_slot = make_extra_slot();
signal_type signal;
CHECK_THROW(signal.connect({ free_slot, lambda_slot, static_slot, instance_slot, functor_slot, extra_slot }), etl::signal_full);
}
#endif
//***************************************************************************
TEST(test_connect_from_span)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
const slot_type slot_list[] = { free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
signal_type signal;
CHECK_TRUE(signal.connect(slot_list));
}
//***************************************************************************
TEST(test_connect_from_span_too_many_slots)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
const auto extra_slot = make_extra_slot();
const slot_type slot_list[] = { free_slot, lambda_slot, static_slot, instance_slot, functor_slot, extra_slot };
signal_type signal;
CHECK_THROW(signal.connect(slot_list), etl::signal_full);
}
//***************************************************************************
TEST(test_disconnect)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
const slot_type slot_list[] = { free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
signal_type signal;
CHECK_TRUE(signal.connect(slot_list));
signal.disconnect(free_slot);
CHECK_EQUAL(4U, signal.size());
CHECK_EQUAL(1U, signal.available());
CHECK_FALSE(signal.connected(free_slot));
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
// Try to remove it again - nothing should change.
signal.disconnect(free_slot);
CHECK_EQUAL(4U, signal.size());
CHECK_EQUAL(1U, signal.available());
CHECK_FALSE(signal.connected(free_slot));
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
signal.disconnect(lambda_slot);
CHECK_EQUAL(3U, signal.size());
CHECK_EQUAL(2U, signal.available());
CHECK_FALSE(signal.connected(lambda_slot));
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
signal.disconnect(static_slot);
CHECK_EQUAL(2U, signal.size());
CHECK_EQUAL(3U, signal.available());
CHECK_FALSE(signal.connected(static_slot));
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
signal.disconnect(instance_slot);
CHECK_EQUAL(1U, signal.size());
CHECK_EQUAL(4U, signal.available());
CHECK_FALSE(signal.connected(instance_slot));
CHECK_FALSE(signal.empty());
CHECK_FALSE(signal.full());
signal.disconnect(functor_slot);
CHECK_EQUAL(0U, signal.size());
CHECK_EQUAL(5U, signal.available());
CHECK_FALSE(signal.connected(functor_slot));
CHECK_TRUE(signal.empty());
CHECK_FALSE(signal.full());
}
}
#if ETL_USING_CPP17
//***************************************************************************
TEST(test_disconnect_from_initializer_list)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
signal_type signal;
CHECK_TRUE(signal.connect({ free_slot, lambda_slot, static_slot, instance_slot, functor_slot }));
signal.disconnect({ lambda_slot, instance_slot });
CHECK_EQUAL(3U, signal.size());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.connected(free_slot));
CHECK_FALSE(signal.connected(lambda_slot));
CHECK_TRUE(signal.connected(static_slot));
CHECK_FALSE(signal.connected(instance_slot));
CHECK_TRUE(signal.connected(functor_slot));
}
#endif
//***************************************************************************
TEST(test_disconnect_from_span_list)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
signal_type signal;
CHECK_TRUE(signal.connect(free_slot));
CHECK_TRUE(signal.connect(lambda_slot));
CHECK_TRUE(signal.connect(static_slot));
CHECK_TRUE(signal.connect(instance_slot));
CHECK_TRUE(signal.connect(functor_slot));
const slot_type slot_list[] = { lambda_slot, instance_slot };
signal.disconnect(span_type(slot_list));
CHECK_EQUAL(3U, signal.size());
CHECK_FALSE(signal.empty());
CHECK_TRUE(signal.connected(free_slot));
CHECK_FALSE(signal.connected(lambda_slot));
CHECK_TRUE(signal.connected(static_slot));
CHECK_FALSE(signal.connected(instance_slot));
CHECK_TRUE(signal.connected(functor_slot));
}
//***************************************************************************
TEST(test_disconnect_all)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
const slot_type slot_list[] = { free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
signal_type signal;
CHECK_TRUE(signal.connect(slot_list));
signal.disconnect_all();
CHECK_EQUAL(0U, signal.size());
CHECK_TRUE(signal.empty());
CHECK_FALSE(signal.connected(free_slot));
CHECK_FALSE(signal.connected(lambda_slot));
CHECK_FALSE(signal.connected(static_slot));
CHECK_FALSE(signal.connected(instance_slot));
CHECK_FALSE(signal.connected(functor_slot));
}
//***************************************************************************
TEST(test_call_empty)
{
std::stringstream ss;
signal_type signal;
signal(ss);
CHECK_EQUAL(std::string{""}, ss.str());
}
//***************************************************************************
TEST(test_call)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_lambda_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_instance_slot();
const auto functor_slot = make_functor_slot();
signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
std::stringstream ss;
signal(ss);
// Expect all signals got called
const std::string expected_string{"freelambdastaticmethodfunctor"};
CHECK_EQUAL(expected_string, ss.str());
}
#if ETL_USING_CPP14
//***************************************************************************
TEST(test_constexpr_call)
{
static constexpr auto free_slot = make_free_slot();
static constexpr auto lambda_slot = make_lambda_slot();
static constexpr auto static_slot = make_static_slot();
static constexpr auto instance_slot = make_instance_slot();
static constexpr auto functor_slot = make_functor_slot();
static constexpr signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
std::stringstream ss;
signal(ss);
// Expect all slots got called
const std::string expected_string{"freelambdastaticmethodfunctor"};
CHECK_EQUAL(expected_string, ss.str());
}
#endif
//***************************************************************************
TEST(test_call_empty_slots)
{
const auto free_slot = make_free_slot();
const auto lambda_slot = make_empty_slot();
const auto static_slot = make_static_slot();
const auto instance_slot = make_empty_slot();
const auto functor_slot = make_functor_slot();
signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
std::stringstream ss;
signal(ss);
// Expect only valid slots got called
const std::string expected_string{"freestaticfunctor"};
CHECK_EQUAL(expected_string, ss.str());
}
#if ETL_USING_CPP14
//***************************************************************************
TEST(test_call_constexpr_empty_slots)
{
static constexpr auto free_slot = make_free_slot();
static constexpr auto lambda_slot = make_empty_slot();
static constexpr auto static_slot = make_static_slot();
static constexpr auto instance_slot = make_empty_slot();
static constexpr auto functor_slot = make_functor_slot();
static constexpr signal_type signal{ free_slot, lambda_slot, static_slot, instance_slot, functor_slot };
std::stringstream ss;
signal(ss);
// Expect only valid slots got called
const std::string expected_string{"freestaticfunctor"};
CHECK_EQUAL(expected_string, ss.str());
}
#endif
}
|