Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_assert/light.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_assert/assert.h" |
| 19 | |
| 20 | // PW_ASSERT() should always be enabled, and always evaluate the expression. |
| 21 | TEST(Light, AssertTrue) { |
| 22 | int evaluated = 1; |
| 23 | PW_ASSERT(++evaluated); |
| 24 | EXPECT_EQ(evaluated, 2); |
| 25 | } |
| 26 | |
| 27 | // PW_DASSERT() might be disabled sometimes. |
| 28 | TEST(Light, DebugAssertTrue) { |
| 29 | int evaluated = 1; |
| 30 | PW_DASSERT(++evaluated); |
| 31 | if (PW_ASSERT_ENABLE_DEBUG == 1) { |
| 32 | EXPECT_EQ(evaluated, 2); |
| 33 | } else { |
| 34 | EXPECT_EQ(evaluated, 1); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Unfortunately, we don't have the infrastructure to test failure handling |
| 39 | // automatically, since the harness crashes in the process of running this |
| 40 | // test. The unsatisfying alternative is to test the functionality manually, |
| 41 | // then disable the test. |
| 42 | |
| 43 | TEST(Light, AssertFalse) { |
| 44 | if (0) { |
| 45 | PW_ASSERT(false); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST(Light, DebugAssertFalse) { |
| 50 | if (0) { |
| 51 | PW_DASSERT(false); |
| 52 | } |
| 53 | } |