完成了创建轨道函数
修复了query.c中错误的定义 添加了创建轨道的测试
This commit is contained in:
		@@ -3,6 +3,5 @@
 | 
			
		||||
//
 | 
			
		||||
#include "query.h"
 | 
			
		||||
 | 
			
		||||
up_bus_t* up_queries = NULL;
 | 
			
		||||
down_bus_t *down_queries = NULL;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								src/rail.c
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								src/rail.c
									
									
									
									
									
								
							@@ -5,4 +5,38 @@ rail_node_t *rails = NULL;
 | 
			
		||||
int add(int a, int b)
 | 
			
		||||
{
 | 
			
		||||
    return a+ b;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
rail_node_t *CreateRails(int length, int node_num)
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * 表示轨道的头节点
 | 
			
		||||
     */
 | 
			
		||||
    rail_node_t *head = NULL;
 | 
			
		||||
    rail_node_t *node = NULL;
 | 
			
		||||
 | 
			
		||||
    head = (rail_node_t*) malloc(sizeof (rail_node_t));
 | 
			
		||||
    head->id = 1;
 | 
			
		||||
    head->last_node_distance = length;
 | 
			
		||||
    head->next_node_distance = length;
 | 
			
		||||
 | 
			
		||||
    node = head;
 | 
			
		||||
    // 循环创建每一个节点
 | 
			
		||||
    for(int i = 2; i <= node_num; i++)
 | 
			
		||||
    {
 | 
			
		||||
        rail_node_t *p = (rail_node_t*) malloc(sizeof (rail_node_t));
 | 
			
		||||
 | 
			
		||||
        p->id = i;
 | 
			
		||||
        p->last_node_distance = length;
 | 
			
		||||
        p->next_node_distance = length;
 | 
			
		||||
        p->last_node = node;
 | 
			
		||||
        node->next_node = p;
 | 
			
		||||
        node = p;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 循环结束时node就是最后一个节点
 | 
			
		||||
    node->next_node = head;
 | 
			
		||||
    head->last_node = node;
 | 
			
		||||
 | 
			
		||||
    return head;
 | 
			
		||||
}
 | 
			
		||||
@@ -9,48 +9,14 @@ extern "C"
 | 
			
		||||
{
 | 
			
		||||
#endif
 | 
			
		||||
#include "rail.h"
 | 
			
		||||
#include "bus_io.h"
 | 
			
		||||
#include "query.h"
 | 
			
		||||
#include "define.h"
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
using ::testing::Return;
 | 
			
		||||
using ::testing::AtLeast;
 | 
			
		||||
using ::testing::Exactly;
 | 
			
		||||
 | 
			
		||||
using namespace testing;
 | 
			
		||||
 | 
			
		||||
TEST(test, test)
 | 
			
		||||
{
 | 
			
		||||
    int result = add(1, 1);
 | 
			
		||||
    EXPECT_EQ(2, result);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 测试输入读取函数读取clock时的行为
 | 
			
		||||
 */
 | 
			
		||||
TEST(io_test, io_clock)
 | 
			
		||||
{
 | 
			
		||||
    char input[7] = "clock\n";
 | 
			
		||||
    int result = ReadInput(input);
 | 
			
		||||
    EXPECT_EQ(result, IO_CLOCK);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 测试顺时针3号站台上车请求
 | 
			
		||||
 */
 | 
			
		||||
TEST(io_test, reading)
 | 
			
		||||
{
 | 
			
		||||
    char input[] = "counterclockwise 3\n";
 | 
			
		||||
    int result = ReadInput(input);
 | 
			
		||||
    EXPECT_EQ(result, IO_READING);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(io_test, end)
 | 
			
		||||
{
 | 
			
		||||
    char input[] = "end\n";
 | 
			
		||||
    int result = ReadInput(input);
 | 
			
		||||
    EXPECT_EQ(result, IO_END);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								test/rail_test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								test/rail_test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by ricardo on 2022/5/17.
 | 
			
		||||
//
 | 
			
		||||
#include "gtest/gtest.h"
 | 
			
		||||
#include "gmock/gmock.h"
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C"
 | 
			
		||||
{
 | 
			
		||||
#endif
 | 
			
		||||
#include "rail.h"
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
using namespace testing;
 | 
			
		||||
 | 
			
		||||
TEST(rail, CreateRails)
 | 
			
		||||
{
 | 
			
		||||
    rail_node_t *head = CreateRails(10, 10);
 | 
			
		||||
    rail_node_t *p = head;
 | 
			
		||||
 | 
			
		||||
    for(int i = 1; i <= 10; i++)
 | 
			
		||||
    {
 | 
			
		||||
        EXPECT_EQ(p->id, i);
 | 
			
		||||
        EXPECT_EQ(p->next_node_distance, 10);
 | 
			
		||||
        EXPECT_EQ(p->last_node_distance, 10);
 | 
			
		||||
        p = p->next_node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    p = head->last_node;
 | 
			
		||||
    for(int i = 10; i >= 1; i--)
 | 
			
		||||
    {
 | 
			
		||||
        EXPECT_EQ(p->id, i);
 | 
			
		||||
        EXPECT_EQ(p->next_node_distance, 10);
 | 
			
		||||
        EXPECT_EQ(p->last_node_distance, 10);
 | 
			
		||||
        p = p->last_node;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user