Skip to content

Commit

Permalink
Merge pull request #18 from neftales/numeral
Browse files Browse the repository at this point in the history
Intro to property based tests
  • Loading branch information
neftales authored Jun 22, 2024
2 parents b3ee2cc + 6486384 commit a1b0c42
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
50 changes: 50 additions & 0 deletions roman_numerals/numeral.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package romannumerals

import "strings"

type RomanNumeral struct {
Value uint16
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"},
}

func ConvertToRoman(arabic uint16) string {
var result strings.Builder

for _, numeral := range allRomanNumerals {
for arabic >= numeral.Value {
result.WriteString(numeral.Symbol)
arabic -= numeral.Value
}
}

return result.String()
}

func ConvertToArabic(roman string) uint16 {
var arabic uint16 = 0

for _, numeral := range allRomanNumerals {
for strings.HasPrefix(roman, numeral.Symbol) {
arabic += numeral.Value
roman = strings.TrimPrefix(roman, numeral.Symbol)
}
}

return arabic
}
84 changes: 84 additions & 0 deletions roman_numerals/numeral_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package romannumerals

import (
"fmt"
"testing"
"testing/quick"
)

var cases = []struct {
Arabic uint16
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"},
}

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)
}
})
}
}

func TestConvertingToArabic(t *testing.T) {
for _, test := range cases[:1] {
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)
}
})
}
}

func TestPropertiesOfConverstion(t *testing.T) {
assertion := func(arabic uint16) bool {
if arabic > 3999 {
return true
}

roman := ConvertToRoman(arabic)
fromRoman := ConvertToArabic(roman)

return fromRoman == arabic
}

if err := quick.Check(assertion, &quick.Config{
MaxCount: 1000,
}); err != nil {
t.Error("failed checks", err)
}
}

0 comments on commit a1b0c42

Please sign in to comment.