diff --git a/CMakeLists.txt b/CMakeLists.txt index 245a3fd..cadff1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.10) # 设置cmake项目需要的cmake最小版本 add_subdirectory(test) +add_subdirectory(all_test) project(auto_pilot_bus) # 设置项目的名称 diff --git a/all_test/CMakeLists.txt b/all_test/CMakeLists.txt new file mode 100644 index 0000000..18be2fa --- /dev/null +++ b/all_test/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.0) +project(bus_all_test) + +include_directories(../include) +include_directories(include) + +aux_source_directory("../src/" SRCS) +aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}/src" ALL_TEST_SRCS) + +add_executable(bus_all_test ${SRCS} ${ALL_TEST_SRCS} main.c) \ No newline at end of file diff --git a/all_test/include/tools.h b/all_test/include/tools.h new file mode 100644 index 0000000..4f0f914 --- /dev/null +++ b/all_test/include/tools.h @@ -0,0 +1,18 @@ +// +// Created by ricardo on 2022/5/20. +// + +#ifndef AUTO_PILOT_BUS_TOOLS_H +#define AUTO_PILOT_BUS_TOOLS_H +#include "stdio.h" +#include "string.h" +#include "define.h" + +void ChooseTestCaseInput(char *path, int index); + +void ChooseTestCaseOutput(char *path, int index); + +void ReadOutputFile(char *result, FILE *f); + +int CheckOutput(char *program_output, char *read_output); +#endif //AUTO_PILOT_BUS_TOOLS_H diff --git a/all_test/main.c b/all_test/main.c new file mode 100644 index 0000000..ec5f4cb --- /dev/null +++ b/all_test/main.c @@ -0,0 +1,133 @@ +// +// Created by ricardo on 2022/5/20. +// +#include "bus_io.h" +#include "tools.h" + +int main() +{ + /** + * 输入的字符串 + */ + char input[30]; + /** + * 输出的字符串 + */ + char output[150]; + /** + * the_bus指针的本体 + */ + bus_t main_bus; + /** + * 公交车前进的方向 + */ + int direction; + /** + * 完成的请求 + */ + bus_query_t *finished_query; + int index; + char path[50]; + char read_output[150]; + FILE *input_file = NULL; + FILE *output_file = NULL; + + printf("Please choose which test case to use:"); + scanf("%d\n", &index); + + ChooseTestCaseInput(path, index); + input_file = fopen(path, "r"); + ChooseTestCaseOutput(path, index); + output_file = fopen(path, "r"); + + // 读取配置文件 + rails = ReadConfigFile(); + + // 制造公交车 + the_bus = &main_bus; + the_bus->distance = 0; + the_bus->rail_node_pos = FindNode(rails, 1); + + // 开始时公交车应该是停下的 + direction = BUS_STOP; + + PrintState(output); + ReadOutputFile(read_output, output_file); + if(CheckOutput(output, read_output) == BUS_FAlSE) + { + printf("%s\n", output); + } + else + { + printf("%d Ok\n", bus_time); + } + + for(;;) + { + fgets(input, sizeof input, input_file); + + int result = ReadInput(input); + if(result == IO_CLOCK) + { + // 时间流动 + AddTime(); + + switch (chosen_strategy) + { + case BUS_FCFS: + // 如果到站,处理请求和 + if(JudgeOnStation() == BUS_TRUE) + { + direction = FCFSDirection(); + finished_query = FCFSQuery(); + + if(finished_query != NULL) // 有请求就处理请求 + { + // 循环处理所有可以处理的请求,总共消耗一秒 + while (finished_query != NULL) + { + DeleteQuery(finished_query); + finished_query = FCFSQuery(); + } + } + else //如果没有请求就继续前进 + { + RunBus(direction); + } + } + else + { + RunBus(direction); + } + break; + case BUS_SSTF: + break; + case BUS_SCAN: + break; + default: + break; + } + + PrintState(output); + ReadOutputFile(read_output, output_file); + if(CheckOutput(output, read_output) == BUS_FAlSE) + { + printf("%s\n", output); + } + else + { + printf("%d Ok\n", bus_time); + } + + } + else if(result == IO_END) + { + printf("end\n"); + break; + } + else + { + //在读取到创建请求的情况下,不做任何事 + } + } +} diff --git a/all_test/src/tools.c b/all_test/src/tools.c new file mode 100644 index 0000000..1daf90b --- /dev/null +++ b/all_test/src/tools.c @@ -0,0 +1,60 @@ +// +// Created by ricardo on 2022/5/20. +// +#include "tools.h" + +void ChooseTestCaseInput(char *path, int index) +{ + memset(path, 0, 50); + + char root_path[] = "./test_cases/"; + char input_file[] = "/input.txt"; + char case_path[3]; + + sprintf(case_path, "%d", index); + + strcat(path, root_path); + strcat(path, case_path); + strcat(path, input_file); +} + +void ChooseTestCaseOutput(char *path, int index) +{ + memset(path, 0, 50); + + char root_path[] = "./test_cases/"; + char output_file[] = "/output.txt"; + char case_path[3]; + + sprintf(case_path, "%d", index); + + strcat(path, root_path); + strcat(path, case_path); + strcat(path, output_file); +} + +void ReadOutputFile(char *result, FILE *f) +{ + memset(result, 0, 150); + + for(size_t i = 0; i < 7; i++) + { + char temp[50]; + fgets(temp, 50, f); + strcat(result, temp); + } +} + +int CheckOutput(char *program_output, char *read_output) +{ + int result = strcmp(program_output, read_output); + + if(result == 0) + { + return BUS_TRUE; + } + else + { + return BUS_FAlSE; + } +} \ No newline at end of file diff --git a/all_test/test_cases/1/input.txt b/all_test/test_cases/1/input.txt new file mode 100644 index 0000000..cc018ca --- /dev/null +++ b/all_test/test_cases/1/input.txt @@ -0,0 +1,21 @@ +clock +counterclockwise 3 +clock +clock +clock +clock +clock +clock +target 10 +clock +clock +clock +clock +clock +clock +clock +clock +clock +clock +clock +end diff --git a/all_test/test_cases/1/ouput.txt b/all_test/test_cases/1/ouput.txt new file mode 100644 index 0000000..0a94ab4 --- /dev/null +++ b/all_test/test_cases/1/ouput.txt @@ -0,0 +1,134 @@ +TIME:0 +BUS: +position:0 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:1 +BUS: +position:0 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:2 +BUS: +position:1 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0010000000 +TIME:3 +BUS: +position:2 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0010000000 +TIME:4 +BUS: +position:3 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0010000000 +TIME:5 +BUS: +position:4 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0010000000 +TIME:6 +BUS: +position:5 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0010000000 +TIME:7 +BUS: +position:6 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0010000000 +TIME:8 +BUS: +position:6 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:9 +BUS: +position:5 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:10 +BUS: +position:4 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:11 +BUS: +position:3 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:12 +BUS: +position:2 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:13 +BUS: +position:1 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:14 +BUS: +position:0 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:15 +BUS: +position:29 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:16 +BUS: +position:28 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:17 +BUS: +position:27 +target:0000000001 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +TIME:18 +BUS: +position:27 +target:0000000000 +STATION: +clockwise:0000000000 +counterclockwise:0000000000 +end