|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2021 Huawei Technologies Co., Ltd. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | +#include <sstream> |
| 20 | +#include <unordered_map> |
| 21 | +#include <vector> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include <graphblas.hpp> |
| 25 | +#include <graphblas/ops.hpp> |
| 26 | + |
| 27 | +using namespace grb; |
| 28 | +using namespace grb::operators; |
| 29 | + |
| 30 | +constexpr std::array< std::pair< bool, bool >, 4 > test_values = { |
| 31 | + std::make_pair( false, false ), |
| 32 | + std::make_pair( false, true ), |
| 33 | + std::make_pair( true, false ), |
| 34 | + std::make_pair( true, true ) |
| 35 | +}; |
| 36 | + |
| 37 | +template< |
| 38 | + Descriptor descr = descriptors::no_operation, |
| 39 | + typename OP |
| 40 | +> |
| 41 | +void test_apply( |
| 42 | + RC &rc, |
| 43 | + std::array< bool, 4 > &values, |
| 44 | + const std::array< bool, 4 > &expected |
| 45 | +) { |
| 46 | + rc = apply< descr, OP >( values[0], false, false ); |
| 47 | + rc = rc ? rc : apply< descr, OP >( values[1], false, true ); |
| 48 | + rc = rc ? rc : apply< descr, OP >( values[2], true, false ); |
| 49 | + rc = rc ? rc : apply< descr, OP >( values[3], true, true ); |
| 50 | + if( ! std::equal( values.cbegin(), values.cend(), expected.cbegin() ) ) { |
| 51 | + rc = FAILED; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +template< |
| 56 | + Descriptor descr = descriptors::no_operation, |
| 57 | + typename OP |
| 58 | +> |
| 59 | +void test_foldl( |
| 60 | + RC &rc, |
| 61 | + std::array< bool, 4 > &values, |
| 62 | + const std::array< bool, 4 > &expected |
| 63 | +) { |
| 64 | + values[0] = false; |
| 65 | + rc = foldl< descr, OP >( values[0], false ); |
| 66 | + values[1] = false; |
| 67 | + rc = rc ? rc : foldl< descr, OP >( values[1], true ); |
| 68 | + values[2] = true; |
| 69 | + rc = rc ? rc : foldl< descr, OP >( values[2], false ); |
| 70 | + values[3] = true; |
| 71 | + rc = rc ? rc : foldl< descr, OP >( values[3], true ); |
| 72 | + if( ! std::equal( values.cbegin(), values.cend(), expected.cbegin() ) ) { |
| 73 | + rc = FAILED; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +template< |
| 78 | + Descriptor descr = descriptors::no_operation, |
| 79 | + typename OP |
| 80 | +> |
| 81 | +void test_foldr( |
| 82 | + RC &rc, |
| 83 | + std::array< bool, 4 > &values, |
| 84 | + const std::array< bool, 4 > &expected |
| 85 | +) { |
| 86 | + values[0] = false; |
| 87 | + rc = foldr< descr, OP >( false, values[0] ); |
| 88 | + values[1] = false; |
| 89 | + rc = rc ? rc : foldr< descr, OP >( true, values[1] ); |
| 90 | + values[2] = true; |
| 91 | + rc = rc ? rc : foldr< descr, OP >( false, values[2] ); |
| 92 | + values[3] = true; |
| 93 | + rc = rc ? rc : foldr< descr, OP >( true, values[3] ); |
| 94 | + if( ! std::equal( values.cbegin(), values.cend(), expected.cbegin() ) ) { |
| 95 | + rc = FAILED; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +template< |
| 100 | + Descriptor descr = descriptors::no_operation, |
| 101 | + typename OP |
| 102 | +> |
| 103 | +void test_operator( |
| 104 | + RC &rc, |
| 105 | + const std::array< bool, 4 > &expected, |
| 106 | + const bool expected_associative, |
| 107 | + const typename std::enable_if< |
| 108 | + grb::is_operator< OP >::value, |
| 109 | + void |
| 110 | + >::type* = nullptr |
| 111 | +) { |
| 112 | + |
| 113 | + if( grb::is_associative< OP >::value != expected_associative ) { |
| 114 | + std::cerr << "Operator associtativity property is " |
| 115 | + << grb::is_associative< OP >::value << ", should be " |
| 116 | + << expected_associative << "\n"; |
| 117 | + rc = FAILED; |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + std::array< bool, 4 > values; |
| 122 | + |
| 123 | + test_apply< descr, OP >( rc, values, expected ); |
| 124 | + if( rc != SUCCESS ) { |
| 125 | + std::cerr << "Test_apply FAILED\n"; |
| 126 | + std::cerr << "values ?= expected\n"; |
| 127 | + for( size_t i = 0; i < 4; i++ ) { |
| 128 | + std::cerr << "OP( " << test_values[i].first << ";" |
| 129 | + << test_values[i].second << " ): "<< values[i] << " ?= " |
| 130 | + << expected[i] << "\n"; |
| 131 | + } |
| 132 | + return; |
| 133 | + } |
| 134 | + test_foldl< descr, OP >( rc, values, expected ); |
| 135 | + if( rc != SUCCESS ) { |
| 136 | + std::cerr << "Test_foldl FAILED\n"; |
| 137 | + std::cerr << "values ?= expected\n"; |
| 138 | + for( size_t i = 0; i < 4; i++ ) { |
| 139 | + std::cerr << "OP( " << test_values[i].first << ";" |
| 140 | + << test_values[i].second << " ): "<< values[i] << " ?= " |
| 141 | + << expected[i] << "\n"; |
| 142 | + } |
| 143 | + return; |
| 144 | + } |
| 145 | + test_foldr< descr, OP >( rc, values, expected ); |
| 146 | + if( rc != SUCCESS ) { |
| 147 | + std::cerr << "Test_foldr FAILED\n"; |
| 148 | + std::cerr << "values ?= expected\n"; |
| 149 | + for( size_t i = 0; i < 4; i++ ) { |
| 150 | + std::cerr << "OP( " << test_values[i].first << ";" |
| 151 | + << test_values[i].second << " ): "<< values[i] << " ?= " |
| 152 | + << expected[i] << "\n"; |
| 153 | + } |
| 154 | + return; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +void grb_program( const size_t&, grb::RC &rc ) { |
| 159 | + rc = SUCCESS; |
| 160 | + |
| 161 | + // Logical operators |
| 162 | + { // logical_and< bool > |
| 163 | + std::cout << "Testing operator: logical_and<bool>" << std::endl; |
| 164 | + const std::array<bool, 4> expected = { false, false, false, true }; |
| 165 | + bool expected_associative = true; |
| 166 | + test_operator< |
| 167 | + descriptors::no_operation, |
| 168 | + logical_and< bool > |
| 169 | + >( rc, expected, expected_associative ); |
| 170 | + } |
| 171 | + { // logical_or< bool > |
| 172 | + std::cout << "Testing operator: logical_or<bool>" << std::endl; |
| 173 | + const std::array<bool, 4> expected = { false, true, true, true }; |
| 174 | + bool expected_associative = true; |
| 175 | + test_operator< |
| 176 | + descriptors::no_operation, |
| 177 | + logical_or< bool > |
| 178 | + >( rc, expected, expected_associative ); |
| 179 | + } |
| 180 | + { // logical_xor< bool > |
| 181 | + std::cout << "Testing operator: logical_xor<bool>" << std::endl; |
| 182 | + const std::array<bool, 4> expected = { false, true, true, false }; |
| 183 | + bool expected_associative = true; |
| 184 | + test_operator< |
| 185 | + descriptors::no_operation, |
| 186 | + logical_xor< bool > |
| 187 | + >( rc, expected, expected_associative ); |
| 188 | + } |
| 189 | + |
| 190 | + // Negated operators |
| 191 | + { // logical_not< logical_and< bool > > |
| 192 | + std::cout << "Testing operator: logical_not< logical_and< bool > >" << std::endl; |
| 193 | + const std::array<bool, 4> expected = { true, true, true, false }; |
| 194 | + bool expected_associative = false; |
| 195 | + test_operator< |
| 196 | + descriptors::no_operation, |
| 197 | + logical_not< logical_and< bool > > |
| 198 | + >( rc, expected, expected_associative ); |
| 199 | + } |
| 200 | + { // logical_not< logical_or< bool > > |
| 201 | + std::cout << "Testing operator: logical_not< logical_or< bool > >" << std::endl; |
| 202 | + const std::array<bool, 4> expected = { true, false, false, false }; |
| 203 | + bool expected_associative = false; |
| 204 | + test_operator< |
| 205 | + descriptors::no_operation, |
| 206 | + logical_not< logical_or< bool > > |
| 207 | + >( rc, expected, expected_associative ); |
| 208 | + } |
| 209 | + { // logical_not< logical_xor< bool > > |
| 210 | + std::cout << "Testing operator: logical_not< logical_xor< bool > >" << std::endl; |
| 211 | + const std::array<bool, 4> expected = { true, false, false, true }; |
| 212 | + bool expected_associative = false; |
| 213 | + test_operator< |
| 214 | + descriptors::no_operation, |
| 215 | + logical_not< logical_xor< bool > > |
| 216 | + >( rc, expected, expected_associative ); |
| 217 | + } |
| 218 | + |
| 219 | + // Double-negated operators |
| 220 | + { // logical_not< logical_not < logical_and< bool > > > |
| 221 | + std::cout << "Testing operator: logical_not< logical_not < logical_and< bool > > >" << std::endl; |
| 222 | + const std::array<bool, 4> expected = { false, false, false, true }; |
| 223 | + bool expected_associative = true; |
| 224 | + test_operator< |
| 225 | + descriptors::no_operation, |
| 226 | + logical_not< logical_not< logical_and< bool > > > |
| 227 | + >( rc, expected, expected_associative ); |
| 228 | + } |
| 229 | + { // logical_not< logical_not < logical_or< bool > > > |
| 230 | + std::cout << "Testing operator: logical_not< logical_not < logical_or< bool > > >" << std::endl; |
| 231 | + const std::array<bool, 4> expected = { false, true, true, true }; |
| 232 | + bool expected_associative = true; |
| 233 | + test_operator< |
| 234 | + descriptors::no_operation, |
| 235 | + logical_not< logical_not< logical_or< bool > > > |
| 236 | + >( rc, expected, expected_associative ); |
| 237 | + } |
| 238 | + { // logical_not< logical_not < logical_xor< bool > > > |
| 239 | + std::cout << "Testing operator: logical_not< logical_not < logical_xor< bool > > >" << std::endl; |
| 240 | + const std::array<bool, 4> expected = { false, true, true, false }; |
| 241 | + bool expected_associative = true; |
| 242 | + |
| 243 | + std::cout << "Is lnegated: " << grb::is_logically_negated< logical_not< logical_not < logical_xor< bool > > > >::value << std::endl; |
| 244 | + std::cout << "Is lnegated: " << grb::is_logically_negated< logical_not< logical_xor< bool > > >::value << std::endl; |
| 245 | + std::cout << "Is lnegated: " << grb::is_logically_negated< logical_xor< bool > >::value << std::endl; |
| 246 | + |
| 247 | + std::cout << "Is associative: " << |
| 248 | + is_associative< logical_not< logical_not < logical_xor< bool > > > >::value << std::endl; |
| 249 | + std::cout << "Is associative: " << |
| 250 | + is_associative< logical_not< logical_xor< bool > > >::value << std::endl; |
| 251 | + std::cout << "Is associative: " << |
| 252 | + is_associative< logical_xor< bool > >::value << std::endl; |
| 253 | + |
| 254 | + test_operator< |
| 255 | + descriptors::no_operation, |
| 256 | + logical_not< logical_not< logical_xor< bool > > > |
| 257 | + >( rc, expected, expected_associative ); |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +int main( int argc, char ** argv ) { |
| 262 | + // error checking |
| 263 | + if( argc > 2 ) { |
| 264 | + std::cerr << "Usage: " << argv[ 0 ] << "\n"; |
| 265 | + return 1; |
| 266 | + } |
| 267 | + |
| 268 | + std::cout << "This is functional test " << argv[ 0 ] << "\n"; |
| 269 | + |
| 270 | + grb::Launcher< AUTOMATIC > launcher; |
| 271 | + RC out = SUCCESS; |
| 272 | + size_t unused = 0; |
| 273 | + if( launcher.exec( &grb_program, unused, out, true ) != SUCCESS ) { |
| 274 | + std::cerr << "Launching test FAILED\n"; |
| 275 | + return 255; |
| 276 | + } |
| 277 | + if( out != SUCCESS ) { |
| 278 | + std::cerr << std::flush; |
| 279 | + std::cout << "Test FAILED (" << grb::toString( out ) << ")\n" << std::flush; |
| 280 | + } else { |
| 281 | + std::cout << "Test OK\n" << std::flush; |
| 282 | + } |
| 283 | + return 0; |
| 284 | +} |
0 commit comments