Classes | Namespaces | Typedefs | Functions
TupleLookupByTag_test.cc File Reference

Unit tests on TupleLookupByTag.h utilities. More...

#include "lardata/Utilities/TupleLookupByTag.h"
#include "larcorealg/CoreUtils/UncopiableAndUnmovableClass.h"
#include <tuple>
#include <type_traits>
#include <memory>
#include <utility>
#include <cstddef>
#include <cassert>

Go to the source code of this file.

Classes

struct  TestTagged< Tag >
 
struct  TestTagA
 
struct  TestTagB
 
struct  TestTagC
 
struct  TestExtractTag< Tagged >
 
class  my::MyTuple< Data >
 
class  std::tuple_element< I, my::MyTuple< T... > >
 
class  std::tuple_size< my::MyTuple< T... > >
 
struct  TaggedType< Tag, Payload >
 
struct  TaggedType< Tag, void >
 

Namespaces

 my
 

Typedefs

using TestTaggedA = TestTagged< TestTagA >
 
using TestTaggedB = TestTagged< TestTagB >
 
using TestTaggedC = TestTagged< TestTagC >
 
using TestTuple_t = std::tuple< int, char, int >
 
using TestTaggedTuple_t = std::tuple< TestTaggedA, TestTaggedB, TestTaggedA >
 
using TagA = util::TagN< 0 >
 
using TagB = util::TagN< 1 >
 
using TagC = util::TagN< 2 >
 

Functions

template<typename... Data>
auto my::make_my_tuple (Data &&...data)
 
template<std::size_t I, typename... T>
auto my::get (MyTuple< T... > const &t) -> decltype(auto)
 
void testMakeTagged ()
 
int main ()
 

Detailed Description

Unit tests on TupleLookupByTag.h utilities.

Author
Gianluca Petrillo
Date
August 17, 2018

Most of the tests are static and their failure will trigger compilation errors.

Definition in file TupleLookupByTag_test.cc.

Typedef Documentation

using TagA = util::TagN<0>

Definition at line 359 of file TupleLookupByTag_test.cc.

using TagB = util::TagN<1>

Definition at line 360 of file TupleLookupByTag_test.cc.

using TagC = util::TagN<2>

Definition at line 361 of file TupleLookupByTag_test.cc.

Definition at line 48 of file TupleLookupByTag_test.cc.

Definition at line 49 of file TupleLookupByTag_test.cc.

Definition at line 50 of file TupleLookupByTag_test.cc.

Definition at line 53 of file TupleLookupByTag_test.cc.

using TestTuple_t = std::tuple<int, char, int>

Definition at line 52 of file TupleLookupByTag_test.cc.

Function Documentation

int main ( void  )

Definition at line 391 of file TupleLookupByTag_test.cc.

