Skip to content

Commit 721830e

Browse files
test: cpp exercises
1 parent 5705ba8 commit 721830e

59 files changed

Lines changed: 758 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

exercises/00_intro/01_intro.cpp

100755100644
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
1+
#include <gtest/gtest.h>
12
#include <iostream>
3+
#include <string>
24

3-
void splashScreen()
5+
void intro01()
46
{
7+
std::cout << "Welcome to cpplings!\n";
8+
}
59

6-
// // user input
7-
// char userInput{};
8-
9-
// splash screen
10-
// std::cout << " Welcome to cpplings! \n";
11-
std::cout << " _ _ \n";
12-
std::cout << " | (_) \n";
13-
std::cout << " ___ _ __ _ __ | |_ _ __ __ _ ___ \n";
14-
std::cout << " / __| '_ \\| '_ \\| | | '_ \\ / _` / __|\n";
15-
std::cout << "| (__| |_) | |_) | | | | | | (_| \\__ \\\n";
16-
std::cout << " \\___| .__/| .__/|_|_|_| |_|\\__, |___/\n";
17-
std::cout << " | | | | __/ | \n";
18-
std::cout << " |_| |_| |___/ \n";
19-
std::cout << " \n";
20-
// std::cout << " Enter Y/y to continue \n";
21-
22-
// while (true)
23-
// {
24-
// std::cin >> userInput;
25-
//
26-
// if (userInput == 'y' || userInput == 'Y')
27-
// {
28-
// break;
29-
// }
30-
// }
10+
TEST(Intro, Intro01)
11+
{
12+
testing::internal::CaptureStdout();
13+
intro01();
14+
std::string stdout{testing::internal::GetCapturedStdout()};
15+
ASSERT_EQ(stdout, "Welcome to cpplings!\n");
3116
}

exercises/00_intro/02_intro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <iostream>
1919

20-
void message()
20+
void intro02()
2121
{
2222
// TODO: Fix the line of code to print "Hello World!" to the console
2323
std::cout << "Hello World!";

exercises/01_variables/01_variables.cpp

100755100644
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
// https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/
1616

1717
// TODO: Add the missing module for printing
18+
#include <gtest/gtest.h>
1819
#include <iostream>
1920

2021
void variables01()
2122
{
22-
// TODO: Add the missing keyword
23+
// TODO: Assign an integer to a variable name "x" using direct list initialization
2324
int x{5};
2425

25-
std::cout << x;
26+
// TODO: Assign an integer to a variable named "y" using the classic way
27+
int y = 10;
28+
29+
std::cout << x << y;
2630
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
void variables04()
3+
{
4+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
int x;
2-
31
void variables04()
42
{
53
}

exercises/02_functions/01_functions.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//
2-
//
3-
//
41
// https://www.learncpp.com/cpp-tutorial/introduction-to-functions/
52
// https://www.learncpp.com/cpp-tutorial/function-return-values-value-returning-functions/
63

@@ -11,6 +8,8 @@ int function01(int x)
118
{
129
int y{x + 1};
1310

11+
std::cout << "x: " << x << " " << "y: " << y << "\n";
12+
1413
return y;
1514
}
1615

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO: lambdas
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
3+
int max(int a, int b)
4+
{
5+
if (a > b)
6+
return a;
7+
else
8+
return b;
9+
}
10+
11+
void controlFlow01()
12+
{
13+
std::cout << max(3, 7) << "\n"; // 7
14+
std::cout << max(10, 4) << "\n"; // 10
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
std::string weekday(int day)
5+
{
6+
switch (day)
7+
{
8+
case 1:
9+
return "Monday";
10+
case 2:
11+
return "Tuesday";
12+
case 3:
13+
return "Wednesday";
14+
case 4:
15+
return "Thursday";
16+
case 5:
17+
return "Friday";
18+
case 6:
19+
return "Saturday";
20+
case 7:
21+
return "Sunday";
22+
default:
23+
return "Invalid";
24+
}
25+
}
26+
27+
void controlFlow02()
28+
{
29+
std::cout << weekday(1) << "\n";
30+
std::cout << weekday(5) << "\n";
31+
std::cout << weekday(9) << "\n";
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <iostream>
2+
3+
void controlFlow03()
4+
{
5+
for (int i = 1; i <= 5; ++i)
6+
std::cout << i << " ";
7+
std::cout << "\n";
8+
9+
int n = 5;
10+
while (n--)
11+
std::cout << "tick ";
12+
std::cout << "\n";
13+
}

0 commit comments

Comments
 (0)