RTXI  2.4
The Real-Time eXperiment Interface Documentation
atomic_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 <atomic_fifo_tests.h>
21 #include <thread>
22 
23 
24 TEST_F(AtomicFifoTest, isLockFree)
25 {
26  fifo = new AtomicFifo((size_t) 100);
27  ASSERT_TRUE(fifo->isLockFree());
28  delete fifo;
29 }
30 
31 TEST_F(AtomicFifoTest, ReadAndWrite)
32 {
33  fifo = new AtomicFifo((size_t) 100);
34  char inbuff[22];
35  char outbuff[22];
36  for(int i=0; i<9; i++){
37  inbuff[i] = (char) i+97;
38  }
39  inbuff[9] = '\0';
40 
41  // There should be zero bits read if the fifo is empty
42  EXPECT_EQ((size_t) 0, fifo->read(outbuff, (size_t) 10));
43 
44  // check that read and write work properly
45  bool write_success = fifo->write(inbuff, (size_t) 21);
46  bool read_success = fifo->read(outbuff, (size_t) 21);
47  EXPECT_STREQ(inbuff, outbuff);
48  EXPECT_EQ(write_success, read_success);
49 
50  // The fifo should be empty after reading
51  EXPECT_EQ((size_t) 0, fifo->read(outbuff, (size_t) 21));
52 
53  // should be able to write multiple times
54  for(int i = 0; i < 10; i++){
55  write_success = fifo->write(inbuff, (size_t) 11);
56  read_success = fifo->read(outbuff, (size_t) 11);
57  EXPECT_STREQ(inbuff, outbuff);
58  EXPECT_EQ(write_success, read_success);
59  }
60 
61  delete fifo;
62 }
63 
65 {
66  fifo = new AtomicFifo((size_t) 2);
67  char buff[8] = "message";
68 
69  // Test whether FIFO fails when overwritting to it
70  EXPECT_EQ(fifo->write(buff, (size_t) 8), (size_t) 0);
71 }
72 
73 void send(std::mutex &m, std::condition_variable &cv, std::atomic<bool> &ready, AtomicFifo *fifo, char *message, size_t size)
74 {
75  std::unique_lock<std::mutex> lk(m);
76  fifo->write(message, size);
77  ready.store(true);
78  cv.notify_one();
79 }
80 
81 void receive(std::mutex &m, std::condition_variable &cv, std::atomic<bool> &ready, AtomicFifo *fifo, char *output, size_t size)
82 {
83  std::unique_lock<std::mutex> lk(m);
84  // handle spurious calls
85  cv.wait(lk, [&]{return ready.load();});
86 
87  fifo->read(output, size);
88 }
89 
91 {
92  std::mutex m;
93  std::condition_variable cv;
94  std::atomic<bool> ready;
95  ready.store(false);
96 
97  char message[8] = "message";
98  char output[8];
99  size_t size = 8;
100  fifo = new AtomicFifo((size_t) 10);
101  std::thread sender(&send, std::ref(m), std::ref(cv), std::ref(ready), fifo, message, size);
102  std::thread receiver(&receive, std::ref(m), std::ref(cv), std::ref(ready), fifo, output, size);
103  sender.join();
104  receiver.join();
105  EXPECT_STREQ(message, output);
106  delete fifo;
107 }
108 
AtomicFifo::write
bool write(const void *buffer, size_t itemSize)
Definition: atomic_fifo.cpp:35
AtomicFifo
Lockfree SINGLE producer / SINGLE consumer FIFO.
Definition: atomic_fifo.h:33
AtomicFifoTest
Definition: atomic_fifo_tests.h:32
receive
void receive(std::mutex &m, std::condition_variable &cv, std::atomic< bool > &ready, AtomicFifo *fifo, char *output, size_t size)
Definition: atomic_fifo_tests.cpp:81
atomic_fifo_tests.h
send
void send(std::mutex &m, std::condition_variable &cv, std::atomic< bool > &ready, AtomicFifo *fifo, char *message, size_t size)
Definition: atomic_fifo_tests.cpp:73
AtomicFifo::read
bool read(void *buffer, size_t itemSize)
Definition: atomic_fifo.cpp:61
TEST_F
TEST_F(AtomicFifoTest, isLockFree)
Definition: atomic_fifo_tests.cpp:24