added RetroDb:

- tests added which pass on windows. 

git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-new_cache_system@5036 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
chrisparker126 2012-03-18 14:06:36 +00:00
parent e5ed6bf479
commit 030b009f9a
6 changed files with 1858 additions and 28 deletions

View file

@ -0,0 +1,374 @@
#include <memory.h>
#include "retrodb.h"
#include "utest.h"
bool operator==(const ContentValue& lCV, ContentValue& rCV){
std::map<std::string, uint8_t> leftKtMap, rightKtMap;
lCV.getKeyTypeMap(leftKtMap);
rCV.getKeyTypeMap(rightKtMap);
return (leftKtMap == rightKtMap);
}
/*!
* Test content value ability to take a set of data and ability to get it out
* For all data types
*/
void testEnterAndRetrieve();
/*!
* Check to see if copy constructor intialises
* 'this' correctly
*/
void testCopyConstructor();
/*!
* check that key type map returned is consistent
* with data contained in ContentValue
*/
void testGetKeyTypeMap();
/*!
* Test the clearing functionality
*/
void testClear();
/*!
* check errors are meaningful
*/
void testErrors();
/*!
* enter the same key twice and ensure previous data gets overwritten
*
*/
void testSameKey();
INITTEST();
int main(){
testEnterAndRetrieve();
testCopyConstructor();
testClear();
testSameKey();
testGetKeyTypeMap();
testErrors();
FINALREPORT("TEST_CONTENT_VALUE");
}
void testSameKey(){
ContentValue cv;
// test data
std::string src = "adalkjalfalfalfkal";
const char* data = src.data();
uint32_t data_len = src.length();
std::string data_key = "private_key";
cv.put(data_key, data_len, data);
std::string other_src = "daldko5202402224";
data_len = other_src.length();
data = other_src.data();
cv.put(data_key, data_len, data);
uint32_t val_len;
char* val;
cv.getAsData(data_key, val_len, val);
std::string str_val(val, val_len);
CHECK(str_val == other_src);
// now check string
std::string key = "peer";
cv.put(key, std::string("sexy_girl"));
cv.put(key, std::string("manly man"));
std::string val_str;
cv.getAsString(key, val_str);
CHECK(val_str == "manly man");
// and double
cv.put(key, 4.);
cv.put(key, 32.);
double val_d;
cv.getAsDouble(key, val_d);
CHECK(val_d == 32.);
// and int64
int64_t large(20420492040123);
cv.put(key, large);
cv.put(key, large+34);
int64_t val_i64;
cv.getAsInt64(key, val_i64);
CHECK(val_i64 == large+34);
// and bool
cv.put(key, false);
cv.put(key, true);
bool bool_val = false;
cv.getAsBool(key, bool_val);
CHECK(bool_val == true);
// and int32
int32_t medium = 20432123;
cv.put(key, medium);
cv.put(key, medium+34);
int32_t val_i32;
cv.getAsInt32(key, val_i32);
CHECK(val_i32 == medium+34);
REPORT("testSameKey()");
}
void testErrors(){
ContentValue cv;
int32_t val_32;
int64_t val_64;
char* data;
uint32_t data_len;
std::string val_str;
bool val_bool;
double val_d;
CHECK(!cv.getAsInt32("dda", val_32));
CHECK(!cv.getAsInt64("dda", val_64));
CHECK(!cv.getAsData("ds", data_len, data));
CHECK(!cv.getAsString("d3536356336356356s", val_str));
CHECK(!cv.getAsBool("d424s", val_bool));
CHECK(!cv.getAsDouble("daads", val_d));
REPORT("testErrors()");
}
void testEnterAndRetrieve(){
// INT 32
int32_t value32 = 1, retval32;
std::string key = "one";
ContentValue cv;
cv.put(key, value32);
cv.getAsInt32(key, retval32);
CHECK(value32 == retval32);
// INT 64
int64_t value64 = 423425242, retval64;
key = "int64";
cv.put(key, value64);
cv.getAsInt64(key, retval64);
CHECK(value64 == retval64);
// Double
double dvalue = 3.5, retvaldbl;
key = "double";
cv.put(key, dvalue);
cv.getAsDouble(key, retvaldbl);
CHECK(dvalue == retvaldbl );
// BLOB
uint32_t data_len = 45, get_len = 0;
char* src = new char[data_len];
char* get_src = NULL;
memset(src, '$', data_len);
key = "data";
cv.put(key, data_len, src);
cv.getAsData(key, get_len, get_src);
bool fine = true;
CHECK(get_len = data_len);
if(data_len == get_len){
for(int i=0; i < data_len; i++){
if(src[i] != get_src[i])
fine &= false;
}
}
delete[] src;
CHECK(fine);
// STRING
std::string strVal = "whoo", getVal("");
key = "string";
cv.put(key, strVal);
cv.getAsString(key, getVal);
CHECK(getVal == strVal);
// BOOL
bool mefalse = false, retvalBool;
key = "bool";
cv.put(key, mefalse);
cv.getAsBool(key, retvalBool);
CHECK(mefalse == retvalBool);
cv.clear();
REPORT("testEnterAndRetrieve()");
}
void testGetKeyTypeMap(){
ContentValue cv;
std::string key1="key1", key2="key2", key3="key3", key4="key4";
std::string key1_val;
int32_t key2_val = 42;
double key3_val = 23;
int64_t key4_val = 42052094224;
cv.put(key1, key1_val);
cv.put(key2, key2_val);
cv.put(key3, key3_val);
cv.put(key4, key4_val);
std::map<std::string, uint8_t> kvMap;
cv.getKeyTypeMap(kvMap);
CHECK(kvMap.size() == 4);
CHECK(kvMap[key1] == ContentValue::STRING_TYPE);
CHECK(kvMap[key2] == ContentValue::INT32_TYPE);
CHECK(kvMap[key3] == ContentValue::DOUBLE_TYPE);
CHECK(kvMap[key4] == ContentValue::INT64_TYPE);
REPORT("testGetKeyTypeMap()");
}
void testCopyConstructor(){
ContentValue cv1;
// INT 32
int value32 = 1;
std::string key = "one";
cv1.put(key, value32);
// INT 64
int64_t value64 = 423425242;
key = "int64";
cv1.put(key, value64);
// Double
double dvalue = 3.5;
key = "double";
cv1.put(key, dvalue);
// BLOB
uint32_t data_len = 45;
char* src = new char[data_len];
memset(src, '$', data_len);
key = "data";
cv1.put(key, data_len, src);
delete[] src;
// STRING
std::string strVal = "whoo";
key = "string";
cv1.put(key, strVal);
// BOOL
bool mefalse = false;
key = "bool";
cv1.put(key, mefalse);
ContentValue cv2(cv1);
CHECK(cv1 == cv2);
cv1.clear();
cv2.clear();
REPORT("testCopyConstructor()");
}
void testClear(){
ContentValue cv1;
// INT 32
int value32 = 1;
std::string key = "one";
cv1.put(key, value32);
// INT 64
int64_t value64 = 423425242;
key = "int64";
cv1.put(key, value64);
// Double
double dvalue = 3.5;
key = "double";
cv1.put(key, dvalue);
// BLOB
uint32_t data_len = 45;
char* src = new char[data_len];
memset(src, '$', data_len);
key = "data";
cv1.put(key, data_len, src);
delete[] src;
// STRING
std::string strVal = "whoo";
key = "string";
cv1.put(key, strVal);
// BOOL
bool mefalse = false;
key = "bool";
cv1.put(key, mefalse);
std::map<std::string, uint8_t> ktMap;
cv1.getKeyTypeMap(ktMap);
CHECK(ktMap.size() > 0);
cv1.clear();
cv1.getKeyTypeMap(ktMap);
CHECK(ktMap.size() == 0);
REPORT("testClear()");
}

