F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PrimitiveStructTest.cpp
Go to the documentation of this file.
1// ======================================================================
2// \title PrimitiveStructTest.cpp
3// \author T. Chieu
4// \brief cpp file for PrimitiveStructTest class
5//
6// \copyright
7// Copyright (C) 2009-2022 California Institute of Technology.
8// ALL RIGHTS RESERVED. United States Government Sponsorship
9// acknowledged.
10//
11// ======================================================================
12
13#include "FppTest/struct/PrimitiveSerializableAc.hpp"
15
18#include "STest/Pick/Pick.hpp"
19
20#include "gtest/gtest.h"
21
22#include <sstream>
23
24// Test Primitive struct class
25class PrimitiveStructTest : public ::testing::Test {
26protected:
27 void SetUp() override {
28 testBool = true;
30 testI16 = static_cast<I16>(FppTest::Utils::getNonzeroU32());
31 testF64 = static_cast<F64>(FppTest::Utils::getNonzeroU32());
32 }
33
34 void assertStructMembers(const Primitive& s) {
35 ASSERT_EQ(s.getmBool(), testBool);
36 ASSERT_EQ(s.getmU32(), testU32);
37 ASSERT_EQ(s.getmI16(), testI16);
38 ASSERT_EQ(s.getmF64(), testF64);
39 }
40
41 void assertUnsuccessfulSerialization(Primitive& s, U32 bufSize) {
42 U8 data[bufSize];
43 Fw::SerialBuffer buf(data, sizeof(data));
45
46 // Serialize
47 status = buf.serialize(s);
48 ASSERT_NE(status, Fw::FW_SERIALIZE_OK);
49
50 // Deserialize
51 status = buf.deserialize(s);
52 ASSERT_NE(status, Fw::FW_SERIALIZE_OK);
53 }
54
59};
60
61// Test struct constants and default constructor
63 Primitive s;
64
65 // Constants
66 ASSERT_EQ(
67 Primitive::SERIALIZED_SIZE,
68 sizeof(U8)
69 + sizeof(U32)
70 + sizeof(I16)
71 + sizeof(F64)
72 );
73
74 // Default constructor
75 ASSERT_EQ(s.getmBool(), false);
76 ASSERT_EQ(s.getmU32(), 0);
77 ASSERT_EQ(s.getmI16(), 0);
78 ASSERT_EQ(s.getmF64(), 0.0);
79}
80
81// Test struct constructors
83 // Member constructor
84 Primitive s1(testBool, testU32, testI16, testF64);
85 assertStructMembers(s1);
86
87 // Copy constructor
88 Primitive s2(s1);
89 assertStructMembers(s2);
90}
91
92// Test struct assignment operator
94 Primitive s1;
95 Primitive s2(testBool, testU32, testI16, testF64);
96
97 // Copy assignment
98 s1 = s2;
99 assertStructMembers(s1);
100
101 Primitive& s1Ref = s1;
102 s1 = s1Ref;
103 ASSERT_EQ(&s1, &s1Ref);
104}
105
106// Test struct equality and inequality operators
108 Primitive s1, s2;
109
110 ASSERT_TRUE(s1 == s2);
111 ASSERT_FALSE(s1 != s2);
112
113 s1.setmBool(testBool);
114
115 ASSERT_FALSE(s1 == s2);
116 ASSERT_TRUE(s1 != s2);
117
118 s2.setmBool(testBool);
119 s1.setmU32(testU32);
120
121 ASSERT_FALSE(s1 == s2);
122 ASSERT_TRUE(s1 != s2);
123
124 s2.setmU32(testU32);
125 s1.setmI16(testI16);
126
127 ASSERT_FALSE(s1 == s2);
128 ASSERT_TRUE(s1 != s2);
129
130 s2.setmI16(testI16);
131 s1.setmF64(testF64);
132
133 ASSERT_FALSE(s1 == s2);
134 ASSERT_TRUE(s1 != s2);
135
136 s2.setmF64(testF64);
137
138 ASSERT_TRUE(s1 == s2);
139 ASSERT_FALSE(s1 != s2);
140}
141
142// Test struct getter and setter functions
143TEST_F(PrimitiveStructTest, GetterSetterFunctions) {
144 Primitive s1, s2;
145
146 // Set all members
147 s1.set(testBool, testU32, testI16, testF64);
148 assertStructMembers(s1);
149
150 // Set individual members
151 s2.setmBool(testBool);
152 ASSERT_EQ(s2.getmBool(), testBool);
153
154 s2.setmU32(testU32);
155 ASSERT_EQ(s2.getmU32(), testU32);
156
157 s2.setmI16(testI16);
158 ASSERT_EQ(s2.getmI16(), testI16);
159
160 s2.setmF64(testF64);
161 ASSERT_EQ(s2.getmF64(), testF64);
162}
163
164// Test struct serialization and deserialization
166 Primitive s(testBool, testU32, testI16, testF64);
167 Primitive sCopy;
168
169 Fw::SerializeStatus status;
170
171 // Test successful serialization
172 U8 data[Primitive::SERIALIZED_SIZE];
173 Fw::SerialBuffer buf(data, sizeof(data));
174
175 // Serialize
176 status = buf.serialize(s);
177
178 ASSERT_EQ(status, Fw::FW_SERIALIZE_OK);
179 ASSERT_EQ(buf.getBuffLength(), Primitive::SERIALIZED_SIZE);
180
181 // Deserialize
182 status = buf.deserialize(sCopy);
183
184 ASSERT_EQ(status, Fw::FW_SERIALIZE_OK);
185 ASSERT_EQ(s, sCopy);
186
187 // Test unsuccessful serialization
188 assertUnsuccessfulSerialization(s, sizeof(U8) - 1);
189 assertUnsuccessfulSerialization(s, sizeof(U8) + sizeof(U32) - 1);
190 assertUnsuccessfulSerialization(s, sizeof(U8) + sizeof(U32)
191 + sizeof(I16) - 1);
192 assertUnsuccessfulSerialization(s, Primitive::SERIALIZED_SIZE - 1);
193}
194
195// Test struct toString() and ostream operator functions
197 Primitive s(testBool, testU32, testI16, testF64);
198 std::stringstream buf1, buf2;
199
200 buf1 << s;
201
202 buf2 << "( "
203 << "mBool = " << testBool << ", "
204 << "mU32 = " << testU32 << ", "
205 << "mI16 = " << testI16 << ", "
206 << "mF64 = " << std::fixed << testF64
207 << " )";
208
209 // Truncate string output
210 char buf2Str[FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE];
211 Fw::StringUtils::string_copy(buf2Str, buf2.str().c_str(),
212 FW_SERIALIZABLE_TO_STRING_BUFFER_SIZE);
213
214 ASSERT_STREQ(buf1.str().c_str(), buf2Str);
215}
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
TEST_F(PrimitiveStructTest, Default)
A variable-length serializable buffer.
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
NATIVE_UINT_TYPE getBuffLength() const
returns current buffer size
void assertStructMembers(const Primitive &s)
void assertUnsuccessfulSerialization(Primitive &s, U32 bufSize)
U32 getNonzeroU32()
Definition Utils.cpp:30
char * string_copy(char *destination, const char *source, U32 num)
copy string with null-termination guaranteed
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.