Skip to content

Repository files navigation

Roman numerals kata in C++

CI C++17 License: MIT Replit

Implement conversions between Arabic numerals and Roman numerals in C++17 with GoogleTest. Setup is complete when the existing starter test passes.

Overview

This kata complements Clean Code: Advanced TDD, Ep. 19.

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

Instructions

Roman numerals are a numeral system used in ancient Rome. Numbers in this system use seven letters from the Latin alphabet:

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Instead of writing the same letter four times, a subtraction rule is used: the letter is written once, followed by the next larger Roman numeral. For example, 4 is not written as IIII, but instead as IV, because IV is V (5) minus I (1).

In general, the values for 5, 50, and 500 are not subtracted.

Exercise 1

Implement std::string to_roman(int number) to convert Arabic numerals into Roman numerals, such as:

  • 4 → IV
  • 7 → VII
  • 9 → IX

The smallest supported number is 1 (I), and the largest is 3999 (MMMCMXCIX).

Exercise 2

Implement int from_roman(const std::string& number) to perform the reverse conversion from Roman numerals to Arabic numerals.

Guiding principles

  • If you don't know an existing algorithm, follow the principles of strict Test-Driven Development (TDD) to derive one.
  • Reflect on whether the sequence in which you write tests influences the final design of your algorithm.
  • Consider whether it's more beneficial to devise an algorithm before embarking on TDD, especially if you don't already know one.
  • If you do know an algorithm, evaluate if it can be implemented using strict TDD principles.

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 is retained. The local setup below is the validated development path.

  1. Clone the repository:

    git clone https://github.com/Coding-Cuddles/roman-numerals-cpp-kata.git
  2. Enter the repository directory:

    cd roman-numerals-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=Debug
    cmake --build build --config Debug
    ctest --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

Add one test at a time to test_roman_numerals.cpp, then implement enough code in roman_numerals.h to make the test pass. Keep the existing exercises and constraints 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 Debug
ctest --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.

Command Result
make all Build and run the test suite
make help List public Make targets
make build Configure and build without running tests
make run Build and run the example executable
make test Build and run the test suite
make format Format tracked C++ and header files
make format-check Check formatting without changing files
make clean Remove generated build artifacts

About

Roman numerals kata in C++

Topics

Resources

Stars

0 stars

Watchers

2 watching

Forks

Used by

Contributors

Languages