Skip to content

Commit

Permalink
Merge pull request #13 from jay-tux/fix/misc
Browse files Browse the repository at this point in the history
Fix/misc
  • Loading branch information
jay-tux authored Feb 28, 2022
2 parents 3b07e7f + 1950f8c commit c3e46f3
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 202 deletions.
2 changes: 1 addition & 1 deletion Doxyfile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,7 @@ MAN_LINKS = NO
# captures the structure of the code including all documentation.
# The default value is: NO.

GENERATE_XML = NO
GENERATE_XML = YES

# The XML_OUTPUT tag is used to specify where the XML pages will be put. If a
# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
Expand Down
1 change: 0 additions & 1 deletion inc/fpgen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
#include "generator.hpp"
#include "manipulators.hpp"
#include "sources.hpp"
#include "stream.hpp"

#endif
8 changes: 4 additions & 4 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DEPS=$(SOURCES:$(SRCD)/%.cpp=$(OBJD)/%.d)
TESTS=generator sources manip aggreg chain
TESTOBJ=$(TESTS:%=$(OBJD)/test_%.o)

CONAN_PKG_OVERRIDE=gtest
CONAN_PKG_OVERRIDE=
CONAN_MODIFY=$(CONAN_PKG_OVERRIDE:%=-s %:compiler.version=11.2)

all: dirs conan/conanbuildinfo.mak $(BIND)/_test
Expand All @@ -18,19 +18,19 @@ dirs:
@([ ! -d $(OBJD)/sources ] && mkdir -p $(OBJD)/sources) || true

$(BIND)/_test: $(TESTOBJ) $(OBJD)/test_main.o
$(CC) $(LDXTRA) $(LDARGS) $^ -o $@
$(CC) $(LDARGS) $(LDXTRA) $^ -o $@
$(BIND)/_test

$(OBJD)/%.o: $(SRCD)/%.cpp Makefile
$(CC) $(CXXARGS) $(CXXXTRA) $< -o $@

conan/conanbuildinfo.mak: conanfile.txt
@([ ! -d conan/ ] && mkdir -p conan/) || true
cd conan && CC=gcc CXX=$(CC) conan install .. $(CONAN_MODIFY) --build=gtest
cd conan && CC=gcc CXX=$(CC) conan install .. $(CONAN_MODIFY)

clean:
find ./bin/ -type f | grep -v '.gitkeep' | xargs rm -rf
find ./obj/ -type f | grep -v '.gitkeep' | xargs rm -rf
find ./conan/ -type f | grep -v '.gitkeep' | xargs rm -rf

.PHONY: all dirs clean $(TESTS:%=$(BIND)/%)
.PHONY: all dirs clean $(TESTS:%=$(BIND)/%) $(BIND)/_test
2 changes: 1 addition & 1 deletion test/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[requires]
gtest/cci.20210126
doctest/2.4.8

[generators]
make
131 changes: 62 additions & 69 deletions test/src/test_aggreg.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "aggregators.hpp"
#include "doctest/doctest.h"
#include "generator.hpp"
#include "manipulators.hpp"
#include "sources.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <map>
#include <sstream>
Expand Down Expand Up @@ -46,165 +45,159 @@ size_t sum_ref(size_t &old, size_t in) {
return old;
}

