Skip to content

Commit

Permalink
refactor: simplify tests
Browse files Browse the repository at this point in the history
Signed-off-by: aw <[email protected]>
  • Loading branch information
a-w50 authored and SebaLukas committed Jun 19, 2024
1 parent 1654f1c commit 88590c5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 60 deletions.
9 changes: 9 additions & 0 deletions tests/ISO20/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
add_executable(test_ac_charge_loop ac_charge_loop.cpp)
target_sources(test_ac_charge_loop
PRIVATE
codec_helper.cpp
)

target_link_libraries(test_ac_charge_loop
PRIVATE
Expand All @@ -10,6 +14,11 @@ catch_discover_tests(test_ac_charge_loop)


add_executable(test_dc_charge_loop dc_charge_loop.cpp)
target_sources(test_dc_charge_loop
PRIVATE
codec_helper.cpp
)


target_link_libraries(test_dc_charge_loop
PRIVATE
Expand Down
38 changes: 8 additions & 30 deletions tests/ISO20/ac_charge_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <cbv2g/iso_20/iso20_AC_Decoder.h>
#include <cbv2g/iso_20/iso20_AC_Encoder.h>

#include "codec_helper.hpp"

// Exi Stream: 8008041e9869d6a61dc1ef895b9b4a8062832418640096
// XML:
// <AC_ChargeLoopReq>
Expand Down Expand Up @@ -66,23 +68,11 @@ SCENARIO("Encode and decode AC charge loop message") {
init_iso20_ac_BPT_Scheduled_AC_CLReqControlModeType(&charge_loop.BPT_Scheduled_AC_CLReqControlMode);
charge_loop.BPT_Scheduled_AC_CLReqControlMode.EVPresentActivePower = {3, 200};

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(request, doc_raw, sizeof(doc_raw));

REQUIRE(encode_iso20_ac_exiDocument(&exi_stream_in, &request) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

Expand All @@ -107,23 +97,11 @@ SCENARIO("Encode and decode AC charge loop message") {
charge_loop.BPT_Scheduled_AC_CLResControlMode_isUsed = true;
charge_loop.BPT_Scheduled_AC_CLResControlMode = {};

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(response, doc_raw, sizeof(doc_raw));

REQUIRE(encode_iso20_ac_exiDocument(&exi_stream_in, &response) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/ISO20/codec_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "codec_helper.hpp"

#include <vector>

#include <cbv2g/iso_20/iso20_AC_Encoder.h>
#include <cbv2g/iso_20/iso20_DC_Encoder.h>

template <typename DocType>
static EncodingTestResult encode(int (*encode_func)(exi_bitstream_t*, DocType*), DocType& request,
const uint8_t* compare_data, std::size_t length) {
// FIXME (aw): what general size to take here?
uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

Check notice on line 15 in tests/ISO20/codec_helper.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ISO20/codec_helper.cpp#L15

Variable 'errn' is assigned a value that is never used.

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

if (0 != encode_func(&exi_stream_in, &request)) {
return {false, false};
}

// FIXME (aw): why +1?
const auto encoded_stream = std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(compare_data, compare_data + length);

return {true, encoded_stream == expected_exi_stream};
}

template <>
EncodingTestResult encode_iso20_and_test(iso20_ac_exiDocument& request, const uint8_t* compare_data,
std::size_t length) {
return encode(&encode_iso20_ac_exiDocument, request, compare_data, length);
}

template <>
EncodingTestResult encode_iso20_and_test(iso20_dc_exiDocument& request, const uint8_t* compare_data,
std::size_t length) {
return encode(&encode_iso20_dc_exiDocument, request, compare_data, length);
}
15 changes: 15 additions & 0 deletions tests/ISO20/codec_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <cstdint>
#include <memory>

#include <cbv2g/iso_20/iso20_AC_Encoder.h>

struct EncodingTestResult {
bool encoding_successful;

Check notice on line 9 in tests/ISO20/codec_helper.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ISO20/codec_helper.hpp#L9

struct member 'EncodingTestResult::encoding_successful' is never used.
bool bitstream_match;

Check notice on line 10 in tests/ISO20/codec_helper.hpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ISO20/codec_helper.hpp#L10

struct member 'EncodingTestResult::bitstream_match' is never used.
};

// FIXME (aw): the first parameter should be const ref, but the API of cbexigen doesn't respect that
template <typename DocType>
EncodingTestResult encode_iso20_and_test(DocType&, const uint8_t* compare_data, std::size_t length);
38 changes: 8 additions & 30 deletions tests/ISO20/dc_charge_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <cbv2g/iso_20/iso20_DC_Decoder.h>
#include <cbv2g/iso_20/iso20_DC_Encoder.h>

#include "codec_helper.hpp"

// Exi Stream: 8034042d166f29fb80ea560aebdbfb3062810012006164000a02002400c800
// XML:
// <DC_ChargeLoopReq>
Expand Down Expand Up @@ -88,23 +90,11 @@ SCENARIO("Encode and decode DC charge loop message") {
charge_loop.BPT_Scheduled_DC_CLReqControlMode.EVTargetCurrent = {0, 20};
charge_loop.BPT_Scheduled_DC_CLReqControlMode.EVTargetVoltage = {0, 400};

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(request, doc_raw, sizeof(doc_raw));

REQUIRE(encode_iso20_dc_exiDocument(&exi_stream_in, &request) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

Expand Down Expand Up @@ -136,23 +126,11 @@ SCENARIO("Encode and decode DC charge loop message") {
charge_loop.BPT_Scheduled_DC_CLResControlMode_isUsed = true;
charge_loop.BPT_Scheduled_DC_CLResControlMode = {};

uint8_t stream[256] = {};
exi_bitstream_t exi_stream_in;
size_t pos1 = 0;
int errn = 0;

exi_bitstream_init(&exi_stream_in, stream, sizeof(stream), pos1, nullptr);

THEN("It should be encoded succussfully") {
const auto result = encode_iso20_and_test(response, doc_raw, sizeof(doc_raw));

REQUIRE(encode_iso20_dc_exiDocument(&exi_stream_in, &response) == 0);

const auto encoded_stream =
std::vector<uint8_t>(stream, stream + exi_bitstream_get_length(&exi_stream_in) + 1);

const auto expected_exi_stream = std::vector<uint8_t>(std::begin(doc_raw), std::end(doc_raw));

REQUIRE(encoded_stream == expected_exi_stream);
REQUIRE(result.encoding_successful);
REQUIRE(result.bitstream_match);
}
}

Expand Down

0 comments on commit 88590c5

Please sign in to comment.