First cut at testsuite for json/prefs

This commit is contained in:
Aaron Culliney 2016-02-25 20:46:41 -08:00
parent b5dfd86310
commit 6e978810db
5 changed files with 264 additions and 200 deletions

View File

@ -49,7 +49,7 @@ AUDIO_SRC = \
META_SRC = \
src/meta/debug.l src/meta/debugger.c src/meta/opcodes.c src/test/sha1.c \
src/meta/lintrace.c
src/meta/lintrace.c src/json_parse.c externals/jsmn/jsmn.c
# NOTE : selectively enabled through configuration process ...
EXTRA_apple2ix_SOURCES = \
@ -92,9 +92,10 @@ LOG_DRIVER = testcpu ## hack TODO/FIXME ... should be wrapper shell script acce
A2_TEST_SOURCES = $(apple2ix_SOURCES) src/test/testcommon.c
A2_TEST_CFLAGS = -DTESTING=1 -DCPU_TRACING=1 -DDISK_TRACING=1 -DVM_TRACING=1 -Isrc/test
TESTS = testcpu testdisplay testvm testdisk testtrace
check_PROGRAMS = testcpu testdisplay testvm testdisk testtrace
TESTS = testcpu testdisplay testvm testdisk testprefs testtrace
check_PROGRAMS = testcpu testdisplay testvm testdisk testprefs testtrace
#######################################
testcpu_SOURCES = src/test/testcpu.c $(A2_TEST_SOURCES) $(META_SRC)
testcpu_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC
testcpu_CCASFLAGS = $(testcpu_CFLAGS)
@ -104,6 +105,7 @@ testcpu_DEPENDENCIES = @ASM_O@ @META_O@ @VIDEO_O@
EXTRA_testcpu_SOURCES = $(ASM_SRC_x86)
#######################################
testdisplay_SOURCES = src/test/testdisplay.c $(A2_TEST_SOURCES) $(META_SRC)
testdisplay_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC
testdisplay_CCASFLAGS = $(testdisplay_CFLAGS)
@ -113,6 +115,7 @@ testdisplay_DEPENDENCIES = @ASM_O@ @META_O@ @VIDEO_O@
EXTRA_testdisplay_SOURCES = $(ASM_SRC_x86) $(VIDEO_SRC)
#######################################
testvm_SOURCES = src/test/testvm.c $(A2_TEST_SOURCES) $(META_SRC)
testvm_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC
testvm_CCASFLAGS = $(testvm_CFLAGS)
@ -123,6 +126,7 @@ testvm_DEPENDENCIES = @TESTVM_ASM_O@ @META_O@ @VIDEO_O@
EXTRA_testvm_SOURCES = $(ASM_SRC_x86) $(VIDEO_SRC)
#######################################
testdisk_SOURCES = src/test/testdisk.c $(A2_TEST_SOURCES) $(META_SRC)
testdisk_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC
testdisk_CCASFLAGS = $(testdisk_CFLAGS)
@ -133,6 +137,18 @@ testdisk_DEPENDENCIES = @TESTDISK_ASM_O@ @META_O@ @VIDEO_O@
EXTRA_testdisk_SOURCES = $(ASM_SRC_x86) $(VIDEO_SRC)
#######################################
testprefs_SOURCES = src/test/testprefs.c $(A2_TEST_SOURCES) $(META_SRC)
testprefs_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC
testprefs_CCASFLAGS = $(testprefs_CFLAGS)
testprefs_LDFLAGS = $(apple2ix_LDFLAGS)
# HACK FIXME TODO NOTE: specify testprefs_ASM_O to force it to rebuild with proper CCASFLAGS ... automake bug?
testprefs_LDADD = @TESTPREFS_ASM_O@ @VIDEO_O@
testprefs_DEPENDENCIES = @TESTPREFS_ASM_O@ @META_O@ @VIDEO_O@
EXTRA_testprefs_SOURCES = $(ASM_SRC_x86) $(VIDEO_SRC)
#######################################
testtrace_SOURCES = src/test/testtrace.c $(A2_TEST_SOURCES) $(META_SRC)
testtrace_CFLAGS = $(apple2ix_CFLAGS) $(A2_TEST_CFLAGS) -UAUDIO_ENABLED -UINTERFACE_CLASSIC
testtrace_CCASFLAGS = $(testtrace_CFLAGS)

