完成了配置文件读取函数
添加了配置文件单元测试 配置文件读取函数单元测试完成
This commit is contained in:
parent
6095d0849b
commit
bcff483797
|
@ -7,6 +7,7 @@
|
||||||
#include "rail.h"
|
#include "rail.h"
|
||||||
#include "query.h"
|
#include "query.h"
|
||||||
#include "define.h"
|
#include "define.h"
|
||||||
|
#include "controller.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,10 @@
|
||||||
* 当前正在处理的请求
|
* 当前正在处理的请求
|
||||||
*/
|
*/
|
||||||
extern bus_query_t *target_query;
|
extern bus_query_t *target_query;
|
||||||
|
/**
|
||||||
|
* 当前选择的策略
|
||||||
|
*/
|
||||||
|
extern int chosen_strategy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在先来先服务策略下应该前进的方向
|
* 在先来先服务策略下应该前进的方向
|
||||||
|
@ -25,5 +29,5 @@ int FCFSDirection();
|
||||||
* 在先来先服务策略下给出处理的请求
|
* 在先来先服务策略下给出处理的请求
|
||||||
* @return 需要处理的请求
|
* @return 需要处理的请求
|
||||||
*/
|
*/
|
||||||
bus_query_t FCFSQuery();
|
bus_query_t *FCFSQuery();
|
||||||
#endif //AUTO_PILOT_BUS_CONTROLLER_H
|
#endif //AUTO_PILOT_BUS_CONTROLLER_H
|
||||||
|
|
|
@ -14,5 +14,8 @@
|
||||||
#define IO_CLOCK 0 // 读取时钟指令
|
#define IO_CLOCK 0 // 读取时钟指令
|
||||||
#define IO_READING 1 // 读取请求指令
|
#define IO_READING 1 // 读取请求指令
|
||||||
#define IO_END 2 // 读取结束指令
|
#define IO_END 2 // 读取结束指令
|
||||||
|
#define BUS_FCFS 0 // 先来先服务
|
||||||
|
#define BUS_SSTF 1 // 最短寻找时间优先
|
||||||
|
#define BUS_SCAN 2 // 顺便服务
|
||||||
|
|
||||||
#endif //AUTO_PILOT_BUS_DEFINE_H
|
#endif //AUTO_PILOT_BUS_DEFINE_H
|
||||||
|
|
110
src/bus_io.c
110
src/bus_io.c
|
@ -8,7 +8,7 @@ int ReadInput(char* inputString)
|
||||||
|
|
||||||
char src[20];
|
char src[20];
|
||||||
int num;
|
int num;
|
||||||
sscanf(inputString,"%[a-z] %d",src,&num);
|
sscanf_s(inputString,"%[a-z] %d",src,&num);
|
||||||
if (0 == strcmp("clock",src))
|
if (0 == strcmp("clock",src))
|
||||||
{
|
{
|
||||||
return IO_CLOCK;
|
return IO_CLOCK;
|
||||||
|
@ -37,5 +37,111 @@ int ReadInput(char* inputString)
|
||||||
// 匹配失败则返回-1
|
// 匹配失败则返回-1
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rail_node_t *ReadConfigFile()
|
||||||
|
{
|
||||||
|
FILE *config_file = NULL;
|
||||||
|
char buffer[30];
|
||||||
|
int total_station = 0;
|
||||||
|
int distance = 0;
|
||||||
|
|
||||||
|
config_file = fopen("dict.dic", "r");
|
||||||
|
|
||||||
|
// 循环读取文件的每一行
|
||||||
|
while(fgets(buffer, sizeof buffer, config_file) != NULL)
|
||||||
|
{
|
||||||
|
char first_char = buffer[0];
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
switch (first_char)
|
||||||
|
{
|
||||||
|
case '#':
|
||||||
|
// 如果读取到#什么都不做
|
||||||
|
break;
|
||||||
|
case 'T':
|
||||||
|
// TOTAL_STATION
|
||||||
|
p = buffer;
|
||||||
|
|
||||||
|
// 把数字前面的所有字符全部干掉
|
||||||
|
while (*p < '0' || *p > '9')
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*p == '1' && *(p + 1) != '\n')
|
||||||
|
{
|
||||||
|
total_station = 10;
|
||||||
|
}
|
||||||
|
else if(*(p + 1) == '\n')
|
||||||
|
{
|
||||||
|
total_station = *p - 48;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
// STRATEGY
|
||||||
|
p = buffer;
|
||||||
|
// 将=前的字符全部略去
|
||||||
|
while(*p != '=')
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
// =也去掉
|
||||||
|
p++;
|
||||||
|
// =和策略之间的空格也去掉
|
||||||
|
while(*p == ' ')
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*p == 'F' && *(p + 1) == 'C') //FCFS
|
||||||
|
{
|
||||||
|
chosen_strategy = BUS_FCFS;
|
||||||
|
}
|
||||||
|
else if(*p == 'S' && *(p + 1) == 'S') //SSTF
|
||||||
|
{
|
||||||
|
chosen_strategy = BUS_SSTF;
|
||||||
|
}
|
||||||
|
else if(*p == 'S' && *(p + 1) == 'C') //SCAN
|
||||||
|
{
|
||||||
|
chosen_strategy = BUS_SCAN;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// 读取失败
|
||||||
|
chosen_strategy = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'D':
|
||||||
|
// DISTANCE
|
||||||
|
p = buffer;
|
||||||
|
|
||||||
|
// 把数字前面的所有字符全部干掉
|
||||||
|
while (*p < '0' || *p > '9')
|
||||||
|
{
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(*(p + 1) == '\n')
|
||||||
|
{
|
||||||
|
distance = *p - 48;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(distance != 0 && total_station != 0)
|
||||||
|
{
|
||||||
|
return CreateRails(distance, total_station);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,5 @@
|
||||||
//
|
//
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
|
|
||||||
int BUS_TIME = 0;
|
|
||||||
|
|
||||||
bus_query_t *target_query = NULL;
|
bus_query_t *target_query = NULL;
|
||||||
|
int chosen_strategy = -1;
|
|
@ -22,3 +22,9 @@ target_link_libraries(bus_test
|
||||||
gmock_main
|
gmock_main
|
||||||
pthread
|
pthread
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_custom_command(TARGET bus_test POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/dict.dic"
|
||||||
|
$<TARGET_FILE_DIR:bus_test>
|
||||||
|
)
|
6
test/dict.dic
Normal file
6
test/dict.dic
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# The Config file of bus
|
||||||
|
# Comment
|
||||||
|
TOTAL_STATION = 10
|
||||||
|
DISTANCE = 10
|
||||||
|
# STRATEGY = SCAN
|
||||||
|
STRATEGY = FCFS
|
|
@ -10,6 +10,7 @@ extern "C"
|
||||||
#endif
|
#endif
|
||||||
#include "bus_io.h"
|
#include "bus_io.h"
|
||||||
#include "define.h"
|
#include "define.h"
|
||||||
|
#include "controller.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -37,3 +38,30 @@ TEST(bus_io, ReadInput_reading)
|
||||||
int result = ReadInput(str);
|
int result = ReadInput(str);
|
||||||
EXPECT_EQ(result, IO_READING);
|
EXPECT_EQ(result, IO_READING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(bus_file, all)
|
||||||
|
{
|
||||||
|
rail_node_t *result = ReadConfigFile();
|
||||||
|
// 先测试轨道的参数读取是否正确
|
||||||
|
rail_node_t *p = result;
|
||||||
|
|
||||||
|
ASSERT_TRUE(p != nullptr);
|
||||||
|
for(int i = 1; i <= 10; i++)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(p->id, i);
|
||||||
|
EXPECT_EQ(p->next_node_distance, 5);
|
||||||
|
EXPECT_EQ(p->last_node_distance, 5);
|
||||||
|
p = p->next_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = result->last_node;
|
||||||
|
for(int i = 10; i >= 1; i--)
|
||||||
|
{
|
||||||
|
EXPECT_EQ(p->id, i);
|
||||||
|
EXPECT_EQ(p->next_node_distance, 5);
|
||||||
|
EXPECT_EQ(p->last_node_distance, 5);
|
||||||
|
p = p->last_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(chosen_strategy, BUS_FCFS);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user