go-by-test/roman/roman_test.go

86 lines
1.9 KiB
Go
Raw Normal View History

package roman
2024-09-22 19:24:24 +00:00
import (
"fmt"
"testing"
"testing/quick"
2024-09-22 19:24:24 +00:00
)
2024-09-22 19:28:24 +00:00
var cases = []struct {
2024-09-22 19:51:40 +00:00
Arabic uint16
2024-09-22 19:28:24 +00:00
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 TestRomanNemerals(t *testing.T) {
for _, test := range cases {
2024-09-22 19:24:24 +00:00
t.Run(fmt.Sprintf("%d gets converted to %q", test.Arabic, test.Roman), func(t *testing.T) {
2024-09-22 20:00:50 +00:00
got, _ := ConvertToRoman(test.Arabic)
2024-09-22 19:24:24 +00:00
want := test.Roman
if got != want {
t.Errorf("got %q, want %q", got, want)
}
})
}
}
2024-09-22 19:28:24 +00:00
func TestConvertingToArabic(t *testing.T) {
for _, test := range cases {
2024-09-22 19:28:24 +00:00
t.Run(fmt.Sprintf("%q gets converted to %d", test.Roman, test.Arabic), func(t *testing.T) {
got := ConvertToArabic(test.Roman)
want := test.Arabic
if got != want {
t.Errorf("got %d, want %d", got, want)
}
})
}
}
func TestPropertiesOfConversion(t *testing.T) {
2024-09-22 19:51:40 +00:00
assertion := func(arabic uint16) bool {
2024-09-22 20:00:50 +00:00
roman, err := ConvertToRoman(arabic)
2024-09-22 19:51:40 +00:00
if arabic > 3999 {
2024-09-22 20:00:50 +00:00
return err == ErrNumberOutOfRange
2024-09-22 19:51:40 +00:00
}
fromRoman := ConvertToArabic(roman)
return fromRoman == arabic
}
2024-09-22 19:51:40 +00:00
if err := quick.Check(assertion, &quick.Config{
MaxCount: 1000,
}); err != nil {
t.Error("failed checks", err)
}
}