Mercurial > hg > CbC > CbC_llvm
comparison unittests/Support/ErrorTest.cpp @ 121:803732b1fca8
LLVM 5.0
author | kono |
---|---|
date | Fri, 27 Oct 2017 17:07:41 +0900 |
parents | 1172e4bd9c6f |
children | 3a76565eade5 |
comparison
equal
deleted
inserted
replaced
120:1172e4bd9c6f | 121:803732b1fca8 |
---|---|
100 Error E = Error::success(); | 100 Error E = Error::success(); |
101 EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'"; | 101 EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'"; |
102 } | 102 } |
103 | 103 |
104 // Test that unchecked succes values cause an abort. | 104 // Test that unchecked succes values cause an abort. |
105 #ifndef NDEBUG | 105 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
106 TEST(Error, UncheckedSuccess) { | 106 TEST(Error, UncheckedSuccess) { |
107 EXPECT_DEATH({ Error E = Error::success(); }, | 107 EXPECT_DEATH({ Error E = Error::success(); }, |
108 "Program aborted due to an unhandled Error:") | 108 "Program aborted due to an unhandled Error:") |
109 << "Unchecked Error Succes value did not cause abort()"; | 109 << "Unchecked Error Succes value did not cause abort()"; |
110 } | 110 } |
126 errAsOutParamHelper(E); | 126 errAsOutParamHelper(E); |
127 (void)!!E; | 127 (void)!!E; |
128 } | 128 } |
129 | 129 |
130 // Test that ErrorAsOutParameter clears the checked flag on destruction. | 130 // Test that ErrorAsOutParameter clears the checked flag on destruction. |
131 #ifndef NDEBUG | 131 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
132 TEST(Error, ErrorAsOutParameterUnchecked) { | 132 TEST(Error, ErrorAsOutParameterUnchecked) { |
133 EXPECT_DEATH({ Error E = Error::success(); errAsOutParamHelper(E); }, | 133 EXPECT_DEATH({ Error E = Error::success(); errAsOutParamHelper(E); }, |
134 "Program aborted due to an unhandled Error:") | 134 "Program aborted due to an unhandled Error:") |
135 << "ErrorAsOutParameter did not clear the checked flag on destruction."; | 135 << "ErrorAsOutParameter did not clear the checked flag on destruction."; |
136 } | 136 } |
137 #endif | 137 #endif |
138 | 138 |
139 // Check that we abort on unhandled failure cases. (Force conversion to bool | 139 // Check that we abort on unhandled failure cases. (Force conversion to bool |
140 // to make sure that we don't accidentally treat checked errors as handled). | 140 // to make sure that we don't accidentally treat checked errors as handled). |
141 // Test runs in debug mode only. | 141 // Test runs in debug mode only. |
142 #ifndef NDEBUG | 142 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
143 TEST(Error, UncheckedError) { | 143 TEST(Error, UncheckedError) { |
144 auto DropUnhandledError = []() { | 144 auto DropUnhandledError = []() { |
145 Error E = make_error<CustomError>(42); | 145 Error E = make_error<CustomError>(42); |
146 (void)!E; | 146 (void)!E; |
147 }; | 147 }; |
358 make_error<CustomError>(7), | 358 make_error<CustomError>(7), |
359 make_error<CustomError>(7))), | 359 make_error<CustomError>(7))), |
360 [&](const CustomError &CE) { | 360 [&](const CustomError &CE) { |
361 Sum += CE.getInfo(); | 361 Sum += CE.getInfo(); |
362 }); | 362 }); |
363 EXPECT_EQ(Sum, 28) << "Failed to correctly concatenate erorr lists."; | 363 EXPECT_EQ(Sum, 28) << "Failed to correctly concatenate error lists."; |
364 } | 364 } |
365 } | 365 } |
366 | 366 |
367 // Test that we can consume success values. | 367 // Test that we can consume success values. |
368 TEST(Error, ConsumeSuccess) { | 368 TEST(Error, ConsumeSuccess) { |
375 consumeError(std::move(E)); | 375 consumeError(std::move(E)); |
376 } | 376 } |
377 | 377 |
378 // Test that handleAllUnhandledErrors crashes if an error is not caught. | 378 // Test that handleAllUnhandledErrors crashes if an error is not caught. |
379 // Test runs in debug mode only. | 379 // Test runs in debug mode only. |
380 #ifndef NDEBUG | 380 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
381 TEST(Error, FailureToHandle) { | 381 TEST(Error, FailureToHandle) { |
382 auto FailToHandle = []() { | 382 auto FailToHandle = []() { |
383 handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { | 383 handleAllErrors(make_error<CustomError>(7), [&](const CustomSubError &SE) { |
384 errs() << "This should never be called"; | 384 errs() << "This should never be called"; |
385 exit(1); | 385 exit(1); |
386 }); | 386 }); |
387 }; | 387 }; |
388 | 388 |
389 EXPECT_DEATH(FailToHandle(), "Program aborted due to an unhandled Error:") | 389 EXPECT_DEATH(FailToHandle(), |
390 "Failure value returned from cantFail wrapped call") | |
390 << "Unhandled Error in handleAllErrors call did not cause an " | 391 << "Unhandled Error in handleAllErrors call did not cause an " |
391 "abort()"; | 392 "abort()"; |
392 } | 393 } |
393 #endif | 394 #endif |
394 | 395 |
395 // Test that handleAllUnhandledErrors crashes if an error is returned from a | 396 // Test that handleAllUnhandledErrors crashes if an error is returned from a |
396 // handler. | 397 // handler. |
397 // Test runs in debug mode only. | 398 // Test runs in debug mode only. |
398 #ifndef NDEBUG | 399 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
399 TEST(Error, FailureFromHandler) { | 400 TEST(Error, FailureFromHandler) { |
400 auto ReturnErrorFromHandler = []() { | 401 auto ReturnErrorFromHandler = []() { |
401 handleAllErrors(make_error<CustomError>(7), | 402 handleAllErrors(make_error<CustomError>(7), |
402 [&](std::unique_ptr<CustomSubError> SE) { | 403 [&](std::unique_ptr<CustomSubError> SE) { |
403 return Error(std::move(SE)); | 404 return Error(std::move(SE)); |
404 }); | 405 }); |
405 }; | 406 }; |
406 | 407 |
407 EXPECT_DEATH(ReturnErrorFromHandler(), | 408 EXPECT_DEATH(ReturnErrorFromHandler(), |
408 "Program aborted due to an unhandled Error:") | 409 "Failure value returned from cantFail wrapped call") |
409 << " Error returned from handler in handleAllErrors call did not " | 410 << " Error returned from handler in handleAllErrors call did not " |
410 "cause abort()"; | 411 "cause abort()"; |
411 } | 412 } |
412 #endif | 413 #endif |
413 | 414 |
467 EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), | 468 EXPECT_EXIT(ExitOnErr(Expected<int>(make_error<CustomSubError>(0, 0))), |
468 ::testing::ExitedWithCode(2), "Error in tool:") | 469 ::testing::ExitedWithCode(2), "Error in tool:") |
469 << "exitOnError returned an unexpected error result"; | 470 << "exitOnError returned an unexpected error result"; |
470 } | 471 } |
471 | 472 |
473 // Test that the ExitOnError utility works as expected. | |
474 TEST(Error, CantFailSuccess) { | |
475 cantFail(Error::success()); | |
476 | |
477 int X = cantFail(Expected<int>(42)); | |
478 EXPECT_EQ(X, 42) << "Expected value modified by cantFail"; | |
479 | |
480 int Dummy = 42; | |
481 int &Y = cantFail(Expected<int&>(Dummy)); | |
482 EXPECT_EQ(&Dummy, &Y) << "Reference mangled by cantFail"; | |
483 } | |
484 | |
485 // Test that cantFail results in a crash if you pass it a failure value. | |
486 #if LLVM_ENABLE_ABI_BREAKING_CHECKS && !defined(NDEBUG) | |
487 TEST(Error, CantFailDeath) { | |
488 EXPECT_DEATH( | |
489 cantFail(make_error<StringError>("foo", inconvertibleErrorCode()), | |
490 "Cantfail call failed"), | |
491 "Cantfail call failed") | |
492 << "cantFail(Error) did not cause an abort for failure value"; | |
493 | |
494 EXPECT_DEATH( | |
495 { | |
496 auto IEC = inconvertibleErrorCode(); | |
497 int X = cantFail(Expected<int>(make_error<StringError>("foo", IEC))); | |
498 (void)X; | |
499 }, | |
500 "Failure value returned from cantFail wrapped call") | |
501 << "cantFail(Expected<int>) did not cause an abort for failure value"; | |
502 } | |
503 #endif | |
504 | |
505 | |
472 // Test Checked Expected<T> in success mode. | 506 // Test Checked Expected<T> in success mode. |
473 TEST(Error, CheckedExpectedInSuccessMode) { | 507 TEST(Error, CheckedExpectedInSuccessMode) { |
474 Expected<int> A = 7; | 508 Expected<int> A = 7; |
475 EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; | 509 EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'"; |
476 // Access is safe in second test, since we checked the error in the first. | 510 // Access is safe in second test, since we checked the error in the first. |
488 } | 522 } |
489 | 523 |
490 // Test Unchecked Expected<T> in success mode. | 524 // Test Unchecked Expected<T> in success mode. |
491 // We expect this to blow up the same way Error would. | 525 // We expect this to blow up the same way Error would. |
492 // Test runs in debug mode only. | 526 // Test runs in debug mode only. |
493 #ifndef NDEBUG | 527 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
494 TEST(Error, UncheckedExpectedInSuccessModeDestruction) { | 528 TEST(Error, UncheckedExpectedInSuccessModeDestruction) { |
495 EXPECT_DEATH({ Expected<int> A = 7; }, | 529 EXPECT_DEATH({ Expected<int> A = 7; }, |
496 "Expected<T> must be checked before access or destruction.") | 530 "Expected<T> must be checked before access or destruction.") |
497 << "Unchecekd Expected<T> success value did not cause an abort()."; | 531 << "Unchecekd Expected<T> success value did not cause an abort()."; |
498 } | 532 } |
499 #endif | 533 #endif |
500 | 534 |
501 // Test Unchecked Expected<T> in success mode. | 535 // Test Unchecked Expected<T> in success mode. |
502 // We expect this to blow up the same way Error would. | 536 // We expect this to blow up the same way Error would. |
503 // Test runs in debug mode only. | 537 // Test runs in debug mode only. |
504 #ifndef NDEBUG | 538 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
505 TEST(Error, UncheckedExpectedInSuccessModeAccess) { | 539 TEST(Error, UncheckedExpectedInSuccessModeAccess) { |
506 EXPECT_DEATH({ Expected<int> A = 7; *A; }, | 540 EXPECT_DEATH({ Expected<int> A = 7; *A; }, |
507 "Expected<T> must be checked before access or destruction.") | 541 "Expected<T> must be checked before access or destruction.") |
508 << "Unchecekd Expected<T> success value did not cause an abort()."; | 542 << "Unchecekd Expected<T> success value did not cause an abort()."; |
509 } | 543 } |
510 #endif | 544 #endif |
511 | 545 |
512 // Test Unchecked Expected<T> in success mode. | 546 // Test Unchecked Expected<T> in success mode. |
513 // We expect this to blow up the same way Error would. | 547 // We expect this to blow up the same way Error would. |
514 // Test runs in debug mode only. | 548 // Test runs in debug mode only. |
515 #ifndef NDEBUG | 549 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
516 TEST(Error, UncheckedExpectedInSuccessModeAssignment) { | 550 TEST(Error, UncheckedExpectedInSuccessModeAssignment) { |
517 EXPECT_DEATH({ Expected<int> A = 7; A = 7; }, | 551 EXPECT_DEATH({ Expected<int> A = 7; A = 7; }, |
518 "Expected<T> must be checked before access or destruction.") | 552 "Expected<T> must be checked before access or destruction.") |
519 << "Unchecekd Expected<T> success value did not cause an abort()."; | 553 << "Unchecekd Expected<T> success value did not cause an abort()."; |
520 } | 554 } |
530 } | 564 } |
531 | 565 |
532 // Check that an Expected instance with an error value doesn't allow access to | 566 // Check that an Expected instance with an error value doesn't allow access to |
533 // operator*. | 567 // operator*. |
534 // Test runs in debug mode only. | 568 // Test runs in debug mode only. |
535 #ifndef NDEBUG | 569 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
536 TEST(Error, AccessExpectedInFailureMode) { | 570 TEST(Error, AccessExpectedInFailureMode) { |
537 Expected<int> A = make_error<CustomError>(42); | 571 Expected<int> A = make_error<CustomError>(42); |
538 EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.") | 572 EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.") |
539 << "Incorrect Expected error value"; | 573 << "Incorrect Expected error value"; |
540 consumeError(A.takeError()); | 574 consumeError(A.takeError()); |
542 #endif | 576 #endif |
543 | 577 |
544 // Check that an Expected instance with an error triggers an abort if | 578 // Check that an Expected instance with an error triggers an abort if |
545 // unhandled. | 579 // unhandled. |
546 // Test runs in debug mode only. | 580 // Test runs in debug mode only. |
547 #ifndef NDEBUG | 581 #if LLVM_ENABLE_ABI_BREAKING_CHECKS |
548 TEST(Error, UnhandledExpectedInFailureMode) { | 582 TEST(Error, UnhandledExpectedInFailureMode) { |
549 EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); }, | 583 EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); }, |
550 "Expected<T> must be checked before access or destruction.") | 584 "Expected<T> must be checked before access or destruction.") |
551 << "Unchecked Expected<T> failure value did not cause an abort()"; | 585 << "Unchecked Expected<T> failure value did not cause an abort()"; |
552 } | 586 } |
568 // Check A2 by converting to bool before assigning to it. | 602 // Check A2 by converting to bool before assigning to it. |
569 (void)!!A2; | 603 (void)!!A2; |
570 A2 = Expected<std::unique_ptr<D>>(nullptr); | 604 A2 = Expected<std::unique_ptr<D>>(nullptr); |
571 // Check A2 again before destruction. | 605 // Check A2 again before destruction. |
572 (void)!!A2; | 606 (void)!!A2; |
607 } | |
608 | |
609 // Test that handleExpected just returns success values. | |
610 TEST(Error, HandleExpectedSuccess) { | |
611 auto ValOrErr = | |
612 handleExpected(Expected<int>(42), | |
613 []() { return Expected<int>(43); }); | |
614 EXPECT_TRUE(!!ValOrErr) | |
615 << "handleExpected should have returned a success value here"; | |
616 EXPECT_EQ(*ValOrErr, 42) | |
617 << "handleExpected should have returned the original success value here"; | |
618 } | |
619 | |
620 enum FooStrategy { Aggressive, Conservative }; | |
621 | |
622 static Expected<int> foo(FooStrategy S) { | |
623 if (S == Aggressive) | |
624 return make_error<CustomError>(7); | |
625 return 42; | |
626 } | |
627 | |
628 // Test that handleExpected invokes the error path if errors are not handled. | |
629 TEST(Error, HandleExpectedUnhandledError) { | |
630 // foo(Aggressive) should return a CustomError which should pass through as | |
631 // there is no handler for CustomError. | |
632 auto ValOrErr = | |
633 handleExpected( | |
634 foo(Aggressive), | |
635 []() { return foo(Conservative); }); | |
636 | |
637 EXPECT_FALSE(!!ValOrErr) | |
638 << "handleExpected should have returned an error here"; | |
639 auto Err = ValOrErr.takeError(); | |
640 EXPECT_TRUE(Err.isA<CustomError>()) | |
641 << "handleExpected should have returned the CustomError generated by " | |
642 "foo(Aggressive) here"; | |
643 consumeError(std::move(Err)); | |
644 } | |
645 | |
646 // Test that handleExpected invokes the fallback path if errors are handled. | |
647 TEST(Error, HandleExpectedHandledError) { | |
648 // foo(Aggressive) should return a CustomError which should handle triggering | |
649 // the fallback path. | |
650 auto ValOrErr = | |
651 handleExpected( | |
652 foo(Aggressive), | |
653 []() { return foo(Conservative); }, | |
654 [](const CustomError&) { /* do nothing */ }); | |
655 | |
656 EXPECT_TRUE(!!ValOrErr) | |
657 << "handleExpected should have returned a success value here"; | |
658 EXPECT_EQ(*ValOrErr, 42) | |
659 << "handleExpected returned the wrong success value"; | |
573 } | 660 } |
574 | 661 |
575 TEST(Error, ErrorCodeConversions) { | 662 TEST(Error, ErrorCodeConversions) { |
576 // Round-trip a success value to check that it converts correctly. | 663 // Round-trip a success value to check that it converts correctly. |
577 EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())), | 664 EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())), |