| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (C) 2010 by Thomas Moulard. | ||
| 2 | // | ||
| 3 | |||
| 4 | // Redistribution and use in source and binary forms, with or without | ||
| 5 | // modification, are permitted provided that the following conditions are | ||
| 6 | // met: | ||
| 7 | // | ||
| 8 | // 1. Redistributions of source code must retain the above copyright | ||
| 9 | // notice, this list of conditions and the following disclaimer. | ||
| 10 | // | ||
| 11 | // 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | // notice, this list of conditions and the following disclaimer in the | ||
| 13 | // documentation and/or other materials provided with the distribution. | ||
| 14 | // | ||
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 16 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 17 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| 18 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| 19 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 20 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 21 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 22 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 23 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 24 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
| 26 | // DAMAGE. | ||
| 27 | |||
| 28 | // Make sure assertions are enabled (otherwise this test does not make | ||
| 29 | // sense). | ||
| 30 | #ifndef HPP_ENABLE_ASSERTIONS | ||
| 31 | #define HPP_ENABLE_ASSERTIONS | ||
| 32 | #endif // !HPP_ENABLE_ASSERTIONS | ||
| 33 | |||
| 34 | #include <cassert> | ||
| 35 | #include <hpp/util/assertion.hh> | ||
| 36 | #include <iostream> | ||
| 37 | |||
| 38 | #include "common.hh" | ||
| 39 | #include "config.h" | ||
| 40 | |||
| 41 | int run_test(); | ||
| 42 | int my_plus_function(int a, int b); | ||
| 43 | int my_broken_plus_function(int a, int b); | ||
| 44 | |||
| 45 | 3 | int my_plus_function(int a, int b) { | |
| 46 | // This algorithm does not work with negative numbers. | ||
| 47 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
7 | HPP_PRECONDITION(a >= 0); |
| 48 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
2 | HPP_PRECONDITION(b >= 0); |
| 49 | |||
| 50 | 2 | int res = 0, expected = a + b; | |
| 51 | |||
| 52 | 2 | res = b; | |
| 53 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 2 times.
|
13 | while (a > 0) --a, ++res; |
| 54 | |||
| 55 | // This algorithm should have computed a + b. | ||
| 56 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
2 | HPP_ASSERT(res == expected); |
| 57 | |||
| 58 | 2 | return res; | |
| 59 | } | ||
| 60 | |||
| 61 | 1 | int my_broken_plus_function(int a, int b) { | |
| 62 | // This algorithm does not work with negative numbers. | ||
| 63 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
1 | HPP_PRECONDITION(a >= 0); |
| 64 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
1 | HPP_PRECONDITION(b >= 0); |
| 65 | |||
| 66 | 1 | int res = 0, expected = a + b; | |
| 67 | |||
| 68 | 1 | res = b; // This is wrong. | |
| 69 | |||
| 70 | // This algorithm should have computed a + b. | ||
| 71 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
5 | HPP_ASSERT(res == expected); |
| 72 | |||
| 73 | ✗ | return res; | |
| 74 | } | ||
| 75 | |||
| 76 | 1 | int run_test() { | |
| 77 | // Assertion. | ||
| 78 | try { | ||
| 79 | 1 | int i = 4; | |
| 80 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
1 | HPP_ASSERT(i > 2); |
| 81 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
|
5 | HPP_ASSERT(i < 3); |
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } catch (::hpp::AssertionError& assertionError) { |
| 83 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << assertionError << std::endl; |
| 84 | 1 | } | |
| 85 | |||
| 86 | // Good calls. | ||
| 87 | 1 | my_plus_function(4, 5); | |
| 88 | 1 | my_plus_function(7, 9); | |
| 89 | |||
| 90 | // Check for precondition failure. | ||
| 91 | try { | ||
| 92 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | my_plus_function(-2, 5); |
| 93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } catch (::hpp::AssertionError& assertionError) { |
| 94 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << assertionError << std::endl; |
| 95 | 1 | } | |
| 96 | |||
| 97 | // Check for postcondition failure. | ||
| 98 | try { | ||
| 99 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | my_broken_plus_function(3, 5); |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | } catch (::hpp::AssertionError& assertionError) { |
| 101 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | std::cout << assertionError << std::endl; |
| 102 | 1 | } | |
| 103 | |||
| 104 | 1 | return 0; | |
| 105 | } | ||
| 106 | |||
| 107 |
5/30✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
|
1 | GENERATE_TEST() |
| 108 |