View File

@ -22,6 +22,7 @@ dnl Arch checks
ASM_O="src/x86/glue.o src/x86/cpu.o"
TESTVM_ASM_O="src/x86/testvm-glue.o src/x86/testvm-cpu.o"
TESTDISK_ASM_O="src/x86/testdisk-glue.o src/x86/testdisk-cpu.o"
TESTPREFS_ASM_O="src/x86/testprefs-glue.o src/x86/testprefs-cpu.o"
TESTTRACE_ASM_O="src/x86/testtrace-glue.o src/x86/testtrace-cpu.o"
arch=''
case $target in
@ -78,6 +79,7 @@ fi
AC_SUBST(ASM_O)
AC_SUBST(TESTVM_ASM_O)
AC_SUBST(TESTDISK_ASM_O)
AC_SUBST(TESTPREFS_ASM_O)
AC_SUBST(TESTTRACE_ASM_O)
AC_SUBST([AM_CFLAGS])

View File

@ -290,196 +290,3 @@ void json_destroy(JSON_s *parsedData) {
FREE(parsedData->jsonTokens);
}
#if TEST_JSON
bool do_logging = true;
FILE *error_log = NULL;
// Runs jsmn against an arbitrary a test_file.json
static void _dumpJSON(JSON_s parsedData) {
fprintf(stderr, "TOK COUNT:%d\n", parsedData.numTokens);
for (int i=0; i<parsedData.numTokens; i++) {
fprintf(stderr, "%6d %s", i, "type:");
switch(parsedData.jsonTokens[i].type) {
case JSMN_PRIMITIVE:
fprintf(stderr, "%s", "PRIMITIVE ");
break;
case JSMN_OBJECT:
fprintf(stderr, "%s", "OBJECT ");
break;
case JSMN_ARRAY:
fprintf(stderr, "%s", "ARRAY ");
break;
case JSMN_STRING:
fprintf(stderr, "%s", "STRING ");
break;
default:
fprintf(stderr, "%s", "UNKNOWN ");
break;
}
jsmntok_t tok = parsedData.jsonTokens[i];
int start = tok.start;
int end = tok.end;
fprintf(stderr, "start:%6d end:%6d size:%6d skipIdx:%6d parent:%3d", start, end, tok.size, tok.skip, tok.parent);
if (tok.type == JSMN_STRING || tok.type == JSMN_PRIMITIVE) {
char lastChar = parsedData.jsonString[end];
parsedData.jsonString[end] = '\0';
fprintf(stderr, " VALUE:%s", &parsedData.jsonString[start]);
parsedData.jsonString[end] = lastChar;
}
fprintf(stderr, "%s", "\n");
}
}
int main(int argc, char **argv) {
error_log = stderr;
if (argc < 2) {
fprintf(stderr, "Please specify a JSON file to parse on CLI, e.g. : %s path/to/some_file.json\n", argv[0]);
exit(1);
}
JSON_s parsedData = { 0 };
int tokCount = json_createFromFile(argv[1], &parsedData);
if (tokCount < 0) {
return 1;
}
fprintf(stderr, "-------------------------------------------------------------------------------\n");
fprintf(stderr, "DUMPING FILE %s ...\n", argv[1]);
fprintf(stderr, "-------------------------------------------------------------------------------\n");
fprintf(stderr, "\n");
_dumpJSON(parsedData);
json_destroy(&parsedData);
fprintf(stderr, "\n");
fprintf(stderr, "-------------------------------------------------------------------------------\n");
fprintf(stderr, "NOW TESTING MAP FUNCTIONS...\n");
fprintf(stderr, "-------------------------------------------------------------------------------\n");
fprintf(stderr, "\n");
const char *testMapStr0 =
" { "
" \"key0\" : \"a value zero\", "
" \"key1\" : \" \", "
" \"key2\" : { \n"
"} , "
" \"key3\" : { \n"
" \"subkey0\" : \"subval0\", "
" \"subkey1\" : { \"moar\" : \"recursion\" } , "
" \"subkey2\" : \"line0 \n"
" line1 "
" line2\" \n"
"}, "
" \"key4\" : [ \"Q\", \"W\", \"E\", "
" \"R\", \"T\", \"Y\", \"U\", \"I\", "
" \"O\", \"P\", { \"x\" : [ 22, 4, \"ab\" ] } ],"
" \"intKey0\" : 42 , "
" \"key5\" : \"\" , "
" \"intKey1\" : -101, "
" \"intKey2\" : -000000000, "
" \"intKey3\" : 0x2400, "
" \"intKey4\" : 10111111, "
" \"floatKey0\" : 0.0 , "
" \"floatKey1\" : -.0001220703125 ,"
" \"floatKey2\" : 3.1415928 , "
" \"floatKey3\" : -3.1e2 "
" } "
;
tokCount = json_createFromString(testMapStr0, &parsedData);
if (tokCount < 0) {
return 1;
}
_dumpJSON(parsedData);
long lVal;
float fVal;
char *val;
json_mapParseLongValue(&parsedData, "intKey2", &lVal, 10);
assert(lVal == 0);
json_mapCopyStringValue(&parsedData, "key0", &val);
assert(strcmp(val, "a value zero") == 0);
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key1", &val);
assert(strcmp(val, " \t ") == 0);
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key2", &val);
assert(strcmp(val, "{ \t \n}") == 0);
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key3", &val);
assert(strcmp(val, "{ \t \n \"subkey0\" : \"subval0\", \"subkey1\" : { \"moar\" : \"recursion\" } , \"subkey2\" : \"line0 \n \tline1 \tline2\" \n}") == 0);
{
JSON_s parsedSubData = { 0 };
int tokSubCount = json_createFromString(val, &parsedSubData);
if (tokSubCount < 0) {
return 1;
}
char *subval;
json_mapCopyStringValue(&parsedSubData, "subkey0", &subval);
assert(strcmp(subval, "subval0") == 0);
STRDUP_FREE(subval);
json_mapCopyStringValue(&parsedSubData, "subkey1", &subval);
assert(strcmp(subval, "{ \"moar\" : \"recursion\" }") == 0);
STRDUP_FREE(subval);
json_mapCopyStringValue(&parsedSubData, "subkey2", &subval);
assert(strcmp(subval, "line0 \n \tline1 \tline2") == 0);
STRDUP_FREE(subval);
}
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key4", &val);
assert(strcmp(val, "[ \"Q\", \"W\", \"E\", \"R\", \"T\", \"Y\", \"U\", \"I\", \"O\", \"P\", { \"x\" : [ 22, 4, \"ab\" ] } ]") == 0);
// TODO : subarray checks
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key5", &val);
assert(strcmp(val, "") == 0);
STRDUP_FREE(val);
json_mapParseLongValue(&parsedData, "intKey0", &lVal, 10);
assert(lVal == 42);
json_mapParseLongValue(&parsedData, "intKey1", &lVal, 10);
assert(lVal == -101);
json_mapParseLongValue(&parsedData, "intKey3", &lVal, 16);
assert(lVal == 0x2400);
json_mapParseLongValue(&parsedData, "intKey4", &lVal, 2);
assert(lVal == 191);
json_mapParseLongValue(&parsedData, "intKey4", &lVal, 10);
assert(lVal == 10111111);
json_mapParseFloatValue(&parsedData, "floatKey0", &fVal);
assert(fVal == 0.f);
json_mapParseFloatValue(&parsedData, "floatKey1", &fVal);
assert(fVal == -.0001220703125);
json_mapParseFloatValue(&parsedData, "floatKey2", &fVal);
assert((long)(fVal*10000000) == 31415928);
json_mapParseFloatValue(&parsedData, "floatKey3", &fVal);
assert((long)fVal == -310);
json_destroy(&parsedData);
fprintf(stderr, "...map functions passed\n");
return 0;
}
#endif

View File

@ -20,9 +20,6 @@ static char input_str[TESTBUF_SZ]; // ASCII
static unsigned int input_length = 0;
static unsigned int input_counter = 0;
static struct timespec t0 = { 0 };
static struct timespec ti = { 0 };
#if defined(ANDROID)
// We basically compile everything including audio into the Android build, even for testing =)
#else
@ -57,7 +54,6 @@ void test_common_setup() {
input_counter = 0;
input_length = 0;
input_str[0] = '\0';
clock_gettime(CLOCK_MONOTONIC, &t0);
}
// ----------------------------------------------------------------------------

243
src/test/testprefs.c Normal file
View File

@ -0,0 +1,243 @@
/*
* Apple // emulator for *ix
*
* This software package is subject to the GNU General Public License
* version 3 or later (your choice) as published by the Free Software
* Foundation.
*
* Copyright 2016 Aaron Culliney
*
*/
#include "testcommon.h"
static bool test_thread_running = false;
extern pthread_mutex_t interface_mutex; // TODO FIXME : raw access to CPU mutex because stepping debugger ...
static void testprefs_setup(void *unused) {
}
static void testprefs_teardown(void *unused) {
}
static const char *get_default_preferences(void) {
return
"{\n"
" \"cpu\" : {\n"
" \"speed\" : 1.0,\n"
" \"altspeed\" : 4.0\n"
" },\n"
" \"disk\" : {\n"
" \"diskPath\" : \"/usr/local/games/apple2/disks\"\n"
" },\n"
" \"video\" : {\n"
" \"color\" : \"interpolated\"\n"
" },\n"
" \"speaker\" : {\n"
" \"volume\" : 5\n"
" },\n"
" \"joystick\" : {\n"
" \"variant\" : \"keypad\",\n"
" \"pcJoystickParms\" : \"128 128 255 1 255 1\",\n"
" \"kpJoystickParms\" : \"8 1\"\n"
" },\n"
" \"keyboard\" : {\n"
" \"caps\" : true\n"
" }\n"
"}\n"
;
}
// ----------------------------------------------------------------------------
// JSON/prefs tests ...
TEST test_json_map_1() {
const char *testMapStr0 =
" { "
" \"key0\" : \"a value zero\", "
" \"key1\" : \" \", "
" \"key2\" : { \n"
"} , "
" \"key3\" : { \n"
" \"subkey0\" : \"subval0\", "
" \"subkey1\" : { \"moar\" : \"recursion\" } , "
" \"subkey2\" : \"line0 \n"
" line1 "
" line2\" \n"
"}, "
" \"key4\" : [ \"Q\", \"W\", \"E\", "
" \"R\", \"T\", \"Y\", \"U\", \"I\", "
" \"O\", \"P\", { \"x\" : [ 22, 4, \"ab\" ] } ],"
" \"intKey0\" : 42 , "
" \"key5\" : \"\" , "
" \"intKey1\" : -101, "
" \"intKey2\" : -000000000, "
" \"intKey3\" : 0x2400, "
" \"intKey4\" : 10111111, "
" \"floatKey0\" : 0.0 , "
" \"floatKey1\" : -.0001220703125 ,"
" \"floatKey2\" : 3.1415928 , "
" \"floatKey3\" : -3.1e2 "
" } "
;
JSON_s parsedData = { 0 };
int tokCount = json_createFromString(testMapStr0, &parsedData);
if (tokCount < 0) {
return 1;
}
long lVal;
float fVal;
char *val;
json_mapParseLongValue(&parsedData, "intKey2", &lVal, 10);
ASSERT(lVal == 0);
json_mapCopyStringValue(&parsedData, "key0", &val);
ASSERT(strcmp(val, "a value zero") == 0);
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key1", &val);
ASSERT(strcmp(val, " \t ") == 0);
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key2", &val);
ASSERT(strcmp(val, "{ \t \n}") == 0);
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key3", &val);
ASSERT(strcmp(val, "{ \t \n \"subkey0\" : \"subval0\", \"subkey1\" : { \"moar\" : \"recursion\" } , \"subkey2\" : \"line0 \n \tline1 \tline2\" \n}") == 0);
{
JSON_s parsedSubData = { 0 };
int tokSubCount = json_createFromString(val, &parsedSubData);
if (tokSubCount < 0) {
return 1;
}
char *subval;
json_mapCopyStringValue(&parsedSubData, "subkey0", &subval);
ASSERT(strcmp(subval, "subval0") == 0);
STRDUP_FREE(subval);
json_mapCopyStringValue(&parsedSubData, "subkey1", &subval);
ASSERT(strcmp(subval, "{ \"moar\" : \"recursion\" }") == 0);
STRDUP_FREE(subval);
json_mapCopyStringValue(&parsedSubData, "subkey2", &subval);
ASSERT(strcmp(subval, "line0 \n \tline1 \tline2") == 0);
STRDUP_FREE(subval);
}
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key4", &val);
ASSERT(strcmp(val, "[ \"Q\", \"W\", \"E\", \"R\", \"T\", \"Y\", \"U\", \"I\", \"O\", \"P\", { \"x\" : [ 22, 4, \"ab\" ] } ]") == 0);
// TODO : subarray checks
STRDUP_FREE(val);
json_mapCopyStringValue(&parsedData, "key5", &val);
ASSERT(strcmp(val, "") == 0);
STRDUP_FREE(val);
json_mapParseLongValue(&parsedData, "intKey0", &lVal, 10);
ASSERT(lVal == 42);
json_mapParseLongValue(&parsedData, "intKey1", &lVal, 10);
ASSERT(lVal == -101);
json_mapParseLongValue(&parsedData, "intKey3", &lVal, 16);
ASSERT(lVal == 0x2400);
json_mapParseLongValue(&parsedData, "intKey4", &lVal, 2);
ASSERT(lVal == 191);
json_mapParseLongValue(&parsedData, "intKey4", &lVal, 10);
ASSERT(lVal == 10111111);
json_mapParseFloatValue(&parsedData, "floatKey0", &fVal);
ASSERT(fVal == 0.f);
json_mapParseFloatValue(&parsedData, "floatKey1", &fVal);
ASSERT(fVal == -.0001220703125);
json_mapParseFloatValue(&parsedData, "floatKey2", &fVal);
ASSERT((long)(fVal*10000000) == 31415928);
json_mapParseFloatValue(&parsedData, "floatKey3", &fVal);
ASSERT((long)fVal == -310);
json_destroy(&parsedData);
PASS();
}
#if 0
TEST test_prefs_loadString_1() {
const char *prefsJSON = get_default_preferences();
bool loaded = prefs_loadString(prefsJSON);
ASSERT(loaded);
}
#endif
// ----------------------------------------------------------------------------
// Test Suite
GREATEST_SUITE(test_suite_prefs) {
GREATEST_SET_SETUP_CB(testprefs_setup, NULL);
GREATEST_SET_TEARDOWN_CB(testprefs_teardown, NULL);
GREATEST_SET_BREAKPOINT_CB(test_breakpoint, NULL);
// TESTS --------------------------
test_thread_running = true;
RUN_TESTp(test_json_map_1);
// --------------------------------
pthread_mutex_unlock(&interface_mutex);
}
SUITE(test_suite_prefs);
GREATEST_MAIN_DEFS();
static char **test_argv = NULL;
static int test_argc = 0;
static int _test_thread(void) {
int argc = test_argc;
char **argv = test_argv;
GREATEST_MAIN_BEGIN();
RUN_SUITE(test_suite_prefs);
GREATEST_MAIN_END();
}
static void *test_thread(void *dummyptr) {
_test_thread();
return NULL;
}
void test_prefs(int argc, char **argv) {
test_argc = argc;
test_argv = argv;
pthread_mutex_lock(&interface_mutex);
test_common_init();
pthread_t p;
pthread_create(&p, NULL, (void *)&test_thread, (void *)NULL);
while (!test_thread_running) {
struct timespec ts = { .tv_sec=0, .tv_nsec=33333333 };
nanosleep(&ts, NULL);
}
emulator_start();
//pthread_join(p, NULL);
}
#if !defined(__APPLE__) && !defined(ANDROID)
int main(int argc, char **argv) {
test_prefs(argc, argv);
}
#endif