etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
NodeTest.cpp
[詳解]
1 
5 #include "Node.h"
6 #include <gtest/gtest.h>
7 
8 namespace etrobocon2018_test {
9 
10  // ノードIDの初期値を返す
11  TEST(NodeTest, getInitialValueOfNodeIDTest)
12  {
13  Node obj;
14  std::int8_t expected = 0;
15 
16  auto actual = obj.getNodeID();
17 
18  ASSERT_EQ(expected, actual);
19  }
20 
21  // ブロック存在性の初期値を返す
22  TEST(NodeTest, getInitialValueOfBlockExistsTest)
23  {
24  Node obj;
25 
26  auto actual = obj.hasBlock();
27 
28  ASSERT_FALSE(actual);
29  }
30 
31  // 位置コードの初期値を返す
32  TEST(NodeTest, getInitialValueOfPositionTest)
33  {
34  Node obj;
35  std::int8_t expectedX = 0;
36  std::int8_t expectedY = 0;
37 
38  auto actualX = obj.getPositionX();
39  auto actualY = obj.getPositionY();
40 
41  ASSERT_EQ(expectedX, actualX);
42  ASSERT_EQ(expectedY, actualY);
43  }
44 
45  // ノードIDの設定値を返す
46  TEST(NodeTest, getValueOfNodeIDTest)
47  {
48  Node obj;
49  std::int8_t expected = 10;
50 
51  obj.setNodeID(10);
52  auto actual = obj.getNodeID();
53 
54  ASSERT_EQ(expected, actual);
55  }
56 
57  // ノードIDの初期設定値を返す
58  TEST(NodeTest, initializeValueOfNodeIDTest)
59  {
60  std::int8_t expected = 9;
61 
62  Node obj(9);
63  auto actual = obj.getNodeID();
64 
65  ASSERT_EQ(expected, actual);
66  }
67 
68  // ブロック存在性の設定値を返す
69  TEST(NodeTest, getValueOfBlockExistsTest)
70  {
71  Node obj;
72 
73  obj.setHasBlock(true);
74  auto actual = obj.hasBlock();
75 
76  ASSERT_TRUE(actual);
77  }
78 
79  // 位置コードの設定値を返す
80  TEST(NodeTest, getValueOfPositionTest)
81  {
82  Node obj;
83  std::int8_t expectedX = 5;
84  std::int8_t expectedY = 8;
85 
86  obj.setPosition(5, 8);
87  auto actualX = obj.getPositionX();
88  auto actualY = obj.getPositionY();
89 
90  ASSERT_EQ(expectedX, actualX);
91  ASSERT_EQ(expectedY, actualY);
92  }
93 
94  // 隣接ノードリストの設定値を返す
95  TEST(NodeTest, getValueOfNeighborListTest)
96  {
97  Node nodeA;
98  Node nodeB;
99  nodeA.setNodeID(1);
100  nodeB.setNodeID(2);
101  std::vector<Node*> expected = {&nodeA, &nodeB};
102  Node obj{&expected};
103 
104  auto actual = obj.getNeighbors();
105 
106  ASSERT_EQ(&expected, actual);
107  ASSERT_EQ(expected.at(0), actual->at(0));
108 
109  expected.at(0)->setNodeID(3);
110  ASSERT_EQ(&expected, actual);
111  ASSERT_EQ(expected.at(0), actual->at(0));
112  ASSERT_EQ(expected.at(0)->getNodeID(), actual->at(0)->getNodeID());
113  }
114 
115  // 3隣接ノードリストの設定値を返す
116  TEST(NodeTest, getValueOf3NeighborListTest)
117  {
118  Node node1(1);
119  Node node2(2);
120  Node node3(3);
121 
122  std::vector<Node*> neighbors1 = {&node2};
123  std::vector<Node*> neighbors2 = {&node1, &node3};
124  std::vector<Node*> neighbors3 = {&node2};
125 
126  node1.setNeighbors(&neighbors1);
127  node2.setNeighbors(&neighbors2);
128  node3.setNeighbors(&neighbors3);
129 
130  auto actual1 = node1.getNeighbors();
131  auto actual2 = node2.getNeighbors();
132  auto actual3 = node3.getNeighbors();
133 
134  ASSERT_EQ(&neighbors1, actual1);
135  ASSERT_EQ(&neighbors2, actual2);
136  ASSERT_EQ(&neighbors3, actual3);
137 
138  ASSERT_EQ(&node1, actual1->at(0)->getNeighbors()->at(0));
139  ASSERT_EQ(&node2, actual2->at(0)->getNeighbors()->at(0));
140  ASSERT_EQ(&node2, actual2->at(1)->getNeighbors()->at(0));
141  ASSERT_EQ(&node3, actual3->at(0)->getNeighbors()->at(1));
142  }
143 
144  // 3x3の隣接ノードリストを返す
145  TEST(NodeTest, getValueOfNeighborListOf3x3Test)
146  {
147  std::vector<Node*> blockAreaNodePtrList(9);
148  std::vector<Node> blockAreaNodeList(9);
149 
150  for (std::int8_t i = 0; i < 9; i++)
151  {
152  Node currentNode(i);
153  blockAreaNodeList[i] = currentNode;
154  blockAreaNodePtrList[i] = &blockAreaNodeList[i];
155  }
156  std::vector<Node*>* blockAreaNodePtrListPtr = &blockAreaNodePtrList;
157 
158 
159  std::vector<Node*> neighborsOfNode0;
160  std::vector<Node*> neighborsOfNode1;
161  std::vector<Node*> neighborsOfNode2;
162 
163  std::vector<Node*> neighborsOfNode3;
164  std::vector<Node*> neighborsOfNode4;
165  std::vector<Node*> neighborsOfNode5;
166 
167  std::vector<Node*> neighborsOfNode6;
168  std::vector<Node*> neighborsOfNode7;
169  std::vector<Node*> neighborsOfNode8;
170 
171 
172  int numbers0[2] = {1, 3};
173  for (int i = 0; i < 2; i++)
174  {
175  neighborsOfNode0.push_back(blockAreaNodePtrListPtr->at(numbers0[i]));
176  }
177  int numbers1[3] = {0, 2, 4};
178  for (int i = 0; i < 3; i++)
179  {
180  neighborsOfNode1.push_back(blockAreaNodePtrListPtr->at(numbers1[i]));
181  }
182  int numbers2[2] = {1, 5};
183  for (int i = 0; i < 2; i++)
184  {
185  neighborsOfNode2.push_back(blockAreaNodePtrListPtr->at(numbers2[i]));
186  }
187 
188 
189  int numbers3[3] = {0, 4, 6};
190  for (int i = 0; i < 3; i++)
191  {
192  neighborsOfNode3.push_back(blockAreaNodePtrListPtr->at(numbers3[i]));
193  }
194  int numbers4[4] = {1, 3, 5, 7};
195  for (int i = 0; i < 4; i++)
196  {
197  neighborsOfNode4.push_back(blockAreaNodePtrListPtr->at(numbers4[i]));
198  }
199  int numbers5[3] = {2, 4, 8};
200  for (int i = 0; i < 3; i++)
201  {
202  neighborsOfNode5.push_back(blockAreaNodePtrListPtr->at(numbers5[i]));
203  }
204 
205 
206  int numbers6[2] = {3, 7};
207  for (int i = 0; i < 2; i++)
208  {
209  neighborsOfNode6.push_back(blockAreaNodePtrListPtr->at(numbers6[i]));
210  }
211  int numbers7[3] = {4, 6, 8};
212  for (int i = 0; i < 3; i++)
213  {
214  neighborsOfNode7.push_back(blockAreaNodePtrListPtr->at(numbers7[i]));
215  }
216  int numbers8[2] = {5, 7};
217  for (int i = 0; i < 2; i++)
218  {
219  neighborsOfNode8.push_back(blockAreaNodePtrListPtr->at(numbers8[i]));
220  }
221 
222 
223  blockAreaNodePtrListPtr->at(0)->setNeighbors(&neighborsOfNode0);
224  blockAreaNodePtrListPtr->at(1)->setNeighbors(&neighborsOfNode1);
225  blockAreaNodePtrListPtr->at(2)->setNeighbors(&neighborsOfNode2);
226 
227  blockAreaNodePtrListPtr->at(3)->setNeighbors(&neighborsOfNode3);
228  blockAreaNodePtrListPtr->at(4)->setNeighbors(&neighborsOfNode4);
229  blockAreaNodePtrListPtr->at(5)->setNeighbors(&neighborsOfNode5);
230 
231  blockAreaNodePtrListPtr->at(6)->setNeighbors(&neighborsOfNode6);
232  blockAreaNodePtrListPtr->at(7)->setNeighbors(&neighborsOfNode7);
233  blockAreaNodePtrListPtr->at(8)->setNeighbors(&neighborsOfNode8);
234 
235 
236  ASSERT_EQ(blockAreaNodePtrListPtr->at(0)->getNodeID(), blockAreaNodeList[1].getNeighbors()->at(0)->getNodeID());
237 
238  ASSERT_EQ(&blockAreaNodeList[0], blockAreaNodeList[1].getNeighbors()->at(0));
239  ASSERT_EQ(&blockAreaNodeList[0], neighborsOfNode1[0]);
240  ASSERT_EQ(blockAreaNodeList[1].getNodeID(), blockAreaNodeList[0].getNeighbors()->at(0)->getNodeID());
241  ASSERT_EQ(&blockAreaNodeList[1], blockAreaNodeList[0].getNeighbors()->at(0));
242 
243  // 中央は4方の隣接ノードと等しい
244  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(1)->getNeighbors()->at(2));
245  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(3)->getNeighbors()->at(1));
246  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(5)->getNeighbors()->at(1));
247  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(7)->getNeighbors()->at(0));
248 
249  // 中央は4隅の隣接ノードの隣接ノードと等しい
250  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(0)->getNeighbors()->at(0)->getNeighbors()->at(2));
251  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(2)->getNeighbors()->at(1)->getNeighbors()->at(1));
252  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(6)->getNeighbors()->at(0)->getNeighbors()->at(1));
253  ASSERT_EQ(blockAreaNodePtrListPtr->at(4), blockAreaNodePtrListPtr->at(8)->getNeighbors()->at(1)->getNeighbors()->at(0));
254  }
255 
256  // 4x4の隣接ノードリストを返す
257  TEST(NodeTest, getValueOfNeighborListOf4x4Test)
258  {
259  Node node0;
260  Node node1(1);
261  Node node2(2);
262  Node node3(3);
263  Node node4(4);
264  Node node5(5);
265  Node node6(6);
266  Node node7(7);
267  Node node8(8);
268  Node node9(9);
269  Node node10(10);
270  Node node11(11);
271  Node node12(12);
272  Node node13(13);
273  Node node14(14);
274  Node node15(15);
275 
276  std::vector<Node*> neighbors0 = {&node1, &node4};
277  std::vector<Node*> neighbors1 = {&node0, &node2, &node5};
278  std::vector<Node*> neighbors2 = {&node1, &node3, &node6};
279  std::vector<Node*> neighbors3 = {&node2, &node7};
280  std::vector<Node*> neighbors4 = {&node0, &node5, &node8};
281  std::vector<Node*> neighbors5 = {&node1, &node4, &node6, &node9};
282  std::vector<Node*> neighbors6 = {&node2, &node5, &node7, &node10};
283  std::vector<Node*> neighbors7 = {&node3, &node6, &node11};
284  std::vector<Node*> neighbors8 = {&node4, &node9, &node12};
285  std::vector<Node*> neighbors9 = {&node5, &node8, &node10, &node13};
286  std::vector<Node*> neighbors10 = {&node6, &node9, &node11, &node14};
287  std::vector<Node*> neighbors11 = {&node7, &node10, &node15};
288  std::vector<Node*> neighbors12 = {&node8, &node13};
289  std::vector<Node*> neighbors13 = {&node9, &node12, &node14};
290  std::vector<Node*> neighbors14 = {&node10, &node13, &node15};
291  std::vector<Node*> neighbors15 = {&node11, &node14};
292 
293  node0.setNeighbors(&neighbors0);
294  node1.setNeighbors(&neighbors1);
295  node2.setNeighbors(&neighbors2);
296  node3.setNeighbors(&neighbors3);
297  node4.setNeighbors(&neighbors4);
298  node5.setNeighbors(&neighbors5);
299  node6.setNeighbors(&neighbors6);
300  node7.setNeighbors(&neighbors7);
301  node8.setNeighbors(&neighbors8);
302  node9.setNeighbors(&neighbors9);
303  node10.setNeighbors(&neighbors10);
304  node11.setNeighbors(&neighbors11);
305  node12.setNeighbors(&neighbors12);
306  node13.setNeighbors(&neighbors13);
307  node14.setNeighbors(&neighbors14);
308  node15.setNeighbors(&neighbors15);
309 
310  auto actual0 = node0.getNeighbors();
311  auto actual1 = node1.getNeighbors();
312  auto actual2 = node2.getNeighbors();
313  auto actual3 = node3.getNeighbors();
314  auto actual4 = node4.getNeighbors();
315  auto actual5 = node5.getNeighbors();
316  auto actual6 = node6.getNeighbors();
317  auto actual7 = node7.getNeighbors();
318  auto actual8 = node8.getNeighbors();
319  auto actual9 = node9.getNeighbors();
320  auto actual10 = node10.getNeighbors();
321  auto actual11 = node11.getNeighbors();
322  auto actual12 = node12.getNeighbors();
323  auto actual13 = node13.getNeighbors();
324  auto actual14 = node14.getNeighbors();
325  auto actual15 = node15.getNeighbors();
326 
327  ASSERT_EQ(&neighbors0, actual0);
328  ASSERT_EQ(&neighbors1, actual1);
329  ASSERT_EQ(&neighbors2, actual2);
330  ASSERT_EQ(&neighbors3, actual3);
331  ASSERT_EQ(&neighbors4, actual4);
332  ASSERT_EQ(&neighbors5, actual5);
333  ASSERT_EQ(&neighbors6, actual6);
334  ASSERT_EQ(&neighbors7, actual7);
335  ASSERT_EQ(&neighbors8, actual8);
336  ASSERT_EQ(&neighbors9, actual9);
337  ASSERT_EQ(&neighbors10, actual10);
338  ASSERT_EQ(&neighbors11, actual11);
339  ASSERT_EQ(&neighbors12, actual12);
340  ASSERT_EQ(&neighbors13, actual13);
341  ASSERT_EQ(&neighbors14, actual14);
342  ASSERT_EQ(&neighbors15, actual15);
343  }
344 
345  // ETロボコン2018ブロック並べエリアにおける隣接ノードリストを返す
346  TEST(NodeTest, getValueOfNeighborListOfETRobocon2018Test)
347  {
348  std::vector<std::vector<int>> neighborsIDList = {
349  {1, 4},
350  {0, 2, 5},
351  {1, 3, 6},
352  {2, 7},
353  {0, 5, 8},
354  {1, 4, 6, 9},
355  {2, 5, 7, 10},
356  {3, 6, 11},
357  {4, 9, 12},
358  {5, 8, 10, 13},
359  {6, 9, 11, 14},
360  {7, 10, 15},
361  {8, 13},
362  {9, 12, 14},
363  {10, 13, 15},
364  {11, 14}};
365  int nodeCount = neighborsIDList.size();
366 
367  std::vector<Node> nodes(nodeCount);
368  std::vector<Node*> nodePtrs(nodeCount);
369  for (int i = 0; i < nodeCount; i++)
370  {
371  nodes[i].setNodeID(i);
372  nodePtrs[i] = &nodes[i];
373  }
374 
375  std::vector<std::vector<Node*>> neighbors(nodeCount);
376  for (int i = 0; i < nodeCount; i++)
377  {
378  for (int nodeID : neighborsIDList[i])
379  {
380  neighbors[i].push_back(nodePtrs[nodeID]);
381  }
382  }
383  for (int i = 0; i < nodeCount; i++)
384  {
385  nodePtrs[i]->setNeighbors(&neighbors[i]);
386  }
387 
388  auto actual0 = nodePtrs[0]->getNeighbors();
389  auto actual1 = nodePtrs[1]->getNeighbors();
390  auto actual2 = nodePtrs[2]->getNeighbors();
391  auto actual3 = nodePtrs[3]->getNeighbors();
392  auto actual4 = nodePtrs[4]->getNeighbors();
393  auto actual5 = nodePtrs[5]->getNeighbors();
394  auto actual6 = nodePtrs[6]->getNeighbors();
395  auto actual7 = nodePtrs[7]->getNeighbors();
396  auto actual8 = nodePtrs[8]->getNeighbors();
397  auto actual9 = nodePtrs[9]->getNeighbors();
398  auto actual10 = nodePtrs[10]->getNeighbors();
399  auto actual11 = nodePtrs[11]->getNeighbors();
400  auto actual12 = nodePtrs[12]->getNeighbors();
401  auto actual13 = nodePtrs[13]->getNeighbors();
402  auto actual14 = nodePtrs[14]->getNeighbors();
403  auto actual15 = nodePtrs[15]->getNeighbors();
404 
405  ASSERT_EQ(&neighbors[0], actual0);
406  ASSERT_EQ(&neighbors[1], actual1);
407  ASSERT_EQ(&neighbors[2], actual2);
408  ASSERT_EQ(&neighbors[3], actual3);
409  ASSERT_EQ(&neighbors[4], actual4);
410  ASSERT_EQ(&neighbors[5], actual5);
411  ASSERT_EQ(&neighbors[6], actual6);
412  ASSERT_EQ(&neighbors[7], actual7);
413  ASSERT_EQ(&neighbors[8], actual8);
414  ASSERT_EQ(&neighbors[9], actual9);
415  ASSERT_EQ(&neighbors[10], actual10);
416  ASSERT_EQ(&neighbors[11], actual11);
417  ASSERT_EQ(&neighbors[12], actual12);
418  ASSERT_EQ(&neighbors[13], actual13);
419  ASSERT_EQ(&neighbors[14], actual14);
420  ASSERT_EQ(&neighbors[15], actual15);
421 
422  ASSERT_EQ(&neighbors[10], actual0->at(1)->getNeighbors()->at(1)->getNeighbors()->at(2)->getNeighbors()->at(3)->getNeighbors());
423  }
424 
425  // ループを利用したETロボコン2017ブロック並べエリアにおける隣接ノードリストを返す
426  TEST(NodeTest, getValueOfNeighborListOfETRobocon2017Test)
427  {
428  std::vector<std::vector<int>> neighborsIDList = {
429  {1, 4, 9},
430  {0, 2, 4, 5},
431  {1, 3, 5, 6},
432  {2, 6, 10},
433  {0, 1, 7, 9},
434  {1, 2, 7, 8},
435  {2, 3, 8, 9},
436  {4, 5, 11, 12},
437  {5, 6, 13, 14},
438  {0, 4, 11},
439  {3, 6, 14},
440  {7, 9, 12},
441  {7, 11, 13},
442  {8, 12, 14},
443  {8, 10, 13}};
444  int nodeCount = neighborsIDList.size();
445 
446  std::vector<Node> nodes(nodeCount);
447  std::vector<Node*> nodePtrs(nodeCount);
448  for (int i = 0; i < nodeCount; i++)
449  {
450  nodes[i].setNodeID(i);
451  nodePtrs[i] = &nodes[i];
452  }
453 
454  std::vector<std::vector<Node*>> neighbors(nodeCount);
455  for (int i = 0; i < nodeCount; i++)
456  {
457  for (int nodeID : neighborsIDList[i])
458  {
459  neighbors[i].push_back(nodePtrs[nodeID]);
460  }
461  }
462  for (int i = 0; i < nodeCount; i++)
463  {
464  nodePtrs[i]->setNeighbors(&neighbors[i]);
465  }
466 
467  auto actual0 = nodePtrs[0]->getNeighbors();
468  auto actual1 = nodePtrs[1]->getNeighbors();
469  auto actual2 = nodePtrs[2]->getNeighbors();
470  auto actual3 = nodePtrs[3]->getNeighbors();
471  auto actual4 = nodePtrs[4]->getNeighbors();
472  auto actual5 = nodePtrs[5]->getNeighbors();
473  auto actual6 = nodePtrs[6]->getNeighbors();
474  auto actual7 = nodePtrs[7]->getNeighbors();
475  auto actual8 = nodePtrs[8]->getNeighbors();
476  auto actual9 = nodePtrs[9]->getNeighbors();
477  auto actual10 = nodePtrs[10]->getNeighbors();
478  auto actual11 = nodePtrs[11]->getNeighbors();
479  auto actual12 = nodePtrs[12]->getNeighbors();
480  auto actual13 = nodePtrs[13]->getNeighbors();
481  auto actual14 = nodePtrs[14]->getNeighbors();
482 
483  ASSERT_EQ(&neighbors[0], actual0);
484  ASSERT_EQ(&neighbors[1], actual1);
485  ASSERT_EQ(&neighbors[2], actual2);
486  ASSERT_EQ(&neighbors[3], actual3);
487  ASSERT_EQ(&neighbors[4], actual4);
488  ASSERT_EQ(&neighbors[5], actual5);
489  ASSERT_EQ(&neighbors[6], actual6);
490  ASSERT_EQ(&neighbors[7], actual7);
491  ASSERT_EQ(&neighbors[8], actual8);
492  ASSERT_EQ(&neighbors[9], actual9);
493  ASSERT_EQ(&neighbors[10], actual10);
494  ASSERT_EQ(&neighbors[11], actual11);
495  ASSERT_EQ(&neighbors[12], actual12);
496  ASSERT_EQ(&neighbors[13], actual13);
497  ASSERT_EQ(&neighbors[14], actual14);
498  }
499 
500 } // namespace etrobocon2018_test
void setPosition(std::int8_t x, std::int8_t y)
Definition: Node.cpp:33
void setNodeID(std::int8_t nodeNumber)
Definition: Node.cpp:13
std::int8_t getPositionX()
Definition: Node.cpp:39
Definition: Node.h:142
std::vector< Node * > * getNeighbors()
Definition: Node.cpp:8
void setNeighbors(std::vector< Node * > *nodes)
Definition: Node.cpp:3
TEST(AIAnswerArrayTest, construct)
void setHasBlock(bool exists)
Definition: Node.cpp:23
A*アルゴリズムにおけるノードクラス
bool hasBlock()
Definition: Node.cpp:28
std::int8_t getNodeID()
Definition: Node.cpp:18
std::int8_t getPositionY()
Definition: Node.cpp:44