1 /* 2 Copyright (c) 2020 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18 #include "common/test.h" 19 20 #include "common/utils.h" 21 #include "common/graph_utils.h" 22 23 #include "oneapi/tbb/flow_graph.h" 24 #include "oneapi/tbb/task_arena.h" 25 #include "oneapi/tbb/global_control.h" 26 27 #include "conformance_flowgraph.h" 28 29 //! \file conformance_write_once_node.cpp 30 //! \brief Test for [flow_graph.write_once_node] specification 31 32 /* 33 TODO: implement missing conformance tests for write_once_node: 34 - [ ] The copy constructor and copy assignment are called for the node's type template parameter. 35 - [ ] Improve copy constructor and general constructor tests. 36 - [ ] Write test checking the value is initially invalid. 37 - [ ] Write test checking that the gets from the node are non-destructive, but the first `try_get' 38 fails. 39 - [ ] Add test on `write_once_node::is_valid()' method. 40 - [ ] Add test on `write_once_node::clear()' method. 41 - [ ] Add test with reserving `join_node' as node's successor. Use example from the spec. 42 */ 43 44 template<typename T> 45 void test_inheritance(){ 46 using namespace oneapi::tbb::flow; 47 48 CHECK_MESSAGE( (std::is_base_of<graph_node, write_once_node<T>>::value), "write_once_node should be derived from graph_node"); 49 CHECK_MESSAGE( (std::is_base_of<receiver<T>, write_once_node<T>>::value), "write_once_node should be derived from receiver<T>"); 50 CHECK_MESSAGE( (std::is_base_of<sender<T>, write_once_node<T>>::value), "write_once_node should be derived from sender<T>"); 51 } 52 53 void test_copies(){ 54 using namespace oneapi::tbb::flow; 55 56 graph g; 57 write_once_node<int> fn(g); 58 write_once_node<int> fn2(fn); 59 60 } 61 void test_buffering(){ 62 oneapi::tbb::flow::graph g; 63 64 oneapi::tbb::flow::write_once_node<int> node(g); 65 oneapi::tbb::flow::limiter_node<int> rejecter(g, 0); 66 67 oneapi::tbb::flow::make_edge(node, rejecter); 68 node.try_put(1); 69 70 int tmp = -1; 71 CHECK_MESSAGE( (node.try_get(tmp) == true), "try_get after rejection should succeed"); 72 CHECK_MESSAGE( (tmp == 1), "try_get after rejection should set value"); 73 g.wait_for_all(); 74 } 75 76 void test_forwarding(){ 77 oneapi::tbb::flow::graph g; 78 79 oneapi::tbb::flow::write_once_node<int> node1(g); 80 test_push_receiver<int> node2(g); 81 test_push_receiver<int> node3(g); 82 83 oneapi::tbb::flow::make_edge(node1, node2); 84 oneapi::tbb::flow::make_edge(node1, node3); 85 86 node1.try_put(1); 87 g.wait_for_all(); 88 89 CHECK_MESSAGE( (get_count(node2) == 1), "Descendant of the node must receive one message."); 90 CHECK_MESSAGE( (get_count(node3) == 1), "Descendant of the node must receive one message."); 91 } 92 93 94 void test_writing_once(){ 95 oneapi::tbb::flow::graph g; 96 97 oneapi::tbb::flow::write_once_node<int> node1(g); 98 99 int tmp = -1; 100 node1.try_put(1); 101 102 CHECK_MESSAGE( (node1.try_get(tmp) == true), "Descendant needs to receive"); 103 CHECK_MESSAGE( (tmp == 1), "Descendant needs be receive correct message"); 104 105 CHECK_MESSAGE( (node1.try_put(2) == false), "Putting again should not succeed"); 106 107 CHECK_MESSAGE( (node1.try_get(tmp) == true), "try_get should still succeed"); 108 CHECK_MESSAGE( (tmp == 1), "try_get should receive initial value"); 109 110 g.wait_for_all(); 111 } 112 113 //! Test overwrite_node behavior 114 //! \brief \ref requirement 115 TEST_CASE("write_once_node messages"){ 116 test_writing_once(); 117 } 118 119 //! Test function_node broadcast 120 //! \brief \ref requirement 121 TEST_CASE("overwrite_node broadcast"){ 122 test_forwarding(); 123 } 124 125 //! Test write_once_node buffering 126 //! \brief \ref requirement 127 TEST_CASE("write_once_node buffering"){ 128 test_buffering(); 129 } 130 131 //! Test copy constructor 132 //! \brief \ref interface 133 TEST_CASE("write_once_node copy constructor"){ 134 test_copies(); 135 } 136 137 //! Test inheritance relations 138 //! \brief \ref interface 139 TEST_CASE("write_once_node superclasses"){ 140 test_inheritance<int>(); 141 test_inheritance<void*>(); 142 } 143 144