Skip to content

Commit

Permalink
expansion test and options move constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeathlyCow committed Sep 18, 2023
1 parent 38ec4ea commit be07efb
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/include/string_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace calyx
using String_t = std::string;

/**
* @brief The constructor for the default string converter for `String_t`s to be used by Calyx. If the String_t type is redefined
* @brief The type macro for the default string converter for `String_t`s to be used by Calyx. If the String_t type is redefined
* (e.g. to UE's FString), then the constructor this is referencing will also need to be changed.
*
* This is the constructor for the default string converter passed as an argument to any methods of Calyx that require
* This is the type for the default string converter passed as an argument to any methods of Calyx that require
* reading/modifying strings.
*/
#define DEFAULT_STRING_CONVERTER() StdStringConverter()
#define DEFAULT_STRING_CONVERTER StdStringConverter

/**
* @brief Converts a string-like type S into a std::string
Expand Down
24 changes: 16 additions & 8 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@ using namespace calyx;

const bool Options::DEFAULT_STRICT = false;

Options::Options(bool strict, std::unique_ptr<StringConverter<String_t>> converter)
: _strict(strict),
Options::Options(bool strict, std::unique_ptr<StringConverter> converter):
_strict(strict),
_rng(),
_converter(std::move(converter))
{
}

Options::Options(unsigned int seed, bool strict, std::unique_ptr<StringConverter<String_t>> converter)
: _strict(strict),
Options::Options(int seed, bool strict, std::unique_ptr<StringConverter> converter):
_strict(strict),
_rng(seed),
_converter(std::move(converter))
{

}

Options::Options(std::mt19937 rng, bool strict, std::unique_ptr<StringConverter<String_t>> converter)
: _strict(strict),
Options::Options(std::mt19937 rng, bool strict, std::unique_ptr<StringConverter> converter):
_strict(strict),
_rng(rng),
_converter(std::move(converter))
{
}

Options::Options(Options&& other) noexcept:
_strict(other._strict),
_rng(other._rng),
_converter(std::move(other._converter))
{
}


int
Options::randInt()
{
const std::uniform_int_distribution distribution(
std::numeric_limits<int>::min(),
std::numeric_limits<int>::min(),
std::numeric_limits<int>::max()
);

Expand Down
29 changes: 21 additions & 8 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

namespace calyx
{

class ErrorHolder;

class Options : public StringConverter<String_t>
{

public:
static const bool DEFAULT_STRICT;

Expand All @@ -25,7 +23,10 @@ namespace calyx
* @param strict Determines if the parser should throw an error when encountering an undefined key
* @param converter The string converter to use
*/
Options(bool strict = DEFAULT_STRICT, std::unique_ptr<StringConverter<String_t>> converter = std::unique_ptr<StringConverter<String_t>>(new DEFAULT_STRING_CONVERTER()));
Options(
bool strict = DEFAULT_STRICT,
std::unique_ptr<StringConverter> converter = std::make_unique<DEFAULT_STRING_CONVERTER>()
);

/**
* @brief Construct a new Options object with a specified random seed
Expand All @@ -34,7 +35,11 @@ namespace calyx
* @param strict Determines if the parser should throw an error when encountering an undefined key
* @param converter The string converter to use
*/
Options(unsigned int seed, bool strict = DEFAULT_STRICT, std::unique_ptr<StringConverter<String_t>> converter = std::unique_ptr<StringConverter<String_t>>(new DEFAULT_STRING_CONVERTER()));
Options(
int seed,
bool strict = DEFAULT_STRICT,
std::unique_ptr<StringConverter> converter = std::make_unique<DEFAULT_STRING_CONVERTER>()
);

/**
* @brief Construct a new Options object with a specific random number generator
Expand All @@ -43,12 +48,20 @@ namespace calyx
* @param strict Determines if the parser should throw an error when encountering an undefined key
* @param converter The string converter to use
*/
Options(std::mt19937 rng, bool strict = DEFAULT_STRICT, std::unique_ptr<StringConverter<String_t>> converter = std::unique_ptr<StringConverter<String_t>>(new DEFAULT_STRING_CONVERTER()));
Options(
std::mt19937 rng,
bool strict = DEFAULT_STRICT,
std::unique_ptr<StringConverter> converter = std::make_unique<DEFAULT_STRING_CONVERTER>()
);

Options(const Options& old) = delete;
Options(Options&& other) noexcept;

~Options() override = default;

Options(const Options& old) = delete;

Options operator=(const Options& other) = delete;

/**
* @brief Generates a random number
*
Expand Down Expand Up @@ -143,6 +156,6 @@ namespace calyx
private:
bool _strict;
std::mt19937 _rng;
std::unique_ptr<StringConverter<String_t>> _converter;
std::unique_ptr<StringConverter> _converter;
};
}
}
2 changes: 1 addition & 1 deletion src/syntax/atom_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using namespace calyx;

AtomNode::AtomNode(String_t atom)
: _atom(atom)
: _atom(std::move(atom))
{

}
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/atom_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace calyx

private:

const String_t _atom;
String_t _atom;

};
}
2 changes: 1 addition & 1 deletion test/cycle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEST_CASE("Cycle length 1 always returns 0th index")
auto ops = std::shared_ptr<calyx::Options>();
auto errs = calyx::ErrorHolder();
std::optional<calyx::Cycle> cycle = calyx::Cycle::create(ops, 1, errs);

REQUIRE(cycle.has_value());
REQUIRE_FALSE(errs.hasError());
REQUIRE(0 == cycle->poll());
Expand Down
28 changes: 27 additions & 1 deletion test/expansion_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,37 @@
#include <include/calyx.h>
#include <expansion.h>
#include <vector>
#include <memory>
#include <options.h>

using namespace calyx;

TEST_CASE("Construct expansion terminal test")
{
const Options ops;
const String_t term = ops.fromString("T E R M");
const auto exp = Expansion(Exp::ATOM, term);

REQUIRE(exp.getTerm() == term);
}

TEST_CASE("Construct nested expansion test")
{
const std::vector tail {
Expansion(Exp::ATOM, "-TAHI-"),
Expansion(Exp::ATOM, "-RUA-"),
Expansion(Exp::ATOM, "-TORU-"),
};
const Expansion exp(Exp::TEMPLATE, tail);

REQUIRE(exp.getSymbol() == Exp::TEMPLATE);
REQUIRE(exp.getTail()[0].getSymbol() == Exp::ATOM);
REQUIRE(exp.getTail()[0].getTerm() == "-TAHI-");
REQUIRE(exp.getTail()[1].getSymbol() == Exp::ATOM);
REQUIRE(exp.getTail()[1].getTerm() == "-RUA-");
REQUIRE(exp.getTail()[2].getSymbol() == Exp::ATOM);
REQUIRE(exp.getTail()[2].getTerm() == "-TORU-");
}

TEST_CASE("Flatten expansion to atoms")
{

Expand Down
28 changes: 28 additions & 0 deletions test/options_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <catch2/catch_all.hpp>

#include <include/calyx.h>
#include <options.h>

using namespace calyx;

TEST_CASE("Strict off by default")
{
const Options ops;
REQUIRE_FALSE(ops.isStrict());
}

TEST_CASE("Strict on with flag set")
{
const Options ops(true);
REQUIRE(ops.isStrict());
}

TEST_CASE("Seeded rng is deterministic")
{
int seed = 123;
Options ops(seed);
Options other(seed);

REQUIRE(ops.randInt() == other.randInt());
}

2 changes: 1 addition & 1 deletion test/result_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST_CASE("Flattens expression tree to string")
{
Expansion tripleAtomTree = Expansion(
Exp::TEMPLATE,
std::vector<Expansion> {
std::vector {
Expansion(Exp::ATOM, "O N E"),
Expansion(Exp::ATOM, " | "),
Expansion(Exp::ATOM, "T W O")
Expand Down
1 change: 1 addition & 0 deletions vsxmake2019/test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ xmake version 2.7.8+master.d6ee79eed
<ClCompile Include="$(XmakeProjectDir)\test\syntax\template_test.cpp">
<CompileAs>CompileAsCpp</CompileAs>
</ClCompile>
<ClCompile Include="..\..\test\options_test.cpp" />



Expand Down

0 comments on commit be07efb

Please sign in to comment.