Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise component unit test code gen #331

Merged
merged 10 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ abstract class ComponentCppWriterUtils(
case _ => portParamMap(p.getUnqualifiedName)
}

/** Determine whether a port has params */
def hasPortParams(p: PortInstance): Boolean = getPortFunctionParams(p) != Nil

/** Get a return type of a port instance as an optional C++ type */
def getPortReturnType(p: PortInstance): Option[String] =
p.getType match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ case class ComponentGTestBaseWriter(

functionClassMember(
Some(s"From port: $portName"),
fromPortAssertionFuncName(portName),
fromPortSizeAssertionFuncName(portName),
sizeAssertionFunctionParams,
CppDoc.Type("void"),
lines(
Expand Down Expand Up @@ -426,9 +426,10 @@ case class ComponentGTestBaseWriter(
|"""
),
eventParamTypeMap(id).flatMap((name, tn) => {
val assertEq = writeEventAssertEq(tn)
val eventValue = writeEventValue(s"_e.$name", tn)
lines(
s"""ASSERT_EQ($name, $eventValue)
s"""$assertEq($name, $eventValue)
| << "\\n"
| << __callSiteFileName << ":" << __callSiteLineNumber << "\\n"
| << " Value: Value of argument $name at index "
Expand Down Expand Up @@ -523,6 +524,7 @@ case class ComponentGTestBaseWriter(
val channelName = channel.getName
val historyName = tlmHistoryName(channelName)
val entryName = tlmEntryName(channelName)
val assertEq = writeAssertEq(channel.channelType)
val value = writeValue("_e.arg", channel.channelType)
functionClassMember(
Some(s"Channel: $channelName"),
Expand All @@ -545,7 +547,7 @@ case class ComponentGTestBaseWriter(
| << " Actual: " << __index << "\\n";
|const $entryName& _e =
| this->$historyName->at(__index);
|ASSERT_EQ(val, $value)
|$assertEq(val, $value)
| << "\\n"
| << __callSiteFileName << ":" << __callSiteLineNumber << "\\n"
| << " Value: Value at index "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class ComponentTestUtils(
)

val timeTagParam: CppDoc.Function.Param = CppDoc.Function.Param(
CppDoc.Type("Fw::Time&"),
CppDoc.Type("const Fw::Time&"),
"timeTag",
Some("The time")
)
Expand Down Expand Up @@ -119,12 +119,24 @@ abstract class ComponentTestUtils(
case _ => value
}

def writeAssertEq(t: Type): String =
t match {
case Type.String(_) => "ASSERT_STREQ"
case _ => "ASSERT_EQ"
}

def writeEventValue(value: String, typeName: String): String =
typeName match {
case "Fw::LogStringArg" => s"$value.toChar()"
case _ => value
}

def writeEventAssertEq(typeName: String): String =
typeName match {
case "Fw::LogStringArg" => "ASSERT_STREQ"
case _ => "ASSERT_EQ"
}

def writeCppType(t: Type): String = {
val typeName = TypeCppWriter.getName(s, t, Some("char*"))
t match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ case class ComponentTesterBaseWriter(
}),
{
lazy val portHistories = line("// Initialize port histories") ::
typedOutputPorts.map(p => {
typedOutputPorts.filter(hasPortParams).map(p => {
val historyName = fromPortHistoryName(p.getUnqualifiedName)
val entryName = fromPortEntryName(p.getUnqualifiedName)
line(s"this->$historyName = new History<$entryName>(maxHistorySize);")
Expand Down Expand Up @@ -239,7 +239,7 @@ case class ComponentTesterBaseWriter(
Some(s"Destroy object $testerBaseClassName"),
{
lazy val destroyPortHistories = line("// Destroy port histories") ::
typedOutputPorts.map(p => {
typedOutputPorts.filter(hasPortParams).map(p => {
val portHistoryName = fromPortHistoryName(p.getUnqualifiedName)
line(s"delete this->$portHistoryName;")
})
Expand Down Expand Up @@ -746,7 +746,8 @@ case class ComponentTesterBaseWriter(
s"""|TextLogEntry e = { id, timeTag, severity, text };
|textLogHistory->push_back(e);
|"""
)
),
CppDoc.Function.Virtual
)
)
List.concat(
Expand Down
2 changes: 1 addition & 1 deletion compiler/tools/fpp-to-cpp/test/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Before running `check-cpp`, do the following:

. Clone the
https://github.com/nasa/fprime[F Prime repository]
and check out branch release/v3.0.0.
and check out the devel branch.

. Set the shell environment variable `FPRIME` to point to
the root of the F Prime repository.
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,48 @@ void ActiveCommandsComponentBase ::
#endif
}

// Connect output port noArgsOut
for (
PlatformIntType port = 0;
port < static_cast<PlatformIntType>(this->getNum_noArgsOut_OutputPorts());
port++
) {
this->m_noArgsOut_OutputPort[port].init();

#if FW_OBJECT_NAMES == 1
char portName[120];
(void) snprintf(
portName,
sizeof(portName),
"%s_noArgsOut_OutputPort[%" PRI_PlatformIntType "]",
this->m_objName,
port
);
this->m_noArgsOut_OutputPort[port].setObjName(portName);
#endif
}

// Connect output port noArgsReturnOut
for (
PlatformIntType port = 0;
port < static_cast<PlatformIntType>(this->getNum_noArgsReturnOut_OutputPorts());
port++
) {
this->m_noArgsReturnOut_OutputPort[port].init();

#if FW_OBJECT_NAMES == 1
char portName[120];
(void) snprintf(
portName,
sizeof(portName),
"%s_noArgsReturnOut_OutputPort[%" PRI_PlatformIntType "]",
this->m_objName,
port
);
this->m_noArgsReturnOut_OutputPort[port].setObjName(portName);
#endif
}

// Connect output port typedOut
for (
PlatformIntType port = 0;
Expand Down Expand Up @@ -965,6 +1007,34 @@ void ActiveCommandsComponentBase ::
// Connect typed input ports to typed output ports
// ----------------------------------------------------------------------

void ActiveCommandsComponentBase ::
set_noArgsOut_OutputPort(
NATIVE_INT_TYPE portNum,
Ports::InputNoArgsPort* port
)
{
FW_ASSERT(
portNum < this->getNum_noArgsOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);

this->m_noArgsOut_OutputPort[portNum].addCallPort(port);
}

void ActiveCommandsComponentBase ::
set_noArgsReturnOut_OutputPort(
NATIVE_INT_TYPE portNum,
Ports::InputNoArgsReturnPort* port
)
{
FW_ASSERT(
portNum < this->getNum_noArgsReturnOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);

this->m_noArgsReturnOut_OutputPort[portNum].addCallPort(port);
}

void ActiveCommandsComponentBase ::
set_typedOut_OutputPort(
NATIVE_INT_TYPE portNum,
Expand Down Expand Up @@ -1109,6 +1179,20 @@ void ActiveCommandsComponentBase ::
// Connect serial input ports to typed output ports
// ----------------------------------------------------------------------

void ActiveCommandsComponentBase ::
set_noArgsOut_OutputPort(
NATIVE_INT_TYPE portNum,
Fw::InputSerializePort* port
)
{
FW_ASSERT(
portNum < this->getNum_noArgsOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);

this->m_noArgsOut_OutputPort[portNum].registerSerialPort(port);
}

void ActiveCommandsComponentBase ::
set_typedOut_OutputPort(
NATIVE_INT_TYPE portNum,
Expand Down Expand Up @@ -1372,6 +1456,18 @@ NATIVE_INT_TYPE ActiveCommandsComponentBase ::
// Getters for numbers of typed output ports
// ----------------------------------------------------------------------

NATIVE_INT_TYPE ActiveCommandsComponentBase ::
getNum_noArgsOut_OutputPorts() const
{
return static_cast<NATIVE_INT_TYPE>(FW_NUM_ARRAY_ELEMENTS(this->m_noArgsOut_OutputPort));
}

NATIVE_INT_TYPE ActiveCommandsComponentBase ::
getNum_noArgsReturnOut_OutputPorts() const
{
return static_cast<NATIVE_INT_TYPE>(FW_NUM_ARRAY_ELEMENTS(this->m_noArgsReturnOut_OutputPort));
}

NATIVE_INT_TYPE ActiveCommandsComponentBase ::
getNum_typedOut_OutputPorts() const
{
Expand Down Expand Up @@ -1484,6 +1580,28 @@ bool ActiveCommandsComponentBase ::
// Connection status queries for typed output ports
// ----------------------------------------------------------------------

bool ActiveCommandsComponentBase ::
isConnected_noArgsOut_OutputPort(NATIVE_INT_TYPE portNum)
{
FW_ASSERT(
portNum < this->getNum_noArgsOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);

return this->m_noArgsOut_OutputPort[portNum].isConnected();
}

bool ActiveCommandsComponentBase ::
isConnected_noArgsReturnOut_OutputPort(NATIVE_INT_TYPE portNum)
{
FW_ASSERT(
portNum < this->getNum_noArgsReturnOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);

return this->m_noArgsReturnOut_OutputPort[portNum].isConnected();
}

bool ActiveCommandsComponentBase ::
isConnected_typedOut_OutputPort(NATIVE_INT_TYPE portNum)
{
Expand Down Expand Up @@ -2279,6 +2397,26 @@ void ActiveCommandsComponentBase ::
// Invocation functions for typed output ports
// ----------------------------------------------------------------------

void ActiveCommandsComponentBase ::
noArgsOut_out(NATIVE_INT_TYPE portNum)
{
FW_ASSERT(
portNum < this->getNum_noArgsOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);
this->m_noArgsOut_OutputPort[portNum].invoke();
}

U32 ActiveCommandsComponentBase ::
noArgsReturnOut_out(NATIVE_INT_TYPE portNum)
{
FW_ASSERT(
portNum < this->getNum_noArgsReturnOut_OutputPorts(),
static_cast<FwAssertArgType>(portNum)
);
return this->m_noArgsReturnOut_OutputPort[portNum].invoke();
}

void ActiveCommandsComponentBase ::
typedOut_out(
NATIVE_INT_TYPE portNum,
Expand Down
Loading