Skip to content

Commit

Permalink
add template testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
wtffqbpl committed Oct 8, 2023
1 parent cb1dfaf commit 507a629
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ add_executable(cpp_weekly
topics/memory_block_management/memory_block_management_main.cc

youtube/cpp_weekly_youtube_test.cc
youtube/e340_string_split_test.cc)
youtube/e340_string_split_test.cc template/basics_test.cc)

include_directories(include
new_features/cpp_17/inline_variable
Expand Down
53 changes: 20 additions & 33 deletions algorithms/partial_sort_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <array>
#include <gtest/gtest.h>

#include "internal_check_conds.h"

Expand All @@ -20,31 +20,25 @@ namespace {
/// Compare comp);
/// \endcode


void print_res(auto const &s, int middle)
{
void print_res(auto const &s, int middle) {
for (int a : s)
std::cout << a << ' ';
std::cout << '\n';

if (middle > 0)
{
if (middle > 0) {
while (middle-- > 0)
std::cout << "--";
std::cout << '^';
}
else if (middle < 0)
{
for (auto i = s.size() + middle; --i; std::cout << " ")
{}
for (std::cout << '^'; middle++ < 0; std::cout << "--")
{}
} else if (middle < 0) {
for (auto i = s.size() + middle; --i; std::cout << " ") {
}
for (std::cout << '^'; middle++ < 0; std::cout << "--") {
}
}
std::cout << '\n';
}

void test1()
{
void test1() {
std::stringstream oss;
testing::internal::CaptureStdout();

Expand Down Expand Up @@ -80,15 +74,13 @@ void test1()

namespace impl {
template <typename RandomIt, typename Compare>
void sift_down(RandomIt first, RandomIt last, const Compare &comp)
{
void sift_down(RandomIt first, RandomIt last, const Compare &comp) {
// shift down element at 'first'
const auto length = static_cast<size_t>(last - first);
std::size_t current = 0;
std::size_t next = 2;

while (next < length)
{
while (next < length) {
if (comp(*(first + next), *(first + (next - 1))))
--next;
if (!comp(*(first + current), *(first + next)))
Expand All @@ -103,31 +95,26 @@ void sift_down(RandomIt first, RandomIt last, const Compare &comp)
}

template <typename RandomIt, typename Compare>
void heap_select(RandomIt first, RandomIt middle, RandomIt last, const Compare &comp)
{
void heap_select(RandomIt first, RandomIt middle, RandomIt last,
const Compare &comp) {
std::make_heap(first, middle, comp);
for (auto i = middle; i != last; ++i)
{
if (comp(*i, *first))
{
for (auto i = middle; i != last; ++i) {
if (comp(*i, *first)) {
std::iter_swap(first, i);
sift_down(first, middle, comp);
}
}
}

template <typename RandomIt, typename Compare>
void partial_sort_new(RandomIt first, RandomIt middle, RandomIt last, Compare comp)
{
void partial_sort_new(RandomIt first, RandomIt middle, RandomIt last,
Compare comp) {
impl::heap_select(first, middle, last, comp);
std::sort_heap(first, middle, comp);
}

}
} // namespace impl

}
} // namespace

TEST(partial_sort_test, test1)
{
test1();
}
TEST(partial_sort_test, test1) { test1(); }
207 changes: 207 additions & 0 deletions template/basics_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#include "internal_check_conds.h"
#include <gtest/gtest.h>
#include <list>

namespace class_variable_alias_templates {

template <typename T> class my_vector {
public:
typedef T *my_iterator;

public:
my_vector();

my_vector(T tmp) {}

my_vector &operator=(const my_vector &item) = default;

public:
void my_func() { std::cout << "myfunc() is called." << std::endl; }

public:
my_iterator my_begin() {}
my_iterator my_end() {}
};

template <typename T> my_vector<T>::my_vector() {}

void test_class_template() {
my_vector<int> tmp_vec;
tmp_vec.my_func();

my_vector tmp_vec2(12);
}

// Deduction Guide
template <typename T> struct A {
A(T val, T val2) {
std::cout << "A::A(T val, T val2) is called." << std::endl;
}

A(T val) { std::cout << "A::A(T val) is called." << std::endl; }
};

// Deduction Guide
template <typename T> A(T, T) -> A<T>;

template <typename T>
struct B // Aggregate type
{
T m_b;
};

template <typename T> B(T) -> B<T>;

void deduction_guide_test() {
A aobj1{15, 16};
A aobj2{12.8};

B<int> bobj1;
B<int> bobj2{15};

B bobj3{15}; // Must be deduced with deduction guide
}

// Partial specialization
template <typename T, typename U> struct TC {
TC() { std::cout << "TC general constructor function" << std::endl; }

void functest1() { std::cout << "functest1 general version" << std::endl; }
};

template <> struct TC<int, int> {
TC() {
std::cout << "TC<int, int> fully partial specialization version"
<< std::endl;
}

void functest1();
};

void TC<int, int>::functest1() {
std::cout << "functest1 partial specialization version" << std::endl;
}

void partial_specialization_test1() {
TC<int, float> mytc;
mytc.functest1();

TC<int, int> mytc2;
mytc2.functest1();
}

template <typename U> struct TC<float, U> {
TC() {
std::cout << "TC<float, U> partial specialization constructor" << std::endl;
}

void functest1();
};

template <typename U> void TC<float, U>::functest1() {
std::cout << "TC<float, U>::functest1 partial specialization version"
<< std::endl;
}

void partial_specialization_test2() {
TC<float, int> mytc4;
mytc4.functest1();
}

// Template Template Parameters
template <typename T, template <class> class Container = std::vector>
class my_class {
public:
void func();

my_class() {
for (int i = 0; i < 10; ++i) {
myc.push_back(i);
}
}

public:
Container<T> myc;
};

template <typename T, template <class> class Container>
void my_class<T, Container>::func() {
std::cout << "good!" << std::endl;
}

void template_template_parameter_test() {
my_class<double, std::list> mylistobj2;
mylistobj2.func();
}

} // namespace class_variable_alias_templates

TEST(class_template, test1) {
std::stringstream oss;
testing::internal::CaptureStdout();

class_variable_alias_templates::test_class_template();

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

oss << "myfunc() is called.\n";

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

EXPECT_TRUE(oss.str() == act_output);
}

TEST(deduction_guide, test1) {
std::stringstream oss;
testing::internal::CaptureStdout();

class_variable_alias_templates::deduction_guide_test();

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

oss << "A::A(T val, T val2) is called.\n"
<< "A::A(T val) is called.\n";

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

EXPECT_TRUE(oss.str() == act_output);
}

TEST(partial_specialization, test1) {
std::stringstream oss;
testing::internal::CaptureStdout();

class_variable_alias_templates::partial_specialization_test1();

oss << "TC general constructor function\n"
"functest1 general version\n"
"TC<int, int> fully partial specialization version\n"
"functest1 partial specialization version\n";

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

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

EXPECT_TRUE(oss.str() == act_output);
}

TEST(partial_specialization, test2) {
std::stringstream oss;
testing::internal::CaptureStdout();

class_variable_alias_templates::partial_specialization_test2();

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

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

EXPECT_TRUE(oss.str() == act_output);
}
8 changes: 3 additions & 5 deletions template/chap_4_variadic_templates.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,9 @@ TEST(variadic_templates, using_pack_expasion_test) {
derived<int, std::string, bool> d3 = true;
}

template <typename F, typename... Args>
auto delay_invoke(F f, Args... args)
{
return [f = std::move(f), f_args = std::make_tuple(std::move(args)...)]()
-> decltype(auto) {
template <typename F, typename... Args> auto delay_invoke(F f, Args... args) {
return [f = std::move(f),
f_args = std::make_tuple(std::move(args)...)]() -> decltype(auto) {
return std::apply(f, f_args);
};
}
Expand Down

0 comments on commit 507a629

Please sign in to comment.