Following the Learn Go with Tests guide, exploring property-based testing.
Table tests for Roman numeral conversion, and property-based testing at the end.
Table-Driven Tests #
Test Cases #
var cases = []struct {
Arabic uint
Roman string
}{
{Arabic: 1, Roman: "I"},
{Arabic: 2, Roman: "II"},
{Arabic: 3, Roman: "III"},
{Arabic: 4, Roman: "IV"},
{Arabic: 5, Roman: "V"},
{Arabic: 6, Roman: "VI"},
{Arabic: 7, Roman: "VII"},
{Arabic: 8, Roman: "VIII"},
{Arabic: 9, Roman: "IX"},
{Arabic: 10, Roman: "X"},
{Arabic: 14, Roman: "XIV"},
{Arabic: 18, Roman: "XVIII"},
{Arabic: 20, Roman: "XX"},
{Arabic: 39, Roman: "XXXIX"},
{Arabic: 40, Roman: "XL"},
{Arabic: 47, Roman: "XLVII"},
{Arabic: 49, Roman: "XLIX"},
{Arabic: 50, Roman: "L"},
{Arabic: 100, Roman: "C"},
{Arabic: 90, Roman: "XC"},
{Arabic: 400, Roman: "CD"},
{Arabic: 500, Roman: "D"},
{Arabic: 900, Roman: "CM"},
{Arabic: 1000, Roman: "M"},
{Arabic: 1984, Roman: "MCMLXXXIV"},
{Arabic: 3999, Roman: "MMMCMXCIX"},
{Arabic: 2014, Roman: "MMXIV"},
{Arabic: 1006, Roman: "MVI"},
{Arabic: 798, Roman: "DCCXCVIII"},
}Testing Arabic to Roman #
func TestRomanNumerals(t *testing.T) {
for _, test := range cases {
t.Run(fmt.Sprintf(
"%d gets converted to %q",
test.Arabic,
test.Roman,
), func(t *testing.T) {
got := ConvertToRoman(test.Arabic)
if got != test.Roman {
t.Errorf("got %q, want %q", got, test.Roman)
}
})
}
}Testing Roman to Arabic #
func TestConvertToArabic(t *testing.T) {
for _, test := range cases {
t.Run(fmt.Sprintf(
"%q gets converted to %d",
test.Roman,
test.Arabic,
), func(t *testing.T) {
got := ConvertToArabic(test.Roman)
if got != test.Arabic {
t.Errorf("got %d, want %d", got, test.Arabic)
}
})
}
}Roman Numeral Converter #
Data Structure #
type RomanNumeral struct {
Value uint
Symbol string
}
var allRomanNumerals = []RomanNumeral{
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"},
}Conversion Functions #
Arabic to Roman Conversion #
This function uses Go’s strings.Builder for efficient string concatenation, which is much more performant than repeatedly concatenating strings with the + operator.
func ConvertToRoman(arabic uint) string {
var result strings.Builder
for _, numbers := range allRomanNumerals {
for arabic >= numbers.Value {
result.WriteString(numbers.Symbol)
arabic -= numbers.Value
}
}
return result.String()
}Roman to Arabic Conversion #
func ConvertToArabic(roman string) uint {
var arabic = 0
for _, numerals := range allRomanNumerals {
for strings.HasPrefix(roman, numerals.Symbol) {
arabic += numerals.Value
roman = strings.TrimPrefix(
roman,
numerals.Symbol,
)
}
}
return arabic
}Property-Based Testing #
Property-based testing is a testing technique that verifies that certain properties hold for a wide range of inputs, rather than testing specific cases. Go’s testing/quick package provides tools for property-based testing by generating random inputs.
Round-Trip Property Test #
func TestPropertiesOfConversion(t *testing.T) {
// Define a property: converting to Roman and back
// should yield the original number
assertion := func(arabic uint) bool {
// Skip values outside Roman numeral range (1-3999)
// Roman numerals traditionally don't represent
// numbers > 3999
if arabic > 3999 {
return true // Skip this test case
}
t.Log("testing", arabic)
roman := ConvertToRoman(arabic)
fromRoman := ConvertToArabic(roman)
return fromRoman == arabic
}
// quick.Check generates random uint values and
// tests our assertion
if err := quick.Check(assertion, &quick.Config{
MaxCount: 1000, // Test with 1000 random inputs
}); err != nil {
t.Error("failed checks", err)
}
}Why Property-Based Testing? #
Property-based testing helps us:
- Discover edge cases we might not think to test manually
- Test with much broader input coverage than manual test cases