Skip to content

Commit

Permalink
pw_unit_test: Expectation buffer cleanup
Browse files Browse the repository at this point in the history
- Consolidate the two kExpectationBufferSize variables.
- Make kExpectationBufferSize configurable.
- Use a larger default for kExpectationBufferSize on 64-bit machines.

Bug: b/387513166
Change-Id: Ia7d050ed44eb144816559fc956160e36968d7344
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/259055
Reviewed-by: Armando Montanez <[email protected]>
Commit-Queue: Wyatt Hepler <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Jan 9, 2025
1 parent 7f196d0 commit 203c6c8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 54 deletions.
6 changes: 2 additions & 4 deletions pw_unit_test/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,6 @@ output:

Run a subset of test suites
===========================
.. note:: This feature is only supported in C++17.

.. _//pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h

To run only a subset of registered test suites, use the
Expand Down Expand Up @@ -671,8 +669,8 @@ Event handlers

Configuration
=============
.. doxygendefine:: PW_UNIT_TEST_CONFIG_EVENT_BUFFER_SIZE
.. doxygendefine:: PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE
.. doxygenfile:: pw_unit_test/config.h
:sections: define

.. _module-pw_unit_test-cpp-helpers:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// The Pigweed unit test framework requires C++17 to use its full functionality.
#pragma once

// IWYU pragma: private, include "pw_unit_test/framework.h"
Expand Down Expand Up @@ -466,8 +464,7 @@ class Framework {
int RunAllTests();

// Only run test suites whose names are included in the provided list during
// the next test run. This is C++17 only; older versions of C++ will run all
// non-disabled tests.
// the next test run.
void SetTestSuitesToRun(span<std::string_view> test_suites) {
test_suites_to_run_ = test_suites;
}
Expand Down Expand Up @@ -525,57 +522,45 @@ class Framework {
}

template <typename Expectation, typename Lhs, typename Rhs, typename Epsilon>
[[nodiscard]] bool CurrentTestExpect(Expectation expectation,
const Lhs& lhs,
const Rhs& rhs,
const Epsilon& epsilon,
const char* expression,
int line) {
// Size of the buffer into which to write the string with the evaluated
// version of the arguments. This buffer is allocated on the unit test's
// stack, so it shouldn't be too large.
// TODO(hepler): Make this configurable.
[[maybe_unused]] constexpr size_t kExpectationBufferSizeBytes = 192;

[[nodiscard]] bool CurrentTestExpectWithEpsilon(Expectation expectation,
const Lhs& lhs,
const Rhs& rhs,
const Epsilon& epsilon,
const char* expression,
int line) {
const bool success = expectation(lhs, rhs, epsilon);
if (!success) {
CurrentTestExpectSimple(
expression,
MakeString<kExpectationBufferSizeBytes>(ConvertForPrint(lhs),
" within ",
ConvertForPrint(epsilon),
" of ",
ConvertForPrint(rhs))
.c_str(),
line,
success);
CurrentTestExpectSimple(expression,
MakeString<config::kExpectationBufferSizeBytes>(
ConvertForPrint(lhs),
" within ",
ConvertForPrint(epsilon),
" of ",
ConvertForPrint(rhs))
.c_str(),
line,
success);
}
return success;
}

// Runs an expectation function for the currently active test case.
template <typename Expectation, typename Lhs, typename Rhs>
bool CurrentTestExpect(Expectation expectation,
const Lhs& lhs,
const Rhs& rhs,
[[maybe_unused]] const char* expectation_string,
const char* expression,
int line) {
// Size of the buffer into which to write the string with the evaluated
// version of the arguments. This buffer is allocated on the unit test's
// stack, so it shouldn't be too large.
// TODO(hepler): Make this configurable.
[[maybe_unused]] constexpr size_t kExpectationBufferSizeBytes = 192;

[[nodiscard]] bool CurrentTestExpect(Expectation expectation,
const Lhs& lhs,
const Rhs& rhs,
const char* expectation_string,
const char* expression,
int line) {
const bool success = expectation(lhs, rhs);
if (!success) {
CurrentTestExpectSimple(
expression,
MakeString<kExpectationBufferSizeBytes>(ConvertForPrint(lhs),
' ',
expectation_string,
' ',
ConvertForPrint(rhs))
MakeString<config::kExpectationBufferSizeBytes>(ConvertForPrint(lhs),
' ',
expectation_string,
' ',
ConvertForPrint(rhs))
.c_str(),
line,
success);
Expand Down Expand Up @@ -845,7 +830,7 @@ inline int RUN_ALL_TESTS() {
__LINE__)

#define _PW_TEST_NEAR(lhs, rhs, epsilon) \
::pw::unit_test::internal::Framework::Get().CurrentTestExpect( \
::pw::unit_test::internal::Framework::Get().CurrentTestExpectWithEpsilon( \
[](const auto& _pw_lhs, const auto& _pw_rhs, const auto& _pw_epsilon) { \
return std::abs(_pw_lhs - _pw_rhs) <= _pw_epsilon; \
}, \
Expand Down
22 changes: 16 additions & 6 deletions pw_unit_test/public/pw_unit_test/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@
#define PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE 16384
#endif // PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE

namespace pw {
namespace unit_test {
namespace config {
#ifndef PW_UNIT_TEST_CONFIG_EXPECTATION_BUFFER_SIZE
/// Size of the buffer into which to write the string with the evaluated
/// version of the arguments. This buffer is allocated on the unit test's
/// stack, so it shouldn't be too large.
#define PW_UNIT_TEST_CONFIG_EXPECTATION_BUFFER_SIZE \
(sizeof(void*) > 4 ? 512 : 192)
#endif // PW_UNIT_TEST_CONFIG_EXPECTATION_BUFFER_SIZE

namespace pw::unit_test::config {

inline constexpr size_t kEventBufferSize =
PW_UNIT_TEST_CONFIG_EVENT_BUFFER_SIZE;
#undef PW_UNIT_TEST_CONFIG_EVENT_BUFFER_SIZE

inline constexpr size_t kMemoryPoolSize = PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE;
#undef PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE

inline constexpr size_t kExpectationBufferSizeBytes =
PW_UNIT_TEST_CONFIG_EXPECTATION_BUFFER_SIZE;
#undef PW_UNIT_TEST_CONFIG_EXPECTATION_BUFFER_SIZE

} // namespace config
} // namespace unit_test
} // namespace pw
} // namespace pw::unit_test::config

0 comments on commit 203c6c8

Please sign in to comment.