TEST(aggregate, empty) {
TEST_CASE("Aggregate empty generator") {
auto gen = a_empty();
// gen();
std::vector<size_t> res;
EXPECT_EQ(0, fpgen::aggregate_to(gen, res).size());
CHECK(0 == fpgen::aggregate_to(gen, res).size());
}

TEST(aggregate, vector) {
TEST_CASE("Aggregate to std::vector") {
auto gen = values();
std::vector<size_t> res;
fpgen::aggregate_to(gen, res);

EXPECT_EQ(0, res[0]);
EXPECT_EQ(1, res[1]);
EXPECT_EQ(1, res[2]);
EXPECT_EQ(2, res[3]);
EXPECT_EQ(3, res[4]);
EXPECT_EQ(5, res[5]);
EXPECT_EQ(8, res[6]);
EXPECT_EQ(13, res[7]);
EXPECT_EQ(21, res[8]);
EXPECT_EQ(34, res[9]);
EXPECT_EQ(res.size(), 10);
CHECK(0 == res[0]);
CHECK(1 == res[1]);
CHECK(1 == res[2]);
CHECK(2 == res[3]);
CHECK(3 == res[4]);
CHECK(5 == res[5]);
CHECK(8 == res[6]);
CHECK(13 == res[7]);
CHECK(21 == res[8]);
CHECK(34 == res[9]);
CHECK(res.size() == 10);
}

TEST(aggregate, vec_to_vec) {
TEST_CASE("Aggregate: std::vector to generator to std::vector") {
std::vector<size_t> in = {0, 1, 2, 3, 4, 5, 6};
std::vector<size_t> out = {};
auto gen = fpgen::from(in);
out = fpgen::aggregate_to(gen, out);
EXPECT_EQ(in, out);
CHECK(in == out);
}

TEST(aggregate, map) {
TEST_CASE("Aggregate to std::map") {
fpgen::generator<size_t> sources[2] = {values(), values()};
auto gen = fpgen::zip(sources[0], sources[1]);
std::map<size_t, size_t> res;
fpgen::tup_aggregate_to(gen, res);

EXPECT_EQ(0, res[0]);
EXPECT_EQ(1, res[1]);
EXPECT_EQ(2, res[2]);
EXPECT_EQ(3, res[3]);
EXPECT_EQ(5, res[5]);
EXPECT_EQ(8, res[8]);
EXPECT_EQ(13, res[13]);
EXPECT_EQ(21, res[21]);
EXPECT_EQ(34, res[34]);
EXPECT_EQ(res.size(), 9);
CHECK(0 == res[0]);
CHECK(1 == res[1]);
CHECK(2 == res[2]);
CHECK(3 == res[3]);
CHECK(5 == res[5]);
CHECK(8 == res[8]);
CHECK(13 == res[13]);
CHECK(21 == res[21]);
CHECK(34 == res[34]);
CHECK(res.size() == 9);
}

TEST(aggregate, count_empty) {
TEST_CASE("Count empty generator") {
auto gen = a_empty();
// gen();
EXPECT_EQ(0, fpgen::count(gen));
CHECK(0 == fpgen::count(gen));
}

TEST(aggregate, count) {
TEST_CASE("Count generator") {
auto gen = values();
EXPECT_EQ(10, fpgen::count(gen));
CHECK(10 == fpgen::count(gen));
}

TEST(fold, fold_noin_empty) {
TEST_CASE("Fold [using no-input, empty generator]") {
auto gen = a_empty();
// gen();
EXPECT_EQ(0, fpgen::fold<size_t>(gen, sum));
CHECK(0 == fpgen::fold<size_t>(gen, sum));
}

TEST(fold, fold_noin) {
TEST_CASE("Fold [using no-input, non-empty generator]") {
auto gen = values();
EXPECT_EQ(calc_sum(), fpgen::fold<size_t>(gen, sum));
CHECK(calc_sum() == fpgen::fold<size_t>(gen, sum));
}

TEST(fold, fold_in_noref_empty) {
TEST_CASE("Fold [using input, empty generator]") {
auto gen = a_empty();
// gen();
EXPECT_EQ(7, fpgen::fold<size_t>(gen, sum, 7));
CHECK(7 == fpgen::fold<size_t>(gen, sum, 7));
}

TEST(fold, fold_in_noref) {
TEST_CASE("Fold [using input, non-empty generator]") {
auto gen = values();
EXPECT_EQ(calc_sum() + 7, fpgen::fold<size_t>(gen, sum, 7));
CHECK(calc_sum() + 7 == fpgen::fold<size_t>(gen, sum, 7));
}

TEST(fold, fold_in_ref_empty) {
TEST_CASE("Fold [using ref input, empty generator]") {
auto gen = a_empty();
// gen();
size_t res = 7;
EXPECT_EQ(7, fpgen::fold_ref<size_t>(gen, sum, res));
EXPECT_EQ(7, res);
CHECK(7 == fpgen::fold_ref<size_t>(gen, sum, res));
CHECK(7 == res);
}

TEST(fold, fold_in_ref) {
TEST_CASE("Fold [using ref input, non-epty generator]") {
auto gen = values();
size_t res = 7;
EXPECT_EQ(calc_sum() + 7, fpgen::fold_ref<size_t>(gen, sum, res));
EXPECT_EQ(calc_sum() + 7, res);
CHECK(calc_sum() + 7 == fpgen::fold_ref<size_t>(gen, sum, res));
CHECK(calc_sum() + 7 == res);
}

TEST(sum, empty) {
TEST_CASE("Sum empty generator") {
auto gen = a_empty();
// gen();
EXPECT_EQ(0, fpgen::sum(gen));
CHECK(0 == fpgen::sum(gen));
}

TEST(sum, normal) {
TEST_CASE("Sum over generator") {
auto gen = values();
EXPECT_EQ(calc_sum(), fpgen::sum(gen));
CHECK(calc_sum() == fpgen::sum(gen));
}

TEST(foreach, empty) {
TEST_CASE("Foreach over empty generator") {
auto gen = a_empty();
// gen();
size_t res = 0;
fpgen::foreach (gen, [&res](size_t val) { res += val; });
EXPECT_EQ(res, 0);
CHECK(res == 0);
}

TEST(foreach, normal) {
TEST_CASE("Foreach over non-empty generator") {
auto gen = values();
auto gen2 = values();
size_t res = 0;
fpgen::foreach (gen, [&res](size_t val) { res += val; });
EXPECT_EQ(res, fpgen::sum(gen2));
CHECK(res == fpgen::sum(gen2));
}

TEST(stream, nosep) {
TEST_CASE("Output to stream, no separator") {
std::vector<int> vals = {1, 2, 3, 4, 5, 6};
auto gen = fpgen::from(vals);
std::stringstream strm;
fpgen::to_stream(gen, strm);
EXPECT_EQ(strm.str(), "123456");
CHECK(strm.str() == "123456");
}

TEST(stream, sep) {
TEST_CASE("Output to stream, with separator") {
std::vector<int> vals = {1, 2, 3, 4, 5, 6, 7};
auto gen = fpgen::from(vals);
std::stringstream strm;
fpgen::to_stream(gen, strm, " ");
EXPECT_EQ(strm.str(), "1 2 3 4 5 6 7");
CHECK(strm.str() == "1 2 3 4 5 6 7");
}

TEST(stream, lines_trail) {
TEST_CASE("Output to stream (by lines), with trailing line") {
std::vector<int> vals = {1, 2, 3, 4};
auto gen = fpgen::from(vals);
std::stringstream strm;
std::stringstream expect;
for (auto v : vals)
expect << v << std::endl;
fpgen::to_lines(gen, strm);
EXPECT_EQ(strm.str(), expect.str());
CHECK(strm.str() == expect.str());
}

TEST(stream, lines_no_trail) {
TEST_CASE("Output to stream (by lines), without trailing line") {
std::vector<int> vals = {1, 2, 3, 4};
auto gen = fpgen::from(vals);
std::stringstream strm;
std::stringstream expect;
expect << 1 << std::endl << 2 << std::endl << 3 << std::endl << 4;
fpgen::to_lines_no_trail(gen, strm);
EXPECT_EQ(strm.str(), expect.str());
CHECK(strm.str() == expect.str());
}
13 changes: 6 additions & 7 deletions test/src/test_chain.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "aggregators.hpp"
#include "doctest/doctest.h"
#include "generator.hpp"
#include "manipulators.hpp"
#include "sources.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace fpgen;

Expand All @@ -12,7 +11,7 @@ std::ostream &operator<<(std::ostream &in, std::tuple<T1, T2> &other) {
return in << "{ " << std::get<0>(other) << ", " << std::get<1>(other) << " }";
}

TEST(chain, simple_chain) {
TEST_CASE("Simple chain check") {
/*
Chain:
-> [0..] -> [5..] -> [5..13] -> [15...169] \
Expand All @@ -27,10 +26,10 @@ TEST(chain, simple_chain) {

size_t value = 5;
for (auto v : zip(gen, second)) {
EXPECT_EQ(value * value, std::get<0>(v));
EXPECT_EQ('a' + 2 + value, std::get<1>(v));
EXPECT_TRUE(value <= 13);
CHECK(value * value == std::get<0>(v));
CHECK('a' + 2 + value == std::get<1>(v));
CHECK(value <= 13);
value++;
}
EXPECT_EQ(value, 14);
CHECK(value == 14);
}
34 changes: 15 additions & 19 deletions test/src/test_generator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "doctest/doctest.h"
#include "generator.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <iostream>

fpgen::generator<float> empty() { co_return; }
Expand All @@ -20,46 +19,43 @@ fpgen::generator<long> finite_squares(int min, int max) {
co_return; // max *max;
}

TEST(generator, accept_empty_gen) {
auto emptygen = empty();
SUCCEED();
}
TEST_CASE("Generator accepts empty generator") { auto emptygen = empty(); }

TEST(generator, iterator_empty_gen) {
TEST_CASE("Iterator over an empty generator") {
auto gen = empty();
// gen();
for (auto v : gen) {
FAIL();
CHECK(false); // should fail
}
SUCCEED();
}

TEST(generator, can_call_and_continue) {
TEST_CASE("Call-and-continue for a generator") {
auto intgen = infinite();
int value = intgen();
EXPECT_EQ(0, value);
CHECK(0 == value);
value = intgen();
EXPECT_EQ(1, value);
CHECK(1 == value);
}

TEST(generator, can_while_over) {
TEST_CASE("Use while over a generator") {
int value;
int expect = 0;
auto intgen2 = finite_squares(0, 12);
while (intgen2) {
value = intgen2();
EXPECT_THAT(expect, testing::AllOf(testing::Le(12), testing::Ge(0)));
EXPECT_EQ(expect * expect, value);
CHECK(expect <= 12);
CHECK(expect >= 0);
CHECK(expect * expect == value);
expect++;
}
}

TEST(generator, iterator) {
TEST_CASE("Iterate over a generator") {
int expect = -4;
auto intgen3 = finite_squares(-4, 8);
for (auto value : intgen3) {
EXPECT_THAT(expect, testing::AllOf(testing::Le(8), testing::Ge(-4)));
EXPECT_EQ(expect * expect, value);
CHECK(expect <= 8);
CHECK(expect >= -4);
CHECK(expect * expect == value);
expect++;
}
}
Loading

0 comments on commit c3e46f3

Please sign in to comment.