Skip to content

Commit

Permalink
made range v3 test self-contained, fixes #1
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Nov 1, 2023
1 parent eb6622e commit fb2c7ec
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ jobs:
# See George's systems-code-examples for how to run unit tests
# At the moment, these are all standalone programs
working-directory: ${{github.workspace}}/build
run: |
ln -s ../data
run: >
for exe in bin/*; do
echo "Running ./$exe"
"./$exe"
Expand Down
14 changes: 0 additions & 14 deletions data/day1example.txt

This file was deleted.

9 changes: 6 additions & 3 deletions modern-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ foreach(APP_NAME IN LISTS MY_APP_NAMES)
add_my_executable(${APP_NAME})
endforeach()

# add_executable(rangev3-aoc2022day1 rangev3-aoc2022day1.cpp)
# target_link_libraries(rangev3-aoc2022day1 range-v3 gtest_main)

enable_testing()

add_executable(rangev3-aoc2022day1 rangev3-aoc2022day1.cpp)
target_link_libraries(rangev3-aoc2022day1 range-v3 gtest_main)
add_executable(rangev3-aoc2022day1_test rangev3-aoc2022day1_test.cpp rangev3-aoc2022day1.cpp)
target_link_libraries(rangev3-aoc2022day1_test range-v3 gtest_main)
include(GoogleTest)
gtest_discover_tests(rangev3-aoc2022day1)
gtest_discover_tests(rangev3-aoc2022day1_test)
43 changes: 24 additions & 19 deletions modern-cpp/rangev3-aoc2022day1.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "rangev3-aoc2022day1.hpp"

#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include <range/v3/view.hpp>
Expand All @@ -11,19 +14,23 @@

#include <gtest/gtest.h>

std::pair<int, int> most_nutritious_inventories(std::string_view const filename) {
using namespace ranges;
// std::pair<int, int> most_nutritious_inventories(std::string_view const filename) {
// using namespace ranges;

auto line_to_int = [](auto const& s) { return s.empty() ? 0 : std::stoi(s); };
// auto line_to_int = [](auto const& s) { return s.empty() ? 0 : std::stoi(s); };

// read lines into vector to support chunk_by, which requires a forward range
std::ifstream input{filename.data()};
std::vector<int> data;
copy(
getlines(input) | views::transform(line_to_int),
back_inserter(data)
);
// // read lines into vector to support chunk_by, which requires a forward range
// std::ifstream input{filename.data()};
// std::vector<int> data;
// copy(
// getlines(input) | views::transform(line_to_int),
// back_inserter(data)
// );
// }

using namespace ranges;

std::pair<int, int> most_nutritious_inventories(std::vector<int> data) {
auto has_no_zeroes = [](auto const& xs) {
return none_of(xs, [](auto const x) { return x == 0; });
};
Expand All @@ -41,21 +48,19 @@ std::pair<int, int> most_nutritious_inventories(std::string_view const filename)
auto constexpr n = 3;
std::vector<int> top_n;

// retrieve top n inventory values
for_each(views::ints(0, n), [&](auto i) {
top_n.push_back(front(inventories));
pop_heap(inventories);
inventories.pop_back();
});

// return top and sum of top n inventory values
return std::make_pair(front(top_n), accumulate(top_n, 0));
}

TEST(AOC2022Day1, Example) {
EXPECT_EQ(most_nutritious_inventories("data/day1example.txt"), std::pair(24000, 45000));
}

TEST(AOC2022Day1, RealData) {
auto const result = most_nutritious_inventories("data/day1input.txt");
std::cout << "Day 1 part 1 (max) = " << result.first << "\n";
std::cout << "Day 1 part 2 (sum) = " << result.second << "\n";
}
// TEST(AOC2022Day1, RealData) {
// auto const result = most_nutritious_inventories("data/day1input.txt");
// std::cout << "Day 1 part 1 (max) = " << result.first << "\n";
// std::cout << "Day 1 part 2 (sum) = " << result.second << "\n";
// }
9 changes: 9 additions & 0 deletions modern-cpp/rangev3-aoc2022day1.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef RANGEV3_AOC2022DAY1_H
#define RANGEV3_AOC2022DAY1_H

#include <utility>
#include <vector>

std::pair<int, int> most_nutritious_inventories(std::vector<int> data);

#endif // RANGEV3_AOC2022DAY1_H
25 changes: 25 additions & 0 deletions modern-cpp/rangev3-aoc2022day1_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "rangev3-aoc2022day1.hpp"

#include <gtest/gtest.h>

TEST(AOC2022Day1, Example) {
std::vector data{
1000,
2000,
3000,
0,
4000,
0,
5000,
6000,
0,
7000,
8000,
9000,
0,
10000
};
std::pair result{24000, 45000};

EXPECT_EQ(most_nutritious_inventories(data), result);
}

0 comments on commit fb2c7ec

Please sign in to comment.