Skip to content

Repository files navigation

Hyper-optimized telemetry kata in C++

CIC++17License: MITReplit

Implement compact integer encoding for a nine-byte telemetry buffer in C++17 with GoogleTest. Setup is complete when the existing test suite passes.

Overview

This kata complements Clean Code: Advanced TDD, Ep. 20 and Clean Code: Advanced TDD, Ep. 21.

This repository contains two exercises designed to improve your skills in test-driven development.

Instructions

We will work on a telemetry system for a remote control car project. Bandwidth in the telemetry system is at a premium and you have been asked to implement a message protocol for communicating telemetry data.

Data is transmitted in a buffer (byte array). When integers are sent, the number of payload bytes is reduced by employing the protocol described below.

Each value should be represented in the smallest possible C integral type (types of char and unsigned char are not included because the space savings would be trivial):

FromToType
4,294,967,2969,223,372,036,854,775,807long
2,147,483,6484,294,967,295unsigned int
65,5362,147,483,647int
065,535unsigned short
-32,768-1short
-2,147,483,648-32,769int
-9,223,372,036,854,775,808-2,147,483,649long

The value should be converted to the appropriate number of bytes for its assigned type. The complete internal 9-byte buffer comprises three parts:

  • prefix byte: a byte indicating the number of payload bytes in the buffer;
  • payload bytes: the bytes holding the integer;
  • trailing bytes: the zero-fill bytes to complete the buffer.

To distinguish between signed and unsigned types, the protocol introduces a little trick: for signed types, the prefix byte is 256 minus the number of payload bytes in the buffer.

Exercise 1

Implement the static method TelemetryBuffer.to_buffer() to encode an integer value into a buffer.

// Type: unsigned short, bytes: 2, signed: no, prefix byte: 2TelemetryBuffer::to_buffer(5);
// => {0x2, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}// Type: int, bytes: 4, signed: yes, prefix byte: 256 - 4TelemetryBuffer::to_buffer(2'147'483'647);
// => {0xfc, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x0, 0x0}

Tip

The BitConverter class provides a convenient way of converting integer types to and from arrays of bytes.

Exercise 2

Implement the static method TelemetryBuffer.from_buffer() to decode a received buffer and return its integer value.

TelemetryBuffer::from_buffer({0xfc, 0xff, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x0, 0x0});
// => 2'147'483'647

If the prefix byte has an unexpected value, return 0.

Integral numbers in C

Note

For type sizes, we assume a typical 64-bit system.

The C language provides several integer types, each with its own range of values. The system's storage width for each type determines its range:

TypeWidthMinimumMaximum
char8 bits-128+127
short16 bits-32,768+32,767
int32 bits-2,147,483,648+2,147,483,647
long64 bits-9,223,372,036,854,775,808+9,223,372,036,854,775,807
unsigned char8 bits0+255
unsigned short16 bits0+65,535
unsigned int32 bits0+4,294,967,295
unsigned long64 bits0+18,446,744,073,709,551,615

Prerequisites

Required:

Optional:

  • GNU Make, for shorter commands. Every required task also has direct CMake and CTest commands. Make may be unavailable on Windows.

You do not need to install GoogleTest separately. CMake finds an installed copy or downloads the pinned release when needed.

Set up the kata

The tracked Replit configuration and badge are retained. The local setup below is the validated development path.

  1. Clone the repository:

    git clone https://github.com/Coding-Cuddles/hyper-optimized-telemetry-cpp-kata.git
  2. Enter the repository directory:

    cd hyper-optimized-telemetry-cpp-kata
  3. Build and run the tests. Use Make when it is installed:

    make test

    Otherwise, use CMake and CTest directly:

    cmake -S . -B build -DCMAKE_BUILD_TYPE=Debugcmake --build build --config Debugctest --test-dir build --build-config Debug --output-on-failure

The first run may download and build GoogleTest. CTest should report 100% tests passed. If a command reports a missing compiler or CMake, install that prerequisite and run the setup commands again. Setup is complete when CTest reports 100% tests passed.

Work on the kata

Work through the two exercises in order. Add one test at a time, then implement enough code to make the test pass. Keep the protocol, constraints, and expected results above as the target behavior.

After each change, use Make when it is installed:

make test

Otherwise, use CMake and CTest directly:

cmake --build build --config Debugctest --test-dir build --build-config Debug --output-on-failure

Continue when CTest reports 100% tests passed.

Run the example

Use Make when it is installed:

make run

Otherwise, use the CMake run target:

cmake --build build --config Debug --target run

The executable prints Hello World!.

Make command reference

Make is optional. Run make or make help to list these commands in the terminal.

CommandResult
make allBuild and run the test suite
make helpList public Make targets
make buildConfigure and build without running tests
make runBuild and run the example executable
make testBuild and run the test suite
make formatFormat tracked C++ and header files
make format-checkCheck formatting without changing files
make cleanRemove generated build artifacts

Credits and references

About

Hyper-optimized telemetry kata in C++

Topics

Resources

Stars

0 stars

Watchers

2 watching

Forks

Used by

Contributors

Languages