test_input_from_buffer.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Alexander Rykovanov *
3  * rykovanov.as@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
21 
22 #include <gtest/gtest.h>
23 
24 
26 {
27 protected:
29  {
30  const char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
31  SourceBuffer = std::vector<char>(data, data + sizeof(data));
32  }
33  std::vector<char> SourceBuffer;
34 };
35 
36 TEST_F(InputFromBuffer, CanBeCreated)
37 {
39 }
40 
41 TEST_F(InputFromBuffer, ThrowsIfNullBuffer)
42 {
43  ASSERT_THROW(OpcUa::InputFromBuffer(nullptr, 100500), std::invalid_argument);
44 }
45 
46 TEST_F(InputFromBuffer, ThrowsIfZeroSize)
47 {
48  ASSERT_THROW(OpcUa::InputFromBuffer((char *)1, 0), std::invalid_argument);
49 }
50 
51 TEST_F(InputFromBuffer, ReadReturnsZeroWhenReadZero)
52 {
54 
55  std::vector<char> targetBuf{9, 8, 7};
56  std::vector<char> expectedBuf{9, 8, 7};
57  const size_t sizeToRead = 0;
58 
59  ASSERT_EQ(input.Receive(&targetBuf[0], sizeToRead), 0);
60  ASSERT_EQ(targetBuf, expectedBuf);
61 }
62 
63 TEST_F(InputFromBuffer, ReadSmallBuffer)
64 {
66 
67  std::vector<char> targetBuf{9, 8, 7};
68  const std::vector<char> expectedBuf{0, 1, 2};
69  size_t revceivedSize = 0;
70  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf[0], 3));
71  ASSERT_EQ(revceivedSize, 3);
72  ASSERT_EQ(targetBuf, expectedBuf);
73 }
74 
75 TEST_F(InputFromBuffer, ReadLargeBuffer)
76 {
78 
79  const std::vector<char> expectedBuf{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
80  std::vector<char> targetBuf(11, 11);
81  size_t revceivedSize = 0;
82  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf[0], targetBuf.size()));
83  ASSERT_EQ(revceivedSize, SourceBuffer.size());
84  ASSERT_EQ(targetBuf, expectedBuf);
85 }
86 
87 TEST_F(InputFromBuffer, ReadWithLittlePortions)
88 {
90  size_t revceivedSize = 0;
91 
92  // Read first three bytes.
93  std::vector<char> targetBuf1(3, 11);
94  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf1[0], targetBuf1.size()));
95  ASSERT_EQ(revceivedSize, 3);
96  const std::vector<char> expectedBuf1{0, 1, 2};
97  ASSERT_EQ(targetBuf1, expectedBuf1);
98 
99 
100  // Read second three bytes.
101  std::vector<char> targetBuf2(3, 11);
102  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf2[0], targetBuf2.size()));
103  ASSERT_EQ(revceivedSize, 3);
104  const std::vector<char> expectedBuf2{3, 4, 5};
105  ASSERT_EQ(targetBuf2, expectedBuf2);
106 
107  // Read third three bytes.
108  std::vector<char> targetBuf3(3, 11);
109  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf3[0], targetBuf3.size()));
110  ASSERT_EQ(revceivedSize, 3);
111  const std::vector<char> expectedBuf3{6, 7, 8};
112  ASSERT_EQ(targetBuf3, expectedBuf3);
113 
114  // Read forth three bytes.
115  std::vector<char> targetBuf4(3, 11);
116  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf4[0], targetBuf4.size()));
117  ASSERT_EQ(revceivedSize, 1);
118  const std::vector<char> expectedBuf4{9, 11, 11};
119  ASSERT_EQ(targetBuf4, expectedBuf4);
120 
121  // Read fifth three bytes.
122  std::vector<char> targetBuf5(3, 11);
123  ASSERT_NO_THROW(revceivedSize = input.Receive(&targetBuf5[0], targetBuf5.size()));
124  ASSERT_EQ(revceivedSize, 0);
125  const std::vector<char> expectedBuf5{11, 11, 11};
126  ASSERT_EQ(targetBuf5, expectedBuf5);
127 }
#define ASSERT_THROW(statement, expected_exception)
TEST_F(InputFromBuffer, CanBeCreated)
#define ASSERT_NO_THROW(statement)
#define ASSERT_EQ(val1, val2)
std::vector< char > SourceBuffer


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08