Ejemplo n.º 1
0
print(digit2.get_octal()
      )  #443.56314631463146314631463146314631463146314631463146
print(digit2.get_hexadecimal())  #123.B99999999

digit3 = BinaryDigit(digit_value='100100011,1011100110',
                     digit_type=2,
                     separator=',')
print('100100011,1011100110 = ' + digit3.get_binary())
print('100100011,1011100110 = ' + digit3.get_decimal())
print('100100011,1011100110 = ' + digit3.get_octal())
print('100100011,1011100110 = ' + digit3.get_hexadecimal())
print('---------------------------')

digit4 = DecimalDigit(digit_value='399.564', digit_type=10, separator=',')
print("399.564 = " + digit4.get_binary())
print("399.564 = " + digit4.get_decimal())
print("399.564 = " + digit4.get_octal())
print("399.564 = " + digit4.get_hexadecimal())
print('---------------------------')

digit5 = BinaryDigit(digit_value='110011111.10010100011',
                     digit_type=2,
                     separator='.')
print('110011111.1001010001 = ' + digit5.get_binary())
print('110011111.1001010001 = ' + digit5.get_decimal())
print('110011111.1001010001 = ' + digit5.get_octal())
print('110011111.1001010001 = ' + digit5.get_hexadecimal())
print('---------------------------')

digit6 = BinaryDigit(digit_value='110011111', digit_type=2, separator='.')
print('110011111 = ' + digit6.get_binary())
Ejemplo n.º 2
0
class Test_DecimalDigit(unittest.TestCase):

    def setUp(self):
        self.decimal_digit = DecimalDigit()

    def test_conversion_to_binary_only_integer_part(self):
        self.decimal_digit.digit_value = '12'
        binary_digit = self.decimal_digit.get_binary()        
        self.assertEqual(binary_digit, "1100.0", "10: 12 should be 2: 1100.0")

    def test_conversion_to_binary_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '12.18'
        binary_digit = self.decimal_digit.get_binary()        
        self.assertEqual(binary_digit, "1100.0010111000", "10: 12.18 should be 2: 1100.0010111000")

    def test_conversion_to_decimal_only_integer_part(self):
        self.decimal_digit.digit_value = '12'
        decimal_digit_new = self.decimal_digit.get_decimal()        
        self.assertEqual(decimal_digit_new, "12", "10: 12 should be 10: 12")

    def test_conversion_to_decimal_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '12.18'
        decimal_digit_new = self.decimal_digit.get_decimal()        
        self.assertEqual(decimal_digit_new, "12.18", "10: 12.18 should be 10: 12.18")

    def test_conversion_to_octal_only_integer_part(self):
        self.decimal_digit.digit_value = '1234'
        octal_digit = self.decimal_digit.get_octal()        
        self.assertEqual(octal_digit, "2322.0", "10: 1234 should be 8: 2322.0")

    def test_conversion_to_octal_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '1234.56'
        octal_digit = self.decimal_digit.get_octal()        
        self.assertEqual(octal_digit, "2322.4365605075", "10: 1234.56 should be 8: 2322.4365605075")

    def test_conversion_to_hexadecimal_only_integer_part(self):
        self.decimal_digit.digit_value = '1234'
        hexadecimal_digit = self.decimal_digit.get_hexadecimal()        
        self.assertEqual(hexadecimal_digit, "4D2.0", "10: 1234 should be 8: 4D2")

    def test_conversion_to_hexadecimal_integer__and_fractional_parts(self):
        self.decimal_digit.digit_value = '1234.56'
        hexadecimal_digit = self.decimal_digit.get_hexadecimal()        
        self.assertEqual(hexadecimal_digit, "4D2.8F5C28F5C2", "10: 1234.56 should be 16: 4D2.8F5C28F5C2")

    def test_passed_value_is_null_hexadecimal(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_null_binary(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_null_decimal(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_null_octal(self):
        self.decimal_digit.digit_value = None
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal()
    
    def test_passed_value_is_empty_hexadecimal(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_empty_binary(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_empty_decimal(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_empty_octal(self):
        self.decimal_digit.digit_value = ''
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal() 

    def test_passed_value_is_nan_hexadecimal(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_nan_binary(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_nan_decimal(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_nan_octal(self):
        self.decimal_digit.digit_value = 'ABC'
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal() 

    def test_passed_value_is_whitespace_hexadecimal(self):
        self.decimal_digit.digit_value = '  '
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_hexadecimal() 
    
    def test_passed_value_is_whitespace_binary(self):
        self.decimal_digit.digit_value = '   '
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_binary() 

    def test_passed_value_is_whitespace_decimal(self):
        self.decimal_digit.digit_value = '   '
        with self.assertRaises(ValueError) as cm: 
            self.decimal_digit.get_decimal() 

    def test_passed_value_is_whitespace_octal(self):
        self.decimal_digit.digit_value = '   '
        with self.assertRaises(ValueError) as cm:    
            self.decimal_digit.get_octal()