treewide: restucture source tree per project

Signed-off-by: Max Regan <mgregan2@gmail.com>
This commit is contained in:
2017-04-20 18:12:28 -07:00
parent 6ba525b64b
commit e5acc3088c
24 changed files with 43 additions and 45 deletions

View File

@@ -5,7 +5,7 @@
#include "gbasm/types.h"
#include "gbasm/errors.h"
#include "gbasm/opcodes.h"
#include "common.h"
#include "common/common.h"
#define GBASM_MAX_INSTS 1024

View File

@@ -4,7 +4,7 @@
#include <unistd.h>
#include "gbasm/emitter.h"
#include "common.h"
#include "common/common.h"
int emit(struct emitter *emitter, const void *data, size_t size)
{

View File

@@ -2,8 +2,8 @@
#include "gbasm/opcodes.h"
#include "gbasm/errors.h"
#include "tri.h"
#include "common.h"
#include "common/tri.h"
#include "common/common.h"
#define MAX_OPCODE_LEN 10 /*TODO: Check that's enough */

View File

@@ -2,8 +2,8 @@
#include "gbasm/opcodes.h"
#include "gbasm/errors.h"
#include "tri.h"
#include "common.h"
#include "common/tri.h"
#include "common/common.h"
#include <string.h>

View File

@@ -4,7 +4,7 @@
#include <errno.h>
#include <stdbool.h>
#include "common.h"
#include "common/common.h"
#include "gbasm/parser.h"
#include "gbasm/gb_types.h"
#include "gbasm/operands.h"

51
src/gbasm/tests/fixed.c Normal file
View File

@@ -0,0 +1,51 @@
#include "gbasm/tests/test.h"
#include "common/common.h"
#define GEN_FIXED_TEST(_opcode, _hex)\
uint8_t _opcode##_output[] = _hex; \
static const struct gbasm_test _opcode = { \
.name = #_opcode, \
.asm_source = #_opcode, \
.expected_output = _opcode##_output, \
.expected_output_len = sizeof(_opcode##_output), \
};
GEN_FIXED_TEST(nop, {0x00})
GEN_FIXED_TEST(halt, {0x76})
GEN_FIXED_TEST(stop, {0x10})
GEN_FIXED_TEST(di, {0xf3})
GEN_FIXED_TEST(ei, {0xfb})
GEN_FIXED_TEST(rla, {0x17})
GEN_FIXED_TEST(rra, {0x1f})
GEN_FIXED_TEST(rlca, {0x07})
GEN_FIXED_TEST(rrca, {0x0f})
GEN_FIXED_TEST(daa, {0x27})
GEN_FIXED_TEST(scf, {0x37})
GEN_FIXED_TEST(reti, {0xd9})
GEN_FIXED_TEST(cpl, {0x2f})
GEN_FIXED_TEST(ccf, {0x3f})
static const struct gbasm_test *tests[] = {
&nop,
&halt,
&stop,
&di,
&ei,
&rla,
&rra,
&rrca,
&daa,
&scf,
&reti,
&cpl,
&ccf,
};
struct gbasm_tests gbasm_fixed_tests = {
.name = "fixed",
.num_tests = ARRAY_SIZE(tests),
.tests = tests,
};

9
src/gbasm/tests/fixed.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef GBASM_TEST_FIXED_H
#define GBASM_TEST_FIXED_H
#include "gbasm/tests/test.h"
#include "common/common.h"
extern const struct gbasm_tests gbasm_fixed_tests;
#endif

41
src/gbasm/tests/inc.c Normal file
View File

@@ -0,0 +1,41 @@
#include "gbasm/tests/test.h"
#include "common/common.h"
/* TODO: There is probably a better way to do this */
static const char all_src[] =
"INC A\n"
"INC B\n"
"INC C\n"
"INC D\n"
"INC E\n"
"INC H\n"
"INC L\n"
"INC BC\n"
"INC DE\n"
"INC HL\n"
"INC SP\n"
"INC (HL)\n";
static const uint8_t all_output[] = {
0x3c, 0x04, 0x0c, 0x14,
0x1c, 0x24, 0x2c, 0x03,
0x13, 0x23, 0x33, 0x34,
};
static const struct gbasm_test all = {
.name = "all",
.asm_source = all_src,
.expected_output = all_output,
.expected_output_len = sizeof(all_output),
};
static const struct gbasm_test *tests[] = {
&all,
};
struct gbasm_tests gbasm_inc_tests = {
.name = "inc",
.num_tests = ARRAY_SIZE(tests),
.tests = tests,
};

9
src/gbasm/tests/inc.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef GBASM_TEST_INC_H
#define GBASM_TEST_INC_H
#include "gbasm/tests/test.h"
#include "common/common.h"
extern const struct gbasm_tests gbasm_inc_tests;
#endif

79
src/gbasm/tests/test.c Normal file
View File

@@ -0,0 +1,79 @@
#include "gbasm/assemble.h"
#include "gbasm/emitter.h"
#include "gbasm/tests/test.h"
#include "gbasm/tests/inc.h"
#include "gbasm/tests/fixed.h"
#include "common/common.h"
#include <string.h> /* memset */
#include <glib.h>
const static struct gbasm_tests *tests[] = {
&gbasm_fixed_tests,
&gbasm_inc_tests,
};
static int cmp_mem(const uint8_t *test_data, const uint8_t *expected_data, size_t len)
{
for (size_t i = 0; i < len; i++) {
if (test_data[i] != expected_data[i]) {
g_test_message("memory contents differed at index %zu, data=%x, expected=%x",
i, test_data[i], expected_data[i]);
return -1;
}
}
return 0;
}
static void run_test(const void *test_data)
{
static uint8_t program[4096];
const struct gbasm_test *test = test_data;
struct emitter emitter;
struct buffer_emitter buffer_emitter;
char *src;
int rc;
size_t len;
src = strdup(test->asm_source);
memset(program, 0, sizeof(program));
buffer_emitter_init(&emitter, &buffer_emitter, program, sizeof(program));
rc = gbasm_assemble(src, &emitter);
g_assert_cmpint(rc, ==, 0);
len = test->expected_output_len;
if (len != buffer_emitter.cursor) {
g_warning("output lengths were not equal expected %zu, actual %zu",
len, buffer_emitter.cursor);
g_test_fail();
len = MIN(len, buffer_emitter.cursor);
}
rc = cmp_mem(program, test->expected_output, len);
g_assert_cmpint(rc, ==, 0);
free(src);
}
int main(int argc, char **argv)
{
char test_name_buff[256];
g_test_init(&argc, &argv, NULL);
for (int i = 0; i < ARRAY_SIZE(tests); i++) {
for (int j = 0; j < tests[i]->num_tests; j++) {
snprintf(test_name_buff, sizeof(test_name_buff),
"/gbasm/%s/%s",
tests[i]->name,
tests[i]->tests[j]->name);
g_test_add_data_func(test_name_buff, tests[i]->tests[j], run_test);
}
}
return g_test_run();
}

19
src/gbasm/tests/test.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef GBASM_TEST_H
#define GBASM_TEST_H
#include <stdint.h>
struct gbasm_test {
const char *name;
const char *asm_source;
const uint8_t *expected_output;
const int expected_output_len;
};
struct gbasm_tests {
const char *name;
int num_tests;
const struct gbasm_test **tests;
};
#endif