Skip to content

Commit e22b6c2

Browse files
committed
[C] Handle fields in the correct order and use all declared fields.
1 parent e7c1bbd commit e22b6c2

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

sbe-tool/src/test/c/CodeGenTest.cpp

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static const char FUEL_FIGURES_3_USAGE_DESCRIPTION[] = "Highway Cycle";
4646
static const char MANUFACTURER[] = "Honda";
4747
static const char MODEL[] = "Civic VTi";
4848
static const char ACTIVATION_CODE[] = "deadbeef";
49+
static const char COLOR[] = "silver/white";
4950

5051
static const std::size_t VEHICLE_CODE_LENGTH = sizeof(VEHICLE_CODE);
5152
static const std::size_t MANUFACTURER_CODE_LENGTH = sizeof(MANUFACTURER_CODE);
@@ -55,12 +56,13 @@ static const std::size_t FUEL_FIGURES_3_USAGE_DESCRIPTION_LENGTH = strlen(FUEL_F
5556
static const std::size_t MANUFACTURER_LENGTH = strlen(MANUFACTURER);
5657
static const std::size_t MODEL_LENGTH = strlen(MODEL);
5758
static const std::size_t ACTIVATION_CODE_LENGTH = strlen(ACTIVATION_CODE);
59+
static const std::size_t COLOR_LENGTH = strlen(COLOR);
5860
static const std::uint8_t PERFORMANCE_FIGURES_COUNT = 2;
5961
static const std::uint8_t FUEL_FIGURES_COUNT = 3;
6062
static const std::uint8_t ACCELERATION_COUNT = 3;
6163

6264
static const std::uint64_t expectedHeaderSize = 8;
63-
static const std::uint64_t expectedCarEncodedLength = 191;
65+
static const std::uint64_t expectedCarEncodedLength = 207;
6466

6567
static const std::uint16_t fuel1Speed = 30;
6668
static const float fuel1Mpg = 35.9f;
@@ -107,13 +109,14 @@ class CodeGenTest : public testing::Test
107109
CGT(car_set_modelYear)(&car, MODEL_YEAR);
108110
CGT(car_set_available)(&car, AVAILABLE);
109111
CGT(car_set_code)(&car, CODE);
110-
CGT(car_put_vehicleCode)(&car, VEHICLE_CODE);
111112

112113
for (std::uint64_t i = 0; i < CGT(car_someNumbers_length)(); i++)
113114
{
114115
CGT(car_set_someNumbers_unsafe)(&car, i, static_cast<std::int32_t>(i));
115116
}
116117

118+
CGT(car_put_vehicleCode)(&car, VEHICLE_CODE);
119+
117120
CGT(optionalExtras) extras;
118121
if (!CGT(car_extras)(&car, &extras))
119122
{
@@ -213,9 +216,10 @@ class CodeGenTest : public testing::Test
213216
CGT(car_performanceFigures_acceleration_set_mph)(&acc, perf2cMph);
214217
CGT(car_performanceFigures_acceleration_set_seconds)(&acc, perf2cSeconds);
215218

216-
CGT(car_put_manufacturer)(&car, MANUFACTURER, static_cast<int>(strlen(MANUFACTURER)));
217-
CGT(car_put_model)(&car, MODEL, static_cast<int>(strlen(MODEL)));
218-
CGT(car_put_activationCode)(&car, ACTIVATION_CODE, static_cast<int>(strlen(ACTIVATION_CODE)));
219+
CGT(car_put_manufacturer)(&car, MANUFACTURER, MANUFACTURER_LENGTH);
220+
CGT(car_put_model)(&car, MODEL, MODEL_LENGTH);
221+
CGT(car_put_activationCode)(&car, ACTIVATION_CODE, ACTIVATION_CODE_LENGTH);
222+
CGT(car_put_color)(&car, COLOR, COLOR_LENGTH);
219223

220224
return CGT(car_encoded_length)(&car);
221225
}
@@ -246,6 +250,8 @@ class CodeGenTest : public testing::Test
246250
CGT(optionalExtras_cruiseControl)(&extras) << ';';
247251
}
248252

