From 9902af7548fcafeabc74dbcb5162f60d57beb177 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 17 May 2022 21:15:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=BD=A8=E9=81=93=E5=87=BD=E6=95=B0=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E4=BA=86query.c=E4=B8=AD=E9=94=99=E8=AF=AF=E7=9A=84=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=BD=A8=E9=81=93=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/query.c | 3 +-- src/rail.c | 34 ++++++++++++++++++++++++++++++++++ test/io_test.cpp | 34 ---------------------------------- test/rail_test.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 test/rail_test.cpp diff --git a/src/query.c b/src/query.c index c705912..642474b 100644 --- a/src/query.c +++ b/src/query.c @@ -3,6 +3,5 @@ // #include "query.h" -up_bus_t* up_queries = NULL; -down_bus_t *down_queries = NULL; + diff --git a/src/rail.c b/src/rail.c index 721e09f..bd21bb6 100644 --- a/src/rail.c +++ b/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; } \ No newline at end of file diff --git a/test/io_test.cpp b/test/io_test.cpp index 6e44831..fe79dd4 100644 --- a/test/io_test.cpp +++ b/test/io_test.cpp @@ -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); } \ No newline at end of file diff --git a/test/rail_test.cpp b/test/rail_test.cpp new file mode 100644 index 0000000..d30b3f1 --- /dev/null +++ b/test/rail_test.cpp @@ -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; + } +} + + + + +