Skip to content

Commit

Permalink
Merge pull request #10 from NMannall/refresh-material-oct-24
Browse files Browse the repository at this point in the history
Split containers exercise into part1 and part2 directories
  • Loading branch information
NMannall authored Oct 28, 2024
2 parents da11177 + 86a419f commit 319e59b
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
10 changes: 10 additions & 0 deletions exercises/containers/part1/Makefile
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
56 changes: 56 additions & 0 deletions exercises/containers/part1/test.cpp
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.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXXFLAGS = --std=c++17 -I../include
CXXFLAGS = --std=c++17 -I../../include

test : vector_ex.o map_ex.o test.o
$(CXX) $^ -o $@
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions exercises/containers/part2/vector_ex.cpp
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) {
}

18 changes: 18 additions & 0 deletions exercises/containers/part2/vector_ex.hpp
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

0 comments on commit 319e59b

Please sign in to comment.