-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from NMannall/refresh-material-oct-24
Split containers exercise into part1 and part2 directories
- Loading branch information
Showing
8 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CXXFLAGS = --std=c++17 -I../../include | ||
|
||
test : vector_ex.o test.o | ||
$(CXX) $^ -o $@ | ||
|
||
run : test | ||
./test | ||
|
||
clean : | ||
rm -rf *.o test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Catch2 is a unit testing library | ||
// Here we let it create a main() function for us | ||
#define CATCH_CONFIG_MAIN | ||
#include "catch.hpp" | ||
|
||
#include "vector_ex.hpp" | ||
#include <sstream> | ||
|
||
// type alias | ||
using IV = std::vector<int>; | ||
|
||
IV IntRange(int n) { | ||
auto ans = IV(n); | ||
for (int i = 0; i < n; ++i) | ||
ans[i] = i; | ||
return ans; | ||
} | ||
|
||
TEST_CASE("Vector Even") { | ||
IV const empty; | ||
REQUIRE(empty == GetEven(empty)); | ||
|
||
auto const zero = IntRange(1); | ||
// Test the testing code! | ||
REQUIRE(zero.size() == 1); | ||
REQUIRE(zero[0] == 0); | ||
|
||
REQUIRE(zero == GetEven(zero)); | ||
|
||
auto const zero_one = IntRange(2); | ||
// Test the testing code! | ||
REQUIRE(zero_one.size() == 2); | ||
REQUIRE(zero_one[0] == 0); | ||
REQUIRE(zero_one[1] == 1); | ||
|
||
REQUIRE(zero == GetEven(zero_one)); | ||
|
||
// edited toddler random numbers | ||
auto const random = IV{8, 127356, 1, 29, 4376, 263478, 1537}; | ||
auto const random_even = IV{ 8, 127356, 4376, 263478}; | ||
REQUIRE(GetEven(random) == random_even); | ||
} | ||
|
||
std::string STR(IV const& v) { | ||
std::stringstream ss; | ||
PrintVectorOfInt(ss, v); | ||
return ss.str(); | ||
} | ||
|
||
TEST_CASE("Vector Print") { | ||
REQUIRE(STR(IV{}) == "[ ]"); | ||
REQUIRE(STR(IntRange(1)) == "[ 0]"); | ||
REQUIRE(STR(IntRange(2)) == "[ 0, 1]"); | ||
// edited toddler random numbers | ||
REQUIRE(STR(IV{8, 127356, 1, 29, 4376, 263478, 1537}) == "[ 8, 127356, 1, 29, 4376, 263478, 1537]"); | ||
} |
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
exercises/containers/Makefile → exercises/containers/part2/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "vector_ex.hpp" | ||
|
||
// std::vector documentation: | ||
// https://en.cppreference.com/w/cpp/container/vector | ||
|
||
std::vector<int> GetEven(std::vector<int> const& source) { | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef CPPEX_VECTOR_EX_HPP | ||
#define CPPEX_VECTOR_EX_HPP | ||
|
||
#include <vector> | ||
#include <iostream> | ||
|
||
// Here we declare two functions. | ||
// Provide implementations, in vector_ex.cpp | ||
|
||
// Given a vector of integers, return a new vector with only the even | ||
// elements from our input | ||
std::vector<int> GetEven(std::vector<int> const& source); | ||
|
||
// Given a vector of ints, print the data to the stream | ||
// [0, 1] | ||
void PrintVectorOfInt(std::ostream& output, std::vector<int> const& data); | ||
|
||
#endif |