391  {
392 
393  //
394  // test data
395  //
396  auto const testTuple = std::make_tuple<TestTaggedA, TestTaggedB, TestTaggedA>( 1, 2, 3 );
397  assert((std::get<0>(testTuple).value == 1));
398  assert((std::get<1>(testTuple).value == 2));
399  assert((std::get<2>(testTuple).value == 3));
400  using std::get;
401 // static_assert((index_of_extracted_type<TestExtractTag, TestTagA, TestTaggedTuple_t>(testTuple)>::value == 0), "Bug!");
403  assert(get<1U>(testTuple).value == 2);
405 // assert((util::getByExtractedType<TestExtractTag, TestTagA>(testTuple).value == 1));
406  assert((util::getByExtractedType<TestExtractTag, TestTagB>(testTuple).value == 2));
407 // assert((util::getByExtractedType<TestExtractTag, TestTagC>(testTuple).value == 3));
408 
409  using DataA = TaggedType<TagA, int>;
410  using DataB = TaggedType<TagB, int>;
411  using DataC = TaggedType<TagC, char>;
412 
413  auto data = my::make_my_tuple<DataA, DataC, DataB>(64, 'b', 66);
414  auto dataWithDupl = my::make_my_tuple<DataA, DataC, DataA>(64, 'b', 66);
415 
416  //
417  // traditional std::get()
418  //
419  using std::get;
420  static_assert(std::is_same<std::decay_t<decltype(get<0U>(data ))>, DataA>(), "Unexpected type 1");
421  static_assert(std::is_same<std::decay_t<decltype(get<1U>(data ))>, DataC>(), "Unexpected type 2");
422  static_assert(std::is_same<std::decay_t<decltype(get<2U>(data ))>, DataB>(), "Unexpected type 3");
423  static_assert(std::is_same<std::decay_t<decltype(get<0U>(dataWithDupl))>, DataA>(), "Unexpected type 1 (dupl)");
424  static_assert(std::is_same<std::decay_t<decltype(get<1U>(dataWithDupl))>, DataC>(), "Unexpected type 2 (dupl)");
425  static_assert(std::is_same<std::decay_t<decltype(get<2U>(dataWithDupl))>, DataA>(), "Unexpected type 3 (dupl)");
426 
427  //
428  // traditional std::get() (by type)
429  //
430  using std::get;
431  static_assert(std::is_same<std::decay_t<decltype(get<DataA>(data))>, DataA>(), "Unexpected type 1" );
432  static_assert(std::is_same<std::decay_t<decltype(get<DataC>(data))>, DataC>(), "Unexpected type 2" );
433  static_assert(std::is_same<std::decay_t<decltype(get<DataB>(data))>, DataB>(), "Unexpected type 3" );
434 // static_assert(std::is_same<std::decay_t<decltype(get<DataA>(dataWithDupl))>, DataA>(), "Unexpected type 1 (dupl)"); // does not compile: duplicate types!
435  static_assert(std::is_same<std::decay_t<decltype(get<DataC>(dataWithDupl))>, DataC>(), "Unexpected type 2 (dupl)");
436 // static_assert(std::is_same<std::decay_t<decltype(get<DataA>(dataWithDupl))>, DataA>(), "Unexpected type 3 (dupl)"); // does not compile: duplicate types!
437 
438  //
439  // traditional std::tuple_element()
440  //
441  using std::get;
442  static_assert(std::is_same<std::tuple_element_t<0U, decltype(data )>, DataA>(), "Unexpected type 1");
443  static_assert(std::is_same<std::tuple_element_t<1U, decltype(data )>, DataC>(), "Unexpected type 2");
444  static_assert(std::is_same<std::tuple_element_t<2U, decltype(data )>, DataB>(), "Unexpected type 3");
445  static_assert(std::is_same<std::tuple_element_t<0U, decltype(dataWithDupl)>, DataA>(), "Unexpected type 1 (dupl)");
446  static_assert(std::is_same<std::tuple_element_t<1U, decltype(dataWithDupl)>, DataC>(), "Unexpected type 2 (dupl)");
447  static_assert(std::is_same<std::tuple_element_t<2U, decltype(dataWithDupl)>, DataA>(), "Unexpected type 3 (dupl)");
448 
449  //
450  // traditional std::tuple_size
451  //
452  static_assert(std::tuple_size<decltype(data )>() == 3U, "Unexpected tuple size");
453  static_assert(std::tuple_size<decltype(dataWithDupl)>() == 3U, "Unexpected tuple size (dupl)");
454 
455 
456  //
457  // util::index_of_type()
458  //
459  static_assert(util::index_of_type<DataA, decltype(data) >() == 0U, "Unexpected type 1");
460  static_assert(util::index_of_type<DataC, decltype(data) >() == 1U, "Unexpected type 2");
461  static_assert(util::index_of_type<DataB, decltype(data) >() == 2U, "Unexpected type 3");
462 // static_assert(util::index_of_type<DataA, decltype(dataWithDupl)>() == 0U, "Unexpected type 1 (dupl)");
463  static_assert(util::index_of_type<DataC, decltype(dataWithDupl)>() == 1U, "Unexpected type 2 (dupl)");
464 // static_assert(util::index_of_type<DataA, decltype(dataWithDupl)>() == 2U, "Unexpected type 3 (dupl)");
465 
466  //
467  // util::index_of_tag()
468  //
469  static_assert(util::index_of_tag<TagA, decltype(data) >() == 0U, "Unexpected tagged type 1");
470  static_assert(util::index_of_tag<TagC, decltype(data) >() == 1U, "Unexpected tagged type 2");
471  static_assert(util::index_of_tag<TagB, decltype(data) >() == 2U, "Unexpected tagged type 3");
472 // static_assert(util::index_of_tag<TagA, decltype(dataWithDupl)>() == 0U, "Unexpected tagged type 1 (dupl)");
473  static_assert(util::index_of_tag<TagC, decltype(dataWithDupl)>() == 1U, "Unexpected tagged type 2 (dupl)");
474 // static_assert(util::index_of_tag<TagA, decltype(dataWithDupl)>() == 2U, "Unexpected tagged type 3 (dupl)");
475 
476  //
477  // util::type_with_tag()
478  //
479  static_assert(std::is_same<util::type_with_tag_t<TagA, decltype(data) >, DataA>(), "Unexpected tagged type 1");
480  static_assert(std::is_same<util::type_with_tag_t<TagC, decltype(data) >, DataC>(), "Unexpected tagged type 2");
481  static_assert(std::is_same<util::type_with_tag_t<TagB, decltype(data) >, DataB>(), "Unexpected tagged type 3");
482 // static_assert(std::is_same<util::type_with_tag_t<TagA, decltype(dataWithDupl)>, DataA>(), "Unexpected tagged type 1 (dupl)");
483  static_assert(std::is_same<util::type_with_tag_t<TagC, decltype(dataWithDupl)>, DataC>(), "Unexpected tagged type 2 (dupl)");
484 // static_assert(std::is_same<util::type_with_tag_t<TagA, decltype(dataWithDupl)>, DataA>(), "Unexpected tagged type 3 (dupl)");
485 
486  //
487  // util::has_type()
488  //
489  static_assert( util::has_type<DataA, decltype(data) >(), "Unexpected type 1");
490  static_assert( util::has_type<DataC, decltype(data) >(), "Unexpected type 2");
491  static_assert( util::has_type<DataB, decltype(data) >(), "Unexpected type 3");
492  static_assert( util::has_type<DataA, decltype(dataWithDupl)>(), "Unexpected type 1 (dupl)");
493  static_assert( util::has_type<DataC, decltype(dataWithDupl)>(), "Unexpected type 2 (dupl)");
494  static_assert(!util::has_type<DataB, decltype(dataWithDupl)>(), "Unexpected type 3 (dupl)");
495 
496  //
497  // util::has_tag()
498  //
499  static_assert( util::has_tag<TagA, decltype(data) >(), "Unexpected tagged type 1");
500  static_assert( util::has_tag<TagC, decltype(data) >(), "Unexpected tagged type 2");
501  static_assert( util::has_tag<TagB, decltype(data) >(), "Unexpected tagged type 3");
502  static_assert( util::has_tag<TagA, decltype(dataWithDupl)>(), "Unexpected tagged type 1 (dupl)");
503  static_assert( util::has_tag<TagC, decltype(dataWithDupl)>(), "Unexpected tagged type 2 (dupl)");
504  static_assert(!util::has_tag<TagB, decltype(dataWithDupl)>(), "Unexpected tagged type 3 (dupl)");
505 
506  //
507  // util::count_types()
508  //
509  static_assert(util::count_types<DataA, decltype(data) >() == 1, "Unexpected type 1");
510  static_assert(util::count_types<DataC, decltype(data) >() == 1, "Unexpected type 2");
511  static_assert(util::count_types<DataB, decltype(data) >() == 1, "Unexpected type 3");
512  static_assert(util::count_types<DataA, decltype(dataWithDupl)>() == 2, "Unexpected type 1 (dupl)");
513  static_assert(util::count_types<DataC, decltype(dataWithDupl)>() == 1, "Unexpected type 2 (dupl)");
514  static_assert(util::count_types<DataB, decltype(dataWithDupl)>() == 0, "Unexpected type 3 (dupl)");
515 
516  //
517  // util::count_tags()
518  //
519  static_assert(util::count_tags<TagA, decltype(data) >() == 1, "Unexpected type 1");
520  static_assert(util::count_tags<TagC, decltype(data) >() == 1, "Unexpected type 2");
521  static_assert(util::count_tags<TagB, decltype(data) >() == 1, "Unexpected type 3");
522  static_assert(util::count_tags<TagA, decltype(dataWithDupl)>() == 2, "Unexpected type 1 (dupl)");
523  static_assert(util::count_tags<TagC, decltype(dataWithDupl)>() == 1, "Unexpected type 2 (dupl)");
524  static_assert(util::count_tags<TagB, decltype(dataWithDupl)>() == 0, "Unexpected type 3 (dupl)");
525 
526  //
527  // util::has_duplicate_types()
528  //
529  static_assert(!util::has_duplicate_types<decltype(data) >(), "Type has duplicate tags!");
530  static_assert( util::has_duplicate_types<decltype(dataWithDupl)>(), "Type has no duplicate tags");
531 
532  //
533  // util::has_duplicate_tags()
534  //
535  static_assert(!util::has_duplicate_tags<decltype(data) >(), "Type has duplicate tags!");
536  static_assert( util::has_duplicate_tags<decltype(dataWithDupl)>(), "Type has no duplicate tags");
537 
538  //
539  // util::getByTag()
540  //
541  static_assert(std::is_same<std::decay_t<decltype(util::getByTag<TagA>(data) .data)>, int >(), "Unexpected type 1" );
542  static_assert(std::is_same<std::decay_t<decltype(util::getByTag<TagC>(data) .data)>, char>(), "Unexpected type 2" );
543  static_assert(std::is_same<std::decay_t<decltype(util::getByTag<TagB>(data) .data)>, int >(), "Unexpected type 3" );
544 // static_assert(std::is_same<std::decay_t<decltype(util::getByTag<TagA>(dataWithDupl).data)>, int >(), "Unexpected type 1 (dupl)"); // does not compile: duplicate types!
545  static_assert(std::is_same<std::decay_t<decltype(util::getByTag<TagC>(dataWithDupl).data)>, char>(), "Unexpected type 2 (dupl)");
546 // static_assert(std::is_same<std::decay_t<decltype(util::getByTag<TagA>(dataWithDupl).data)>, int >(), "Unexpected type 3 (dupl)"); // does not compile: duplicate types!
547 
548  assert((util::getByTag<TagA>(data) ).data == 64);
549  assert((util::getByTag<TagC>(data) ).data == 'b');
550  assert((util::getByTag<TagB>(data) ).data == 66);
551 // assert((util::getByTag<TagA>(dataWithDupl)).data == 64); // does not compile: duplicate types!
552  assert((util::getByTag<TagC>(dataWithDupl)).data == 'b');
553 // assert((util::getByTag<TagA>(dataWithDupl)).data == 66); // does not compile: duplicate types!
554 
555  //
556  // makeTagged()
557  //
558  testMakeTagged();
559 
560  return 0;
561 } // main()
Counts the elements of a tuple-like type containing a Target type.
Tag class parametrized by a sequence of numbers.
auto get(MyTuple< T... > const &t) -> decltype(auto)
typename tuple_element< I, Node >::type tuple_element_t
Definition: AssnsNode.h:56
void testMakeTagged()
Trait holding whether an element in Tuple type contains Target.
Traits holding whether elements of Tuple have duplicate types.
Returns the index of the element in Tuple with the specified type.
typename type_with_tag< Tag, Tuple >::type type_with_tag_t
Direct access to the value in type_with_tag.
void testMakeTagged ( )