253+
output << static_cast<char>(CGT(car_discountedModel(&car))) << ';';
254+
249255
char code_buf[4];
250256
CGT(engine) engine = {};
251257
if (CGT(car_engine)(&car, &engine))
@@ -307,6 +313,16 @@ class CodeGenTest : public testing::Test
307313
{
308314
output << std::string(model.data, model.length) << ';';
309315
}
316+
CGT(car_string_view) activationCode = CGT(car_get_activationCode_as_string_view(&car));
317+
if (nullptr != activationCode.data)
318+
{
319+
output << std::string(activationCode.data, activationCode.length) << ';';
320+
}
321+
CGT(car_string_view) color = CGT(car_get_color_as_string_view(&car));
322+
if (nullptr != color.data)
323+
{
324+
output << std::string(color.data, color.length) << ';';
325+
}
310326

311327
return output.str();
312328
}
@@ -653,6 +669,10 @@ TEST_F(CodeGenTest, shouldBeAbleToEncodeCarCorrectly)
653669
offset += sizeof(std::uint16_t);
654670
EXPECT_EQ(std::string(bp + offset, ACTIVATION_CODE_LENGTH), ACTIVATION_CODE);
655671
offset += ACTIVATION_CODE_LENGTH;
672+
EXPECT_EQ(*(std::uint32_t *)(bp + offset), static_cast<std::uint32_t>(COLOR_LENGTH));
673+
offset += sizeof(std::uint32_t);
674+
EXPECT_EQ(std::string(bp + offset, COLOR_LENGTH), COLOR);
675+
offset += COLOR_LENGTH;
656676

657677
EXPECT_EQ(sz, offset);
658678
}
@@ -670,10 +690,10 @@ TEST_F(CodeGenTest, shouldBeAbleToEncodeHeaderPlusCarCorrectly)
670690
EXPECT_EQ(carEncodedLength, expectedCarEncodedLength);
671691

672692
EXPECT_EQ(*((std::uint16_t *)bp), CGT(car_sbe_block_length)());
673-
const size_t activationCodePosition = hdrSz + carEncodedLength - ACTIVATION_CODE_LENGTH;
674-
const size_t activationCodeLengthPosition = activationCodePosition - sizeof(std::uint16_t);
675-
EXPECT_EQ(*(std::uint16_t *)(bp + activationCodeLengthPosition), ACTIVATION_CODE_LENGTH);
676-
EXPECT_EQ(std::string(bp + activationCodePosition, ACTIVATION_CODE_LENGTH), ACTIVATION_CODE);
693+
const size_t colorPosition = hdrSz + carEncodedLength - COLOR_LENGTH;
694+
const size_t colorLengthPosition = colorPosition - sizeof(std::uint32_t);
695+
EXPECT_EQ(*(std::uint16_t *)(bp + colorLengthPosition), COLOR_LENGTH);
696+
EXPECT_EQ(std::string(bp + colorPosition, COLOR_LENGTH), COLOR);
677697
}
678698

679699
TEST_F(CodeGenTest, shouldBeAbleToEncodeAndDecodeHeaderPlusCarCorrectly)
@@ -845,6 +865,9 @@ TEST_F(CodeGenTest, shouldBeAbleToEncodeAndDecodeHeaderPlusCarCorrectly)
845865
EXPECT_EQ(CGT(car_activationCode_length)(&m_carDecoder), ACTIVATION_CODE_LENGTH);
846866
EXPECT_EQ(std::string(CGT(car_activationCode)(&m_carDecoder), ACTIVATION_CODE_LENGTH), ACTIVATION_CODE);
847867

