* StreamSink is a utility class used by the testing framework that either * captures values printed via C++'s operator<< (as with regular * std::ostreams) or discards them. (If a test has failed, then * StreamSink does the former, and if it has passed, it does the latter.) * In the event of a test failure, passes along the failure message to the * provided GPUUnitTestContext's |reportFailure
| 361 | * provided GPUUnitTestContext's |reportFailure| method. |
| 362 | */ |
| 363 | class StreamSink |
| 364 | { |
| 365 | public: |
| 366 | /** |
| 367 | * We need to declare this constructor in order to return |
| 368 | * StreamSinks as rvalues from functions because we've declared a |
| 369 | * StreamSink destructor below. |
| 370 | */ |
| 371 | StreamSink(StreamSink&&) = default; |
| 372 | |
| 373 | /** |
| 374 | * Construct a StreamSink for a test context. |
| 375 | * If a non-nullptr UnitTestContext is provided, the values printed |
| 376 | * will be accumulated and passed to the context's reportFailure() |
| 377 | * method when the StreamSink destructor runs. |
| 378 | */ |
| 379 | StreamSink(UnitTestContext* ctx) : mpCtx(ctx) {} |
| 380 | |
| 381 | ~StreamSink() |
| 382 | { |
| 383 | if (mpCtx) |
| 384 | mpCtx->reportFailure(mSs.str()); |
| 385 | } |
| 386 | |
| 387 | void setInsertNewLine() { mInsertNewLine = true; } |
| 388 | |
| 389 | template<typename T> |
| 390 | StreamSink& operator<<(T&& s) |
| 391 | { |
| 392 | if (mpCtx) |
| 393 | { |
| 394 | if (mInsertNewLine) |
| 395 | { |
| 396 | mSs << "\n"; |
| 397 | mInsertNewLine = false; |
| 398 | } |
| 399 | mSs << s; |
| 400 | } |
| 401 | return *this; |
| 402 | } |
| 403 | |
| 404 | private: |
| 405 | std::stringstream mSs; |
| 406 | UnitTestContext* mpCtx = nullptr; |
| 407 | bool mInsertNewLine = false; |
| 408 | }; |
| 409 | |
| 410 | /// A test has failed when there is a failureMsg, if the value is not set, the test has passed |
| 411 | inline StreamSink createFullMessage( |
no outgoing calls