View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.util;
18  
19  import org.hipparchus.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  
24  class RomanNumeralTest {
25  
26      @Test
27      public void testZero() {
28          doTestWrongRange(0);
29      }
30  
31      @Test
32      public void testNegative() {
33          doTestWrongRange(-4);
34      }
35  
36      @Test
37      public void testTooLargeValue() {
38          doTestWrongRange(4000);
39      }
40  
41      @Test
42      public void testTooLargeRoman() {
43          try {
44              RomanNumeral.parse("MMMM");
45              Assertions.fail("an exception should have been thrown");
46          } catch (MathIllegalArgumentException e) {
47              Assertions.assertEquals(LocalizedCoreFormats.ROMAN_NUMERAL_RANGE, e.getSpecifier());
48              Assertions.assertEquals(4000, (Integer) e.getParts()[0]);
49          }
50      }
51  
52      @Test
53      public void testComponents() {
54          Assertions.assertEquals( "I", RomanNumeral.toRoman(   1));
55          Assertions.assertEquals("IV", RomanNumeral.toRoman(   4));
56          Assertions.assertEquals( "V", RomanNumeral.toRoman(   5));
57          Assertions.assertEquals("IX", RomanNumeral.toRoman(   9));
58          Assertions.assertEquals( "X", RomanNumeral.toRoman(  10));
59          Assertions.assertEquals("XL", RomanNumeral.toRoman(  40));
60          Assertions.assertEquals( "L", RomanNumeral.toRoman(  50));
61          Assertions.assertEquals("XC", RomanNumeral.toRoman(  90));
62          Assertions.assertEquals( "C", RomanNumeral.toRoman( 100));
63          Assertions.assertEquals("CD", RomanNumeral.toRoman( 400));
64          Assertions.assertEquals( "D", RomanNumeral.toRoman( 500));
65          Assertions.assertEquals("CM", RomanNumeral.toRoman( 900));
66          Assertions.assertEquals( "M", RomanNumeral.toRoman(1000));
67      }
68  
69      @Test
70      public void testStandardForm() {
71          // reference values from the Wikipedia page
72          Assertions.assertEquals(     "XXXIX", RomanNumeral.toRoman(  39));
73          Assertions.assertEquals(    "CCXLVI", RomanNumeral.toRoman( 246));
74          Assertions.assertEquals( "DCCLXXXIX", RomanNumeral.toRoman( 789));
75          Assertions.assertEquals(   "MMCDXXI", RomanNumeral.toRoman(2421));
76          Assertions.assertEquals(       "CLX", RomanNumeral.toRoman( 160));
77          Assertions.assertEquals(     "CCVII", RomanNumeral.toRoman( 207));
78          Assertions.assertEquals(       "MIX", RomanNumeral.toRoman(1009));
79          Assertions.assertEquals(     "MLXVI", RomanNumeral.toRoman(1066));
80          Assertions.assertEquals( "MMMCMXCIX", RomanNumeral.toRoman(3999));
81          Assertions.assertEquals(  "MCMXVIII", RomanNumeral.toRoman(1918));
82          Assertions.assertEquals(   "MCMXLIV", RomanNumeral.toRoman(1944));
83          Assertions.assertEquals(    "MMXXVI", RomanNumeral.toRoman(2026));
84      }
85  
86      @Test
87      public void testOtherAdditiveForm() {
88          // reference values from the Wikipedia page
89          Assertions.assertEquals(   4, RomanNumeral.parse(     "IIII"));
90          Assertions.assertEquals(  40, RomanNumeral.parse(     "XXXX"));
91          Assertions.assertEquals( 400, RomanNumeral.parse(     "CCCC"));
92          Assertions.assertEquals(  24, RomanNumeral.parse(   "XXIIII"));
93          Assertions.assertEquals(  74, RomanNumeral.parse(  "LXXIIII"));
94          Assertions.assertEquals( 490, RomanNumeral.parse("CCCCLXXXX"));
95          Assertions.assertEquals(   9, RomanNumeral.parse(    "VIIII"));
96          Assertions.assertEquals(  90, RomanNumeral.parse(    "LXXXX"));
97          Assertions.assertEquals( 900, RomanNumeral.parse(    "DCCCC"));
98          Assertions.assertEquals(  44, RomanNumeral.parse(   "XLIIII"));
99          Assertions.assertEquals(1910, RomanNumeral.parse(  "MDCCCCX"));
100         Assertions.assertEquals(1903, RomanNumeral.parse(  "MDCDIII"));
101     }
102 
103     @Test
104     public void testEmpty() {
105         doTestInvalid("");
106     }
107 
108     @Test
109     public void testNoRepeatedSubtraction() {
110         doTestInvalid("IVIV");
111     }
112 
113     @Test
114     public void testRoundTrip() {
115         for (int i = 1; i < 4000; ++i) {
116             Assertions.assertEquals(i, RomanNumeral.parse(RomanNumeral.toRoman(i)));
117         }
118     }
119 
120     private void doTestWrongRange(final int n) {
121         try {
122             RomanNumeral.toRoman(n);
123             Assertions.fail("an exception should have been thrown");
124         } catch (final MathIllegalArgumentException e) {
125             Assertions.assertEquals(LocalizedCoreFormats.ROMAN_NUMERAL_RANGE, e.getSpecifier());
126             Assertions.assertEquals(n, (Integer) e.getParts()[0]);
127         }
128     }
129 
130     private void doTestInvalid(final String r) {
131         try {
132             RomanNumeral.parse(r);
133             Assertions.fail("an exception should have been thrown");
134         } catch (final MathIllegalArgumentException e) {
135             Assertions.assertEquals(LocalizedCoreFormats.INVALID_ROMAN_NUMERAL, e.getSpecifier());
136             Assertions.assertEquals(r, e.getParts()[0]);
137         }
138     }
139 
140 }