完成了配置文件读取函数

添加了配置文件单元测试
配置文件读取函数单元测试完成
This commit is contained in:
jackfiled 2022-05-20 11:18:12 +08:00
parent 6095d0849b
commit bcff483797
8 changed files with 159 additions and 6 deletions

View File

@ -7,6 +7,7 @@
#include "rail.h"
#include "query.h"
#include "define.h"
#include "controller.h"
#include "string.h"
#include "stdio.h"

View File

@ -14,6 +14,10 @@
*
*/
extern bus_query_t *target_query;
/**
*
*/
extern int chosen_strategy;
/**
*
@ -25,5 +29,5 @@ int FCFSDirection();
*
* @return
*/
bus_query_t FCFSQuery();
bus_query_t *FCFSQuery();
#endif //AUTO_PILOT_BUS_CONTROLLER_H

View File

@ -14,5 +14,8 @@
#define IO_CLOCK 0 // 读取时钟指令
#define IO_READING 1 // 读取请求指令
#define IO_END 2 // 读取结束指令
#define BUS_FCFS 0 // 先来先服务
#define BUS_SSTF 1 // 最短寻找时间优先
#define BUS_SCAN 2 // 顺便服务
#endif //AUTO_PILOT_BUS_DEFINE_H

View File

@ -8,7 +8,7 @@ int ReadInput(char* inputString)
char src[20];
int num;
sscanf(inputString,"%[a-z] %d",src,&num);
sscanf_s(inputString,"%[a-z] %d",src,&num);
if (0 == strcmp("clock",src))
{
return IO_CLOCK;
@ -37,5 +37,111 @@ int ReadInput(char* inputString)
// 匹配失败则返回-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;
}
}

View File

@ -3,6 +3,5 @@
//
#include "controller.h"
int BUS_TIME = 0;
bus_query_t *target_query = NULL;
bus_query_t *target_query = NULL;
int chosen_strategy = -1;

View File

@ -22,3 +22,9 @@ target_link_libraries(bus_test
gmock_main
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
View File

@ -0,0 +1,6 @@
# The Config file of bus
# Comment
TOTAL_STATION = 10
DISTANCE = 10
# STRATEGY = SCAN
STRATEGY = FCFS

View File

@ -10,6 +10,7 @@ extern "C"
#endif
#include "bus_io.h"
#include "define.h"
#include "controller.h"
#ifdef __cplusplus
}
#endif
@ -36,4 +37,31 @@ TEST(bus_io, ReadInput_reading)
char str[20] = "target 8\n";
int result = ReadInput(str);
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);
}