another test. moving some deckchairs around.

This commit is contained in:
Enno Rehling 2017-10-14 09:01:14 +02:00
parent 6d79f85628
commit 4257a9891c
5 changed files with 46 additions and 57 deletions

View file

@ -173,19 +173,6 @@ void free_orders(order ** olist)
}
}
static char *mkdata(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
data = malloc(sizeof(order_data) + len + 1);
result = (char *)(data + 1);
data->_refcount = 1;
data->_str = (len > 0) ? result : NULL;
if (str) strcpy(result, str);
if (pdata) *pdata = data;
return result;
}
static int create_data(keyword_t kwd, const char *s,
const struct locale *lang)
{
@ -206,7 +193,7 @@ static int create_data(keyword_t kwd, const char *s,
}
/* TODO: between mkdata and odata_release, this object is very
* short-lived. */
mkdata(&data, strlen(s), s);
odata_create(&data, strlen(s), s);
id = odata_save(data);
odata_release(data);
return id;

View file

@ -6,6 +6,7 @@
#include <critbit.h>
#include <stdlib.h>
#include <string.h>
static critbit_tree cb_orders = { 0 };
static int auto_id = 0;
@ -32,6 +33,19 @@ order_data *odata_load(int id)
return NULL;
}
void odata_create(order_data **pdata, size_t len, const char *str)
{
order_data *data;
char *result;
data = malloc(sizeof(order_data) + len + 1);
data->_refcount = 1;
result = (char *)(data + 1);
data->_str = (len > 0) ? result : NULL;
if (str) strcpy(result, str);
if (pdata) *pdata = data;
}
int odata_save(order_data *od)
{
if (od->_str) {

View file

@ -1,6 +1,8 @@
#ifndef H_ORDERDB
#define H_ORDERDB
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -21,9 +23,11 @@ extern "C" {
db_backend orderdb_open(db_backend choices[]);
void orderdb_close(void);
void odata_create(order_data **pdata, size_t len, const char *str);
void odata_release(order_data * od);
order_data *odata_load(int id);
int odata_save(order_data *od);
void odata_release(order_data * od);
#ifdef __cplusplus
}

View file

@ -6,6 +6,8 @@
#include <CuTest.h>
#include <tests.h>
#include <string.h>
static void test_orderdb_open_close(CuTest *tc) {
db_backend choices[] = { DB_MEMORY, DB_NONE };
db_backend nochoice[] = { DB_SQLITE, DB_NONE };
@ -18,10 +20,34 @@ static void test_orderdb_open_close(CuTest *tc) {
orderdb_close();
}
static void test_odata_save_load(CuTest *tc) {
db_backend choices[] = { DB_MEMORY, DB_NONE };
order_data *od;
int id;
const char * s = "GIB enno 1 Hodor";
CuAssertIntEquals(tc, DB_MEMORY, orderdb_open(choices));
odata_create(&od, strlen(s) + 1, s);
CuAssertTrue(tc, od->_refcount >= 1);
id = odata_save(od);
odata_release(od);
CuAssertTrue(tc, id != 0);
od = odata_load(id);
CuAssertPtrNotNull(tc, od);
CuAssertTrue(tc, od->_refcount >= 1);
CuAssertStrEquals(tc, s, od->_str);
odata_release(od);
orderdb_close();
}
CuSuite *get_orderdb_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_orderdb_open_close);
SUITE_ADD_TEST(suite, test_odata_save_load);
return suite;
}

View file

@ -23,48 +23,6 @@ faction *get_faction_by_id(int uid)
return NULL;
}
/*
typedef struct stmt_cache {
sqlite3 *db;
sqlite3_stmt *stmt;
const char *sql;
int inuse;
} stmt_cache;
#define MAX_STMT_CACHE 64
static stmt_cache cache[MAX_STMT_CACHE];
static int cache_insert;
static sqlite3_stmt *stmt_cache_get(sqlite3 * db, const char *sql)
{
int i;
sqlite3_stmt *stmt;
for (i = 0; i != MAX_STMT_CACHE && cache[i].db; ++i) {
if (cache[i].sql == sql && cache[i].db == db) {
cache[i].inuse = 1;
stmt = cache[i].stmt;
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
return stmt;
}
}
if (i == MAX_STMT_CACHE) {
while (cache[cache_insert].inuse) {
cache[cache_insert].inuse = 0;
cache_insert = (cache_insert + 1) & (MAX_STMT_CACHE - 1);
}
i = cache_insert;
stmt = cache[i].stmt;
sqlite3_finalize(stmt);
}
cache[i].inuse = 1;
cache[i].db = db;
cache[i].sql = sql;
sqlite3_prepare_v2(db, sql, -1, &cache[i].stmt, NULL);
return cache[i].stmt;
}
*/
typedef struct db_faction {
int uid;
int no;