forked from github/server
add tests for get_param functions
This commit is contained in:
parent
a75d91fb6d
commit
07d10d9ab0
|
@ -1,6 +1,7 @@
|
||||||
project(kernel C)
|
project(kernel C)
|
||||||
|
|
||||||
SET(_TEST_FILES
|
SET(_TEST_FILES
|
||||||
|
config.test.c
|
||||||
ship.test.c
|
ship.test.c
|
||||||
spell.test.c
|
spell.test.c
|
||||||
ally.test.c
|
ally.test.c
|
||||||
|
|
|
@ -1808,22 +1808,23 @@ int getid(void)
|
||||||
|
|
||||||
const char *get_param(const struct param *p, const char *key)
|
const char *get_param(const struct param *p, const char *key)
|
||||||
{
|
{
|
||||||
while (p != NULL) {
|
while (p != NULL) {
|
||||||
if (strcmp(p->name, key) == 0)
|
int cmp = strcmp(p->name, key);
|
||||||
return p->data;
|
if (cmp == 0) {
|
||||||
p = p->next;
|
return p->data;
|
||||||
}
|
}
|
||||||
return NULL;
|
else if (cmp>0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
p = p->next;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_param_int(const struct param *p, const char *key, int def)
|
int get_param_int(const struct param *p, const char *key, int def)
|
||||||
{
|
{
|
||||||
while (p != NULL) {
|
const char * str = get_param(p, key);
|
||||||
if (strcmp(p->name, key) == 0)
|
return str ? atoi(str) : def;
|
||||||
return atoi(p->data);
|
|
||||||
p = p->next;
|
|
||||||
}
|
|
||||||
return def;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *g_datadir;
|
static const char *g_datadir;
|
||||||
|
@ -1879,19 +1880,26 @@ float get_param_flt(const struct param *p, const char *key, float def)
|
||||||
|
|
||||||
void set_param(struct param **p, const char *key, const char *data)
|
void set_param(struct param **p, const char *key, const char *data)
|
||||||
{
|
{
|
||||||
++global.cookie;
|
struct param *par;
|
||||||
while (*p != NULL) {
|
|
||||||
if (strcmp((*p)->name, key) == 0) {
|
++global.cookie;
|
||||||
free((*p)->data);
|
while (*p != NULL) {
|
||||||
(*p)->data = _strdup(data);
|
int cmp = strcmp((*p)->name, key);
|
||||||
return;
|
if (cmp == 0) {
|
||||||
|
free((*p)->data);
|
||||||
|
(*p)->data = _strdup(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (cmp>0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
p = &(*p)->next;
|
||||||
}
|
}
|
||||||
p = &(*p)->next;
|
par = malloc(sizeof(param));
|
||||||
}
|
par->name = _strdup(key);
|
||||||
*p = malloc(sizeof(param));
|
par->data = _strdup(data);
|
||||||
(*p)->name = _strdup(key);
|
par->next = *p;
|
||||||
(*p)->data = _strdup(data);
|
*p = par;
|
||||||
(*p)->next = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void kernel_done(void)
|
void kernel_done(void)
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include <platform.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <kernel/config.h>
|
||||||
|
|
||||||
|
#include <CuTest.h>
|
||||||
|
#include <tests.h>
|
||||||
|
|
||||||
|
static void test_get_set_param(CuTest * tc)
|
||||||
|
{
|
||||||
|
struct param *par = 0;
|
||||||
|
test_cleanup();
|
||||||
|
CuAssertStrEquals(tc, 0, get_param(par, "foo"));
|
||||||
|
set_param(&par, "foo", "bar");
|
||||||
|
set_param(&par, "bar", "foo");
|
||||||
|
CuAssertStrEquals(tc, "bar", get_param(par, "foo"));
|
||||||
|
CuAssertStrEquals(tc, "foo", get_param(par, "bar"));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_param_int(CuTest * tc)
|
||||||
|
{
|
||||||
|
struct param *par = 0;
|
||||||
|
test_cleanup();
|
||||||
|
CuAssertIntEquals(tc, 13, get_param_int(par, "foo", 13));
|
||||||
|
set_param(&par, "foo", "23");
|
||||||
|
set_param(&par, "bar", "42");
|
||||||
|
CuAssertIntEquals(tc, 23, get_param_int(par, "foo", 0));
|
||||||
|
CuAssertIntEquals(tc, 42, get_param_int(par, "bar", 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_param_flt(CuTest * tc)
|
||||||
|
{
|
||||||
|
struct param *par = 0;
|
||||||
|
test_cleanup();
|
||||||
|
CuAssertDblEquals(tc, 13, get_param_flt(par, "foo", 13), 0.01);
|
||||||
|
set_param(&par, "foo", "23.0");
|
||||||
|
set_param(&par, "bar", "42.0");
|
||||||
|
CuAssertDblEquals(tc, 23.0, get_param_flt(par, "foo", 0.0), 0.01);
|
||||||
|
CuAssertDblEquals(tc, 42.0, get_param_flt(par, "bar", 0.0), 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
|
CuSuite *get_config_suite(void)
|
||||||
|
{
|
||||||
|
CuSuite *suite = CuSuiteNew();
|
||||||
|
SUITE_ADD_TEST(suite, test_get_set_param);
|
||||||
|
SUITE_ADD_TEST(suite, test_param_int);
|
||||||
|
SUITE_ADD_TEST(suite, test_param_flt);
|
||||||
|
return suite;
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ CuSuite *get_ship_suite(void);
|
||||||
CuSuite *get_spellbook_suite(void);
|
CuSuite *get_spellbook_suite(void);
|
||||||
CuSuite *get_spell_suite(void);
|
CuSuite *get_spell_suite(void);
|
||||||
CuSuite *get_base36_suite(void);
|
CuSuite *get_base36_suite(void);
|
||||||
|
CuSuite *get_config_suite(void);
|
||||||
CuSuite *get_bsdstring_suite(void);
|
CuSuite *get_bsdstring_suite(void);
|
||||||
CuSuite *get_functions_suite(void);
|
CuSuite *get_functions_suite(void);
|
||||||
CuSuite *get_umlaut_suite(void);
|
CuSuite *get_umlaut_suite(void);
|
||||||
|
@ -45,6 +46,7 @@ int RunAllTests(void)
|
||||||
CuSuiteAddSuite(suite, get_skill_suite());
|
CuSuiteAddSuite(suite, get_skill_suite());
|
||||||
CuSuiteAddSuite(suite, get_keyword_suite());
|
CuSuiteAddSuite(suite, get_keyword_suite());
|
||||||
/* util */
|
/* util */
|
||||||
|
CuSuiteAddSuite(suite, get_config_suite());
|
||||||
CuSuiteAddSuite(suite, get_base36_suite());
|
CuSuiteAddSuite(suite, get_base36_suite());
|
||||||
CuSuiteAddSuite(suite, get_bsdstring_suite());
|
CuSuiteAddSuite(suite, get_bsdstring_suite());
|
||||||
CuSuiteAddSuite(suite, get_functions_suite());
|
CuSuiteAddSuite(suite, get_functions_suite());
|
||||||
|
|
Loading…
Reference in New Issue