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
EnumToStringTest.cpp
Go to the documentation of this file.
1// ======================================================================
2// \title EnumToStringTest.cpp
3// \author T. Chieu
4// \brief cpp file for EnumToStringTest 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/enum/ImplicitEnumAc.hpp"
14#include "FppTest/enum/ExplicitEnumAc.hpp"
15#include "FppTest/enum/DefaultEnumAc.hpp"
16#include "FppTest/enum/IntervalEnumAc.hpp"
17#include "FppTest/enum/SerializeTypeU8EnumAc.hpp"
18#include "FppTest/enum/SerializeTypeU64EnumAc.hpp"
19
20#include "gtest/gtest.h"
21
22#include <cstring>
23#include <sstream>
24
25namespace FppTest {
26
27 // Populate an array with enum values
28 template <typename EnumType>
29 void setEnumValArray(typename EnumType::T (&a)[EnumType::NUM_CONSTANTS+1]) {
30 for (U32 i = 0; i < EnumType::NUM_CONSTANTS + 1; i++) {
31 a[i] = static_cast<typename EnumType::T>(i);
32 }
33 }
34
35 template<>
36 void setEnumValArray<Explicit>(Explicit::T (&a)[Explicit::NUM_CONSTANTS+1]) {
37 a[0] = Explicit::A;
38 a[1] = Explicit::B;
39 a[2] = Explicit::C;
40 a[3] = static_cast<Explicit::T>(11);
41 }
42
43 template<>
44 void setEnumValArray<Interval>(Interval::T (&a)[Interval::NUM_CONSTANTS+1]) {
45 a[0] = Interval::A;
46 a[1] = Interval::B;
47 a[2] = Interval::C;
48 a[3] = Interval::D;
49 a[4] = Interval::E;
50 a[5] = Interval::F;
51 a[6] = Interval::G;
52 a[7] = static_cast<Interval::T>(11);
53 }
54
55 // Populate an array with strings representing enum values
56 template <typename EnumType>
57 void setEnumStrArray(std::string (&a)[EnumType::NUM_CONSTANTS+1]) {
58 a[0] = "A (0)";
59 a[1] = "B (1)";
60 a[2] = "C (2)";
61 a[3] = "D (3)";
62 a[4] = "E (4)";
63 a[5] = "[invalid] (5)";
64 }
65
66 template<>
67 void setEnumStrArray<Explicit>(std::string (&a)[Explicit::NUM_CONSTANTS+1]) {
68 a[0] = "A (-1952875139)";
69 a[1] = "B (2)";
70 a[2] = "C (2000999333)";
71 a[3] = "[invalid] (11)";
72 }
73
74 template <>
75 void setEnumStrArray<Interval>(std::string (&a)[Interval::NUM_CONSTANTS+1]) {
76 a[0] = "A (0)";
77 a[1] = "B (3)";
78 a[2] = "C (4)";
79 a[3] = "D (5)";
80 a[4] = "E (10)";
81 a[5] = "F (100)";
82 a[6] = "G (101)";
83 a[7] = "[invalid] (11)";
84 }
85
86} // namespace FppTest
87
88// Test enum string functions
89template <typename EnumType>
90class EnumToStringTest : public ::testing::Test {
91protected:
92 void SetUp() override {
93 FppTest::setEnumValArray<EnumType>(vals);
94 FppTest::setEnumStrArray<EnumType>(strs);
95 };
96
97 EnumType e;
98 std::stringstream buf;
99
100 typename EnumType::T vals[EnumType::NUM_CONSTANTS+1];
101 std::string strs[EnumType::NUM_CONSTANTS+1];
102};
103
104// Specify type parameters for this test suite
105using EnumTypes = ::testing::Types<
106 Implicit,
107 Explicit,
108 Default,
109 Interval,
110 SerializeTypeU8,
111 SerializeTypeU64
112>;
114
115// Test enum toString() and ostream operator functions
117 for (U32 i = 0; i < TypeParam::NUM_CONSTANTS + 1; i++) {
118 this->e = this->vals[i];
119 this->buf << this->e;
120
121 ASSERT_STREQ(this->buf.str().c_str(), this->strs[i].c_str());
122
123 this->buf.str("");
124 }
125}
TYPED_TEST(EnumToStringTest, ToString)
TYPED_TEST_SUITE(EnumToStringTest, EnumTypes)
::testing::Types< Implicit, Explicit, Default, Interval, SerializeTypeU8, SerializeTypeU64 > EnumTypes
std::stringstream buf
void SetUp() override
EnumType::T vals[EnumType::NUM_CONSTANTS+1]
std::string strs[EnumType::NUM_CONSTANTS+1]
void setEnumStrArray(std::string(&a)[EnumType::NUM_CONSTANTS+1])
void setEnumStrArray< Explicit >(std::string(&a)[Explicit::NUM_CONSTANTS+1])
void setEnumValArray< Interval >(Interval::T(&a)[Interval::NUM_CONSTANTS+1])
void setEnumValArray(typename EnumType::T(&a)[EnumType::NUM_CONSTANTS+1])
void setEnumStrArray< Interval >(std::string(&a)[Interval::NUM_CONSTANTS+1])
void setEnumValArray< Explicit >(Explicit::T(&a)[Explicit::NUM_CONSTANTS+1])