def test_incorrect_numerals(): """is_roman_numeral should return False for strings that are not roman numerals.""" for numeral in incorrect_numerals: assert Roman.is_roman_numeral(numeral) == False with pytest.raises(ValueError): Roman(numeral)
def test_bad_type(): """is_roman_numeral should raise TypeError for non-string arguments""" for numeral in (None, 0, 1, 1.0): with pytest.raises(TypeError): Roman.is_roman_numeral(numeral) for obj in (None, 1.0): with pytest.raises(TypeError): Roman(numeral)
class RomanTests(unittest.TestCase): def setUp(self): self.roman = Roman() def test_input_1_should_return_I(self): result = self.roman.convert(1) self.assertEqual(result, 'I') def test_input_2_should_return_II(self): result = self.roman.convert(2) self.assertEqual(result, 'II') def test_input_3_should_return_III(self): result = self.roman.convert(3) self.assertEqual(result, 'III') def test_input_4_should_return_IV(self): result = self.roman.convert(4) self.assertEqual(result, 'IV') def test_input_5_should_return_V(self): result = self.roman.convert(5) self.assertEqual(result, 'V') def test_input_6_should_return_VI(self): result = self.roman.convert(6) self.assertEqual(result, 'VI') def test_input_7_should_return_VII(self): result = self.roman.convert(7) self.assertEqual(result, 'VII') def test_input_8_should_return_VIII(self): result = self.roman.convert(8) self.assertEqual(result, 'VIII') def test_input_9_should_return_IX(self): result = self.roman.convert(9) self.assertEqual(result, 'IX') def test_input_10_should_return_X(self): result = self.roman.convert(10) self.assertEqual(result, 'X')
def test_exceptions(self): ancient = 'The ancient Romans did not recognize 0 as a number neither did they include negative numbers' try: _ = Roman.convert_to_roman(0) self.fail('Should get an exception for non-positive values') except ValueError as ex: self.assertEqual(ancient, str(ex)) try: _ = Roman.convert_to_roman(-22) self.fail('Should get an exception for non-positive values') except ValueError as ex: self.assertEqual(ancient, str(ex)) try: _ = Roman.convert_to_decimal('T') # T unknown self.fail('Should get an exception for unrecognized characters') except ValueError as ex: self.assertEqual('Not found', str(ex)) try: _ = Roman.convert_to_decimal( 'IIII') # III is 3 and cannot be followed by anything self.fail('Should get an exception for unrecognized characters') except ValueError as ex: self.assertEqual('Wrong level', str(ex)) try: # not following the convention - XX is 20 and cannot be followed by 50 _ = Roman.convert_to_decimal('XXL') self.fail('Should get an exception for unrecognized characters') except ValueError as ex: self.assertEqual('Wrong level', str(ex)) try: _ = Roman.convert_to_decimal( 'MMCCXXC') # 2220 cannot be followed by 100 self.fail('Should get an exception for unrecognized characters') except ValueError as ex: self.assertEqual('Wrong level', str(ex)) try: _ = Roman.convert_to_decimal( 'I^' ) # with the current convention I can never be followed by a ^ self.fail('Should get an exception for unrecognized characters') except ValueError as ex: self.assertEqual('Not found', str(ex)) try: _ = Roman.convert_to_decimal('V^MMMDCCCLXXXVIIIR') # R unknown self.fail('Should get an exception for unrecognized characters') except ValueError as ex: self.assertEqual('Not found', str(ex))
def test_adding_x_to_x(self): """ Test adding 10 + 10 """ numeral_a = Roman('x') numeral_b = Roman('x') self.assertEqual('xx', numeral_a.add(numeral_b)) self.assertEqual('xx', numeral_b.add(numeral_a))
class TestRoman(unittest.TestCase): def setUp(self): self.roman = Roman() def tearDown(self): pass def test_get_value_by_symbol(self): self.assertEqual(self.roman.get_value_by_symbol("I"), 1) self.assertEqual(self.roman.get_value_by_symbol("V"), 5) self.assertEqual(self.roman.get_value_by_symbol("X"), 10) self.assertEqual(self.roman.get_value_by_symbol("L"), 50) self.assertEqual(self.roman.get_value_by_symbol("C"), 100) self.assertEqual(self.roman.get_value_by_symbol("D"), 500) self.assertEqual(self.roman.get_value_by_symbol("M"), 1000) def test_roman_to_arabic(self): self.assertEqual(self.roman.roman_to_arabic('II'), 2) self.assertEqual(self.roman.roman_to_arabic('MCMIII'), 1903) with self.assertRaises(InvalidSyntaxException): self.roman.roman_to_arabic("IIII")
def test_4000_handled_gracefully(self): number = Roman('4000') self.assertFalse(number.is_within_bounds()) self.assertFalse(number.is_valid) self.assertEquals(number.invalid_reason, '4000 is out of bounds.')
def test_number_of_digits_in_9999(self): number = Roman(9999) self.assertEquals(number.number_of_digits(), 4)
def test_equal(self): for i in range(1, 2000): self.assertEqual(str(Roman(i)), text[i]) # Здесь, по сути,
def test_false(self): self.assertFalse(Roman(5) == Roman(6))
def test_incorrect_integers(): """Zero and negative integers are not supported.""" for number in incorrect_integers: with pytest.raises(ValueError): Roman(number)
class TestRomanToArabic(unittest.TestCase): def setUp(self): self.r = Roman() ''' Aide-memoire ------------ I 1 (one) (unus) V 5 (five) (quinque) X 10 (ten) (decem) L 50 (fifty) (quinquaginta) C 100 (one hundred) (centum) D 500 (five hundred) (quingenti) M 1000 (one thousand) (mille) ''' def test_IVXLCDM(self): ''' Test all uppercase digits in one test for brevity. ''' for numeral, decimal in [('I', 1), ('V', 5), ('X', 10), ('L', 50), ('C', 100), ('D', 500), ('M', 1000)]: number = Roman(numeral) self.assertEquals(number.decimal, decimal) def test_lowercase_digits(self): ''' Test all uppercase digits in one test for brevity. ''' for numeral, decimal in [('i', 1), ('v', 5), ('x', 10), ('l', 50), ('c', 100), ('d', 500), ('m', 1000)]: number = Roman(numeral) self.assertEquals(number.decimal, decimal) number = Roman('x') self.assertEquals(number.decimal, 10) def test_two_ones_make_two(self): number = Roman('II') self.assertEquals(number.decimal, 2) def test_iv_equals_four(self): number = Roman('iv') self.assertEquals(number.decimal, 4) def test_i_before_v_worth_minus_one(self): value = self.r.get_roman_value('i', 'v') self.assertEquals(value, -1) def test_i_on_its_own_worth_one(self): value = self.r.get_roman_value('i', None) self.assertEquals(value, 1) def test_v_before_i_worth_five(self): value = self.r.get_roman_value('v', 'i') self.assertEquals(value, 5) def test_iiii_also_equals_four(self): number = Roman('iiii') self.assertEquals(number.decimal, 4) def test_four_cs_1925(self): number = Roman('MDCCCCXXV') self.assertEquals(number.decimal, 1925) def test_subtractive_1925(self): number = Roman('MCMXXV') self.assertEquals(number.decimal, 1925) def test_ic_violates_subtractive_principle(self): ''' "The subtractive principle of the IX, XC, and CM in these examples is that a numeral for 10^n may not precede a numeral larger than 10^n+1, where n is an integer." --Wikipedia, Roman numerals <http://en.wikipedia.org/wiki/Roman_numerals> ''' self.assertTrue(self.r.violates_subtractive_principle('I', 'C')) def test_ix_does_not_violate_subtractive_principle(self): self.assertFalse(self.r.violates_subtractive_principle('I', 'X')) def test_m_does_not_violate_subtractive_principle(self): self.assertFalse(self.r.violates_subtractive_principle('M', None)) def test_violation_of_subtractive_principle_raises_exception(self): self.assertRaises(Exception, self.r.get_roman_value, 'i', 'm') def test_mim_not_valid(self): number = Roman('MIM') self.assertFalse(number.is_valid) self.assertEqual(number.invalid_reason, 'I before M violates the subtractive principle.') def test_MCMXCIX_valid(self): number = Roman('MCMXCIX') self.assertTrue(number.is_valid) self.assertEquals(number.invalid_reason, None) def test_vx_violates_subtractive_principle(self): ''' "The numerals 5*10^n (V, L, D) may not precede a larger numeral at all" --Wikipedia, Roman numerals <http://en.wikipedia.org/wiki/Roman_numerals> ''' self.assertTrue(self.r.violates_subtractive_principle('V', 'X')) def test_vv_invalid(self): number = Roman('VV') self.assertFalse(number.is_valid) self.assertEquals(number.invalid_reason, 'Cannot have two consecutive Vs.') def test_no_two_fives(self): self.assertTrue(self.r.two_consecutive_fives('L', 'L'))
def test_from_integer(): """Roman(integer) makes a correct numeral.""" for numeral, value in known_numerals: assert str(Roman(value)) == numeral
def test_correct_numerals(): """is_roman_numeral should return True for correct roman numerals.""" for numeral in correct_numerals: assert numeral == str(Roman(numeral))
def test_numeric(): assert 1 == Roman("I").numeric assert 2 == Roman("II").numeric assert 3 == Roman("III").numeric assert 4 == Roman("IV").numeric assert 5 == Roman("V").numeric assert 6 == Roman("VI").numeric assert 7 == Roman("VII").numeric assert 8 == Roman("VIII").numeric assert 9 == Roman("IX").numeric assert 10 == Roman("X").numeric assert 11 == Roman("XI").numeric assert 12 == Roman("XII").numeric assert 13 == Roman("XIII").numeric assert 14 == Roman("XIV").numeric assert 15 == Roman("XV").numeric assert 16 == Roman("XVI").numeric assert 17 == Roman("XVII").numeric assert 18 == Roman("XVIII").numeric assert 19 == Roman("XIX").numeric assert 20 == Roman("XX").numeric assert 30 == Roman("XXX").numeric assert 40 == Roman("XL").numeric assert 50 == Roman("L").numeric assert 60 == Roman("LX").numeric assert 70 == Roman("LXX").numeric assert 80 == Roman("LXXX").numeric assert 90 == Roman("XC").numeric assert 100 == Roman("C").numeric assert 101 == Roman("CI").numeric assert 105 == Roman("CV").numeric assert 110 == Roman("CX").numeric assert 150 == Roman("CL").numeric assert 200 == Roman("CC").numeric assert 300 == Roman("CCC").numeric assert 400 == Roman("CD").numeric assert 500 == Roman("D").numeric assert 600 == Roman("DC").numeric assert 700 == Roman("DCC").numeric assert 800 == Roman("DCCC").numeric assert 900 == Roman("CM").numeric assert 1000 == Roman("M").numeric assert 2000 == Roman("MM").numeric assert 2019 == Roman("MMXIX").numeric
def test_roman(): assert Roman(1).roman == "I" assert Roman(2).roman == "II" assert Roman(3).roman == "III" assert Roman(4).roman == "IV" assert Roman(5).roman == "V" assert Roman(6).roman == "VI" assert Roman(7).roman == "VII" assert Roman(8).roman == "VIII" assert Roman(9).roman == "IX" assert Roman(10).roman == "X" assert Roman(11).roman == "XI" assert Roman(12).roman == "XII" assert Roman(13).roman == "XIII" assert Roman(14).roman == "XIV" assert Roman(15).roman == "XV" assert Roman(16).roman == "XVI" assert Roman(17).roman == "XVII" assert Roman(18).roman == "XVIII" assert Roman(19).roman == "XIX" assert Roman(20).roman == "XX" assert Roman(30).roman == "XXX" assert Roman(40).roman == "XL" assert Roman(50).roman == "L" assert Roman(60).roman == "LX" assert Roman(70).roman == "LXX" assert Roman(80).roman == "LXXX" assert Roman(90).roman == "XC" assert Roman(100).roman == "C" assert Roman(101).roman == "CI" assert Roman(105).roman == "CV" assert Roman(110).roman == "CX" assert Roman(150).roman == "CL" assert Roman(200).roman == "CC" assert Roman(300).roman == "CCC" assert Roman(400).roman == "CD" assert Roman(500).roman == "D" assert Roman(600).roman == "DC" assert Roman(700).roman == "DCC" assert Roman(800).roman == "DCCC" assert Roman(900).roman == "CM" assert Roman(1000).roman == "M" assert Roman(2000).roman == "MM" assert Roman(2019).roman == "MMXIX"
def test_mult(): assert Roman("I") == Roman("I") * Roman("I") assert Roman("IV") == Roman("II") * Roman("II") assert Roman("IX") == Roman("III") * Roman("III")
def test_mult_int(): assert Roman("I") == Roman("I") * 1 assert Roman("IV") == Roman("II") * 2 assert Roman("IV") == 2 * Roman("II") assert Roman("IX") == Roman("III") * 3
def setUp(self): self.roman = Roman()
from roman import Roman if __name__ == "__main__": r1 = Roman("X") r2 = Roman(5) print(" Числа:", r1, r2, r1.arabic, r2.arabic) print(" Сумма:", r1 + r2) print(" Разность:", r1 - r2) print("Произведение:", r1 * r2) print(" Частное:", r1 // r2) print("\nПреобразование без создания объекта:") print(2016, "=", Roman.to_roman(2016)) print("MMXVI", "=", Roman.to_arabic("MMXVI")) # ------------- # Пример вывода: # Числа: X V 10 5 # Сумма: XV # Разность: V # Произведение: L # Частное: II # # Преобразование без создания объекта: # 2016 = MMXVI # MMXVI = 2016
def test_inversion(): for i in range(1, 4000): assert int(Roman(i)) == i
def roman(): return Roman()
def test_not_eq(): assert Roman(1) != Roman(2)
def test_number_of_digits_in_1(self): number = Roman(1) self.assertEquals(number.number_of_digits(), 1)
def test_sub(): assert Roman(3987) - Roman(18) == Roman(3969)
def test_add(): assert Roman("II") == Roman("I") + Roman("I") assert Roman("IV") == Roman("II") + Roman("II") assert Roman("V") == Roman("III") + Roman("II") assert Roman("V") == Roman("II") + Roman("III") assert Roman("M") == Roman("D") + Roman("D")
def setUp(self): self.r = Roman()
def test_to_integer(): """Roman(numeral) converts to the right integer.""" for numeral, value in known_numerals: assert int(Roman(numeral)) == value
def test_eq(): for i in range(1, 4000): assert Roman(i) == Roman(i)
def test_roman(self): self.assertEqual(Roman.I, 1) self.assertEqual(Roman.III, 3) self.assertEqual(Roman.IV, 4) self.assertEqual(Roman.V, 5) self.assertEqual(Roman.VI, 6) self.assertEqual(Roman.IX, 9) self.assertEqual(Roman.XXIV, 24) self.assertEqual(Roman.XXXI, 31) self.assertEqual(Roman.CCCLXIX, 369) self.assertEqual(Roman.CD, 400) self.assertEqual(Roman.CDXLVIII, 448) self.assertEqual(Roman.to_roman(1), 'I') self.assertEqual(Roman.to_roman(3), 'III') self.assertEqual(Roman.to_roman(4) , 'IV') self.assertEqual(Roman.to_roman(5), 'V') self.assertEqual(Roman.to_roman(6), 'VI') self.assertEqual(Roman.to_roman(9), 'IX') self.assertEqual(Roman.to_roman(24), 'XXIV') self.assertEqual(Roman.to_roman(31), 'XXXI') self.assertEqual(Roman.to_roman(369), 'CCCLXIX') self.assertEqual(Roman.to_roman(400), 'CD') self.assertEqual(Roman.to_roman(448), 'CDXLVIII')
def test_add(): assert Roman(3987) + Roman(12) == Roman(3999)
def test_roman_init(): assert Roman(5) == 'V' assert Roman('IV') == 'IV' assert Roman('MCMXL') + Roman('I') == Roman('MCMXLI')
def test_mul(): assert Roman(19) * Roman(12) == Roman(228)
def test_adding_xi_to_vi(self): """ Test adding 11 + 6 """ numeral_a = Roman('xi') numeral_b = Roman('vi') self.assertEqual('xvii', numeral_a.add(numeral_b)) self.assertEqual('xvii', numeral_b.add(numeral_a))
def test_true(self): self.assertTrue(Roman(5) == Roman(5))
def test_adding_l_to_i(self): """ Test adding 50 + 1 """ numeral_a = Roman('l') numeral_b = Roman('i') self.assertEqual('li', numeral_a.add(numeral_b)) self.assertEqual('li', numeral_b.add(numeral_a))
def test_errors(self): self.assertRaises(ValueError, Roman, -1) with self.assertRaises(ValueError): a = Roman(1000) + Roman(1000)
from roman import Roman sum = 0 with open('p089_roman.txt', 'r') as file: for line in file: line = line.strip() print(line) roman = Roman(line) print('Decimal: ', roman.to_decimal()) minimal = Roman(Roman(line).to_decimal()).to_str() print('Minimal: ' + minimal) saved = len(line) - len(minimal) print('Saved: ', saved) print() sum += saved print('Answer:', sum)