RTXI  2.4
The Real-Time eXperiment Interface Documentation
io_tests.cpp
Go to the documentation of this file.
1 /*
2  The Real-Time eXperiment Interface (RTXI)
3  Copyright (C) 2011 Georgia Institute of Technology, University of Utah, Weill Cornell Medical College
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  */
19 
20 #include <io.h>
21 #include <iostream>
22 #include <io_tests.h>
23 
25 {
26  // TODO: rename functions to differentiate between getting block name and channel name
27  ASSERT_EQ(block->getName(), defaultBlockName);
28  ASSERT_EQ(block->getName(IO::INPUT, (size_t) 0), defaultInputChannelName);
29  ASSERT_EQ(block->getName(IO::OUTPUT, (size_t) 0), defaultOutputChannelName);
30 }
31 
32 TEST_F(IOBlockTest, getCount)
33 {
34  ASSERT_EQ(block->getCount(IO::INPUT), (size_t) 1);
35  ASSERT_EQ(block->getCount(IO::OUTPUT), (size_t) 1);
36 }
37 
38 TEST_F(IOBlockTest, getDescription)
39 {
40  ASSERT_EQ(block->getDescription(IO::INPUT, (size_t) 0), defaultInputChannelDescription);
41  ASSERT_EQ(block->getDescription(IO::OUTPUT, (size_t) 0), defaultOutputChannelDescription);
42 }
43 
44 TEST_F(IOBlockTest, getValue)
45 {
46  double defaultval = 0.0;
47  EXPECT_DOUBLE_EQ(defaultval, block->getValue(IO::INPUT, (size_t) 1));
48  EXPECT_DOUBLE_EQ(defaultval, block->getValue(IO::OUTPUT, (size_t) 1));
49 }
50 
52 {
53  double defaultval = 0.0;
54  for(size_t i = 0; i < 2; ++i)
55  {
56  EXPECT_DOUBLE_EQ(defaultval, block->input(i));
57  }
58 
59  // Have to build example blocks to test connector
60  IO::channel_t outputchannel = {
61  "OUTPUT CHANNEL",
62  "OUTPUT CHANNEL DESCRIPTION",
63  IO::OUTPUT
64  };
65  IO::channel_t inputchannel = {
66  "INPUT CHANNEL",
67  "INPUT CHANNEL DESCRIPTION",
68  IO::INPUT
69  };
70 
71  IO::channel_t *block1channels = new IO::channel_t[2];
72  block1channels[0] = outputchannel;
73  block1channels[1] = inputchannel;
74  IO::channel_t *block2channels = new IO::channel_t[2];
75  block2channels[0] = outputchannel;
76  block2channels[1] = inputchannel;
77  MockIOBlock *block1 = new MockIOBlock("block1", block1channels, (size_t) 2);
78  MockIOBlock *block2 = new MockIOBlock("block2", block2channels, (size_t) 2);
79 
80  // Now connect the test blocks to default block input channel
81  IO::Connector::getInstance()->connect(block1, (size_t) 0, block, (size_t) 0);
82  IO::Connector::getInstance()->connect(block2, (size_t) 0, block, (size_t) 0);
83  block1->changeOutput(1.0);
84  EXPECT_DOUBLE_EQ(1.0, block->input(0));
85  block2->changeOutput(1.0);
86  EXPECT_DOUBLE_EQ(2.0, block->input(0));
87 
88  // please cleanup after yourself
89  delete block1;
90  delete block2;
91 }
92 
94 {
95  double defaultval = 0.0;
96  // TODO: Maybe reduce output function to single definition instead of two
97  const IO::Block *const_block = block;
98  for(size_t i = 0; i < 2; ++i)
99  {
100  EXPECT_DOUBLE_EQ(defaultval, const_block->output(i));
101  }
102 }
103 
104 TEST_F(IOConnectorTest, getInstance)
105 {
106  connector = IO::Connector::getInstance();
107  EXPECT_EQ(connector, IO::Connector::getInstance());
108  EXPECT_EQ(connector, connector->getInstance());
109 }
110 
111 // checking connections is very involved so could not separate into
112 // individual functin tests. Here we test connect, disconnect, and connected
113 TEST_F(IOConnectorTest, connections)
114 {
115  // Have to build example blocks to test connector
116  IO::channel_t outputchannel = {
117  "OUTPUT CHANNEL",
118  "OUTPUT CHANNEL DESCRIPTION",
119  IO::OUTPUT
120  };
121  IO::channel_t inputchannel = {
122  "INPUT CHANNEL",
123  "INPUT CHANNEL DESCRIPTION",
124  IO::INPUT
125  };
126  IO::channel_t *block1channels = new IO::channel_t[2];
127  block1channels[0] = outputchannel;
128  block1channels[1] = inputchannel;
129  IO::channel_t *block2channels = new IO::channel_t[2];
130  block2channels[0] = outputchannel;
131  block2channels[1] = inputchannel;
132  MockIOBlock *block1 = new MockIOBlock("block1", block1channels, (size_t) 2);
133  MockIOBlock *block2 = new MockIOBlock("block2", block2channels, (size_t) 2);
134 
135  // NOTE: It is not possible to mock static functions, therefore we must use
136  // other means to figure out whether two blocks are connected
137 
138  // connect and disconnect between two blocks
139  EXPECT_FALSE(connector->connected(block1, (size_t) 0, block2, (size_t) 0));
140  EXPECT_FALSE(connector->connected(block2, (size_t) 0, block1, (size_t) 0));
141  connector->connect(block1, (size_t) 0, block2, (size_t) 0);
142  connector->connect(block2, (size_t) 0, block1, (size_t) 0);
143  EXPECT_TRUE(connector->connected(block1, (size_t) 0, block2, (size_t) 0));
144  EXPECT_TRUE(connector->connected(block2, (size_t) 0, block1, (size_t) 0));
145  connector->disconnect(block1, (size_t) 0, block2, (size_t) 0);
146  connector->disconnect(block2, (size_t) 0, block1, (size_t) 0);
147  EXPECT_FALSE(connector->connected(block1, (size_t) 0, block2, (size_t) 0));
148  EXPECT_FALSE(connector->connected(block2, (size_t) 0, block1, (size_t) 0));
149  delete block1;
150  delete block2;
151  delete[] block1channels;
152  delete[] block2channels;
153 }
154 
155 // The cases where foreachBlock function is used are too unique (only used a handful of times)
156 // TODO: Create a test case for IO::Connector::foreachBlock
157 TEST_F(IOConnectorTest, foreachBlock)
158 {
159 }
160 
161 // the cases where foreachConnection function is used are too unique (only used a handful of times)
162 // TODO: create a test case for IO::Connector::foreachConnection
163 TEST_F(IOConnectorTest, foreachConnection)
164 {
165 
166 }
167 
168 
IO::Connector::connect
void connect(IO::Block *outputBlock, size_t outputChannel, IO::Block *inputBlock, size_t inputChannel)
Definition: io.cpp:274
IO::Connector::getInstance
static Connector * getInstance(void)
Definition: io.cpp:421
MockIOBlock
Definition: io_tests.h:66
IOConnectorTest
Definition: io_tests.h:82
MockIOBlock::changeOutput
void changeOutput(double val)
Definition: io_tests.h:79
IO::Block::output
double output(size_t index) const
Definition: io.cpp:129
IO::Block
Definition: io.h:188
IO::channel_t
Definition: io.h:56
IOBlockTest
Definition: io_tests.h:29
io_tests.h
io.h
TEST_F
TEST_F(IOBlockTest, getName)
Definition: io_tests.cpp:24