php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Enum comparison resuls sometimes incorrect

Open berniev opened this issue 4 years ago • 2 comments

Description

The following code:

use PHPUnit\Framework\TestCase;
use function PHPUnit\Framework\assertEquals;

enum EN
{
    case one;
    case two;
}

class CC
{
    public function __construct(
        public EN $fred
    )
    {
    }
}

class EnumTest extends TestCase
{

    protected function setUp(): void
    {
        $this->c = new CC(EN::two);
    }

    function test1()
    {
        assertEquals(EN::one === EN::one, true);
    }

    function test2()
    {
        assertEquals(! EN::one === EN::one, false);
    }

    function test3()
    {
        assertEquals(EN::one !== EN::one, false);
    }

    function test4()
    {
        assertEquals(! EN::one !== EN::one, true);
    }

    function test5()
    {
        $fred = EN::one;
        assertEquals($fred === EN::one, true);
    }

    function test6()
    {
        $fred = EN::one;
        assertEquals(! $fred === EN::one, false);
    }

    function test7()
    {
        $fred = EN::one;
        assertEquals($fred === EN::two, false);
    }

    function test8()
    {
        $fred = EN::one;
        assertEquals(! $fred === EN::two, true);
    }

    function test9()
    {
        $fred = EN::one;
        assertEquals($fred !== EN::two, true);
    }

    function test10()
    {
        $fred = EN::one;
        assertEquals(! $fred !== EN::two, false);
    }

    function test11()
    {
        assertEquals($this->c->fred === EN::one, false);
    }

    function test12()
    {
        assertEquals(! $this->c->fred === EN::one, true);
    }

    function test13()
    {
        assertEquals($this->c->fred === EN::two, true);
    }

    function test14()
    {
        assertEquals(! $this->c->fred === EN::two, false);
    }

    function test15()
    {
        assertEquals($this->c->fred !== EN::two, false);
    }

    function test16()
    {
        assertEquals(! $this->c->fred !== EN::two, true);
    }
}

<?php

Resulted in this output:

tests 8, 10 and 12 failed

PHP Version

PHP 8.1.1

Operating System

MacOS 12.1

berniev avatar Jan 10 '22 19:01 berniev

Please be aware that the logical not operator ! has higher precedence than comparison operators like === or !==

So, ! EN::one === EN::one is just false === EN::one.

drealecs avatar Jan 10 '22 20:01 drealecs

of course. D’oh!

On 11 Jan 2022, at 6:19 am, Alexandru Pătrănescu @.***> wrote:

Please be aware that the logical not operator ! has higher precedence than comparison operators like === or !==

So, ! EN::one === EN::one is just false === EN::one.

— Reply to this email directly, view it on GitHub https://github.com/php/php-src/issues/7925#issuecomment-1009308779, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEXAYOAZMK4OKSM6C3UTVDUVM5OFANCNFSM5LUQKHSQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.

berniev avatar Jan 10 '22 20:01 berniev