separate unittest folder
This commit is contained in:
7
unittests/globalfunctions/CMakeLists.txt
Normal file
7
unittests/globalfunctions/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
target_sources(${FSFW_TEST_TGT} PRIVATE
|
||||
testDleEncoder.cpp
|
||||
testOpDivider.cpp
|
||||
testBitutil.cpp
|
||||
testCRC.cpp
|
||||
testTimevalOperations.cpp
|
||||
)
|
61
unittests/globalfunctions/testBitutil.cpp
Normal file
61
unittests/globalfunctions/testBitutil.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "fsfw/globalfunctions/bitutility.h"
|
||||
|
||||
TEST_CASE("Bitutility", "[Bitutility]") {
|
||||
uint8_t dummyByte = 0;
|
||||
bool bitSet = false;
|
||||
for (uint8_t pos = 0; pos < 8; pos++) {
|
||||
bitutil::set(&dummyByte, pos);
|
||||
REQUIRE(dummyByte == (1 << (7 - pos)));
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == 1);
|
||||
dummyByte = 0;
|
||||
}
|
||||
|
||||
dummyByte = 0xff;
|
||||
for (uint8_t pos = 0; pos < 8; pos++) {
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == 1);
|
||||
bitutil::clear(&dummyByte, pos);
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == 0);
|
||||
dummyByte = 0xff;
|
||||
}
|
||||
|
||||
dummyByte = 0xf0;
|
||||
for (uint8_t pos = 0; pos < 8; pos++) {
|
||||
if (pos < 4) {
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == 1);
|
||||
bitutil::toggle(&dummyByte, pos);
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == 0);
|
||||
} else {
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == false);
|
||||
bitutil::toggle(&dummyByte, pos);
|
||||
bitutil::get(&dummyByte, pos, bitSet);
|
||||
REQUIRE(bitSet == true);
|
||||
}
|
||||
}
|
||||
REQUIRE(dummyByte == 0x0f);
|
||||
|
||||
dummyByte = 0;
|
||||
bitutil::set(&dummyByte, 8);
|
||||
REQUIRE(dummyByte == 0);
|
||||
bitutil::set(&dummyByte, -1);
|
||||
REQUIRE(dummyByte == 0);
|
||||
dummyByte = 0xff;
|
||||
bitutil::clear(&dummyByte, 8);
|
||||
REQUIRE(dummyByte == 0xff);
|
||||
bitutil::clear(&dummyByte, -1);
|
||||
REQUIRE(dummyByte == 0xff);
|
||||
dummyByte = 0x00;
|
||||
bitutil::toggle(&dummyByte, 8);
|
||||
REQUIRE(dummyByte == 0x00);
|
||||
bitutil::toggle(&dummyByte, -1);
|
||||
REQUIRE(dummyByte == 0x00);
|
||||
|
||||
REQUIRE(bitutil::get(&dummyByte, 8, bitSet) == false);
|
||||
}
|
14
unittests/globalfunctions/testCRC.cpp
Normal file
14
unittests/globalfunctions/testCRC.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include <array>
|
||||
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
#include "fsfw/globalfunctions/CRC.h"
|
||||
#include "fsfw_tests/unit/CatchDefinitions.h"
|
||||
|
||||
TEST_CASE("CRC", "[CRC]") {
|
||||
std::array<uint8_t, 10> testData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
uint16_t crc = CRC::crc16ccitt(testData.data(), 10);
|
||||
REQUIRE(crc == 49729);
|
||||
for (uint8_t index = 0; index < testData.size(); index++) {
|
||||
REQUIRE(testData[index] == index);
|
||||
}
|
||||
}
|
212
unittests/globalfunctions/testDleEncoder.cpp
Normal file
212
unittests/globalfunctions/testDleEncoder.cpp
Normal file
@ -0,0 +1,212 @@
|
||||
#include <array>
|
||||
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
#include "fsfw/globalfunctions/DleEncoder.h"
|
||||
#include "fsfw_tests/unit/CatchDefinitions.h"
|
||||
|
||||
const std::vector<uint8_t> TEST_ARRAY_0 = {0, 0, 0, 0, 0};
|
||||
const std::vector<uint8_t> TEST_ARRAY_1 = {0, DleEncoder::DLE_CHAR, 5};
|
||||
const std::vector<uint8_t> TEST_ARRAY_2 = {0, DleEncoder::STX_CHAR, 5};
|
||||
const std::vector<uint8_t> TEST_ARRAY_3 = {0, DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR};
|
||||
const std::vector<uint8_t> TEST_ARRAY_4 = {DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR,
|
||||
DleEncoder::STX_CHAR};
|
||||
|
||||
const std::vector<uint8_t> TEST_ARRAY_0_ENCODED_ESCAPED = {DleEncoder::STX_CHAR, 0, 0, 0, 0, 0,
|
||||
DleEncoder::ETX_CHAR};
|
||||
const std::vector<uint8_t> TEST_ARRAY_0_ENCODED_NON_ESCAPED = {
|
||||
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0, 0, 0, 0, 0,
|
||||
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR};
|
||||
|
||||
const std::vector<uint8_t> TEST_ARRAY_1_ENCODED_ESCAPED = {
|
||||
DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR, 5, DleEncoder::ETX_CHAR};
|
||||
const std::vector<uint8_t> TEST_ARRAY_1_ENCODED_NON_ESCAPED = {
|
||||
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR, 5,
|
||||
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR};
|
||||
|
||||
const std::vector<uint8_t> TEST_ARRAY_2_ENCODED_ESCAPED = {
|
||||
DleEncoder::STX_CHAR, 0, DleEncoder::DLE_CHAR,
|
||||
DleEncoder::STX_CHAR + 0x40, 5, DleEncoder::ETX_CHAR};
|
||||
const std::vector<uint8_t> TEST_ARRAY_2_ENCODED_NON_ESCAPED = {
|
||||
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0, DleEncoder::STX_CHAR, 5,
|
||||
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR};
|
||||
|
||||
const std::vector<uint8_t> TEST_ARRAY_3_ENCODED_ESCAPED = {
|
||||
DleEncoder::STX_CHAR, 0,
|
||||
DleEncoder::CARRIAGE_RETURN, DleEncoder::DLE_CHAR,
|
||||
DleEncoder::ETX_CHAR + 0x40, DleEncoder::ETX_CHAR};
|
||||
const std::vector<uint8_t> TEST_ARRAY_3_ENCODED_NON_ESCAPED = {
|
||||
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, 0,
|
||||
DleEncoder::CARRIAGE_RETURN, DleEncoder::ETX_CHAR, DleEncoder::DLE_CHAR,
|
||||
DleEncoder::ETX_CHAR};
|
||||
|
||||
const std::vector<uint8_t> TEST_ARRAY_4_ENCODED_ESCAPED = {
|
||||
DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
|
||||
DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR + 0x40, DleEncoder::DLE_CHAR,
|
||||
DleEncoder::STX_CHAR + 0x40, DleEncoder::ETX_CHAR};
|
||||
const std::vector<uint8_t> TEST_ARRAY_4_ENCODED_NON_ESCAPED = {
|
||||
DleEncoder::DLE_CHAR, DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::DLE_CHAR,
|
||||
DleEncoder::ETX_CHAR, DleEncoder::STX_CHAR, DleEncoder::DLE_CHAR, DleEncoder::ETX_CHAR};
|
||||
|
||||
TEST_CASE("DleEncoder", "[DleEncoder]") {
|
||||
DleEncoder dleEncoder;
|
||||
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
|
||||
std::array<uint8_t, 32> buffer;
|
||||
|
||||
size_t encodedLen = 0;
|
||||
size_t readLen = 0;
|
||||
size_t decodedLen = 0;
|
||||
|
||||
auto testLambdaEncode = [&](DleEncoder& encoder, const std::vector<uint8_t>& vecToEncode,
|
||||
const std::vector<uint8_t>& expectedVec) {
|
||||
result = encoder.encode(vecToEncode.data(), vecToEncode.size(), buffer.data(), buffer.size(),
|
||||
&encodedLen);
|
||||
REQUIRE(result == retval::CATCH_OK);
|
||||
for (size_t idx = 0; idx < expectedVec.size(); idx++) {
|
||||
REQUIRE(buffer[idx] == expectedVec[idx]);
|
||||
}
|
||||
REQUIRE(encodedLen == expectedVec.size());
|
||||
};
|
||||
|
||||
auto testLambdaDecode = [&](DleEncoder& encoder, const std::vector<uint8_t>& testVecEncoded,
|
||||
const std::vector<uint8_t>& expectedVec) {
|
||||
result = encoder.decode(testVecEncoded.data(), testVecEncoded.size(), &readLen, buffer.data(),
|
||||
buffer.size(), &decodedLen);
|
||||
REQUIRE(result == retval::CATCH_OK);
|
||||
REQUIRE(readLen == testVecEncoded.size());
|
||||
REQUIRE(decodedLen == expectedVec.size());
|
||||
for (size_t idx = 0; idx < decodedLen; idx++) {
|
||||
REQUIRE(buffer[idx] == expectedVec[idx]);
|
||||
}
|
||||
};
|
||||
|
||||
SECTION("Encoding") {
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_ESCAPED);
|
||||
|
||||
auto testFaultyEncoding = [&](const std::vector<uint8_t>& vecToEncode,
|
||||
const std::vector<uint8_t>& expectedVec) {
|
||||
for (size_t faultyDestSize = 0; faultyDestSize < expectedVec.size(); faultyDestSize++) {
|
||||
result = dleEncoder.encode(vecToEncode.data(), vecToEncode.size(), buffer.data(),
|
||||
faultyDestSize, &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::STREAM_TOO_SHORT));
|
||||
}
|
||||
};
|
||||
|
||||
testFaultyEncoding(TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_ESCAPED);
|
||||
|
||||
dleEncoder.setEscapeMode(false);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_NON_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_NON_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_NON_ESCAPED);
|
||||
testLambdaEncode(dleEncoder, TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_NON_ESCAPED);
|
||||
|
||||
testFaultyEncoding(TEST_ARRAY_0, TEST_ARRAY_0_ENCODED_NON_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_1, TEST_ARRAY_1_ENCODED_NON_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_2, TEST_ARRAY_2_ENCODED_NON_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_3, TEST_ARRAY_3_ENCODED_NON_ESCAPED);
|
||||
testFaultyEncoding(TEST_ARRAY_4, TEST_ARRAY_4_ENCODED_NON_ESCAPED);
|
||||
dleEncoder.setEscapeMode(true);
|
||||
}
|
||||
|
||||
SECTION("Decoding") {
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_0_ENCODED_ESCAPED, TEST_ARRAY_0);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_1_ENCODED_ESCAPED, TEST_ARRAY_1);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_2_ENCODED_ESCAPED, TEST_ARRAY_2);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_3_ENCODED_ESCAPED, TEST_ARRAY_3);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_4_ENCODED_ESCAPED, TEST_ARRAY_4);
|
||||
|
||||
// Faulty source data
|
||||
auto testArray1EncodedFaulty = TEST_ARRAY_1_ENCODED_ESCAPED;
|
||||
testArray1EncodedFaulty[3] = 0;
|
||||
result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
auto testArray2EncodedFaulty = TEST_ARRAY_2_ENCODED_ESCAPED;
|
||||
testArray2EncodedFaulty[5] = 0;
|
||||
result = dleEncoder.decode(testArray2EncodedFaulty.data(), testArray2EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
auto testArray4EncodedFaulty = TEST_ARRAY_4_ENCODED_ESCAPED;
|
||||
testArray4EncodedFaulty[2] = 0;
|
||||
result = dleEncoder.decode(testArray4EncodedFaulty.data(), testArray4EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
auto testArray4EncodedFaulty2 = TEST_ARRAY_4_ENCODED_ESCAPED;
|
||||
testArray4EncodedFaulty2[4] = 0;
|
||||
result = dleEncoder.decode(testArray4EncodedFaulty2.data(), testArray4EncodedFaulty2.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
|
||||
auto testFaultyDecoding = [&](const std::vector<uint8_t>& vecToDecode,
|
||||
const std::vector<uint8_t>& expectedVec) {
|
||||
for (size_t faultyDestSizes = 0; faultyDestSizes < expectedVec.size(); faultyDestSizes++) {
|
||||
result = dleEncoder.decode(vecToDecode.data(), vecToDecode.size(), &readLen, buffer.data(),
|
||||
faultyDestSizes, &decodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::STREAM_TOO_SHORT));
|
||||
}
|
||||
};
|
||||
|
||||
testFaultyDecoding(TEST_ARRAY_0_ENCODED_ESCAPED, TEST_ARRAY_0);
|
||||
testFaultyDecoding(TEST_ARRAY_1_ENCODED_ESCAPED, TEST_ARRAY_1);
|
||||
testFaultyDecoding(TEST_ARRAY_2_ENCODED_ESCAPED, TEST_ARRAY_2);
|
||||
testFaultyDecoding(TEST_ARRAY_3_ENCODED_ESCAPED, TEST_ARRAY_3);
|
||||
testFaultyDecoding(TEST_ARRAY_4_ENCODED_ESCAPED, TEST_ARRAY_4);
|
||||
|
||||
dleEncoder.setEscapeMode(false);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_0_ENCODED_NON_ESCAPED, TEST_ARRAY_0);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_1_ENCODED_NON_ESCAPED, TEST_ARRAY_1);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_2_ENCODED_NON_ESCAPED, TEST_ARRAY_2);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_3_ENCODED_NON_ESCAPED, TEST_ARRAY_3);
|
||||
testLambdaDecode(dleEncoder, TEST_ARRAY_4_ENCODED_NON_ESCAPED, TEST_ARRAY_4);
|
||||
|
||||
testFaultyDecoding(TEST_ARRAY_0_ENCODED_NON_ESCAPED, TEST_ARRAY_0);
|
||||
testFaultyDecoding(TEST_ARRAY_1_ENCODED_NON_ESCAPED, TEST_ARRAY_1);
|
||||
testFaultyDecoding(TEST_ARRAY_2_ENCODED_NON_ESCAPED, TEST_ARRAY_2);
|
||||
testFaultyDecoding(TEST_ARRAY_3_ENCODED_NON_ESCAPED, TEST_ARRAY_3);
|
||||
testFaultyDecoding(TEST_ARRAY_4_ENCODED_NON_ESCAPED, TEST_ARRAY_4);
|
||||
|
||||
// Faulty source data
|
||||
testArray1EncodedFaulty = TEST_ARRAY_1_ENCODED_NON_ESCAPED;
|
||||
auto prevVal = testArray1EncodedFaulty[0];
|
||||
testArray1EncodedFaulty[0] = 0;
|
||||
result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
testArray1EncodedFaulty[0] = prevVal;
|
||||
testArray1EncodedFaulty[1] = 0;
|
||||
result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
|
||||
testArray1EncodedFaulty = TEST_ARRAY_1_ENCODED_NON_ESCAPED;
|
||||
testArray1EncodedFaulty[6] = 0;
|
||||
result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
testArray1EncodedFaulty = TEST_ARRAY_1_ENCODED_NON_ESCAPED;
|
||||
testArray1EncodedFaulty[7] = 0;
|
||||
result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
testArray4EncodedFaulty = TEST_ARRAY_4_ENCODED_NON_ESCAPED;
|
||||
testArray4EncodedFaulty[3] = 0;
|
||||
result = dleEncoder.decode(testArray4EncodedFaulty.data(), testArray4EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
|
||||
dleEncoder.setEscapeMode(true);
|
||||
testArray1EncodedFaulty = TEST_ARRAY_1_ENCODED_ESCAPED;
|
||||
testArray1EncodedFaulty[5] = 0;
|
||||
result = dleEncoder.decode(testArray1EncodedFaulty.data(), testArray1EncodedFaulty.size(),
|
||||
&readLen, buffer.data(), buffer.size(), &encodedLen);
|
||||
REQUIRE(result == static_cast<int>(DleEncoder::DECODING_ERROR));
|
||||
}
|
||||
}
|
65
unittests/globalfunctions/testOpDivider.cpp
Normal file
65
unittests/globalfunctions/testOpDivider.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "fsfw/globalfunctions/PeriodicOperationDivider.h"
|
||||
|
||||
TEST_CASE("OpDivider", "[OpDivider]") {
|
||||
auto opDivider = PeriodicOperationDivider(1);
|
||||
REQUIRE(opDivider.getDivider() == 1);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.check() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.check() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
|
||||
opDivider.setDivider(0);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
|
||||
opDivider.setDivider(2);
|
||||
opDivider.resetCounter();
|
||||
REQUIRE(opDivider.getDivider() == 2);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.check() == false);
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
REQUIRE(opDivider.getCounter() == 2);
|
||||
REQUIRE(opDivider.check() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.check() == false);
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
REQUIRE(opDivider.getCounter() == 2);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
|
||||
opDivider.setDivider(3);
|
||||
opDivider.resetCounter();
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
REQUIRE(opDivider.getCounter() == 3);
|
||||
REQUIRE(opDivider.checkAndIncrement() == true);
|
||||
REQUIRE(opDivider.getCounter() == 1);
|
||||
REQUIRE(opDivider.checkAndIncrement() == false);
|
||||
|
||||
auto opDividerNonResetting = PeriodicOperationDivider(2, false);
|
||||
REQUIRE(opDividerNonResetting.getCounter() == 1);
|
||||
REQUIRE(opDividerNonResetting.check() == false);
|
||||
REQUIRE(opDividerNonResetting.checkAndIncrement() == false);
|
||||
REQUIRE(opDividerNonResetting.getCounter() == 2);
|
||||
REQUIRE(opDividerNonResetting.check() == true);
|
||||
REQUIRE(opDividerNonResetting.checkAndIncrement() == true);
|
||||
REQUIRE(opDividerNonResetting.getCounter() == 3);
|
||||
REQUIRE(opDividerNonResetting.checkAndIncrement() == true);
|
||||
REQUIRE(opDividerNonResetting.getCounter() == 4);
|
||||
opDividerNonResetting.resetCounter();
|
||||
REQUIRE(opDividerNonResetting.getCounter() == 1);
|
||||
REQUIRE(opDividerNonResetting.check() == false);
|
||||
REQUIRE(opDividerNonResetting.checkAndIncrement() == false);
|
||||
REQUIRE(opDividerNonResetting.getCounter() == 2);
|
||||
}
|
124
unittests/globalfunctions/testTimevalOperations.cpp
Normal file
124
unittests/globalfunctions/testTimevalOperations.cpp
Normal file
@ -0,0 +1,124 @@
|
||||
#include <fsfw/globalfunctions/timevalOperations.h>
|
||||
|
||||
#include <catch2/catch_approx.hpp>
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include "fsfw_tests/unit/CatchDefinitions.h"
|
||||
|
||||
TEST_CASE("TimevalTest", "[timevalOperations]") {
|
||||
SECTION("Comparison") {
|
||||
timeval t1;
|
||||
t1.tv_sec = 1648227422;
|
||||
t1.tv_usec = 123456;
|
||||
timeval t2;
|
||||
t2.tv_sec = 1648227422;
|
||||
t2.tv_usec = 123456;
|
||||
REQUIRE(t1 == t2);
|
||||
REQUIRE(t2 == t1);
|
||||
REQUIRE_FALSE(t1 != t2);
|
||||
REQUIRE_FALSE(t2 != t1);
|
||||
REQUIRE(t1 <= t2);
|
||||
REQUIRE(t2 <= t1);
|
||||
REQUIRE(t1 >= t2);
|
||||
REQUIRE(t2 >= t1);
|
||||
REQUIRE_FALSE(t1 < t2);
|
||||
REQUIRE_FALSE(t2 < t1);
|
||||
REQUIRE_FALSE(t1 > t2);
|
||||
REQUIRE_FALSE(t2 > t1);
|
||||
|
||||
timeval t3;
|
||||
t3.tv_sec = 1648227422;
|
||||
t3.tv_usec = 123457;
|
||||
REQUIRE_FALSE(t1 == t3);
|
||||
REQUIRE(t1 != t3);
|
||||
REQUIRE(t1 <= t3);
|
||||
REQUIRE_FALSE(t3 <= t1);
|
||||
REQUIRE_FALSE(t1 >= t3);
|
||||
REQUIRE(t3 >= t1);
|
||||
REQUIRE(t1 < t3);
|
||||
REQUIRE_FALSE(t3 < t1);
|
||||
REQUIRE_FALSE(t1 > t3);
|
||||
REQUIRE(t3 > t1);
|
||||
|
||||
timeval t4;
|
||||
t4.tv_sec = 1648227423;
|
||||
t4.tv_usec = 123456;
|
||||
REQUIRE_FALSE(t1 == t4);
|
||||
REQUIRE(t1 != t4);
|
||||
REQUIRE(t1 <= t4);
|
||||
REQUIRE_FALSE(t4 <= t1);
|
||||
REQUIRE_FALSE(t1 >= t4);
|
||||
REQUIRE(t4 >= t1);
|
||||
REQUIRE(t1 < t4);
|
||||
REQUIRE_FALSE(t4 < t1);
|
||||
REQUIRE_FALSE(t1 > t4);
|
||||
REQUIRE(t4 > t1);
|
||||
}
|
||||
SECTION("Operators") {
|
||||
timeval t1;
|
||||
t1.tv_sec = 1648227422;
|
||||
t1.tv_usec = 123456;
|
||||
timeval t2;
|
||||
t2.tv_sec = 1648227422;
|
||||
t2.tv_usec = 123456;
|
||||
timeval t3 = t1 - t2;
|
||||
REQUIRE(t3.tv_sec == 0);
|
||||
REQUIRE(t3.tv_usec == 0);
|
||||
timeval t4 = t1 - t3;
|
||||
REQUIRE(t4.tv_sec == 1648227422);
|
||||
REQUIRE(t4.tv_usec == 123456);
|
||||
timeval t5 = t3 - t1;
|
||||
REQUIRE(t5.tv_sec == -1648227422);
|
||||
REQUIRE(t5.tv_usec == -123456);
|
||||
|
||||
timeval t6;
|
||||
t6.tv_sec = 1648227400;
|
||||
t6.tv_usec = 999999;
|
||||
|
||||
timeval t7 = t6 + t1;
|
||||
REQUIRE(t7.tv_sec == (1648227422ull + 1648227400ull + 1ull));
|
||||
REQUIRE(t7.tv_usec == 123455);
|
||||
|
||||
timeval t8 = t1 - t6;
|
||||
REQUIRE(t8.tv_sec == 1648227422 - 1648227400 - 1);
|
||||
REQUIRE(t8.tv_usec == 123457);
|
||||
|
||||
double scalar = 2;
|
||||
timeval t9 = t1 * scalar;
|
||||
REQUIRE(t9.tv_sec == 3296454844);
|
||||
REQUIRE(t9.tv_usec == 246912);
|
||||
timeval t10 = scalar * t1;
|
||||
REQUIRE(t10.tv_sec == 3296454844);
|
||||
REQUIRE(t10.tv_usec == 246912);
|
||||
timeval t11 = t6 * scalar;
|
||||
REQUIRE(t11.tv_sec == (3296454800 + 1));
|
||||
REQUIRE(t11.tv_usec == 999998);
|
||||
|
||||
timeval t12 = t1 / scalar;
|
||||
REQUIRE(t12.tv_sec == 824113711);
|
||||
REQUIRE(t12.tv_usec == 61728);
|
||||
|
||||
timeval t13 = t6 / scalar;
|
||||
REQUIRE(t13.tv_sec == 824113700);
|
||||
// Rounding issue
|
||||
REQUIRE(t13.tv_usec == 499999);
|
||||
|
||||
double scalar2 = t9 / t1;
|
||||
REQUIRE(scalar2 == Catch::Approx(2.0));
|
||||
double scalar3 = t1 / t6;
|
||||
REQUIRE(scalar3 == Catch::Approx(1.000000013));
|
||||
double scalar4 = t3 / t1;
|
||||
REQUIRE(scalar4 == Catch::Approx(0));
|
||||
double scalar5 = t12 / t1;
|
||||
REQUIRE(scalar5 == Catch::Approx(0.5));
|
||||
}
|
||||
|
||||
SECTION("timevalOperations::toTimeval") {
|
||||
double seconds = 1648227422.123456;
|
||||
timeval t1 = timevalOperations::toTimeval(seconds);
|
||||
REQUIRE(t1.tv_sec == 1648227422);
|
||||
// Allow 1 usec rounding tolerance
|
||||
REQUIRE(t1.tv_usec >= 123455);
|
||||
REQUIRE(t1.tv_usec <= 123457);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user