From 5d12243454edeb96aaa4008864f8bbab7ffd204f Mon Sep 17 00:00:00 2001 From: Bernhard Luedtke Date: Fri, 10 Mar 2023 12:18:45 +0100 Subject: [PATCH 1/3] Refactor duplic code of phys test to common file --- CMakeLists.txt | 4 +- test/physical/swtbahn-full/testsuite.c | 629 +++++++-------------- test/physical/swtbahn-full/testsuite.h | 37 +- test/physical/swtbahn-standard/testsuite.c | 277 ++------- test/physical/swtbahn-standard/testsuite.h | 19 +- test/physical/test_common.c | 263 +++++++++ test/physical/test_common.h | 86 +++ 7 files changed, 595 insertions(+), 720 deletions(-) create mode 100644 test/physical/test_common.c create mode 100644 test/physical/test_common.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ef582b0..5834539 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -135,10 +135,10 @@ IF (${BIDIB_USE_TESTS}) # Physical test - ADD_EXECUTABLE(swtbahn-standard-testsuite test test/physical/swtbahn-standard/main.c test/physical/swtbahn-standard/testsuite.c) + ADD_EXECUTABLE(swtbahn-standard-testsuite test test/physical/swtbahn-standard/main.c test/physical/swtbahn-standard/testsuite.c test/physical/test_common.c) TARGET_LINK_LIBRARIES(swtbahn-standard-testsuite glib-2.0 pthread yaml bidib_static) - ADD_EXECUTABLE(swtbahn-full-testsuite test test/physical/swtbahn-full/main.c test/physical/swtbahn-full/testsuite.c) + ADD_EXECUTABLE(swtbahn-full-testsuite test test/physical/swtbahn-full/main.c test/physical/swtbahn-full/testsuite.c test/physical/test_common.c) TARGET_LINK_LIBRARIES(swtbahn-full-testsuite glib-2.0 pthread yaml bidib_static) ENDIF(${BIDIB_USE_TESTS}) diff --git a/test/physical/swtbahn-full/testsuite.c b/test/physical/swtbahn-full/testsuite.c index 8195647..7810774 100644 --- a/test/physical/swtbahn-full/testsuite.c +++ b/test/physical/swtbahn-full/testsuite.c @@ -38,241 +38,24 @@ #include "testsuite.h" - -#define SIGNAL_WAITING_TIME 3 // in seconds -#define POINT_WAITING_TIME 3 // in seconds -#define TRAIN_WAITING_TIME 250000 // in microseconds - -t_bidib_id_list_query points; -t_bidib_id_list_query signals; - // This initialisation function is specific to SWTbahn Full! t_testsuite_test_result *testsuite_initTestSuite() { - points = bidib_get_connected_points(); - - // Accessories that are not signals - t_testsuite_ids filterOutIds; char *excludedSignalAccessories[4] = {"platformlight1", "platformlight2", "platformlight4a", "platformlight4b"}; - filterOutIds.ids = excludedSignalAccessories; - filterOutIds.length = 4; - t_bidib_id_list_query signalsQuery = bidib_get_connected_signals(); - signals = testsuite_filterOutIds(signalsQuery, filterOutIds); - bidib_free_id_list_query(signalsQuery); - - t_testsuite_test_result *result = malloc(sizeof(t_testsuite_test_result)); - result->points = malloc(points.length * sizeof(t_testsuite_point_result)); - - for (size_t i = 0; i < points.length; i++) { - result->points[i].stateReachedVerified = 0; - result->points[i].stateReached = 0; - result->points[i].stateNotReachedVerified = 0; - result->points[i].stateNotReached = 0; - result->points[i].stateError = 0; - result->points[i].unknownState = 0; - } + t_testsuite_test_result *result = testsuite_initTestSuite_common(excludedSignalAccessories, 4); return result; } -void testsuite_stopBidib(void) { - bidib_free_id_list_query(points); - bidib_free_id_list_query(signals); - bidib_stop(); -} - -void testsuite_signal_callback_handler(int signum) { - testsuite_stopBidib(); - printf("testsuite: SIGINT - stopping libbidib \n"); - exit(signum); -} - -t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, t_testsuite_ids filterOutIds) { - const size_t count = inputIdQuery.length - filterOutIds.length; - - if (count <= 0) { - printf("testsuite: No IDs will be left after filtering\n"); - } - - t_bidib_id_list_query outputIdQuery; - outputIdQuery.length = 0; - outputIdQuery.ids = malloc(sizeof(char *) * count); - - int isFilteredOut = 0; - - for (size_t i = 0; i < inputIdQuery.length; i++) { - isFilteredOut = 0; - for (size_t j = 0; j < filterOutIds.length; j++) { - if (!strcmp(inputIdQuery.ids[i], filterOutIds.ids[j])) { - isFilteredOut = 1; - break; - } - } - - if (!isFilteredOut) { - outputIdQuery.ids[outputIdQuery.length] = malloc(strlen(inputIdQuery.ids[i]) * sizeof(char) + 1) ; - memcpy(outputIdQuery.ids[outputIdQuery.length], inputIdQuery.ids[i], strlen(inputIdQuery.ids[i]) * sizeof(char) + 1); - outputIdQuery.length++; - } - } - - if (outputIdQuery.length != count) { - printf("testsuite: Error: %zu IDs were to be filtered, but %d IDs filtered instead\n", filterOutIds.length, (int)inputIdQuery.length - (int)outputIdQuery.length); - } - - return outputIdQuery; -} - -void testsuite_logTestResult(t_testsuite_test_result *result, t_bidib_unified_accessory_state_query state, int accessory_index) { - if (state.known) { - switch (state.board_accessory_state.execution_state) { - case BIDIB_EXEC_STATE_ERROR: - result->points[accessory_index].stateError++; - break; - case BIDIB_EXEC_STATE_NOTREACHED: - result->points[accessory_index].stateNotReached++; - break; - case BIDIB_EXEC_STATE_NOTREACHED_VERIFIED: - result->points[accessory_index].stateNotReachedVerified++; - break; - case BIDIB_EXEC_STATE_REACHED: - result->points[accessory_index].stateReached++; - break; - case BIDIB_EXEC_STATE_REACHED_VERIFIED: - result->points[accessory_index].stateReachedVerified++; - break; - default: - break; - } - } else { - result->points[accessory_index].unknownState++; - } -} - -void testsuite_printTestResults(t_testsuite_test_result *result) { - for (size_t i = 0; i < points.length; i++) { - printf("\n\n%s\n", points.ids[i]); - printf(" -> stateReachedVerified: %d \n", result->points[i].stateReachedVerified); - printf(" -> stateReached: %d \n", result->points[i].stateReached); - printf(" -> stateNotReachedVerified: %d \n", result->points[i].stateNotReachedVerified); - printf(" -> stateNotReached: %d \n", result->points[i].stateNotReached); - printf(" -> stateError: %d \n", result->points[i].stateError); - printf(" -> unknownState: %d \n", result->points[i].unknownState); - } -} - -bool testsuite_trainReady(const char *train, const char *segment) { - if (bidib_get_train_on_track(train)) { - t_bidib_train_position_query train_position_query = - bidib_get_train_position(train); - if (train_position_query.length > 0) { - for (size_t i = 0; i < train_position_query.length; i++) { - if (strcmp(segment, train_position_query.segments[i]) == 0) { - printf("testsuite: %s train ready on %s \n", train, segment); - bidib_free_train_position_query(train_position_query); - return true; - } - } - } - - printf("testsuite: %s train not on track segment %s \n", train, segment); - bidib_free_train_position_query(train_position_query); - return false; - } else { - printf("testsuite: %s train not detected on any track \n", train); - return false; - } -} - -void testsuite_driveTo(const char *segment, int speed, const char *train) { - bidib_set_train_speed(train, speed, "master"); - bidib_flush(); - - while (1) { - t_bidib_train_position_query trainPosition = bidib_get_train_position(train); - for (size_t i = 0; i < trainPosition.length; i++) { - if (!strcmp(segment, trainPosition.segments[i])) { - bidib_free_train_position_query(trainPosition); - return; - } - } - bidib_free_train_position_query(trainPosition); - usleep(TRAIN_WAITING_TIME); - } -} - -void testsuite_driveToStop(const char *segment, int speed, const char *train) { - testsuite_driveTo(segment, speed, train); - bidib_set_train_speed(train, 0, "master"); - bidib_flush(); -} - -void set_signal(const char *signal, const char *aspect) { - bidib_set_signal(signal, aspect); - bidib_flush(); -} - -void switch_point(const char *point, const char *aspect) { - bidib_switch_point(point, aspect); - bidib_flush(); -} - void testsuite_case_signal() { - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_caution"); - } - sleep(SIGNAL_WAITING_TIME); - - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_go"); - } - sleep(SIGNAL_WAITING_TIME); - - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_stop"); - } - sleep(SIGNAL_WAITING_TIME); - - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_shunt"); - } - sleep(SIGNAL_WAITING_TIME); - + char *signalAspects[4] = {"aspect_caution", "aspect_go", "aspect_stop", "aspect_shunt"}; + testsuite_case_signal_common(signalAspects, 4); } void testsuite_case_pointParallel(t_testsuite_test_result *result) { - for (size_t i = 0; i < points.length; i++) { - switch_point(points.ids[i], "reverse"); - t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - } - - sleep(POINT_WAITING_TIME); - - for (size_t i = 0; i < points.length; i++) { - switch_point(points.ids[i], "normal"); - t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - } - - sleep(POINT_WAITING_TIME); + testsuite_case_pointParallel_common(result); } void testsuite_case_pointSerial(t_testsuite_test_result *result) { - for (size_t i = 0; i < points.length; i++) { - switch_point(points.ids[i], "reverse"); - t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - sleep(POINT_WAITING_TIME); - - switch_point(points.ids[i], "normal"); - state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - sleep(POINT_WAITING_TIME); - } - + testsuite_case_pointSerial_common(result); } bool route1(const char *train) { @@ -280,56 +63,56 @@ bool route1(const char *train) { return false; } - switch_point("point22", "reverse"); - switch_point("point23", "normal"); - switch_point("point24", "reverse"); - switch_point("point12", "reverse"); - switch_point("point13", "reverse"); - switch_point("point14", "reverse"); - switch_point("point15", "normal"); - switch_point("point16", "reverse"); - switch_point("point21", "reverse"); - switch_point("point20", "normal"); - switch_point("point19", "normal"); - switch_point("point18b", "reverse"); - - set_signal("signal30", "aspect_go"); - set_signal("signal33", "aspect_go"); - set_signal("signal35a", "aspect_go"); - set_signal("signal35b", "aspect_go"); - set_signal("signal37", "aspect_go"); + testsuite_switch_point("point22", "reverse"); + testsuite_switch_point("point23", "normal"); + testsuite_switch_point("point24", "reverse"); + testsuite_switch_point("point12", "reverse"); + testsuite_switch_point("point13", "reverse"); + testsuite_switch_point("point14", "reverse"); + testsuite_switch_point("point15", "normal"); + testsuite_switch_point("point16", "reverse"); + testsuite_switch_point("point21", "reverse"); + testsuite_switch_point("point20", "normal"); + testsuite_switch_point("point19", "normal"); + testsuite_switch_point("point18b", "reverse"); + + testsuite_set_signal("signal30", "aspect_go"); + testsuite_set_signal("signal33", "aspect_go"); + testsuite_set_signal("signal35a", "aspect_go"); + testsuite_set_signal("signal35b", "aspect_go"); + testsuite_set_signal("signal37", "aspect_go"); testsuite_driveTo("seg57", 50, train); - set_signal("signal30", "aspect_stop"); + testsuite_set_signal("signal30", "aspect_stop"); testsuite_driveTo("seg64", 50, train); - set_signal("signal33", "aspect_stop"); - set_signal("signal35a", "aspect_stop"); - set_signal("signal35b", "aspect_stop"); + testsuite_set_signal("signal33", "aspect_stop"); + testsuite_set_signal("signal35a", "aspect_stop"); + testsuite_set_signal("signal35b", "aspect_stop"); testsuite_driveTo("seg69", 50, train); - set_signal("signal37", "aspect_stop"); + testsuite_set_signal("signal37", "aspect_stop"); testsuite_driveTo("seg46", 50, train); testsuite_driveToStop("seg47", 20, train); // Drive backwards through the route - set_signal("signal26", "aspect_go"); - set_signal("signal38", "aspect_go"); - set_signal("signal36", "aspect_go"); - set_signal("signal34", "aspect_go"); - set_signal("signal32", "aspect_go"); + testsuite_set_signal("signal26", "aspect_go"); + testsuite_set_signal("signal38", "aspect_go"); + testsuite_set_signal("signal36", "aspect_go"); + testsuite_set_signal("signal34", "aspect_go"); + testsuite_set_signal("signal32", "aspect_go"); testsuite_driveTo("seg45", -50, train); - set_signal("signal26", "aspect_stop"); + testsuite_set_signal("signal26", "aspect_stop"); testsuite_driveTo("seg67", -50, train); - set_signal("signal38", "aspect_stop"); - set_signal("signal36", "aspect_stop"); + testsuite_set_signal("signal38", "aspect_stop"); + testsuite_set_signal("signal36", "aspect_stop"); testsuite_driveTo("seg62", -50, train); - set_signal("signal34", "aspect_stop"); - set_signal("signal32", "aspect_stop"); + testsuite_set_signal("signal34", "aspect_stop"); + testsuite_set_signal("signal32", "aspect_stop"); testsuite_driveTo("seg58", -50, train); testsuite_driveToStop("seg59", -20, train); @@ -341,50 +124,50 @@ bool route2(const char *train) { return false; } - switch_point("point22", "normal"); - switch_point("point20", "reverse"); - switch_point("point21", "reverse"); - switch_point("point16", "reverse"); - switch_point("point15", "reverse"); - switch_point("point5", "reverse"); - switch_point("point4", "reverse"); - switch_point("point12", "reverse"); - switch_point("point11", "reverse"); - switch_point("point27", "reverse"); - switch_point("point29", "reverse"); - switch_point("point28", "reverse"); - switch_point("point26", "normal"); - switch_point("point9", "reverse"); - switch_point("point8", "reverse"); - switch_point("point1", "reverse"); - switch_point("point7", "normal"); - switch_point("point6", "reverse"); - switch_point("point17", "reverse"); + testsuite_switch_point("point22", "normal"); + testsuite_switch_point("point20", "reverse"); + testsuite_switch_point("point21", "reverse"); + testsuite_switch_point("point16", "reverse"); + testsuite_switch_point("point15", "reverse"); + testsuite_switch_point("point5", "reverse"); + testsuite_switch_point("point4", "reverse"); + testsuite_switch_point("point12", "reverse"); + testsuite_switch_point("point11", "reverse"); + testsuite_switch_point("point27", "reverse"); + testsuite_switch_point("point29", "reverse"); + testsuite_switch_point("point28", "reverse"); + testsuite_switch_point("point26", "normal"); + testsuite_switch_point("point9", "reverse"); + testsuite_switch_point("point8", "reverse"); + testsuite_switch_point("point1", "reverse"); + testsuite_switch_point("point7", "normal"); + testsuite_switch_point("point6", "reverse"); + testsuite_switch_point("point17", "reverse"); testsuite_driveTo("seg42b", 50, train); - switch_point("point16", "normal"); - switch_point("point15", "normal"); - switch_point("point14", "reverse"); - switch_point("point13", "reverse"); - switch_point("point12", "reverse"); - switch_point("point24", "reverse"); - switch_point("point23", "reverse"); - switch_point("point19", "reverse"); - switch_point("point18b", "normal"); - switch_point("point18a", "reverse"); - switch_point("point8", "normal"); - switch_point("point9", "reverse"); - switch_point("point26", "reverse"); - switch_point("point27", "normal"); - switch_point("point11", "normal"); - switch_point("point3", "reverse"); - switch_point("point4", "normal"); - switch_point("point5", "normal"); + testsuite_switch_point("point16", "normal"); + testsuite_switch_point("point15", "normal"); + testsuite_switch_point("point14", "reverse"); + testsuite_switch_point("point13", "reverse"); + testsuite_switch_point("point12", "reverse"); + testsuite_switch_point("point24", "reverse"); + testsuite_switch_point("point23", "reverse"); + testsuite_switch_point("point19", "reverse"); + testsuite_switch_point("point18b", "normal"); + testsuite_switch_point("point18a", "reverse"); + testsuite_switch_point("point8", "normal"); + testsuite_switch_point("point9", "reverse"); + testsuite_switch_point("point26", "reverse"); + testsuite_switch_point("point27", "normal"); + testsuite_switch_point("point11", "normal"); + testsuite_switch_point("point3", "reverse"); + testsuite_switch_point("point4", "normal"); + testsuite_switch_point("point5", "normal"); testsuite_driveTo("seg69", 50, train); - switch_point("point6", "normal"); - switch_point("point7", "reverse"); + testsuite_switch_point("point6", "normal"); + testsuite_switch_point("point7", "reverse"); testsuite_driveTo("seg46", 50, train); testsuite_driveToStop("seg47", 20, train); @@ -397,22 +180,22 @@ bool route3(const char *train) { return false; } - switch_point("point18b", "reverse"); - switch_point("point19", "reverse"); - switch_point("point23", "reverse"); - switch_point("point24", "reverse"); - switch_point("point12", "normal"); - switch_point("point4", "reverse"); - switch_point("point5", "reverse"); - switch_point("point15", "reverse"); - switch_point("point16", "normal"); - switch_point("point17", "reverse"); - switch_point("point6", "reverse"); - switch_point("point7", "normal"); - switch_point("point1", "reverse"); - switch_point("point8", "reverse"); - switch_point("point9", "normal"); - switch_point("point10", "reverse"); + testsuite_switch_point("point18b", "reverse"); + testsuite_switch_point("point19", "reverse"); + testsuite_switch_point("point23", "reverse"); + testsuite_switch_point("point24", "reverse"); + testsuite_switch_point("point12", "normal"); + testsuite_switch_point("point4", "reverse"); + testsuite_switch_point("point5", "reverse"); + testsuite_switch_point("point15", "reverse"); + testsuite_switch_point("point16", "normal"); + testsuite_switch_point("point17", "reverse"); + testsuite_switch_point("point6", "reverse"); + testsuite_switch_point("point7", "normal"); + testsuite_switch_point("point1", "reverse"); + testsuite_switch_point("point8", "reverse"); + testsuite_switch_point("point9", "normal"); + testsuite_switch_point("point10", "reverse"); testsuite_driveTo("seg29", -50, train); testsuite_driveToStop("seg78b", -20, train); @@ -425,21 +208,21 @@ bool route4(const char *train) { return false; } - switch_point("point10", "reverse"); - switch_point("point9", "normal"); - switch_point("point8", "reverse"); - switch_point("point1", "reverse"); - switch_point("point7", "normal"); - switch_point("point6", "reverse"); - switch_point("point17", "reverse"); - switch_point("point16", "normal"); - switch_point("point15", "normal"); - switch_point("point14", "reverse"); - switch_point("point13", "reverse"); - switch_point("point12", "reverse"); - switch_point("point24", "reverse"); - switch_point("point23", "normal"); - switch_point("point22", "reverse"); + testsuite_switch_point("point10", "reverse"); + testsuite_switch_point("point9", "normal"); + testsuite_switch_point("point8", "reverse"); + testsuite_switch_point("point1", "reverse"); + testsuite_switch_point("point7", "normal"); + testsuite_switch_point("point6", "reverse"); + testsuite_switch_point("point17", "reverse"); + testsuite_switch_point("point16", "normal"); + testsuite_switch_point("point15", "normal"); + testsuite_switch_point("point14", "reverse"); + testsuite_switch_point("point13", "reverse"); + testsuite_switch_point("point12", "reverse"); + testsuite_switch_point("point24", "reverse"); + testsuite_switch_point("point23", "normal"); + testsuite_switch_point("point22", "reverse"); testsuite_driveTo("seg58", 50, train); testsuite_driveToStop("seg59", 20, train); @@ -452,19 +235,19 @@ bool route5(const char *train) { return false; } - switch_point("point22", "reverse"); - switch_point("point23", "normal"); - switch_point("point24", "reverse"); - switch_point("point12", "reverse"); - switch_point("point13", "reverse"); - switch_point("point14", "reverse"); - switch_point("point15", "normal"); - switch_point("point16", "reverse"); - switch_point("point21", "reverse"); - switch_point("point20", "reverse"); + testsuite_switch_point("point22", "reverse"); + testsuite_switch_point("point23", "normal"); + testsuite_switch_point("point24", "reverse"); + testsuite_switch_point("point12", "reverse"); + testsuite_switch_point("point13", "reverse"); + testsuite_switch_point("point14", "reverse"); + testsuite_switch_point("point15", "normal"); + testsuite_switch_point("point16", "reverse"); + testsuite_switch_point("point21", "reverse"); + testsuite_switch_point("point20", "reverse"); testsuite_driveTo("seg64", -50, train); - switch_point("point22", "normal"); + testsuite_switch_point("point22", "normal"); testsuite_driveTo("seg58", -50, train); testsuite_driveToStop("seg59", -20, train); @@ -503,39 +286,39 @@ static void *route99(void *arg) { while (true) { // train1: forwards - switch_point("point22", "reverse"); - switch_point("point23", "normal"); - switch_point("point24", "reverse"); - switch_point("point12", "reverse"); - switch_point("point13", "reverse"); - switch_point("point14", "reverse"); - switch_point("point15", "normal"); - switch_point("point16", "reverse"); - switch_point("point21", "reverse"); - switch_point("point20", "normal"); - switch_point("point19", "normal"); - switch_point("point18b", "reverse"); + testsuite_switch_point("point22", "reverse"); + testsuite_switch_point("point23", "normal"); + testsuite_switch_point("point24", "reverse"); + testsuite_switch_point("point12", "reverse"); + testsuite_switch_point("point13", "reverse"); + testsuite_switch_point("point14", "reverse"); + testsuite_switch_point("point15", "normal"); + testsuite_switch_point("point16", "reverse"); + testsuite_switch_point("point21", "reverse"); + testsuite_switch_point("point20", "normal"); + testsuite_switch_point("point19", "normal"); + testsuite_switch_point("point18b", "reverse"); sleep(1); - set_signal("signal30", "aspect_go"); - set_signal("signal33", "aspect_go"); - set_signal("signal35a", "aspect_go"); - set_signal("signal35b", "aspect_go"); - set_signal("signal37", "aspect_go"); + testsuite_set_signal("signal30", "aspect_go"); + testsuite_set_signal("signal33", "aspect_go"); + testsuite_set_signal("signal35a", "aspect_go"); + testsuite_set_signal("signal35b", "aspect_go"); + testsuite_set_signal("signal37", "aspect_go"); sleep(1); testsuite_driveTo("seg57", 50, train1); - set_signal("signal30", "aspect_stop"); + testsuite_set_signal("signal30", "aspect_stop"); testsuite_driveTo("seg64", 50, train1); - set_signal("signal33", "aspect_stop"); - set_signal("signal35a", "aspect_stop"); - set_signal("signal35b", "aspect_stop"); + testsuite_set_signal("signal33", "aspect_stop"); + testsuite_set_signal("signal35a", "aspect_stop"); + testsuite_set_signal("signal35b", "aspect_stop"); testsuite_driveTo("seg69", 50, train1); - set_signal("signal37", "aspect_stop"); + testsuite_set_signal("signal37", "aspect_stop"); testsuite_driveTo("seg46", 50, train1); sleep(1); @@ -548,24 +331,24 @@ static void *route99(void *arg) { sleep(5); // train1: backwards - set_signal("signal26", "aspect_go"); - set_signal("signal38", "aspect_go"); - set_signal("signal36", "aspect_go"); - set_signal("signal34", "aspect_go"); - set_signal("signal32", "aspect_go"); + testsuite_set_signal("signal26", "aspect_go"); + testsuite_set_signal("signal38", "aspect_go"); + testsuite_set_signal("signal36", "aspect_go"); + testsuite_set_signal("signal34", "aspect_go"); + testsuite_set_signal("signal32", "aspect_go"); sleep(1); testsuite_driveTo("seg45", -50, train1); - set_signal("signal26", "aspect_stop"); + testsuite_set_signal("signal26", "aspect_stop"); testsuite_driveTo("seg67", -50, train1); - set_signal("signal38", "aspect_stop"); - set_signal("signal36", "aspect_stop"); + testsuite_set_signal("signal38", "aspect_stop"); + testsuite_set_signal("signal36", "aspect_stop"); testsuite_driveTo("seg62", -50, train1); - set_signal("signal34", "aspect_stop"); - set_signal("signal32", "aspect_stop"); + testsuite_set_signal("signal34", "aspect_stop"); + testsuite_set_signal("signal32", "aspect_stop"); testsuite_driveTo("seg60", -50, train1); testsuite_driveTo("seg53", -40, train1); @@ -587,47 +370,47 @@ static void *route100(void *arg) { while (true) { // train2: forwards - switch_point("point10", "reverse"); - switch_point("point9", "normal"); - switch_point("point8", "reverse"); - switch_point("point1", "reverse"); - switch_point("point7", "normal"); - switch_point("point6", "normal"); - switch_point("point5", "normal"); - switch_point("point4", "normal"); - switch_point("point3", "reverse"); - switch_point("point11", "reverse"); + testsuite_switch_point("point10", "reverse"); + testsuite_switch_point("point9", "normal"); + testsuite_switch_point("point8", "reverse"); + testsuite_switch_point("point1", "reverse"); + testsuite_switch_point("point7", "normal"); + testsuite_switch_point("point6", "normal"); + testsuite_switch_point("point5", "normal"); + testsuite_switch_point("point4", "normal"); + testsuite_switch_point("point3", "reverse"); + testsuite_switch_point("point11", "reverse"); sleep(1); - set_signal("signal43", "aspect_shunt"); - set_signal("signal19", "aspect_go"); - set_signal("signal3", "aspect_go"); - set_signal("signal1", "aspect_go"); - set_signal("signal13", "aspect_go"); - set_signal("signal11", "aspect_go"); - set_signal("signal10", "aspect_go"); - set_signal("signal8", "aspect_go"); + testsuite_set_signal("signal43", "aspect_shunt"); + testsuite_set_signal("signal19", "aspect_go"); + testsuite_set_signal("signal3", "aspect_go"); + testsuite_set_signal("signal1", "aspect_go"); + testsuite_set_signal("signal13", "aspect_go"); + testsuite_set_signal("signal11", "aspect_go"); + testsuite_set_signal("signal10", "aspect_go"); + testsuite_set_signal("signal8", "aspect_go"); sleep(1); testsuite_driveTo("seg77", 126, train2); - set_signal("signal43", "aspect_stop"); + testsuite_set_signal("signal43", "aspect_stop"); testsuite_driveTo("seg26", 126, train2); - set_signal("signal19", "aspect_stop"); + testsuite_set_signal("signal19", "aspect_stop"); testsuite_driveTo("seg1", 126, train2); - set_signal("signal3", "aspect_stop"); - set_signal("signal1", "aspect_stop"); + testsuite_set_signal("signal3", "aspect_stop"); + testsuite_set_signal("signal1", "aspect_stop"); testsuite_driveTo("seg15", 126, train2); - set_signal("signal13", "aspect_stop"); - set_signal("signal11", "aspect_stop"); + testsuite_set_signal("signal13", "aspect_stop"); + testsuite_set_signal("signal11", "aspect_stop"); testsuite_driveTo("seg11", 126, train2); - set_signal("signal10", "aspect_stop"); - set_signal("signal8", "aspect_stop"); + testsuite_set_signal("signal10", "aspect_stop"); + testsuite_set_signal("signal8", "aspect_stop"); testsuite_driveTo("seg31b", 50, train2); sleep(1); @@ -640,36 +423,36 @@ static void *route100(void *arg) { sleep(5); // train2: backwards - set_signal("signal22a", "aspect_go"); - set_signal("signal22b", "aspect_go"); - set_signal("signal9", "aspect_go"); - set_signal("signal12", "aspect_go"); - set_signal("signal14", "aspect_go"); - set_signal("signal2", "aspect_go"); - set_signal("signal4a", "aspect_go"); - set_signal("signal4b", "aspect_go"); - set_signal("signal20", "aspect_shunt"); + testsuite_set_signal("signal22a", "aspect_go"); + testsuite_set_signal("signal22b", "aspect_go"); + testsuite_set_signal("signal9", "aspect_go"); + testsuite_set_signal("signal12", "aspect_go"); + testsuite_set_signal("signal14", "aspect_go"); + testsuite_set_signal("signal2", "aspect_go"); + testsuite_set_signal("signal4a", "aspect_go"); + testsuite_set_signal("signal4b", "aspect_go"); + testsuite_set_signal("signal20", "aspect_shunt"); sleep(1); testsuite_driveTo("seg32", -126, train2); - set_signal("signal22a", "aspect_stop"); - set_signal("signal22b", "aspect_stop"); + testsuite_set_signal("signal22a", "aspect_stop"); + testsuite_set_signal("signal22b", "aspect_stop"); testsuite_driveTo("seg13", -126, train2); - set_signal("signal9", "aspect_stop"); + testsuite_set_signal("signal9", "aspect_stop"); testsuite_driveTo("seg17", -126, train2); - set_signal("signal12", "aspect_stop"); - set_signal("signal14", "aspect_stop"); + testsuite_set_signal("signal12", "aspect_stop"); + testsuite_set_signal("signal14", "aspect_stop"); testsuite_driveTo("seg3", -126, train2); - set_signal("signal2", "aspect_stop"); - set_signal("signal4a", "aspect_stop"); - set_signal("signal4b", "aspect_stop"); + testsuite_set_signal("signal2", "aspect_stop"); + testsuite_set_signal("signal4a", "aspect_stop"); + testsuite_set_signal("signal4b", "aspect_stop"); testsuite_driveTo("seg28", -50, train2); - set_signal("signal20", "aspect_stop"); + testsuite_set_signal("signal20", "aspect_stop"); testsuite_driveTo("seg78a", -50, train2); sleep(1); @@ -694,24 +477,23 @@ void testsuite_case_swtbahnFullMultipleTrains(const char *train1, const char *tr pthread_join(route100_thread, NULL); } - bool route_custom_short(const char *train) { if (!testsuite_trainReady(train, "seg7a")) { return false; } - switch_point("point2", "normal"); - switch_point("point1", "normal"); - switch_point("point7", "normal"); - switch_point("point6", "normal"); - switch_point("point5", "normal"); - switch_point("point4", "normal"); - switch_point("point3", "normal"); + testsuite_switch_point("point2", "normal"); + testsuite_switch_point("point1", "normal"); + testsuite_switch_point("point7", "normal"); + testsuite_switch_point("point6", "normal"); + testsuite_switch_point("point5", "normal"); + testsuite_switch_point("point4", "normal"); + testsuite_switch_point("point3", "normal"); - set_signal("signal5", "aspect_go"); - set_signal("signal17", "aspect_go"); - set_signal("signal", "aspect_go"); + testsuite_set_signal("signal5", "aspect_go"); + testsuite_set_signal("signal17", "aspect_go"); + testsuite_set_signal("signal", "aspect_go"); testsuite_driveTo("seg58", 50, train); sleep(1); @@ -719,6 +501,7 @@ bool route_custom_short(const char *train) { sleep(2); return true; } + void testsuite_case_swtbahnFullShortRoute(const char *train) { sleep(1); diff --git a/test/physical/swtbahn-full/testsuite.h b/test/physical/swtbahn-full/testsuite.h index aa48b97..a5b7639 100644 --- a/test/physical/swtbahn-full/testsuite.h +++ b/test/physical/swtbahn-full/testsuite.h @@ -31,45 +31,10 @@ #ifndef TESTSUITE_H #define TESTSUITE_H - -#include "../../../include/bidib.h" - - -typedef struct { - int stateError; - int stateNotReached; - int stateNotReachedVerified; - int stateReached; - int stateReachedVerified; - int unknownState; -} t_testsuite_point_result; - -typedef struct { - t_testsuite_point_result *points; -} t_testsuite_test_result; - -typedef struct { - char **ids; - size_t length; -} t_testsuite_ids; - +#include "../test_common.h" // Setup t_testsuite_test_result *testsuite_initTestSuite(); -t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, t_testsuite_ids filterOutIds); - -// Teardown -void testsuite_stopBidib(void); -void testsuite_signal_callback_handler(int signum); - -// Logging -void testsuite_logTestResult(t_testsuite_test_result *result, t_bidib_unified_accessory_state_query state, int accessory_index); -void testsuite_printTestResults(t_testsuite_test_result *result); - -// Driving -bool testsuite_trainReady(const char *train, const char *segment); -void testsuite_driveTo(const char *segment, int speed, const char *train); -void testsuite_driveToStop(const char *segment, int speed, const char *train); // Test cases void testsuite_case_signal(); diff --git a/test/physical/swtbahn-standard/testsuite.c b/test/physical/swtbahn-standard/testsuite.c index 812027b..e7de35e 100644 --- a/test/physical/swtbahn-standard/testsuite.c +++ b/test/physical/swtbahn-standard/testsuite.c @@ -36,234 +36,25 @@ #include "testsuite.h" - -#define SIGNAL_WAITING_TIME 3 // in seconds -#define POINT_WAITING_TIME 3 // in seconds -#define TRAIN_WAITING_TIME 250000 // in microseconds - -t_bidib_id_list_query points; -t_bidib_id_list_query signals; - - // This initialisation function is specific to SWTbahn Standard! t_testsuite_test_result *testsuite_initTestSuite() { - points = bidib_get_connected_points(); - - // Accessories that are not signals - t_testsuite_ids filterOutIds; char *excludedSignalAccessories[1] = {"platformlights"}; - filterOutIds.ids = excludedSignalAccessories; - filterOutIds.length = 1; - signals = testsuite_filterOutIds(bidib_get_connected_signals(), filterOutIds); - - t_testsuite_test_result *result = malloc(sizeof(t_testsuite_test_result)); - result->points = malloc(points.length * sizeof(t_testsuite_point_result)); - - for (size_t i = 0; i < points.length; i++) { - result->points[i].stateReachedVerified = 0; - result->points[i].stateReached = 0; - result->points[i].stateNotReachedVerified = 0; - result->points[i].stateNotReached = 0; - result->points[i].stateError = 0; - result->points[i].unknownState = 0; - } + t_testsuite_test_result *result = testsuite_initTestSuite_common(excludedSignalAccessories, 1); return result; } -void testsuite_stopBidib(void) { - bidib_free_id_list_query(points); - bidib_free_id_list_query(signals); - bidib_stop(); -} - -void testsuite_signal_callback_handler(int signum) { - testsuite_stopBidib(); - printf("testsuite: SIGINT - stopping libbidib \n"); - exit(signum); -} - -t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, t_testsuite_ids filterOutIds) { - const size_t count = inputIdQuery.length - filterOutIds.length; - - if (count <= 0) { - printf("testsuite: No IDs will be left after filtering\n"); - } - - t_bidib_id_list_query outputIdQuery; - outputIdQuery.length = 0; - outputIdQuery.ids = malloc(sizeof(char *) * count); - - int isFilteredOut = 0; - - for (size_t i = 0; i < inputIdQuery.length; i++) { - isFilteredOut = 0; - for (size_t j = 0; j < filterOutIds.length; j++) { - if (!strcmp(inputIdQuery.ids[i], filterOutIds.ids[j])) { - isFilteredOut = 1; - break; - } - } - - if (!isFilteredOut) { - outputIdQuery.ids[outputIdQuery.length] = malloc(strlen(inputIdQuery.ids[i]) * sizeof(char) + 1) ; - memcpy(outputIdQuery.ids[outputIdQuery.length], inputIdQuery.ids[i], strlen(inputIdQuery.ids[i]) * sizeof(char) + 1); - outputIdQuery.length++; - } - } - - if (outputIdQuery.length != count) { - printf("testsuite: Error: %zu IDs were to be filtered, but %d IDs filtered instead\n", filterOutIds.length, (int)inputIdQuery.length - (int)outputIdQuery.length); - } - - return outputIdQuery; -} - -void testsuite_logTestResult(t_testsuite_test_result *result, t_bidib_unified_accessory_state_query state, int accessory_index) { - if (state.known) { - switch (state.board_accessory_state.execution_state) { - case BIDIB_EXEC_STATE_ERROR: - result->points[accessory_index].stateError++; - break; - case BIDIB_EXEC_STATE_NOTREACHED: - result->points[accessory_index].stateNotReached++; - break; - case BIDIB_EXEC_STATE_NOTREACHED_VERIFIED: - result->points[accessory_index].stateNotReachedVerified++; - break; - case BIDIB_EXEC_STATE_REACHED: - result->points[accessory_index].stateReached++; - break; - case BIDIB_EXEC_STATE_REACHED_VERIFIED: - result->points[accessory_index].stateReachedVerified++; - break; - default: - break; - } - } else { - result->points[accessory_index].unknownState++; - } -} - -void testsuite_printTestResults(t_testsuite_test_result *result) { - for (size_t i = 0; i < points.length; i++) { - printf("\n\n%s\n", points.ids[i]); - printf(" -> stateReachedVerified: %d \n", result->points[i].stateReachedVerified); - printf(" -> stateReached: %d \n", result->points[i].stateReached); - printf(" -> stateNotReachedVerified: %d \n", result->points[i].stateNotReachedVerified); - printf(" -> stateNotReached: %d \n", result->points[i].stateNotReached); - printf(" -> stateError: %d \n", result->points[i].stateError); - printf(" -> unknownState: %d \n", result->points[i].unknownState); - } -} - -bool testsuite_trainReady(const char *train) { - const char *segment = "seg1"; - if (bidib_get_train_on_track(train)) { - t_bidib_train_position_query train_position_query = bidib_get_train_position(train); - if (train_position_query.length > 0) { - for (size_t i = 0; i < train_position_query.length; i++) { - if (strcmp(segment, train_position_query.segments[i]) == 0) { - printf("testsuite: %s train ready on %s \n", train, segment); - bidib_free_train_position_query(train_position_query); - return true; - } - } - } - - printf("testsuite: %s train not on track segment %s \n", train, segment); - bidib_free_train_position_query(train_position_query); - return false; - } else { - printf("testsuite: %s train not detected on any track \n", train); - return false; - } -} - -void testsuite_driveTo(const char *segment, int speed, const char *train) { - bidib_set_train_speed(train, speed, "master"); - bidib_flush(); - - while (1) { - t_bidib_train_position_query trainPosition = bidib_get_train_position(train); - - for (size_t i = 0; i < trainPosition.length; i++) { - if (!strcmp(segment, trainPosition.segments[i])) { - bidib_free_train_position_query(trainPosition); - return; - } - } - bidib_free_train_position_query(trainPosition); - usleep(TRAIN_WAITING_TIME); - } -} - -void testsuite_driveToStop(const char *segment, int speed, const char *train) { - testsuite_driveTo(segment, speed, train); - bidib_set_train_speed(train, 0, "master"); - bidib_flush(); -} - -void set_signal(const char *signal, const char *aspect) { - bidib_set_signal(signal, aspect); - bidib_flush(); -} - -void switch_point(const char *point, const char *aspect) { - bidib_switch_point(point, aspect); - bidib_flush(); -} void testsuite_case_signal() { - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_caution"); - } - sleep(SIGNAL_WAITING_TIME); - - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_go"); - } - sleep(SIGNAL_WAITING_TIME); - - for (size_t i = 0; i < signals.length; i++) { - set_signal(signals.ids[i], "aspect_stop"); - } - sleep(SIGNAL_WAITING_TIME); - + char *signalAspects[3] = {"aspect_caution", "aspect_go", "aspect_stop"}; + testsuite_case_signal_common(signalAspects, 3); } void testsuite_case_pointParallel(t_testsuite_test_result *result) { - for (size_t i = 0; i < points.length; i++) { - switch_point(points.ids[i], "reverse"); - t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - } - - sleep(POINT_WAITING_TIME); - - for (size_t i = 0; i < points.length; i++) { - switch_point(points.ids[i], "normal"); - t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - } - - sleep(POINT_WAITING_TIME); + testsuite_case_pointParallel_common(result); } void testsuite_case_pointSerial(t_testsuite_test_result *result) { - for (size_t i = 0; i < points.length; i++) { - switch_point(points.ids[i], "reverse"); - t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - sleep(POINT_WAITING_TIME); - bidib_free_unified_accessory_state_query(state); - switch_point(points.ids[i], "normal"); - state = bidib_get_point_state(points.ids[i]); - testsuite_logTestResult(result, state, i); - bidib_free_unified_accessory_state_query(state); - sleep(POINT_WAITING_TIME); - } + testsuite_case_pointSerial_common(result); } void testsuite_case_reverser(void) { @@ -313,59 +104,59 @@ void testsuite_case_reverser(void) { } void testsuite_case_swtbahnStandardTrackCoverage(const char *train) { - if (!testsuite_trainReady(train)) { + if (!testsuite_trainReady(train, "seg1")) { return; } - switch_point("point1", "normal"); - switch_point("point2", "normal"); - switch_point("point3", "normal"); + testsuite_switch_point("point1", "normal"); + testsuite_switch_point("point2", "normal"); + testsuite_switch_point("point3", "normal"); testsuite_driveTo("seg12", 80, train); - switch_point("point6", "reverse"); - switch_point("point8", "reverse"); - switch_point("point2", "reverse"); - switch_point("point3", "reverse"); - switch_point("point4", "reverse"); - switch_point("point5", "reverse"); - switch_point("point12", "normal"); - switch_point("point10", "reverse"); - switch_point("point9", "reverse"); - switch_point("point11", "reverse"); + testsuite_switch_point("point6", "reverse"); + testsuite_switch_point("point8", "reverse"); + testsuite_switch_point("point2", "reverse"); + testsuite_switch_point("point3", "reverse"); + testsuite_switch_point("point4", "reverse"); + testsuite_switch_point("point5", "reverse"); + testsuite_switch_point("point12", "normal"); + testsuite_switch_point("point10", "reverse"); + testsuite_switch_point("point9", "reverse"); + testsuite_switch_point("point11", "reverse"); testsuite_driveToStop("seg37", 80, train); - switch_point("point12", "reverse"); + testsuite_switch_point("point12", "reverse"); testsuite_driveToStop("seg40", -80, train); - switch_point("point12", "normal"); - switch_point("point11", "normal"); - switch_point("point10", "normal"); + testsuite_switch_point("point12", "normal"); + testsuite_switch_point("point11", "normal"); + testsuite_switch_point("point10", "normal"); testsuite_driveTo("seg28", 50, train); - switch_point("point7", "normal"); - switch_point("point4", "normal"); - switch_point("point9", "normal"); + testsuite_switch_point("point7", "normal"); + testsuite_switch_point("point4", "normal"); + testsuite_switch_point("point9", "normal"); testsuite_driveTo("seg21", 80, train); - switch_point("point5", "normal"); + testsuite_switch_point("point5", "normal"); testsuite_driveTo("seg28", 80, train); - switch_point("point7", "reverse"); - switch_point("point8", "normal"); - switch_point("point2", "reverse"); - switch_point("point3", "normal"); - switch_point("point6", "normal"); - switch_point("point1", "reverse"); + testsuite_switch_point("point7", "reverse"); + testsuite_switch_point("point8", "normal"); + testsuite_switch_point("point2", "reverse"); + testsuite_switch_point("point3", "normal"); + testsuite_switch_point("point6", "normal"); + testsuite_switch_point("point1", "reverse"); testsuite_driveToStop("seg4", 80, train); - switch_point("point1", "normal"); + testsuite_switch_point("point1", "normal"); testsuite_driveTo("seg1", -20, train); sleep(1); diff --git a/test/physical/swtbahn-standard/testsuite.h b/test/physical/swtbahn-standard/testsuite.h index 248084a..aa3b3a8 100644 --- a/test/physical/swtbahn-standard/testsuite.h +++ b/test/physical/swtbahn-standard/testsuite.h @@ -31,7 +31,8 @@ #ifndef TESTSUITE_H #define TESTSUITE_H - +#include "../test_common.h" +/* #include "../../../include/bidib.h" @@ -52,24 +53,10 @@ typedef struct { char **ids; size_t length; }t_testsuite_ids; - +*/ // Setup t_testsuite_test_result *testsuite_initTestSuite(); -t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, t_testsuite_ids filterOutIds); - -// Teardown -void testsuite_stopBidib(void); -void testsuite_signal_callback_handler(int signum); - -// Logging -void testsuite_logTestResult(t_testsuite_test_result *result, t_bidib_unified_accessory_state_query state, int accessory_index); -void testsuite_printTestResults(t_testsuite_test_result *result); - -// Driving -bool testsuite_trainReady(const char *train); -void testsuite_driveTo(const char *segment, int speed, const char *train); -void testsuite_driveToStop(const char *segment, int speed, const char *train); // Test cases void testsuite_case_signal(); diff --git a/test/physical/test_common.c b/test/physical/test_common.c new file mode 100644 index 0000000..e0a69e6 --- /dev/null +++ b/test/physical/test_common.c @@ -0,0 +1,263 @@ +/* + * + * Copyright (C) 2022 University of Bamberg, Software Technologies Research Group + * , + * + * This file is part of the BiDiB library (libbidib), used to communicate with + * BiDiB systems over a serial connection. This library was + * developed as part of Nicolas Gross’ student project. + * + * libbidib is licensed under the GNU GENERAL PUBLIC LICENSE (Version 3), see + * the LICENSE file at the project's top-level directory for details or consult + * . + * + * libbidib is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or any later version. + * + * libbidib is a RESEARCH PROTOTYPE and distributed WITHOUT ANY WARRANTY, without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * The following people contributed to the conception and realization of the + * present libbidib (in alphabetic order by surname): + * + * - Christof Lehanka + * - Bernhard Luedtke + * - Eugene Yip + * + */ + +#include +#include +#include +#include +#include +#include + +#include "test_common.h" + + +#define SIGNAL_WAITING_TIME_S 3 // in seconds +#define POINT_WAITING_TIME_S 3 // in seconds +#define TRAIN_WAITING_TIME_US 250000 // in microseconds + +t_bidib_id_list_query points; +t_bidib_id_list_query signals; + +t_testsuite_test_result *testsuite_initTestSuite_common(char **excludedSignalAccessories, + size_t excludedSignalAccessories_len) { + points = bidib_get_connected_points(); + + // Accessories that are not signals + t_testsuite_ids filterOutIds; + filterOutIds.ids = excludedSignalAccessories; + filterOutIds.length = excludedSignalAccessories_len; + t_bidib_id_list_query signalsQuery = bidib_get_connected_signals(); + signals = testsuite_filterOutIds(signalsQuery, filterOutIds); + bidib_free_id_list_query(signalsQuery); + + t_testsuite_test_result *result = malloc(sizeof(t_testsuite_test_result)); + result->points = malloc(points.length * sizeof(t_testsuite_point_result)); + + for (size_t i = 0; i < points.length; i++) { + result->points[i].stateReachedVerified = 0; + result->points[i].stateReached = 0; + result->points[i].stateNotReachedVerified = 0; + result->points[i].stateNotReached = 0; + result->points[i].stateError = 0; + result->points[i].unknownState = 0; + } + return result; +} + +t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, t_testsuite_ids filterOutIds) { + const size_t count = inputIdQuery.length - filterOutIds.length; + + if (count <= 0) { + printf("testsuite: No IDs will be left after filtering\n"); + } + + t_bidib_id_list_query outputIdQuery; + outputIdQuery.length = 0; + outputIdQuery.ids = malloc(sizeof(char *) * count); + + bool isFilteredOut = false; + + for (size_t i = 0; i < inputIdQuery.length; i++) { + isFilteredOut = false; + for (size_t j = 0; j < filterOutIds.length; j++) { + if (!strcmp(inputIdQuery.ids[i], filterOutIds.ids[j])) { + isFilteredOut = true; + break; + } + } + + if (!isFilteredOut) { + //const size_t len = sizeof(char) * (strlen(inputIdQuery.ids[i]) + 1); + //outputIdQuery.ids[outputIdQuery.length] = malloc(len) ; + //memcpy(outputIdQuery.ids[outputIdQuery.length], inputIdQuery.ids[i], len); + outputIdQuery.ids[outputIdQuery.length] = strdup(inputIdQuery.ids[i]); + outputIdQuery.length++; + } + } + + if (outputIdQuery.length != count) { + printf("testsuite: Error: %zu IDs were to be filtered, but %d IDs filtered instead\n", + filterOutIds.length, (int)inputIdQuery.length - (int)outputIdQuery.length); + } + + return outputIdQuery; +} + +void testsuite_stopBidib(void) { + bidib_free_id_list_query(points); + bidib_free_id_list_query(signals); + bidib_stop(); +} + +void testsuite_signal_callback_handler(int signum) { + testsuite_stopBidib(); + printf("testsuite: SIGINT - stopping libbidib \n"); + exit(signum); +} + +void testsuite_logTestResult(t_testsuite_test_result *result, + t_bidib_unified_accessory_state_query state, + int accessory_index) { + if (state.known) { + switch (state.board_accessory_state.execution_state) { + case BIDIB_EXEC_STATE_ERROR: + result->points[accessory_index].stateError++; + break; + case BIDIB_EXEC_STATE_NOTREACHED: + result->points[accessory_index].stateNotReached++; + break; + case BIDIB_EXEC_STATE_NOTREACHED_VERIFIED: + result->points[accessory_index].stateNotReachedVerified++; + break; + case BIDIB_EXEC_STATE_REACHED: + result->points[accessory_index].stateReached++; + break; + case BIDIB_EXEC_STATE_REACHED_VERIFIED: + result->points[accessory_index].stateReachedVerified++; + break; + default: + break; + } + } else { + result->points[accessory_index].unknownState++; + } +} + +void testsuite_printTestResults(t_testsuite_test_result *result) { + for (size_t i = 0; i < points.length; i++) { + printf("\n\n%s\n", points.ids[i]); + printf(" -> stateReachedVerified: %d \n", result->points[i].stateReachedVerified); + printf(" -> stateReached: %d \n", result->points[i].stateReached); + printf(" -> stateNotReachedVerified: %d \n", result->points[i].stateNotReachedVerified); + printf(" -> stateNotReached: %d \n", result->points[i].stateNotReached); + printf(" -> stateError: %d \n", result->points[i].stateError); + printf(" -> unknownState: %d \n", result->points[i].unknownState); + } +} + +bool testsuite_trainReady(const char *train, const char *segment) { + if (bidib_get_train_on_track(train)) { + t_bidib_train_position_query train_position_query = bidib_get_train_position(train); + if (train_position_query.length > 0) { + for (size_t i = 0; i < train_position_query.length; i++) { + if (strcmp(segment, train_position_query.segments[i]) == 0) { + printf("testsuite: %s train ready on %s \n", train, segment); + bidib_free_train_position_query(train_position_query); + return true; + } + } + } + + printf("testsuite: %s train not on track segment %s \n", train, segment); + bidib_free_train_position_query(train_position_query); + return false; + } else { + printf("testsuite: %s train not detected on any track \n", train); + return false; + } +} + +void testsuite_driveTo(const char *segment, int speed, const char *train) { + bidib_set_train_speed(train, speed, "master"); + bidib_flush(); + + while (1) { + t_bidib_train_position_query trainPosition = bidib_get_train_position(train); + for (size_t i = 0; i < trainPosition.length; i++) { + if (!strcmp(segment, trainPosition.segments[i])) { + bidib_free_train_position_query(trainPosition); + return; + } + } + bidib_free_train_position_query(trainPosition); + usleep(TRAIN_WAITING_TIME_US); + } +} + +void testsuite_driveToStop(const char *segment, int speed, const char *train) { + testsuite_driveTo(segment, speed, train); + bidib_set_train_speed(train, 0, "master"); + bidib_flush(); +} + +void testsuite_set_signal(const char *signal, const char *aspect) { + bidib_set_signal(signal, aspect); + bidib_flush(); +} + +void testsuite_switch_point(const char *point, const char *aspect) { + bidib_switch_point(point, aspect); + bidib_flush(); +} + +void testsuite_case_signal_common(char **aspects, size_t aspects_len) { + for (size_t i = 0; i < aspects_len; i++) { + for (size_t n = 0; n < signals.length; n++) { + testsuite_set_signal(signals.ids[n], aspects[i]); + } + sleep(SIGNAL_WAITING_TIME_S); + } +} + +void testsuite_case_pointParallel_common(t_testsuite_test_result *result) { + for (size_t i = 0; i < points.length; i++) { + testsuite_switch_point(points.ids[i], "reverse"); + t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); + testsuite_logTestResult(result, state, i); + bidib_free_unified_accessory_state_query(state); + } + + sleep(POINT_WAITING_TIME_S); + + for (size_t i = 0; i < points.length; i++) { + testsuite_switch_point(points.ids[i], "normal"); + t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); + testsuite_logTestResult(result, state, i); + bidib_free_unified_accessory_state_query(state); + } + + sleep(POINT_WAITING_TIME_S); +} + +void testsuite_case_pointSerial_common(t_testsuite_test_result *result) { + for (size_t i = 0; i < points.length; i++) { + testsuite_switch_point(points.ids[i], "reverse"); + t_bidib_unified_accessory_state_query state = bidib_get_point_state(points.ids[i]); + testsuite_logTestResult(result, state, i); + bidib_free_unified_accessory_state_query(state); + sleep(POINT_WAITING_TIME_S); + + testsuite_switch_point(points.ids[i], "normal"); + state = bidib_get_point_state(points.ids[i]); + testsuite_logTestResult(result, state, i); + bidib_free_unified_accessory_state_query(state); + sleep(POINT_WAITING_TIME_S); + } +} \ No newline at end of file diff --git a/test/physical/test_common.h b/test/physical/test_common.h new file mode 100644 index 0000000..60accbc --- /dev/null +++ b/test/physical/test_common.h @@ -0,0 +1,86 @@ +/* + * + * Copyright (C) 2022 University of Bamberg, Software Technologies Research Group + * , + * + * This file is part of the BiDiB library (libbidib), used to communicate with + * BiDiB systems over a serial connection. This library was + * developed as part of Nicolas Gross’ student project. + * + * libbidib is licensed under the GNU GENERAL PUBLIC LICENSE (Version 3), see + * the LICENSE file at the project's top-level directory for details or consult + * . + * + * libbidib is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or any later version. + * + * libbidib is a RESEARCH PROTOTYPE and distributed WITHOUT ANY WARRANTY, without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * The following people contributed to the conception and realization of the + * present libbidib (in alphabetic order by surname): + * + * - Christof Lehanka + * - Bernhard Luedtke + * - Eugene Yip + * + */ + + +#ifndef TEST_COMMON_H +#define TEST_COMMON_H + +#include "../../include/bidib.h" +#include +#include + +typedef struct { + int stateError; + int stateNotReached; + int stateNotReachedVerified; + int stateReached; + int stateReachedVerified; + int unknownState; +} t_testsuite_point_result; + +typedef struct { + t_testsuite_point_result *points; +} t_testsuite_test_result; + +typedef struct { + char **ids; + size_t length; +} t_testsuite_ids; + +extern t_bidib_id_list_query points; +extern t_bidib_id_list_query signals; + +// Setup +t_testsuite_test_result *testsuite_initTestSuite_common(char **excludedSignalAccessories, size_t excludedSignalAccessories_len); +t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, t_testsuite_ids filterOutIds); + +// Teardown +void testsuite_stopBidib(void); +void testsuite_signal_callback_handler(int signum); + +// Logging +void testsuite_logTestResult(t_testsuite_test_result *result, t_bidib_unified_accessory_state_query state, int accessory_index); +void testsuite_printTestResults(t_testsuite_test_result *result); + +// Driving +bool testsuite_trainReady(const char *train, const char *segment); +void testsuite_driveTo(const char *segment, int speed, const char *train); +void testsuite_driveToStop(const char *segment, int speed, const char *train); + +// Accessories +void testsuite_set_signal(const char *signal, const char *aspect); +void testsuite_switch_point(const char *point, const char *aspect); + +// Common test base +void testsuite_case_signal_common(char **aspects, size_t aspects_len); +void testsuite_case_pointParallel_common(t_testsuite_test_result *result); +void testsuite_case_pointSerial_common(t_testsuite_test_result *result); + +#endif From 8f6a25eed3eca57b927daa11695837e58ef27b32 Mon Sep 17 00:00:00 2001 From: Bernhard Luedtke Date: Tue, 8 Aug 2023 09:16:09 +0200 Subject: [PATCH 2/3] Update physical test readme --- test/physical/Readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/physical/Readme.md b/test/physical/Readme.md index 1166dae..95215aa 100644 --- a/test/physical/Readme.md +++ b/test/physical/Readme.md @@ -13,7 +13,9 @@ physical | |-- main.c | |-- testsuite.c | '-- testsuite.h - '-- Readme.md + |-- Readme.md + |-- test_common.c + '-- test_common.h ``` ## Test Cases From 56ba7beecd82f181118c1ff20e1cfdf18aadeb23 Mon Sep 17 00:00:00 2001 From: Bernhard Luedtke Date: Tue, 8 Aug 2023 09:18:56 +0200 Subject: [PATCH 3/3] Updated comments in physical test suite --- test/physical/swtbahn-full/main.c | 3 ++- test/physical/swtbahn-full/testsuite.c | 5 +++-- test/physical/swtbahn-standard/testsuite.c | 5 +++-- test/physical/swtbahn-standard/testsuite.h | 22 ---------------------- test/physical/test_common.c | 3 --- 5 files changed, 8 insertions(+), 30 deletions(-) diff --git a/test/physical/swtbahn-full/main.c b/test/physical/swtbahn-full/main.c index 5c10743..b7e3862 100755 --- a/test/physical/swtbahn-full/main.c +++ b/test/physical/swtbahn-full/main.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { return 0; } - // if (bidib_start_serial("/dev/tty.usbserial-AK06U8H7", "../../swtbahn-cli/configurations/swtbahn-full/", 0)) { + //if (bidib_start_serial("/dev/tty.usbserial-AK06U8H7", "../../swtbahn-cli/configurations/swtbahn-full/", 0)) { if (bidib_start_serial("/dev/ttyUSB0", "../../swtbahn-cli/configurations/swtbahn-full", 0)) { printf("testsuite: libbidib failed to start\n"); return 0; @@ -74,6 +74,7 @@ int main(int argc, char **argv) { const int repetitions = atoi(argv[2]); switch (atoi(argv[1])) { case 1: + ///TODO: Test why this is commented out? Think it was just debugging //bidib_set_track_output_state_all(BIDIB_CS_OFF); for (int i = 0; i < repetitions; i++) { testsuite_case_pointParallel(result); diff --git a/test/physical/swtbahn-full/testsuite.c b/test/physical/swtbahn-full/testsuite.c index 7810774..6b91993 100644 --- a/test/physical/swtbahn-full/testsuite.c +++ b/test/physical/swtbahn-full/testsuite.c @@ -38,9 +38,10 @@ #include "testsuite.h" -// This initialisation function is specific to SWTbahn Full! +// This initialisation function is specific to SWTbahn Full. t_testsuite_test_result *testsuite_initTestSuite() { - char *excludedSignalAccessories[4] = {"platformlight1", "platformlight2", "platformlight4a", "platformlight4b"}; + char *excludedSignalAccessories[4] = {"platformlight1", "platformlight2", + "platformlight4a", "platformlight4b"}; t_testsuite_test_result *result = testsuite_initTestSuite_common(excludedSignalAccessories, 4); return result; } diff --git a/test/physical/swtbahn-standard/testsuite.c b/test/physical/swtbahn-standard/testsuite.c index e7de35e..0e912dd 100644 --- a/test/physical/swtbahn-standard/testsuite.c +++ b/test/physical/swtbahn-standard/testsuite.c @@ -36,7 +36,7 @@ #include "testsuite.h" -// This initialisation function is specific to SWTbahn Standard! +// This initialisation function is specific to SWTbahn Standard. t_testsuite_test_result *testsuite_initTestSuite() { char *excludedSignalAccessories[1] = {"platformlights"}; t_testsuite_test_result *result = testsuite_initTestSuite_common(excludedSignalAccessories, 1); @@ -74,7 +74,8 @@ void testsuite_case_reverser(void) { for (int retry = 0; retry < max_retries && state_unknown; retry++) { t_bidib_reverser_state_query rev_state_query = bidib_get_reverser_state(reverser_id); - state_unknown = !rev_state_query.available || rev_state_query.data.state_value == BIDIB_REV_EXEC_STATE_UNKNOWN; + state_unknown = !rev_state_query.available + || rev_state_query.data.state_value == BIDIB_REV_EXEC_STATE_UNKNOWN; if (!state_unknown) { char *state_value_str = "unknown"; switch (rev_state_query.data.state_value) { diff --git a/test/physical/swtbahn-standard/testsuite.h b/test/physical/swtbahn-standard/testsuite.h index aa3b3a8..6aef2e4 100644 --- a/test/physical/swtbahn-standard/testsuite.h +++ b/test/physical/swtbahn-standard/testsuite.h @@ -32,28 +32,6 @@ #define TESTSUITE_H #include "../test_common.h" -/* -#include "../../../include/bidib.h" - - -typedef struct { - int stateError; - int stateNotReached; - int stateNotReachedVerified; - int stateReached; - int stateReachedVerified; - int unknownState; -}t_testsuite_point_result; - -typedef struct { - t_testsuite_point_result *points; -}t_testsuite_test_result; - -typedef struct { - char **ids; - size_t length; -}t_testsuite_ids; -*/ // Setup t_testsuite_test_result *testsuite_initTestSuite(); diff --git a/test/physical/test_common.c b/test/physical/test_common.c index e0a69e6..32bb784 100644 --- a/test/physical/test_common.c +++ b/test/physical/test_common.c @@ -94,9 +94,6 @@ t_bidib_id_list_query testsuite_filterOutIds(t_bidib_id_list_query inputIdQuery, } if (!isFilteredOut) { - //const size_t len = sizeof(char) * (strlen(inputIdQuery.ids[i]) + 1); - //outputIdQuery.ids[outputIdQuery.length] = malloc(len) ; - //memcpy(outputIdQuery.ids[outputIdQuery.length], inputIdQuery.ids[i], len); outputIdQuery.ids[outputIdQuery.length] = strdup(inputIdQuery.ids[i]); outputIdQuery.length++; }