Пример #1
0
    def test_parse_to_cpp(self):
        """Test Python to C++ expression parser"""

        ufo_to_pythia8 = ufo_expression_parsers.UFOExpressionParserCPP()
        expr = 'cmath.sqrt(2)'
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, 'sqrt(2.)')

        expr = 'randomfunction(2.)'
        converted = ufo_to_pythia8.parse(expr)
        self.assertEqual(converted, 'randomfunction(2.)')

        expr = 'function(2.0000000, a+b)'
        converted = ufo_to_pythia8.parse(expr)
        self.assertEqual(converted, 'function(2.,a+b)')

        expr = 'cmath.sqrt(2.5)'
        converted = ufo_to_pythia8.parse(expr)
        self.assertEqual(converted, 'sqrt(2.5)')

        expr = 'cmath.testfcn(2.00, a+b)'
        converted = ufo_to_pythia8.parse(expr)
        self.assertEqual(converted, 'testfcn(2.,a+b)')

        expr = '(ee**2*IMAG/(2.*sw**2) * (cmath.sin(cmath.sqrt(2)*ee)**2/3.))'
        converted = ufo_to_pythia8.parse(expr)
        self.assertEqual(
            converted,
            '(pow(ee,2.)*IMAG/(2.*pow(sw,2.))*(pow(sin(sqrt(2.)*ee),2.)/3.))')
Пример #2
0
    def test_convert_number_to_cpp(self):
        """ test it can convert number in C++ string"""

        ufo_to_pythia8 = ufo_expression_parsers.UFOExpressionParserCPP()
        expr = str(2)
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '2.')

        expr = str(2.00000)
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '2.')

        expr = str(0.23)
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '0.23')

        expr = '2.5e6'
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '2.5e6')

        expr = '2.5e+6'
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '2.5e+6')

        expr = '.5e-6'
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '.5e-6')

        expr = str(1.5)
        converted = ufo_to_pythia8.parse(expr)
        self.assertTrue(isinstance(converted, str))
        self.assertEqual(converted, '1.5')