Skip to content

Instantly share code, notes, and snippets.

@iglesias
Created February 27, 2024 07:53
Show Gist options
  • Select an option

  • Save iglesias/c480ba6a83e673aef2fc47d7a859870d to your computer and use it in GitHub Desktop.

Select an option

Save iglesias/c480ba6a83e673aef2fc47d7a859870d to your computer and use it in GitHub Desktop.
Rewiring your brain with test driven thinking in C++ - Phil Nash - Meeting C++ 2023
#include <string>
#include <string_view>
#include <catch2/catch_test_macros.hpp>
std::string left_pad(std::string_view str, std::size_t min_len) {
auto const pad_len = std::max<int>(0, min_len-str.length());
return std::string(pad_len, ' ') + std::string(str);
}
TEST_CASE("left_pad") {
REQUIRE(left_pad("", 0) == "");
REQUIRE(left_pad("a", 0) == "a");
REQUIRE(left_pad("", 1) == " ");
REQUIRE(left_pad("", 2) == " ");
REQUIRE(left_pad("abc", 5) == " abc");
}
@iglesias
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment