178 lines
6.3 KiB
C
178 lines
6.3 KiB
C
#include "unity.h"
|
|
#include "memory.h"
|
|
|
|
#include "exceptions.h"
|
|
#include "bus.h"
|
|
|
|
CEXCEPTION_T e;
|
|
|
|
struct BUS_instance *bus;
|
|
void setUp(void) {
|
|
bus = BUS_new();
|
|
TEST_ASSERT_NOT_NULL(bus);
|
|
}
|
|
|
|
void tearDown(void) {
|
|
BUS_delete(&bus);
|
|
TEST_ASSERT_NULL(bus);
|
|
}
|
|
|
|
void test_bus_new_delete(void) {
|
|
TEST_PASS();
|
|
}
|
|
|
|
void test_bus_add_device(void) {
|
|
struct MEMORY_instance *memory = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory);
|
|
BUS_add_device(bus, 0, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
MEMORY_delete(&memory);
|
|
}
|
|
|
|
void test_bus_read_device(void) {
|
|
struct MEMORY_instance *memory = MEMORY_new(2, false);
|
|
MEMORY_write(memory, 0, 0x12);
|
|
MEMORY_write(memory, 1, 0x21);
|
|
TEST_ASSERT_NOT_NULL(memory);
|
|
BUS_add_device(bus, 0, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
TEST_ASSERT_EQUAL(0x12, BUS_read(bus, 0));
|
|
TEST_ASSERT_EQUAL(0x21, BUS_read(bus, 1));
|
|
MEMORY_delete(&memory);
|
|
}
|
|
|
|
void test_bus_read_device_out_of_bounds(void) {
|
|
struct MEMORY_instance *memory = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory);
|
|
BUS_add_device(bus, 0x0000, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_add_device(bus, 0x0004, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_read(bus, 0);
|
|
BUS_read(bus, 1);
|
|
BUS_read(bus, 4);
|
|
BUS_read(bus, 5);
|
|
Try {
|
|
BUS_read(bus, 2);
|
|
TEST_FAIL_MESSAGE("Expected exception to be thrown");
|
|
} Catch(e) {
|
|
TEST_ASSERT_EQUAL(E_INDEX_OUT_OF_BOUNDS, e);
|
|
}
|
|
MEMORY_delete(&memory);
|
|
}
|
|
|
|
void test_bus_write_device(void) {
|
|
struct MEMORY_instance *memory = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory);
|
|
BUS_add_device(bus, 0x0000, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_write(bus, 0, 0x12);
|
|
BUS_write(bus, 1, 0x21);
|
|
TEST_ASSERT_EQUAL(0x12, MEMORY_read(memory, 0));
|
|
TEST_ASSERT_EQUAL(0x21, MEMORY_read(memory, 1));
|
|
MEMORY_delete(&memory);
|
|
}
|
|
|
|
void test_bus_write_device_out_of_bounds(void) {
|
|
struct MEMORY_instance *memory = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory);
|
|
BUS_add_device(bus, 0x0000, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_add_device(bus, 0x0004, 0x0002, memory, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_write(bus, 0, 0x12);
|
|
BUS_write(bus, 1, 0x21);
|
|
BUS_write(bus, 4, 0x12);
|
|
BUS_write(bus, 5, 0x21);
|
|
Try {
|
|
BUS_write(bus, 2, 0x12);
|
|
TEST_FAIL_MESSAGE("Expected exception to be thrown");
|
|
} Catch(e) {
|
|
TEST_ASSERT_EQUAL(E_INDEX_OUT_OF_BOUNDS, e);
|
|
}
|
|
MEMORY_delete(&memory);
|
|
}
|
|
|
|
void test_bus_multiple_devices(void) {
|
|
struct MEMORY_instance *memory1 = MEMORY_new(2, false);
|
|
struct MEMORY_instance *memory2 = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory1);
|
|
TEST_ASSERT_NOT_NULL(memory2);
|
|
|
|
BUS_add_device(bus, 0x0000, 0x0002, memory1, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_add_device(bus, 0x0004, 0x0002, memory2, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
|
|
BUS_write(bus, 0, 0x12);
|
|
BUS_write(bus, 1, 0x21);
|
|
BUS_write(bus, 4, 0x34);
|
|
BUS_write(bus, 5, 0x43);
|
|
TEST_ASSERT_EQUAL(0x12, MEMORY_read(memory1, 0));
|
|
TEST_ASSERT_EQUAL(0x21, MEMORY_read(memory1, 1));
|
|
TEST_ASSERT_EQUAL(0x34, MEMORY_read(memory2, 0));
|
|
TEST_ASSERT_EQUAL(0x43, MEMORY_read(memory2, 1));
|
|
MEMORY_delete(&memory1);
|
|
MEMORY_delete(&memory2);
|
|
}
|
|
|
|
void test_bus_overlapping_devices(void) {
|
|
struct MEMORY_instance *memory1 = MEMORY_new(6, false);
|
|
struct MEMORY_instance *memory2 = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory1);
|
|
TEST_ASSERT_NOT_NULL(memory2);
|
|
|
|
BUS_add_device(bus, 0x0000, 0x0006, memory1, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_add_device(bus, 0x0002, 0x0002, memory2, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
|
|
BUS_write(bus, 0, 0x12);
|
|
BUS_write(bus, 1, 0x21);
|
|
BUS_write(bus, 2, 0x34);
|
|
BUS_write(bus, 3, 0x43);
|
|
BUS_write(bus, 4, 0x56);
|
|
BUS_write(bus, 5, 0x65);
|
|
TEST_ASSERT_EQUAL_HEX8(0x12, MEMORY_read(memory1, 0));
|
|
TEST_ASSERT_EQUAL_HEX8(0x21, MEMORY_read(memory1, 1));
|
|
TEST_ASSERT_EQUAL_HEX8(0x34, MEMORY_read(memory2, 0));
|
|
TEST_ASSERT_EQUAL_HEX8(0x43, MEMORY_read(memory2, 1));
|
|
TEST_ASSERT_EQUAL_HEX8(0x56, MEMORY_read(memory1, 4));
|
|
TEST_ASSERT_EQUAL_HEX8(0x65, MEMORY_read(memory1, 5));
|
|
MEMORY_delete(&memory1);
|
|
MEMORY_delete(&memory2);
|
|
}
|
|
|
|
void test_bus_overlapping_devices_reverse_add_order(void) {
|
|
struct MEMORY_instance *memory1 = MEMORY_new(6, false);
|
|
struct MEMORY_instance *memory2 = MEMORY_new(2, false);
|
|
TEST_ASSERT_NOT_NULL(memory1);
|
|
TEST_ASSERT_NOT_NULL(memory2);
|
|
|
|
BUS_add_device(bus, 0x0002, 0x0002, memory2, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
BUS_add_device(bus, 0x0000, 0x0006, memory1, (COMMON_read_func) MEMORY_read, (COMMON_write_func) MEMORY_write);
|
|
|
|
BUS_write(bus, 0, 0x12);
|
|
BUS_write(bus, 1, 0x21);
|
|
BUS_write(bus, 2, 0x34);
|
|
BUS_write(bus, 3, 0x43);
|
|
BUS_write(bus, 4, 0x56);
|
|
BUS_write(bus, 5, 0x65);
|
|
TEST_ASSERT_EQUAL_HEX8(0x12, MEMORY_read(memory1, 0));
|
|
TEST_ASSERT_EQUAL_HEX8(0x21, MEMORY_read(memory1, 1));
|
|
TEST_ASSERT_EQUAL_HEX8(0x34, MEMORY_read(memory2, 0));
|
|
TEST_ASSERT_EQUAL_HEX8(0x43, MEMORY_read(memory2, 1));
|
|
TEST_ASSERT_EQUAL_HEX8(0x56, MEMORY_read(memory1, 4));
|
|
TEST_ASSERT_EQUAL_HEX8(0x65, MEMORY_read(memory1, 5));
|
|
MEMORY_delete(&memory1);
|
|
MEMORY_delete(&memory2);
|
|
}
|
|
|
|
int main(void) {
|
|
UNITY_BEGIN();
|
|
Try {
|
|
RUN_TEST(test_bus_new_delete);
|
|
RUN_TEST(test_bus_add_device);
|
|
RUN_TEST(test_bus_read_device);
|
|
RUN_TEST(test_bus_read_device_out_of_bounds);
|
|
RUN_TEST(test_bus_write_device);
|
|
RUN_TEST(test_bus_write_device_out_of_bounds);
|
|
RUN_TEST(test_bus_multiple_devices);
|
|
RUN_TEST(test_bus_overlapping_devices);
|
|
RUN_TEST(test_bus_overlapping_devices_reverse_add_order);
|
|
} Catch(e) {
|
|
printf("Exception thrown: %s(%d)\n", EXCEPTIONS_messages[e], e);
|
|
TEST_FAIL_MESSAGE("Unexpected exception thrown");
|
|
}
|
|
return UNITY_END();
|
|
}
|