868+
EXPECT_EQ(CGT(car_color_length)(&m_carDecoder), COLOR_LENGTH);
869+
EXPECT_EQ(std::string(CGT(car_color)(&m_carDecoder), COLOR_LENGTH), COLOR);
870+
848871
EXPECT_EQ(CGT(car_encoded_length)(&m_carDecoder), expectedCarEncodedLength);
849872
}
850873

@@ -968,9 +991,12 @@ TEST_F(CodeGenTest, shouldBeAbleUseOnStackCodecsAndGroupForEach)
968991
EXPECT_EQ(CGT(car_get_model)(&carDecoder, tmp, sizeof(tmp)), MODEL_LENGTH);
969992
EXPECT_EQ(std::string(tmp, MODEL_LENGTH), MODEL);
970993

971-
EXPECT_EQ(CGT(car_get_manufacturer)(&carDecoder, tmp, sizeof(tmp)), ACTIVATION_CODE_LENGTH);
994+
EXPECT_EQ(CGT(car_get_activationCode)(&carDecoder, tmp, sizeof(tmp)), ACTIVATION_CODE_LENGTH);
972995
EXPECT_EQ(std::string(tmp, ACTIVATION_CODE_LENGTH), ACTIVATION_CODE);
973996

997+
EXPECT_EQ(CGT(car_get_color)(&carDecoder, tmp, sizeof(tmp)), COLOR_LENGTH);
998+
EXPECT_EQ(std::string(tmp, COLOR_LENGTH), COLOR);
999+
9741000
EXPECT_EQ(CGT(car_encoded_length)(&carDecoder), expectedCarEncodedLength);
9751001
}
9761002

@@ -997,6 +1023,7 @@ TEST_F(CodeGenTest, shouldBeAbleToUseStdStringMethodsForEncode)
9971023
std::string manufacturer(MANUFACTURER, MANUFACTURER_LENGTH);
9981024
std::string model(MODEL, MODEL_LENGTH);
9991025
std::string activationCode(ACTIVATION_CODE, ACTIVATION_CODE_LENGTH);
1026+
std::string color(COLOR, COLOR_LENGTH);
10001027

10011028
char buffer[BUFFER_LEN] = {};
10021029
auto baseOffset = static_cast<std::uint64_t>(CGT(messageHeader_encoded_length)());
@@ -1046,6 +1073,8 @@ TEST_F(CodeGenTest, shouldBeAbleToUseStdStringMethodsForEncode)
10461073
CGT(car_put_model)(&car, model_c, static_cast<std::uint16_t>(strlen(model_c)));
10471074
const char *acti = activationCode.c_str();
10481075
CGT(car_put_activationCode)(&car, acti, static_cast<std::uint16_t>(strlen(acti)));
1076+
const char *col = color.c_str();
1077+
CGT(car_put_color)(&car, col, static_cast<std::uint32_t>(strlen(col)));
10491078

10501079
EXPECT_EQ(CGT(car_encoded_length)(&car), expectedCarEncodedLength);
10511080

@@ -1179,6 +1208,18 @@ TEST_F(CodeGenTest, shouldBeAbleToUseStdStringMethodsForDecode)
11791208
std::string(ptr, length),
11801209
std::string(ACTIVATION_CODE, ACTIVATION_CODE_LENGTH));
11811210
}
1211+
{
1212+
const uint16_t length = CGT(car_color_length)(&carDecoder);
1213+
const char *const ptr = CGT(car_color)(&carDecoder);
1214+
if (!ptr)
1215+
{
1216+
throw std::runtime_error(sbe_strerror(errno));
1217+
}
1218+
1219+
EXPECT_EQ(
1220+
std::string(ptr, length),
1221+
std::string(COLOR, COLOR_LENGTH));
1222+
}
11821223

11831224
EXPECT_EQ(CGT(car_encoded_length)(&carDecoder), expectedCarEncodedLength);
11841225
}

0 commit comments

Comments
 (0)