Skip to content

Commit

Permalink
[CPP20]Add format header file examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wtffqbpl committed Dec 29, 2023
1 parent 1fe54f6 commit cf89f17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_executable(cpp_weekly
cpp20_features/barrier_test.cc
cpp20_features/semaphores_test.cc
cpp20_features/placeholder_types.cc
cpp20_features/format_test.cc

cpp_challenges/chap1_math_problems.cc

Expand Down
34 changes: 34 additions & 0 deletions cpp20_features/format_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "internal_check_conds.h"

#include <gtest/gtest.h>
#include <format>
#include <string>

namespace {

void basic_example() {
std::string str{"hello"};

std::cout << std::format("String '{}' has {} chars\n", str, str.size()) ;
std::cout << std::format("String '{1}' has {0} chars\n", str, str.size()) ;
}

}

TEST(format_test, basic_test) {
std::stringstream oss;

testing::internal::CaptureStdout();
basic_example();

auto act_output = testing::internal::GetCapturedStdout();

oss << "String 'hello' has 5 chars\n"
"String '5' has hello chars\n";

#ifndef NDEBUG
debug_msg(oss, act_output);
#endif

EXPECT_EQ(oss.str(), act_output);
}

0 comments on commit cf89f17

Please sign in to comment.