View file

@ -0,0 +1,127 @@
#include <iostream>
#include <memory.h>
#include <sstream>
#include <time.h>
#include "retrodb.h"
#include "utest.h"
#define INT32_KEY "day"
#define INT64_KEY "pub_key"
#define DATA_KEY "data"
#define DOUBLE_KEY "a_double"
#define STRING_KEY "a_string"
#define BOOL_KEY "a_bool"
#define DB_FILE_NAME "RetroDb"
static RetroDb* mDb = NULL;
void createRetroDb();
void destroyRetroDb();
INITTEST();
// mainly test ability to move up and down result set
void testTraverseResult();
int main() {
testTraverseResult();
FINALREPORT("RETRO CURSOR TEST");
return 0;
}
void createRetroDb(){
if(mDb)
destroyRetroDb();
remove(DB_FILE_NAME); // account for interrupted tests
mDb = new RetroDb(DB_FILE_NAME, RetroDb::OPEN_READWRITE_CREATE);
}
void destroyRetroDb(){
if(mDb == NULL)
return;
mDb->closeDb();
delete mDb;
mDb = NULL;
int rc = remove(DB_FILE_NAME);
std::cerr << "remove code: " << rc << std::endl;
if(rc !=0){
perror("Could not delete db: ");
}
}
void testTraverseResult(){
createRetroDb();
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, pub_key INT, data BLOB, a_double REAL, a_string VARCHAR(255), a_bool INT);");
CHECK(statementExecuted);
ContentValue cv;
cv.put(INT32_KEY, (int32_t)20);
int64_t large_num = 32432242344423;
cv.put(INT64_KEY, large_num);
std::string str = "3dajaojaljfacjlaf£%£^%\"%\"%$";
const char* data = str.data();
int size = str.size();
cv.put(DATA_KEY, size, data);
cv.put(DOUBLE_KEY, 3.14);
cv.put(STRING_KEY, "hello precious");
cv.put(BOOL_KEY, false);
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
cv.put(INT32_KEY, (int32_t)21);
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
cv.put(INT32_KEY, (int32_t)2);
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
cv.put(INT32_KEY, (int32_t)204);
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
cv.put(INT32_KEY, (int32_t)22);
insertExecuted &= mDb->sqlInsert("retroDB", "", cv);
CHECK(insertExecuted);
std::list<std::string> columns;
columns.push_back(INT32_KEY); columns.push_back(INT64_KEY); columns.push_back(DOUBLE_KEY);
columns.push_back(DATA_KEY); columns.push_back(BOOL_KEY); columns.push_back(STRING_KEY);
std::string orderBy = std::string(INT32_KEY) + " ASC";
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns, "", orderBy);
CHECK(cursor->getResultCount() == 5);
cursor->moveToFirst();
CHECK(cursor->getPosition() == 0);
cursor->moveToLast();
CHECK(cursor->getPosition() == cursor->getResultCount());
cursor->moveToFirst();
CHECK(cursor->getInt32(0) == 2);
cursor->moveToNext();
cursor->moveToNext();
CHECK(cursor->getInt32(0) == 21);
delete cursor;
REPORT("testTraverseResult()");
destroyRetroDb();
}

