Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

romannum_test.gno

1.09 Kb · 62 lines
 1package romannum
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestToRoman(t *testing.T) {
10	cases := []struct {
11		in   int
12		want string
13	}{
14		{1, "I"},
15		{4, "IV"},
16		{9, "IX"},
17		{40, "XL"},
18		{90, "XC"},
19		{400, "CD"},
20		{900, "CM"},
21		{1994, "MCMXCIV"},
22		{2024, "MMXXIV"},
23		{3999, "MMMCMXCIX"},
24	}
25	for _, c := range cases {
26		uassert.Equal(t, c.want, ToRoman(c.in))
27	}
28}
29
30func TestParseRoman(t *testing.T) {
31	// canonical numerals round-trip back to their value
32	good := []struct {
33		in   string
34		want int
35	}{
36		{"I", 1},
37		{"IV", 4},
38		{"mmxxiv", 2024}, // case-insensitive
39		{" XLII ", 42},   // trimmed
40		{"MMMCMXCIX", 3999},
41	}
42	for _, c := range good {
43		got, ok := parseRoman(c.in)
44		uassert.True(t, ok)
45		uassert.Equal(t, c.want, got)
46	}
47
48	// malformed / non-canonical forms are rejected
49	bad := []string{"", "IIII", "VV", "IC", "MMMM", "XIↃ", "ABC"}
50	for _, s := range bad {
51		_, ok := parseRoman(s)
52		uassert.False(t, ok)
53	}
54}
55
56func TestRoundTrip(t *testing.T) {
57	for n := 1; n <= 3999; n++ {
58		got, ok := parseRoman(ToRoman(n))
59		uassert.True(t, ok)
60		uassert.Equal(t, n, got)
61	}
62}