Skip to content
Permalink
Browse files

Puts Generator in Database

  • Loading branch information
remram44 committed Feb 9, 2015
1 parent 6b1683a commit f85f3b761fa80bb00326a89ae41c79e308ef2f71
Showing with 28 additions and 5 deletions.
  1. +12 −1 database.cpp
  2. +6 −1 database.h
  3. +8 −3 test_database.cpp
  4. +2 −0 test_insults.cpp
@@ -22,7 +22,9 @@ const char *DatabaseError::what() const throw()
}


Database::Database(const char *filepath) throw(DatabaseError)
Database::Database(const char *filepath, Generator &generator)
throw(DatabaseError)
: m_Generator(generator)
{
if(sqlite3_open(filepath, &m_DB) != SQLITE_OK)
throw DatabaseError(std::string("Can't open ") + filepath);
@@ -222,3 +224,12 @@ void Database::setState(sqlite3_int64 state) throw(DatabaseError)
sqlerror:
throw DatabaseError("Couldn't set the generator state");
}

Key Database::nextState() throw(DatabaseError)
{
// TODO: transaction
Key state = getState();
state = m_Generator.generate(state);
setState(state);
return state;
}
@@ -4,6 +4,8 @@
#include <exception>
#include <sqlite3.h>

#include "permgen.h"


class DatabaseError : std::exception {

@@ -24,6 +26,8 @@ class DatabaseError : std::exception {
class Database {

private:
Generator &m_Generator;

sqlite3 *m_DB;

sqlite3_stmt *m_stmtGetState;
@@ -37,7 +41,7 @@ class Database {
void incrementViews(const std::string &our_url) throw(DatabaseError);

public:
Database(const char *filepath) throw(DatabaseError);
Database(const char *filepath, Generator &generator) throw(DatabaseError);
~Database();

void storeURL(const std::string &our_url,
@@ -49,6 +53,7 @@ class Database {

sqlite3_int64 getState() throw(DatabaseError);
void setState(sqlite3_int64 state) throw(DatabaseError);
Key nextState() throw(DatabaseError);

};

@@ -1,6 +1,7 @@
#include <cppunit/extensions/HelperMacros.h>

#include "database.h"
#include "insults.h"


class Database_test : public CppUnit::TestFixture {
@@ -9,12 +10,15 @@ class Database_test : public CppUnit::TestFixture {
void test_state()
{
try {
Database db(":memory:");
Generator gen(Insults::CHOICES);
Database db(":memory:", gen);
CPPUNIT_ASSERT(db.getState() == 0);
db.setState(42);
CPPUNIT_ASSERT(db.getState() == 42);
db.setState(189);
CPPUNIT_ASSERT(db.getState() == 189);
Key s = db.nextState();
CPPUNIT_ASSERT(s == 2405963442);
CPPUNIT_ASSERT(db.getState() == (sqlite3_int64)s);
}
catch(DatabaseError &e)
{
@@ -25,7 +29,8 @@ class Database_test : public CppUnit::TestFixture {
void test_urls()
{
try {
Database db(":memory:");
Generator gen(Insults::CHOICES);
Database db(":memory:", gen);
CPPUNIT_ASSERT(
db.resolveURL("http://aaa.clique-salope.ovh/", true) ==
"");
@@ -12,10 +12,12 @@ class Insults_test : public CppUnit::TestFixture {
public:
void test_insults()
{
Generator gen(Insults::CHOICES);
Insults insults;
Key state = 0;
for(size_t i = 0; i < NB_GENERATE; ++i)
{
state = gen.generate(state);
std::string insult = insults.generate(state);
CPPUNIT_ASSERT(insult == expected_insults[i]);
}

0 comments on commit f85f3b7

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