Exemple #1
0
 def test_as_float(self):
     zero = space.float(0.0)
     self.assertTrue(zero.equal(self.array.as_float()).is_true())
     self.array.set(space.int(3), space.int(3))
     one = space.float(1.0)
     self.assertTrue(one.equal(self.array.as_float()).is_true())
     self.assertFalse(zero.equal(self.array.as_float()).is_true())
Exemple #2
0
 def divide(self, number):
     if not number.value:
         raise DivisionByZeroError()
     # if the numbers are evenly divisible, we should return int
     if not self.value % number.value:
         return W_Int(self.value / number.value)
     return space.float(float(self.value) / number.value)
Exemple #3
0
 def test_array_creation_one_key(self):
     raw = [space.int(1), space.int(1),
            space.string("1"), space.int(2),
            space.float(1.5), space.int(3),
            space.bool(True), space.int(4)]
     actual = space.array(raw)
     expected = {space.string('1'): space.int(4)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #4
0
 def test_array_creation_int_branch(self):
     raw = [space.int(3), space.int(1),
            space.float(-1.2), space.int(2),
            space.bool(False), space.int(2),
            space.bool(True), space.int(3)]
     actual = space.array(raw)
     expected = {space.string('3'): space.int(1),
                 space.string('-1'): space.int(2),
                 space.string('0'): space.int(2), space.string('1'): space.int(3)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #5
0
 def test_get_type_conversion_other(self):
     array = space.array([space.int(0), space.int(1),
                          space.int(1), space.int(2),
                          space.int(-1), space.int(3)])
     value = array.get(space.float(1.5)).deref()
     self.assertTrue(value.equal(space.int(2)).is_true())
     value = array.get(space.bool(True)).deref()
     self.assertTrue(value.equal(space.int(2)).is_true())
     value = array.get(space.bool(False)).deref()
     self.assertTrue(value.equal(space.int(1)).is_true())
     with self.assertRaises(KeyError):
         value = array.get(space.null())
     with self.assertRaises(IllegalOffsetType):
         value = array.get(space.array())
Exemple #6
0
    def _handle_float_or_decimal(self, value, begin, end, value_len, strict = False):
        #detect minus
        minus = 1
        if value[0] == '-':
            begin += 1
            end += 1
            minus = -1

        e_symbol = False # for numbers '1e2'
        dot_symbol = False
        number_after_e_symbol = False
        while end < value_len:
            if value[end] not in DECIMAL_SYMBOLS:
                if not dot_symbol and value[end] == '.':
                    dot_symbol = True
                    end += 1
                    continue
                if (not e_symbol and
                        (value[end] == 'e' or value[end] == 'E')):
                    e_symbol = True
                    end += 1
                    continue
                self.convertible_to_number = False
                if strict:
                    raise NotConvertibleToNumber
                else:
                    break
            if e_symbol:
                number_after_e_symbol = True
            end += 1

        # truncate 'e' if it's the last symbol in number
        if e_symbol and not number_after_e_symbol:
            end -= 1

        if minus and end == 0:
            return space.int(0)
        elif minus == -1 and end == 1:
            return space.int(0)

        assert begin >= 0
        assert end >= 0
        value = self.str_w()[begin:end]
        if e_symbol or dot_symbol:
            return space.float(float(value) * minus)
        #TODO add PHP_INT_MAX check
        return space.int(int(value) * minus)
Exemple #7
0
 def test_array_creation_with_no_keys(self):
     raw = [space.undefined(), space.int(1),
            space.undefined(), space.int(2),
            space.float(6.0), space.int(3),
            space.undefined(), space.int(4),
            space.int(3), space.int(5),
            space.undefined(), space.int(6)]
     actual = space.array(raw)
     expected = {space.string('0'): space.int(1), space.string('1'): space.int(2),
                 space.string('6'): space.int(3), space.string('7'): space.int(4),
                 space.string('3'): space.int(5), space.string('8'): space.int(6)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
     raw = [space.string("6"), space.int(3),
            space.string("3"), space.int(5),
            space.undefined(), space.int(6)]
     actual = space.array(raw)
     expected = {space.string('6'): space.int(3),
                 space.string('3'): space.int(5),
                 space.string('7'): space.int(6)}
     for key, value in expected.iteritems():
         self.assertTrue(actual.get(key).deref().equal(value).is_true())
Exemple #8
0
 def get_compiled_value(self):
     return space.float(self.const_value)
Exemple #9
0
def cos(context, params):
    arg = params[0].as_float()
    return space.float(math.cos(arg.value))
Exemple #10
0
 def as_float(self):
     return space.float(0.0)
Exemple #11
0
 def as_float(self):
     return space.float(float(self.value))
Exemple #12
0
 def as_float(self):
     return space.float(float(self.is_true()))