RTXI  2.4
The Real-Time eXperiment Interface Documentation
fifo_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 <fifo_tests.h>
21 #include <thread>
22 
23 TEST_F(FifoTest, ReadAndWrite)
24 {
25  fifo = new Fifo((size_t) 100);
26  char inbuff[22];
27  char outbuff[22];
28  size_t written_bytes;
29  size_t read_bytes;
30  for(int i=0; i<9; i++){
31  inbuff[i] = (char) i+97;
32  }
33  inbuff[9] = '\0';
34 
35  // There should be zero bits read if the fifo is empty
36  EXPECT_EQ((size_t) 0, fifo->read(outbuff, (size_t) 10, false));
37 
38  // check that read and write work properly
39 
40  written_bytes = fifo->write(inbuff, (size_t) 21);
41  read_bytes = fifo->read(outbuff, (size_t) 21);
42  EXPECT_STREQ(inbuff, outbuff);
43  EXPECT_EQ(written_bytes, read_bytes);
44 
45  // The fifo should be empty after reading
46  EXPECT_EQ((size_t) 0, fifo->read(outbuff, (size_t) 21, false));
47 
48  // should be able to write multiple times
49  for(int i = 0; i < 10; i++){
50  written_bytes = fifo->write(inbuff, (size_t) 11);
51  read_bytes = fifo->read(outbuff, (size_t) 11);
52  EXPECT_STREQ(inbuff, outbuff);
53  EXPECT_EQ(written_bytes, read_bytes);
54  }
55 
56  delete fifo;
57 }
58 
59 TEST_F(FifoTest, Failures)
60 {
61  fifo = new Fifo((size_t) 2);
62  char buff[8] = "message";
63 
64  // Test whether FIFO fails when overwritting to it
65  EXPECT_EQ(fifo->write(buff, (size_t) 8), (size_t) 0);
66 }
67 
68 TEST_F(FifoTest, Threaded)
69 {
70  char message[8] = "message";
71  char output[8];
72  size_t size = 8;
73  fifo = new Fifo((size_t) 10);
74  std::thread sender(&Fifo::write, fifo, message, size);
75  std::thread receiver(&Fifo::read, fifo, output, size, true);
76  sender.join();
77  receiver.join();
78  EXPECT_STREQ(message, output);
79  delete fifo;
80 }
FifoTest
Definition: fifo_tests.h:27
TEST_F
TEST_F(FifoTest, ReadAndWrite)
Definition: fifo_tests.cpp:23
Fifo::read
size_t read(void *, size_t, bool=true)
Definition: fifo.cpp:42
Fifo
Definition: fifo.h:31
Fifo::write
size_t write(const void *, size_t)
Definition: fifo.cpp:81
fifo_tests.h