-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from neftales/numeral
Intro to property based tests
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |