Skip to content
Permalink
Browse files

Moves state out of Generator

  • Loading branch information
remram44 committed Feb 7, 2015
1 parent 0b0f75d commit ad831ead2d37952cc5805ffa26bdd990a446d60d
Showing with 31 additions and 39 deletions.
  1. +6 −11 insults.cpp
  2. +3 −4 insults.h
  3. +6 −11 permgen.cpp
  4. +2 −5 permgen.h
  5. +3 −2 test_insults.cpp
  6. +11 −6 test_permgen.cpp
@@ -42,7 +42,7 @@ CombinedChoosers::CombinedChoosers(std::initializer_list<Chooser> choosers)
{
}

std::string CombinedChoosers::operator()(Key &key) const
std::string CombinedChoosers::operator()(Key key) const
{
std::string result;
for(const Chooser &ch : m_Choosers)
@@ -59,8 +59,8 @@ size_t CombinedChoosers::size() const
}


Insults::Insults(Key state)
: m_Generator(state, CHOICES),
Insults::Insults()
: m_Generator(CHOICES),
m_Choosers{
Chooser{"vas-y clique "},
Chooser{"salope",
@@ -114,13 +114,8 @@ Insults::Insults(Key state)
assert(m_Choosers.size() == CHOICES);
}

std::string Insults::generate()
std::string Insults::generate(Key &state)
{
Key key = m_Generator.generate();
return m_Choosers(key);
}

Key Insults::state() const
{
return m_Generator.state();
state = m_Generator.generate(state);
return m_Choosers(state);
}
@@ -32,7 +32,7 @@ class CombinedChoosers {

public:
CombinedChoosers(std::initializer_list<Chooser> choosers);
std::string operator()(Key &key) const;
std::string operator()(Key key) const;
size_t size() const;

};
@@ -48,9 +48,8 @@ class Insults {
CombinedChoosers m_Choosers;

public:
Insults(Key state);
std::string generate();
Key state() const;
Insults();
std::string generate(Key &state);

};

@@ -5,20 +5,15 @@
constexpr Key A = 1103515245;
constexpr Key C = 12345;

Generator::Generator(Key state, Key max)
: m_State(state), m_Max(max)
Generator::Generator(Key max)
: m_Max(max)
{
}

Key Generator::generate()
Key Generator::generate(Key state)
{
do
m_State = (A * m_State + C) & 0xFFFFFFFF;
while(m_State > m_Max);
return m_State;
}

Key Generator::state() const
{
return m_State;
state = (A * state + C) & 0xFFFFFFFF;
while(state > m_Max);
return state;
}
@@ -16,19 +16,16 @@ typedef std::uint_fast32_t Key;
class Generator {

private:
Key m_State;
const Key m_Max;

public:
/**
* Constructor.
*
* @param state Current state of the generator.
* @param max Maximum number to return (inclusive).
*/
Generator(Key state, Key max);
Key generate();
Key state() const;
Generator(Key max);
Key generate(Key previous);

};

@@ -12,10 +12,11 @@ class Insults_test : public CppUnit::TestFixture {
public:
void test_insults()
{
Insults insults(0);
Insults insults;
Key state = 0;
for(size_t i = 0; i < NB_GENERATE; ++i)
{
std::string insult = insults.generate();
std::string insult = insults.generate(state);
CPPUNIT_ASSERT(insult == expected_insults[i]);
}
}
@@ -10,18 +10,23 @@ class PermGen_test : public CppUnit::TestFixture {
{
std::vector<Key> keys;
{
Generator gen(0, 3456789012);
Generator gen(3456789012);
Key state = 0;
for(size_t i = 0; i < 10; ++i)
{
Key g = gen.generate();
CPPUNIT_ASSERT(g <= 3456789012);
keys.push_back(g);
state = gen.generate(state);
CPPUNIT_ASSERT(state <= 3456789012);
keys.push_back(state);
}
}
{
Generator gen2(keys[4], 3456789012);
Generator gen2(3456789012);
Key state = keys[4];
for(size_t i = 0; i < 5; ++i)
CPPUNIT_ASSERT(gen2.generate() == keys[5+i]);
{
state = gen2.generate(state);
CPPUNIT_ASSERT(state == keys[5+i]);
}
}
}

0 comments on commit ad831ea

Please sign in to comment.
You can’t perform that action at this time.