Skip to content

Commit

Permalink
Amended tests and possibilities to cast char
Browse files Browse the repository at this point in the history
See: #1988
  • Loading branch information
sergeyklay committed Oct 31, 2019
1 parent 3da73d0 commit fe19f8a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 5 deletions.
51 changes: 50 additions & 1 deletion Library/Operators/Other/CastOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function compile(array $expression, CompilationContext $compilationContex
case Types::T_NULL:
return new CompiledExpression('int', 0, $expression);

case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression('int', "'{$resolved->getCode()}'", $expression);

case Types::T_INT:
return new CompiledExpression('int', $resolved->getCode(), $expression);

Expand Down Expand Up @@ -115,6 +119,7 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($symbolVariable->getType()) {
case Types::T_INT:
case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression('int', $symbolVariable->getName(), $expression);

case Types::T_DOUBLE:
Expand Down Expand Up @@ -166,6 +171,14 @@ public function compile(array $expression, CompilationContext $compilationContex
case Types::T_INT:
return new CompiledExpression('long', $resolved->getCode(), $expression);

case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression(
'long',
"(long) '{$resolved->getCode()}'",
$expression
);

case Types::T_DOUBLE:
return new CompiledExpression('long', '(long) '.$resolved->getCode(), $expression);

Expand Down Expand Up @@ -196,6 +209,7 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($symbolVariable->getType()) {
case Types::T_INT:
case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression('long', $symbolVariable->getName(), $expression);

case Types::T_DOUBLE:
Expand Down Expand Up @@ -247,6 +261,16 @@ public function compile(array $expression, CompilationContext $compilationContex
case Types::T_BOOL:
return new CompiledExpression('double', $resolved->getBooleanCode(), $expression);

case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression(
'double',
"(double) '{$resolved->getCode()}'",
$expression
);

case Types::T_INT:
case Types::T_LONG:
case Types::T_DOUBLE:
return new CompiledExpression('double', $resolved->getCode(), $expression);

Expand Down Expand Up @@ -276,7 +300,12 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($symbolVariable->getType()) {
case Types::T_INT:
case Types::T_CHAR:
return new CompiledExpression('double', $symbolVariable->getName(), $expression);
case Types::T_UCHAR:
return new CompiledExpression(
'double',
"(double) {$symbolVariable->getName()}",
$expression
);

case Types::T_DOUBLE:
case Types::T_BOOL:
Expand Down Expand Up @@ -330,6 +359,14 @@ public function compile(array $expression, CompilationContext $compilationContex
$expression
);

case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression(
'bool',
"(zend_bool) '{$resolved->getCode()}'",
$expression
);

case Types::T_BOOL:
return new CompiledExpression('bool', $resolved->getCode(), $expression);

Expand All @@ -348,6 +385,7 @@ public function compile(array $expression, CompilationContext $compilationContex
switch ($symbolVariable->getType()) {
case Types::T_INT:
case Types::T_CHAR:
case Types::T_UCHAR:
return new CompiledExpression(
'bool',
sprintf('(zend_bool) %s', $symbolVariable->getName()),
Expand Down Expand Up @@ -388,6 +426,12 @@ public function compile(array $expression, CompilationContext $compilationContex

case Types::T_CHAR:
switch ($resolved->getType()) {
case Types::T_UCHAR:
return new CompiledExpression('uchar', "'{$resolved->getCode()}'", $expression);

case Types::T_CHAR:
return new CompiledExpression('char', "'{$resolved->getCode()}'", $expression);

case Types::T_VARIABLE:
$compilationContext->headersManager->add('kernel/operators');
$symbolVariable = $compilationContext->symbolTable->getVariableForRead(
Expand All @@ -402,6 +446,7 @@ public function compile(array $expression, CompilationContext $compilationContex
);

switch ($symbolVariable->getType()) {
// TODO: uchar
case Types::T_CHAR:
$compilationContext->codePrinter->output(
sprintf('%s = %s;', $tempVariable->getName(), $symbolVariable->getName())
Expand Down Expand Up @@ -433,6 +478,10 @@ public function compile(array $expression, CompilationContext $compilationContex

case Types::T_STRING:
switch ($resolved->getType()) {
case Types::T_UCHAR:
case Types::T_CHAR:
return new CompiledExpression('string', $resolved->getCode(), $expression);

case Types::T_VARIABLE:
$compilationContext->headersManager->add('kernel/operators');
$compilationContext->symbolTable->mustGrownStack(true);
Expand Down
43 changes: 41 additions & 2 deletions test/cast.zep
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class Cast
*/
public function testCharCastFromChar() -> char
{
char a = 'a';
return (char) a;
return (char) 'a';
}

/**
Expand All @@ -29,6 +28,14 @@ class Cast

// To string cast

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
public function testStringCastChar() -> string
{
return (string) 'z';
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
Expand All @@ -51,6 +58,14 @@ class Cast
return (int) a;
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
public function testIntCastFromChar() -> int
{
return (int) 'A';
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
Expand Down Expand Up @@ -155,6 +170,14 @@ class Cast

// To long cast

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
public function testLongCastFromChar() -> long
{
return (long) 'a';
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
Expand Down Expand Up @@ -254,6 +277,14 @@ class Cast

// To double cast

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
public function testDoubleCastFromVChar() -> double
{
return (double) 'a';
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
Expand Down Expand Up @@ -305,6 +336,14 @@ class Cast
return (boolean) a;
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
public function testBooleanCastFromChar() -> boolean
{
return (boolean) 'a';
}

/**
* @see https://github.com/phalcon/zephir/issues/1988
*/
Expand Down
34 changes: 32 additions & 2 deletions unit-tests/Extension/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,27 @@ public function tearDown()
public function testCharCast()
{
/*
* Variable types
* Value
*/

$this->assertSame(97, $this->test->testCharCastFromChar());

/*
* Variable types
*/

$this->assertSame(65, $this->test->testCharCastFromVariableChar());
}

public function testStringCast()
{
/*
* Value
*/

// https://github.com/phalcon/zephir/issues/1988
$this->assertSame('z', $this->test->testStringCastChar());

/*
* Variable types
*/
Expand All @@ -68,6 +80,7 @@ public function testIntCast()
$this->assertSame(0, $this->test->testIntCastFromEmptyArray());
$this->assertSame(1, $this->test->testIntCastFromArray());
$this->assertSame(1, $this->test->testIntCastFromStdClass());
$this->assertSame(65, $this->test->testIntCastFromChar());

/*
* Variable types
Expand Down Expand Up @@ -95,6 +108,13 @@ public function testIntCast()

public function testLongCast()
{
/*
* Value
*/

// https://github.com/phalcon/zephir/issues/1988
$this->assertSame(97, $this->test->testLongCastFromChar());

/*
* Variable types
*/
Expand Down Expand Up @@ -124,12 +144,19 @@ public function testFloatCast()

public function testDoubleCast()
{
/*
* Value
*/

// https://github.com/phalcon/zephir/issues/1988
$this->assertSame(97.0, $this->test->testDoubleCastFromVChar());

/*
* Variable types
*/

// https://github.com/phalcon/zephir/issues/1988
$this->assertSame(65, $this->test->testLongCastFromVariableChar());
$this->assertSame(65.0, $this->test->testDoubleCastFromVariableChar());
}

public function testBooleanCast()
Expand All @@ -142,6 +169,9 @@ public function testBooleanCast()
$this->assertTrue($this->test->testBooleanCastFromIntTrue2());
$this->assertFalse($this->test->testBooleanCastFromIntFalse());

// https://github.com/phalcon/zephir/issues/1988
$this->assertTrue($this->test->testBooleanCastFromChar());

/*
* Variable types
*/
Expand Down

0 comments on commit fe19f8a

Please sign in to comment.