View file

@ -0,0 +1,352 @@
#include <iostream>
#include <memory.h>
#include <sstream>
#include <time.h>
#include "retrodb.h"
#include "utest.h"
#define DAY_KEY "day"
#define PUB_KEY "pub_key"
#define DATA_KEY "data"
#define DOUBLE_KEY "a_double"
#define DB_FILE_NAME "RetroDb"
static RetroDb* mDb = NULL;
void createRetroDb();
void destroyRetroDb();
void testSqlExec();
void testSqlUpdate();
void testSqlInsert();
void testSqlDelete();
void testSqlQuery();
void testBinaryInsertion();
INITTEST();
int main(){
testSqlExec();
testBinaryInsertion();
testSqlInsert();
testSqlUpdate();
testSqlDelete();
testSqlQuery();
FINALREPORT("RETRO DB TEST");
return 0;
}
void createRetroDb(){
if(mDb)
destroyRetroDb();
remove(DB_FILE_NAME); // account for interrupted tests
mDb = new RetroDb(DB_FILE_NAME, RetroDb::OPEN_READWRITE_CREATE);
}
void destroyRetroDb(){
if(mDb == NULL)
return;
mDb->closeDb();
delete mDb;
mDb = NULL;
int rc = remove(DB_FILE_NAME);
std::cerr << "remove code: " << rc << std::endl;
if(rc !=0){
perror("Could not delete db: ");
}
}
void testSqlExec(){
createRetroDb();
// create simple table
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, pub_key, data);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(day, pub_key, data) VALUES(1,2,3);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(day, pub_key, data) VALUES(3,4525624,4524);");
CHECK(statementExecuted);
// now check if you can retrieve records
std::list<std::string> columns;
columns.push_back("day");
columns.push_back("pub_key");
columns.push_back("data");
std::string selection, orderBy;
RetroCursor* c = mDb->sqlQuery("retroDB", columns, selection, orderBy);
CHECK(c->getResultCount() == 2);
// got to first record
c->moveToFirst();
int32_t first =c->getInt32(0), second = c->getInt32(1),
third = c->getInt32(2);
CHECK(first == 1);
CHECK(second == 2);
CHECK(third == 3);
// get 2nd record
c->moveToNext();
first =c->getInt32(0), second = c->getInt32(1),
third = c->getInt32(2);
CHECK(first == 3);
CHECK(second == 4525624);
CHECK(third == 4524);
delete c;
REPORT("testSqlExec()");
destroyRetroDb();
}
void testBinaryInsertion(){
createRetroDb();
// create simple table
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, data BLOB);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(day, data) VALUES(1, 'dafadfad%$£%^£%%\"$R\"$\"$\"');");
// now check if you can retrieve records
std::list<std::string> columns;
columns.push_back("day");
columns.push_back("data");
std::string selection, orderBy;
RetroCursor* c = mDb->sqlQuery("retroDB", columns, selection, orderBy);
c->moveToFirst();
int first = c->getInt32(0);
uint32_t size;
const char* data = (const char*)c->getData(1, size);
std::string str = "dafadfad%$£%^£%%\"$R\"$\"$\"";
const char* data_comp = str.data();
bool binCompare = ok;
for(int i=0; i < 24 ; i++)
binCompare &= data[i] == data_comp[i];
CHECK(first == 1);
CHECK(binCompare);
delete c;
REPORT("testBinaryInsertion()");
destroyRetroDb();
}
void testSqlUpdate(){
createRetroDb();
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, data BLOB, peerId VARCHAR(255), time INT);");
CHECK(statementExecuted);
std::string data = "dadJOOodaodaoro20r2-0r20002ri02fgi3t0***";
ContentValue cv;
cv.put("day", (int32_t)20);
std::string peerid = "TheRetroSquad";
cv.put("peerId", peerid);
cv.put("data", data.size(), data.data());
int64_t now = time(NULL);
cv.put("time", now);
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
CHECK(insertExecuted);
// now check entry is fine
std::list<std::string> columns;
columns.push_back("day"); columns.push_back("peerId"); columns.push_back("data"); columns.push_back("time");
RetroCursor* c = mDb->sqlQuery("retroDB", columns, "", "");
CHECK(c->getResultCount() == 1);
std::string result;
c->moveToFirst();
c->getString(1, result);
CHECK(result == peerid);
delete c;
c = NULL;
// now make an update and see if this is reflected
int64_t now_plus = now+203;
cv.put("time", now_plus);
bool update = mDb->sqlUpdate("retroDB", "", cv);
CHECK(update);
c = mDb->sqlQuery("retroDB", columns, "", "");
c->moveToFirst();
int64_t now_plus_compare = c->getDouble(3);
CHECK(now_plus_compare == now_plus);
// now attempt an update which should find no valid record
delete c;
REPORT("testSqlUpdate()");
destroyRetroDb();
}
void testSqlInsert(){
createRetroDb();
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, a_double DOUBLE, data BLOB);");
CHECK(statementExecuted);
ContentValue cv;
cv.put("day", (int32_t)20);
std::string str = "3dajaojaljfacjlaf£%£^%\"%\"%$";
const char* data = str.data();
int size = 27;
cv.put(DATA_KEY, size, data);
cv.put(DOUBLE_KEY, 3.14);
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
std::list<std::string> columns;
columns.push_back("day"); columns.push_back("a_double"); columns.push_back("data");
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns, "", "");
CHECK(cursor != NULL);
cursor->moveToFirst();
CHECK(20 == cursor->getInt32(0));
CHECK(3.14 == cursor->getDouble(1));
CHECK(insertExecuted);
delete cursor;
FINALREPORT("testSqlInsert()");
destroyRetroDb();
}
void testSqlQuery(){
// test ordering of data and selection clause
createRetroDb();
// create simple table
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(priority INTEGER PRIMARY KEY ASC, name VARCHAR(255), friends, games INTEGER);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(4,'sammy',2,30);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(2,'davy',6,9);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(6,'pammy',2,4);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(5,'tommy',3,4534);");
statementExecuted &= mDb->execSQL("INSERT INTO retroDB(priority, name, friends, games) VALUES(9,'jonny',3,44);");
CHECK(statementExecuted);
std::list<std::string> columns;
columns.push_back("name");
std::string selection = "games <= 31";
std::string orderBy = "priority DESC";
// output should be by name: pammy, sammy and davy
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns,selection, orderBy);
cursor->moveToFirst();
std::string name;
cursor->getString(0,name);
CHECK(name == "pammy");
cursor->moveToNext();
cursor->getString(0,name);
CHECK(name == "sammy");
cursor->moveToNext();
cursor->getString(0,name);
CHECK(name == "davy");
delete cursor;
FINALREPORT("testSqlQuery()");
destroyRetroDb();
return;
}
void testSqlDelete(){
createRetroDb();
bool statementExecuted = mDb->execSQL("CREATE TABLE retroDB(day INTEGER PRIMARY KEY ASC, a_double DOUBLE, data BLOB);");
CHECK(statementExecuted);
ContentValue cv;
cv.put("day", (int32_t)20);
std::string str = "3dajaojaljfacjlaf£%£^%\"%\"%$";
const char* data = str.data();
uint32_t size = str.size();
cv.put(DATA_KEY, size, data);
cv.put(DOUBLE_KEY, 3.14);
// insert to records
bool insertExecuted = mDb->sqlInsert("retroDB", "", cv);
cv.put("day", (int32_t(5)));
CHECK(insertExecuted);
mDb->sqlInsert("retroDB", "", cv);
// now check that their are two records in the db
std::list<std::string> columns;
columns.push_back("day"); columns.push_back("a_double"); columns.push_back("data");
RetroCursor* cursor = mDb->sqlQuery("retroDB", columns, "", "");
CHECK(cursor->getResultCount() == 2);
delete cursor;
// now remove a record and search for the removed record, query should return no records
mDb->sqlDelete("retroDB", "day=5", "");
cursor = mDb->sqlQuery("retroDB", columns, "day=5", "");
CHECK(cursor->getResultCount() == 0);
delete cursor;
// now check for the remaining record
cursor = mDb->sqlQuery("retroDB", columns, "day=20", "");
CHECK(cursor->getResultCount() == 1);
cursor->moveToFirst();
// verify there is no data corruption
const char* data_comp1 = (const char*)cursor->getData(2, size);
const char* data_comp2 = str.data();
bool binCompare = true;
for(int i=0; i < str.size() ; i++)
binCompare &= data_comp1[i] == data_comp2[i];
CHECK(binCompare);
delete cursor;
FINALREPORT("testSqlDelete()");
destroyRetroDb();
}