bit_stream.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
8 
9 
10 TEST(BitStream, ToString)
11 {
12  {
14  uavcan::BitStream bs(buf);
15  ASSERT_EQ("", bs.toString());
16  ASSERT_EQ("", bs.toString());
17  }
18  {
19  const uint8_t data[] = {0xad}; // 10101101
21  uavcan::BitStream bs(buf);
22  ASSERT_EQ(1, bs.write(data, 8)); // all 8
23  ASSERT_EQ("10101101", bs.toString());
24  }
25  {
26  const uint8_t data[] = {0xad, 0xbe}; // 10101101 10111110
28  uavcan::BitStream bs(buf);
29  ASSERT_EQ(1, bs.write(data, 16)); // all 16
30  ASSERT_EQ("10101101 10111110", bs.toString());
31  }
32  {
33  const uint8_t data[] = {0xad, 0xbe, 0xfc}; // 10101101 10111110 11111100
35  uavcan::BitStream bs(buf);
36  ASSERT_EQ(1, bs.write(data, 20)); // 10101101 10111110 1111
37  ASSERT_EQ("10101101 10111110 11110000", bs.toString());
38  }
39 }
40 
41 
42 TEST(BitStream, BitOrderSimple)
43 {
44  /*
45  * a = 1010
46  * b = 1011
47  * c = 1100
48  * d = 1101
49  * e = 1110
50  * f = 1111
51  */
53  { // Write
54  const uint8_t data[] = {0xad, 0xbe}; // adbe
55  uavcan::BitStream bs(buf);
56  ASSERT_EQ(1, bs.write(data, 12)); // adb0
57  ASSERT_EQ("10101101 10110000", bs.toString()); // adb0
58  }
59  { // Read
60  uavcan::BitStream bs(buf);
61  ASSERT_EQ("10101101 10110000", bs.toString()); // Same data
62  uint8_t data[] = {0xFF, 0xFF}; // Uninitialized
63  ASSERT_EQ(1, bs.read(data, 12));
64  ASSERT_EQ(0xad, data[0]);
65  ASSERT_EQ(0xb0, data[1]);
66  }
67 }
68 
69 
70 TEST(BitStream, BitOrderComplex)
71 {
72  static const std::string REFERENCE =
73  "10101101 10111111 11101111 01010110 11011111 01000100 10001101 00010101 10011110 00100110 10101111 00110111 10111100 00000100";
74 
76  { // Write
77  const uint8_t data1[] = {0xad, 0xbe}; // 10101101 10111110
78  const uint8_t data2[] = {0xfc}; // 11111100
79  const uint8_t data3[] = {0xde, 0xad, 0xbe, 0xef}; // 11011110 10101101 10111110 11101111
80  const uint8_t data4[] = {0x12, 0x34, 0x56, 0x78, // 00010010 00110100 01010110 01111000
81  0x9a, 0xbc, 0xde, 0xf0}; // 10011010 10111100 11011110 11110000
82 
83  uavcan::BitStream bs(buf);
84  ASSERT_EQ(1, bs.write(data1, 11)); // 10101101 101
85  std::cout << bs.toString() << std::endl;
86  ASSERT_EQ(1, bs.write(data2, 6)); // 11111 1
87  std::cout << bs.toString() << std::endl;
88  ASSERT_EQ(1, bs.write(data3, 25)); // 1101111 01010110 11011111 01
89  std::cout << bs.toString() << std::endl;
90  ASSERT_EQ(1, bs.write(data4, 64)); // all 64, total 42 + 64 = 106
91  std::cout << bs.toString() << std::endl;
92  ASSERT_EQ(1, bs.write(data4, 4)); // 0001
93  std::cout << bs.toString() << std::endl;
94 
95  std::cout << "Reference:\n" << REFERENCE << std::endl;
96 
97  ASSERT_EQ(REFERENCE, bs.toString());
98  }
99  { // Read back in the same order
100  uint8_t data[8];
101  std::fill(data, data + sizeof(data), 0xA5); // Filling with garbage
102  uavcan::BitStream bs(buf);
103  ASSERT_EQ(REFERENCE, bs.toString());
104 
105  ASSERT_EQ(1, bs.read(data, 11)); // 10101101 10100000
106  ASSERT_EQ(0xad, data[0]);
107  ASSERT_EQ(0xa0, data[1]);
108 
109  ASSERT_EQ(1, bs.read(data, 6)); // 11111100
110  ASSERT_EQ(0xfc, data[0]);
111 
112  ASSERT_EQ(1, bs.read(data, 25)); // 11011110 10101101 10111110 10000000
113  ASSERT_EQ(0xde, data[0]);
114  ASSERT_EQ(0xad, data[1]);
115  ASSERT_EQ(0xbe, data[2]);
116  ASSERT_EQ(0x80, data[3]);
117 
118  ASSERT_EQ(1, bs.read(data, 64)); // Data - see above
119  ASSERT_EQ(0x12, data[0]);
120  ASSERT_EQ(0x34, data[1]);
121  ASSERT_EQ(0x56, data[2]);
122  ASSERT_EQ(0x78, data[3]);
123  ASSERT_EQ(0x9a, data[4]);
124  ASSERT_EQ(0xbc, data[5]);
125  ASSERT_EQ(0xde, data[6]);
126  ASSERT_EQ(0xf0, data[7]);
127  }
128 }
129 
130 
131 TEST(BitStream, BitByBit)
132 {
133  static const int NUM_BYTES = 1024;
135  uavcan::BitStream bs_wr(buf);
136 
137  std::string binary_string;
138  unsigned counter = 0;
139  for (int byte = 0; byte < NUM_BYTES; byte++)
140  {
141  for (int bit = 0; bit < 8; bit++, counter++)
142  {
143  const bool value = counter % 3 == 0;
144  binary_string.push_back(value ? '1' : '0');
145  const uint8_t data[] = { uint8_t(value << 7) };
146  ASSERT_EQ(1, bs_wr.write(data, 1));
147  }
148  binary_string.push_back(' ');
149  }
150  binary_string.erase(binary_string.length() - 1, 1);
151 
152  /*
153  * Currently we have no free buffer space, so next write() must fail
154  */
155  const uint8_t dummy_data_wr[] = { 0xFF };
156  ASSERT_EQ(0, bs_wr.write(dummy_data_wr, 1));
157 
158  /*
159  * Bitstream content validation
160  */
161 // std::cout << bs.toString() << std::endl;
162 // std::cout << "Reference:\n" << binary_string << std::endl;
163  ASSERT_EQ(binary_string, bs_wr.toString());
164 
165  /*
166  * Read back
167  */
168  uavcan::BitStream bs_rd(buf);
169  counter = 0;
170  for (int byte = 0; byte < NUM_BYTES; byte++)
171  {
172  for (int bit = 0; bit < 8; bit++, counter++)
173  {
174  const bool value = counter % 3 == 0;
175  uint8_t data[1];
176  ASSERT_EQ(1, bs_rd.read(data, 1));
177  if (value)
178  {
179  ASSERT_EQ(0x80, data[0]);
180  }
181  else
182  {
183  ASSERT_EQ(0, data[0]);
184  }
185  }
186  }
187 
188  /*
189  * Making sure that reading out of buffer range will fail with error
190  */
191  uint8_t dummy_data_rd[] = { 0xFF };
192  ASSERT_EQ(0, bs_wr.read(dummy_data_rd, 1));
193  ASSERT_EQ(0xFF, dummy_data_rd[0]);
194 }
uavcan::BitStream
Definition: bit_stream.hpp:31
TEST
TEST(BitStream, ToString)
Definition: bit_stream.cpp:10
NUM_BYTES
dictionary NUM_BYTES
bit_stream.hpp
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
uavcan::StaticTransferBuffer
Definition: transfer_buffer.hpp:50
uavcan::BitStream::read
int read(uint8_t *bytes, const unsigned bitlen)
Definition: uc_bit_stream.cpp:55
fill
static void fill(T(&a)[Size], R value)
Definition: transfer_buffer.cpp:30
transfer_buffer.hpp
uavcan::BitStream::write
int write(const uint8_t *bytes, const unsigned bitlen)
Definition: uc_bit_stream.cpp:15


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02