Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #22 from lycantropos/fix-floats-tests
Browse files Browse the repository at this point in the history
`Floats` tests
  • Loading branch information
lycantropos committed Oct 30, 2017
2 parents 40fa5cd + a2d1d56 commit d3305ac
Show file tree
Hide file tree
Showing 41 changed files with 1,126 additions and 598 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ set(TESTS_SOURCE_FILES
tests/test_strings.cpp
tests/vectors_tests/fundamental/test_booleans.cpp
tests/vectors_tests/fundamental/test_integers.cpp
tests/vectors_tests/fundamental/test_floats.cpp
tests/vectors_tests/fundamental/test_characters.cpp
tests/vectors_tests/compound/test_strings.cpp
tests/sets_tests/fundamental/test_booleans.cpp
tests/sets_tests/fundamental/test_integers.cpp
tests/sets_tests/fundamental/test_floats.cpp
tests/sets_tests/fundamental/test_characters.cpp
tests/sets_tests/compound/test_strings.cpp
tests/builder_tests/fundamental/test_booleans.cpp
tests/builder_tests/fundamental/test_integers.cpp
tests/builder_tests/fundamental/test_floats.cpp
tests/builder_tests/fundamental/test_characters.cpp
tests/builder_tests/compound/test_strings.cpp)
add_executable(main
Expand Down
2 changes: 1 addition & 1 deletion cauldron/bases.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "facility.h"


namespace strategies {
namespace cauldron {
template<typename Value>
class Filtered;

Expand Down
2 changes: 1 addition & 1 deletion cauldron/booleans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "booleans.h"


namespace strategies {
namespace cauldron {
Booleans::Booleans(double probability)
: probability_(probability) {}

Expand Down
2 changes: 1 addition & 1 deletion cauldron/booleans.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates pseudo-random ``bool`` values
* with defined probability.
Expand Down
10 changes: 5 additions & 5 deletions cauldron/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates ``Object`` instances
* with constructor arguments generated from corresponding strategies.
Expand All @@ -14,7 +14,7 @@ namespace strategies {
template<class Object, class ...Value>
class Builder : public CloneHelper<Object, Builder<Object, Value...>> {
public:
explicit Builder(std::shared_ptr<strategies::Strategy<Value>>... strategy) :
explicit Builder(std::shared_ptr<cauldron::Strategy<Value>>... strategy) :
strategies_(std::make_tuple(strategy...)) {}

/**
Expand All @@ -27,15 +27,15 @@ class Builder : public CloneHelper<Object, Builder<Object, Value...>> {
}

private:
std::tuple<std::shared_ptr<strategies::Strategy<Value>>...> strategies_;
std::tuple<std::shared_ptr<cauldron::Strategy<Value>>...> strategies_;

/**
* Helper function for unpacking ``Builder::strategies_`` tuple
* into variadic ``Strategy`` instances.
*/
template<std::size_t... Indices>
Object produce(
std::tuple<std::shared_ptr<strategies::Strategy<Value>>...> tuple,
std::tuple<std::shared_ptr<cauldron::Strategy<Value>>...> tuple,
std::index_sequence<Indices...>
) const {
return produce(std::get<Indices>(tuple)...);
Expand All @@ -46,7 +46,7 @@ class Builder : public CloneHelper<Object, Builder<Object, Value...>> {
* from variadic ``Strategy`` instances.
*/
Object produce(
std::shared_ptr<strategies::Strategy<Value>>... strategy
std::shared_ptr<cauldron::Strategy<Value>>... strategy
) const {
return Object(strategy->operator()()...);
}
Expand Down
9 changes: 3 additions & 6 deletions cauldron/characters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "characters.h"


namespace strategies {
namespace cauldron {
void validate_characters(const std::string &characters) {
if (characters.length() == 0) {
throw std::invalid_argument("Characters container should not be empty.");
Expand All @@ -25,13 +25,10 @@ Characters::Characters(const char domain[]) {


char Characters::operator()() const {
if (domain_.length() == 1) {
return domain_[0];
}
static std::random_device random_device;
size_t max_index = domain_.length() - 1;
auto distribution = std::uniform_int_distribution<size_t>(0, max_index);
auto index = distribution(random_device);
std::uniform_int_distribution<size_t> distribution(0, max_index);
size_t index = distribution(random_device);
return domain_[index];
}
}
2 changes: 1 addition & 1 deletion cauldron/characters.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates pseudo-random ``char`` values.
*/
Expand Down
2 changes: 1 addition & 1 deletion cauldron/facility.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <memory>


namespace strategies {
namespace cauldron {
template<typename Product>
using Converter = std::function<Product(Product)>;

Expand Down
2 changes: 1 addition & 1 deletion cauldron/floats.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
template<typename Value>
void validate_floats(Value min_value,
Value max_value) {
Expand Down
10 changes: 4 additions & 6 deletions cauldron/integers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates pseudo-random integer values.
* @tparam Value: type of values generated by strategy.
Expand All @@ -14,7 +14,7 @@ template<typename Value>
class Integers : public CloneHelper<Value, Integers<Value>> {
static_assert(std::is_integral<Value>(),
"``Value`` should be integral type.");
static_assert(not std::is_same<Value, bool>(),
static_assert(!std::is_same<Value, bool>(),
"``Value`` should not be ``bool`` type, "
"use ``strategies::Booleans`` instead.");
public:
Expand All @@ -32,10 +32,8 @@ class Integers : public CloneHelper<Value, Integers<Value>> {
*/
Value operator()() const override {
static std::random_device random_device;
auto distribution = std::uniform_int_distribution<Value>(min_value_,
max_value_);
auto result = distribution(random_device);
return result;
std::uniform_int_distribution<Value> distribution(min_value_, max_value_);
return distribution(random_device);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion cauldron/just.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates the same value.
* @tparam Value: type of value generated by strategy.
Expand Down
2 changes: 1 addition & 1 deletion cauldron/sets.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates ``std::set`` instances
* with sizes and elements generated from corresponding strategies.
Expand Down
7 changes: 4 additions & 3 deletions cauldron/sieve.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include <functional>
#include <string>
#include <vector>


namespace strategies {
namespace cauldron {
static const unsigned MAX_CYCLES = 1'000;

template<typename Product>
Expand Down Expand Up @@ -46,7 +47,7 @@ class Sieve {
bool satisfactory(Product product) const {
for (const auto &requirement: requirements_) {
bool satisfies_requirement = requirement(product);
if (not satisfies_requirement) {
if (!satisfies_requirement) {
return false;
}
}
Expand All @@ -63,7 +64,7 @@ class Sieve {
Product sift(std::function<Product()> producer) const {
for (unsigned _ = 0; _ < max_cycles_; ++_) {
Product product = producer();
if (not satisfactory(product)) {
if (!satisfactory(product)) {
continue;
}
return product;
Expand Down
2 changes: 1 addition & 1 deletion cauldron/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "strings.h"


namespace strategies {
namespace cauldron {
Strings::Strings(std::shared_ptr<Strategy<size_t>> lengths,
std::shared_ptr<Strategy<char>> alphabet) :
lengths_(std::move(lengths)),
Expand Down
2 changes: 1 addition & 1 deletion cauldron/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates ``std::string`` instances
* with lengths and characters generated from corresponding strategies.
Expand Down
2 changes: 1 addition & 1 deletion cauldron/vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "bases.h"


namespace strategies {
namespace cauldron {
/**
* Strategy which generates ``std::vector`` instances
* with sizes and elements generated from corresponding strategies.
Expand Down
66 changes: 33 additions & 33 deletions tests/builder_tests/compound/test_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@
TEST_CASE("strings \"Builder\" strategy", "[Builder]") {
using StringWrapper = Wrapper<std::string>;

auto is_lower_wrapper =
cauldron::Requirement<StringWrapper> is_lower_wrapper(
[&](const StringWrapper &wrapper) -> bool {
return is_lower_string(wrapper.field());
};
auto is_upper_wrapper =
});
cauldron::Requirement<StringWrapper> is_upper_wrapper(
[&](const StringWrapper &wrapper) -> bool {
return is_upper_string(wrapper.field());
};
});

size_t max_length = constants::max_capacity;

std::string non_zero_characters = factories::non_zero_characters();

SECTION("single character alphabet") {
auto ones = std::make_shared<strategies::Just<size_t>>(1);
auto ones = std::make_shared<cauldron::Just<size_t>>(1);
for (char single_character: non_zero_characters) {
std::string single_character_string{single_character};
auto same_character = std::make_shared<strategies::Characters>(
auto same_character = std::make_shared<cauldron::Characters>(
single_character_string);
auto same_character_strings = std::make_shared<strategies::Strings>(
auto same_character_strings = std::make_shared<cauldron::Strings>(
ones,
same_character);
strategies::Builder<StringWrapper, std::string>
cauldron::Builder<StringWrapper, std::string>
same_character_strings_wrappers(same_character_strings);

auto wrapper = same_character_strings_wrappers();
Expand All @@ -47,16 +47,16 @@ TEST_CASE("strings \"Builder\" strategy", "[Builder]") {

SECTION("multiple characters alphabet") {
size_t min_length = 0;
auto lengths = std::make_shared<strategies::Integers<size_t>>(min_length,
max_length);
auto lengths = std::make_shared<cauldron::Integers<size_t>>(min_length,
max_length);
std::string alphabet_characters = factories::characters_string(
constants::min_capacity,
constants::max_capacity);
auto alphabet = std::make_shared<strategies::Characters>(
auto alphabet = std::make_shared<cauldron::Characters>(
alphabet_characters);
auto strings = std::make_shared<strategies::Strings>(lengths,
alphabet);
strategies::Builder<StringWrapper, std::string> strings_wrappers(strings);
auto strings = std::make_shared<cauldron::Strings>(lengths,
alphabet);
cauldron::Builder<StringWrapper, std::string> strings_wrappers(strings);

auto string_wrapper = strings_wrappers();
auto length_stays_in_range = in_range_checker<size_t>(min_length,
Expand All @@ -75,17 +75,17 @@ TEST_CASE("strings \"Builder\" strategy", "[Builder]") {
size_t min_size = constants::min_capacity;
size_t max_size = min_size + 1;
size_t min_length = constants::min_capacity;
auto sizes = std::make_shared<strategies::Integers<size_t>>(min_size,
max_size);
auto lengths = std::make_shared<strategies::Integers<size_t>>(min_length,
max_length);
auto sizes = std::make_shared<cauldron::Integers<size_t>>(min_size,
max_size);
auto lengths = std::make_shared<cauldron::Integers<size_t>>(min_length,
max_length);

auto alphabetic_characters =
strategies::Characters(non_zero_characters).filter(is_alphabetic);
auto alphabetic_strings = std::make_shared<strategies::Strings>(
cauldron::Characters(non_zero_characters).filter(is_alphabetic);
auto alphabetic_strings = std::make_shared<cauldron::Strings>(
lengths,
std::move(alphabetic_characters));
strategies::Builder<StringWrapper, std::string> alphabetic(
cauldron::Builder<StringWrapper, std::string> alphabetic(
alphabetic_strings);

SECTION("case") {
Expand All @@ -110,16 +110,16 @@ TEST_CASE("strings \"Builder\" strategy", "[Builder]") {
alphabetic.filter(is_lower_wrapper)->filter(is_upper_wrapper);

REQUIRE_THROWS_AS((*invalid_wrappers)(),
strategies::OutOfCycles);
cauldron::OutOfCycles);
}
}

SECTION("mapping") {
strategies::Converter<StringWrapper> to_lower_wrapper(
cauldron::Converter<StringWrapper> to_lower_wrapper(
[&](const StringWrapper &wrapper) -> StringWrapper {
return StringWrapper(to_lower_string(wrapper.field()));
});
strategies::Converter<StringWrapper> to_upper_wrapper(
cauldron::Converter<StringWrapper> to_upper_wrapper(
[&](const StringWrapper &wrapper) -> StringWrapper {
return StringWrapper(to_upper_string(wrapper.field()));
});
Expand All @@ -131,17 +131,17 @@ TEST_CASE("strings \"Builder\" strategy", "[Builder]") {
size_t min_size = constants::min_capacity;
size_t max_size = min_size + 1;
size_t min_length = constants::min_capacity;
auto sizes = std::make_shared<strategies::Integers<size_t>>(min_size,
max_size);
auto lengths = std::make_shared<strategies::Integers<size_t>>(min_length,
max_length);
auto sizes = std::make_shared<cauldron::Integers<size_t>>(min_size,
max_size);
auto lengths = std::make_shared<cauldron::Integers<size_t>>(min_length,
max_length);

auto alphabetic_characters =
strategies::Characters(non_zero_characters).filter(is_alphabetic);
auto alphabetic_strings = std::make_shared<strategies::Strings>(
cauldron::Characters(non_zero_characters).filter(is_alphabetic);
auto alphabetic_strings = std::make_shared<cauldron::Strings>(
lengths,
std::move(alphabetic_characters));
strategies::Builder<StringWrapper, std::string> alphabetic(
cauldron::Builder<StringWrapper, std::string> alphabetic(
alphabetic_strings);

SECTION("case") {
Expand All @@ -168,9 +168,9 @@ TEST_CASE("strings \"Builder\" strategy", "[Builder]") {
alphabetic.map(to_lower_wrapper)->filter(is_upper_wrapper);

REQUIRE_THROWS_AS((*invalid_lower_wrappers)(),
strategies::OutOfCycles);
cauldron::OutOfCycles);
REQUIRE_THROWS_AS((*invalid_upper_wrappers)(),
strategies::OutOfCycles);
cauldron::OutOfCycles);
}
}
}

0 comments on commit d3305ac

Please sign in to comment.