Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sourcesandsinks.cpp
1#include <catch2/catch_test_macros.hpp>
2
3#include <Core/Utils/TypesUtils.hpp>
4#include <Dataflow/Core/DataflowGraph.hpp>
5#include <Dataflow/Core/Sinks/Types.hpp>
6#include <Dataflow/Core/Sources/Types.hpp>
7
8#include <iostream>
9#include <string>
10
11using namespace Ra::Dataflow::Core;
12using namespace Ra::Core::Utils;
13
15template <typename T>
16void testGraph( const std::string& name, T in, T& out ) {
17 auto g = std::make_unique<DataflowGraph>( name );
19 auto sink = std::make_shared<Sinks::SinkNode<T>>( "out" );
20 g->add_node( source );
21 g->add_node( sink );
22 auto linked = g->add_link( source, "to", sink, "from" );
23 if ( !linked ) { std::cerr << "Error linking source and sink nodes.\n"; }
24 REQUIRE( linked );
25
26 auto input = g->input_node_port( "in", "from" );
27 REQUIRE( input != nullptr );
28 auto output = g->output_node_port( "out", "data" );
29 REQUIRE( output != nullptr );
30
31 auto compiled = g->compile();
32 if ( !compiled ) { std::cerr << "Error compiling graph.\n"; }
33 REQUIRE( compiled );
34
35 std::cout << "Setting " << simplifiedDemangledType<T>() << " data on interface port ... ";
36 input->set_default_value( in );
37
38 g->execute();
39
40 T r = output->data<T>();
41 std::cout << "Getting a " << simplifiedDemangledType( r ) << " from interface port ... ";
42 out = r;
43
44 source->set_data( in );
45
46 nlohmann::json graphData;
47 g->toJson( graphData );
48 g->destroy();
49
50 g = std::make_unique<DataflowGraph>( name );
51 g->fromJson( graphData );
52 auto ok = g->execute();
53 REQUIRE( ok );
54}
56TEST_CASE( "Dataflow/Core/Sources and Sinks", "[unittests][Dataflow][Core][Sources and Sinks]" ) {
57 SECTION( "Operations on base type : Scalar" ) {
58 using DataType = Scalar;
59 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
60
61 DataType x { 3.141592_ra };
62 DataType y { 0_ra };
63 testGraph<DataType>( "Test on Scalar", x, y );
64
65 REQUIRE( x == y );
66 std::cout << " ... DONE!\n";
67 }
68 SECTION( "Operations on base type : float" ) {
69 using DataType = float;
70 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
71 DataType x { 3.141592 };
72 DataType y { 0 };
73 testGraph<DataType>( "Test on float", x, y );
74
75 REQUIRE( x == y );
76 std::cout << " ... DONE!\n";
77 }
78 SECTION( "Operations on base type : double" ) {
79 using DataType = double;
80 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
81 DataType x { 3.141592 };
82 DataType y { 0 };
83 testGraph<DataType>( "Test on float", x, y );
84
85 REQUIRE( x == y );
86 std::cout << " ... DONE!\n";
87 }
88 SECTION( "Operations on base type : int" ) {
89 using DataType = int;
90 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
91 DataType x { -3 };
92 DataType y { 0 };
93 testGraph<DataType>( "Test on int", x, y );
94
95 REQUIRE( x == y );
96 std::cout << " ... DONE!\n";
97 }
98 SECTION( "Operations on base type : unsigned int" ) {
99 using DataType = unsigned int;
100 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
101 DataType x { 3 };
102 DataType y { 0 };
103 testGraph<DataType>( "Test on unsigned int", x, y );
104
105 REQUIRE( x == y );
106 std::cout << " ... DONE!\n";
107 }
108 SECTION( "Operations on base type : bool" ) {
109 using DataType = bool;
110 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
111 DataType x { true };
112 DataType y { false };
113 testGraph<DataType>( "Test on bool", x, y );
114
115 REQUIRE( x == y );
116 std::cout << " ... DONE!\n";
117 }
118 SECTION( "Operations on base type : Vector2" ) {
119 using DataType = Ra::Core::Vector2;
120 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
121 DataType x { 1_ra, 2_ra };
122 DataType y;
123 testGraph<DataType>( "Test on Vector2", x, y );
124
125 REQUIRE( x == y );
126 std::cout << " ... DONE!\n";
127 }
128 SECTION( "Operations on base type : Vector3" ) {
129 using DataType = Ra::Core::Vector3;
130 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
131 DataType x { 1_ra, 2_ra, 3_ra };
132 DataType y;
133 testGraph<DataType>( "Test on Vector3", x, y );
134
135 REQUIRE( x == y );
136 std::cout << " ... DONE!\n";
137 }
138 SECTION( "Operations on base type : Vector4" ) {
139 using DataType = Ra::Core::Vector4;
140 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
141 DataType x { 1_ra, 2_ra, 3_ra, 4_ra };
142 DataType y;
143 testGraph<DataType>( "Test on Vector4", x, y );
144
145 REQUIRE( x == y );
146 std::cout << " ... DONE!\n";
147 }
148 SECTION( "Operations on base type : Color" ) {
149 using DataType = Ra::Core::Utils::Color;
150 std::cout << "Test on " << simplifiedDemangledType<DataType>() << " ... ";
151 DataType x = Ra::Core::Utils::Color::Skin();
152 DataType y;
153 testGraph<DataType>( "Test on Color", x, y );
154
155 REQUIRE( x == y );
156 std::cout << " ... DONE!\n";
157 }
158}
T make_shared(T... args)