Definition at line 364 of file TupleLookupByTag_test.cc.

364  {
365 
366  struct MyData {
367  int content = 5;
368  };
369 
370  struct MyStonedData: public MyData, public lar::UncopiableAndUnmovableClass {};
371 
372  MyData lightData; // moveable, copiable
373  MyStonedData heavyStone; // unmoveable, uncopiable
374 
375  decltype(auto) lightDataTagged = util::makeTagged<TagA>(lightData );
376  decltype(auto) heavyStoneTagged = util::makeTagged<TagA>(heavyStone );
377  decltype(auto) lightDataCopyTagged = util::makeTagged<TagA>(MyData(lightData));
378 
379  static_assert( std::is_lvalue_reference<decltype(util::makeTagged<TagA>(lightData) )>(), "makeTagged(moveable lvalue) does not produce a reference");
380  static_assert( std::is_lvalue_reference<decltype(util::makeTagged<TagA>(heavyStone) )>(), "makeTagged(unmoveable lvalue) does not produce a reference");
381  static_assert(!std::is_lvalue_reference<decltype(util::makeTagged<TagA>(MyData(lightData)))>(), "makeTagged(rvalue) produces a (lvalue) reference" );
382  static_assert(!std::is_rvalue_reference<decltype(util::makeTagged<TagA>(MyData(lightData)))>(), "makeTagged(rvalue) produces a (rvalue) reference" );
383 
384  assert(std::addressof(lightDataTagged ) == std::addressof(lightData ));
385  assert(std::addressof(heavyStoneTagged ) == std::addressof(heavyStone));
386  assert(std::addressof(lightDataCopyTagged) != std::addressof(lightData ));
387 
388 } // testMakeTagged()
Namespace for general, non-LArSoft-specific utilities.
An empty class that can&#39;t be copied nor moved.
STL namespace.
Tag class parametrized by a sequence of numbers.
const double a
auto makeTagged(T &obj) -> decltype(auto)
"Converts" obj to an object with tag Tag.