Implement conversions between Arabic numerals and Roman numerals in C++17 with GoogleTest. Setup is complete when the existing starter test passes.
This kata complements Clean Code: Advanced TDD, Ep. 19.
This repository contains two exercises designed to improve your skills in test-driven development.
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.
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).
Implement int from_roman(const std::string& number) to perform the reverse
conversion from Roman numerals to Arabic numerals.
- 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.
Required:
- Git
- A compiler with C++17 support. Choose one:
- GCC 10+ on Linux
- LLVM Clang 14+ on Linux
- Apple Clang 17+ on macOS
- MSVC 2022 on Windows
- CMake 3.24 or later
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.
The tracked Replit configuration is retained. The local setup below is the validated development path.
-
Clone the repository:
git clone https://github.com/Coding-Cuddles/roman-numerals-cpp-kata.git -
Enter the repository directory:
cd roman-numerals-cpp-kata -
Build and run the tests. Use Make when it is installed:
make testOtherwise, 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.
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 testOtherwise, use CMake and CTest directly:
cmake --build build --config Debug
ctest --test-dir build --build-config Debug --output-on-failureContinue when CTest reports 100% tests passed.
Use Make when it is installed:
make runOtherwise, use the CMake run target:
cmake --build build --config Debug --target runThe executable prints Hello World!.
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 |