TEST_F(CompositeElementsTest, shouldEncodeAndDecodeMsgCorrectly)
{
    std::uint64_t sz = encodeHdrAndMsg();

    ASSERT_EQ(sz, MessageHeader::encodedLength() + Msg::sbeBlockLength());

    MessageHeader hdr;
    Msg msg;

    hdr.wrap(m_buffer, 0, Msg::sbeSchemaVersion(), sizeof(m_buffer));

    EXPECT_EQ(hdr.blockLength(), Msg::sbeBlockLength());
    EXPECT_EQ(hdr.templateId(), Msg::sbeTemplateId());
    EXPECT_EQ(hdr.schemaId(), Msg::sbeSchemaId());
    EXPECT_EQ(hdr.version(), Msg::sbeSchemaVersion());

    msg.wrapForDecode(m_buffer, MessageHeader::encodedLength(), hdr.blockLength(), hdr.version(), sizeof(m_buffer));

    EXPECT_EQ(msg.structure().enumOne(), EnumOne::Value::Value10);
    EXPECT_EQ(msg.structure().zeroth(), 42u);
    EXPECT_EQ(msg.structure().setOne().bit0(), false);
    EXPECT_EQ(msg.structure().setOne().bit16(), true);
    EXPECT_EQ(msg.structure().setOne().bit26(), false);
    EXPECT_EQ(msg.structure().inner().first(), 101l);
    EXPECT_EQ(msg.structure().inner().second(), 202l);

    EXPECT_EQ(msg.encodedLength(), sz - MessageHeader::encodedLength());
}
int main(int argc, const char* argv[])
{
    char buffer[2048];
    MessageHeader hdr;
    Car car;

    std::size_t encodeHdrLength = encodeHdr(hdr, car, buffer, 0, sizeof(buffer));
    std::size_t encodeMsgLength = encodeCar(car, buffer, hdr.encodedLength(), sizeof(buffer));

    cout << "Encoded Lengths are " << encodeHdrLength << " + " << encodeMsgLength << endl;

    std::size_t decodeHdrLength = decodeHdr(hdr, buffer, 0, sizeof(buffer));
    std::size_t decodeMsgLength = decodeCar(car, buffer, hdr.encodedLength(), hdr.blockLength(), hdr.version(), sizeof(buffer));

    cout << "Decoded Lengths are " << decodeHdrLength << " + " << decodeMsgLength << endl;

    if (encodeHdrLength != decodeHdrLength)
    {
        cerr << "Encode/Decode header lengths do not match\n";
        return EXIT_FAILURE;
    }

    if (encodeMsgLength != decodeMsgLength)
    {
        cerr << "Encode/Decode message lengths do not match\n";
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
    static std::uint64_t encodeHdr(MessageHeader& hdr)
    {
        hdr.blockLength(Car::sbeBlockLength())
        .templateId(Car::sbeTemplateId())
        .schemaId(Car::sbeSchemaId())
        .version(Car::sbeSchemaVersion());

        return hdr.encodedLength();
    }
void decodeHdr(MessageHeader &hdr, char *buffer, int offset)
{
    hdr.wrap(buffer, offset, messageHeaderVersion);

    // decode the header
    cout << "messageHeader.blockLength=" << hdr.blockLength() << endl;
    cout << "messageHeader.templateId=" << hdr.templateId() << endl;
    cout << "messageHeader.version=" << (sbe_uint32_t)hdr.version() << endl;
    cout << "messageHeader.reserved=" << (sbe_uint32_t)hdr.reserved() << endl;
}
std::size_t decodeHdr(MessageHeader &hdr, char *buffer, std::uint64_t offset, std::uint64_t bufferLength)
{
    hdr.wrap(buffer, offset, messageHeaderVersion, bufferLength);

    // decode the header
    cout << "messageHeader.blockLength=" << hdr.blockLength() << endl;
    cout << "messageHeader.templateId=" << hdr.templateId() << endl;
    cout << "messageHeader.schemaId=" << hdr.schemaId() << endl;
    cout << "messageHeader.schemaVersion=" << hdr.version() << endl;
    cout << "messageHeader.encodedLength=" << hdr.encodedLength() << endl;

    return hdr.encodedLength();
}
int main(int argc, const char* argv[])
{
    char buffer[2048];
    MessageHeader hdr;
    Car car;

    encodeHdr(hdr, car, buffer, 0);
    encodeCar(car, buffer, hdr.size());

    cout << "Encoding size is " << hdr.size() << " + " << car.size() << endl;

    decodeHdr(hdr, buffer, 0);
    decodeCar(car, buffer, hdr.size(), hdr.blockLength(), hdr.version());
    return 0;
}
TEST_F(CodeGenTest, shouldbeAbleUseOnStackCodecsAndGroupForEach)
{
    char buffer[BUFFER_LEN];
    MessageHeader hdr(buffer, sizeof(buffer), 0);
    Car car(buffer + hdr.encodedLength(), sizeof(buffer) - hdr.encodedLength());

    std::uint64_t hdrSz = encodeHdr(hdr);
    std::uint64_t carSz = encodeCar(car);

    EXPECT_EQ(hdrSz, expectedHeaderSize);
    EXPECT_EQ(carSz, expectedCarSize);

    const MessageHeader hdrDecoder(buffer, hdrSz, 0);

    EXPECT_EQ(hdrDecoder.blockLength(), Car::sbeBlockLength());
    EXPECT_EQ(hdrDecoder.templateId(), Car::sbeTemplateId());
    EXPECT_EQ(hdrDecoder.schemaId(), Car::sbeSchemaId());
    EXPECT_EQ(hdrDecoder.version(), Car::sbeSchemaVersion());
    EXPECT_EQ(hdrDecoder.encodedLength(), expectedHeaderSize);

    Car carDecoder(buffer + hdrDecoder.encodedLength(), carSz, hdrDecoder.blockLength(), hdrDecoder.version());
    CallbacksForEach cbs;

    Car::FuelFigures &fuelFigures = carDecoder.fuelFigures();
    EXPECT_EQ(fuelFigures.count(), FUEL_FIGURES_COUNT);

#if __cplusplus >= 201103L
    fuelFigures.forEach([&](Car::FuelFigures &figures)
    {
        cbs.countOfFuelFigures++;

        char tmp[256];
        figures.getUsageDescription(tmp, sizeof(tmp));
    });
#else
    fuelFigures.forEach(cbs);
#endif

    Car::PerformanceFigures &performanceFigures = carDecoder.performanceFigures();
    EXPECT_EQ(performanceFigures.count(), PERFORMANCE_FIGURES_COUNT);

#if __cplusplus >= 201103L
    performanceFigures.forEach([&](Car::PerformanceFigures&figures)
    {
        Car::PerformanceFigures::Acceleration acceleration = figures.acceleration();

        cbs.countOfPerformanceFigures++;
        acceleration.forEach([&](Car::PerformanceFigures::Acceleration&)
        {
            cbs.countOfAccelerations++;
        });
    });
#else
    performanceFigures.forEach(cbs);
#endif

    EXPECT_EQ(cbs.countOfFuelFigures, FUEL_FIGURES_COUNT);
    EXPECT_EQ(cbs.countOfPerformanceFigures, PERFORMANCE_FIGURES_COUNT);
    EXPECT_EQ(cbs.countOfAccelerations, ACCELERATION_COUNT * PERFORMANCE_FIGURES_COUNT);

    char tmp[256];

    EXPECT_EQ(carDecoder.getMake(tmp, sizeof(tmp)), MAKE_LENGTH);
    EXPECT_EQ(std::string(tmp, MAKE_LENGTH), MAKE);

    EXPECT_EQ(carDecoder.getModel(tmp, sizeof(tmp)), MODEL_LENGTH);
    EXPECT_EQ(std::string(tmp, MODEL_LENGTH), MODEL);

    EXPECT_EQ(carDecoder.getMake(tmp, sizeof(tmp)), ACTIVATION_CODE_LENGTH);
    EXPECT_EQ(std::string(tmp, ACTIVATION_CODE_LENGTH), ACTIVATION_CODE);

    EXPECT_EQ(carDecoder.encodedLength(), expectedCarSize);
}