<?xml version="1.0" encoding="UTF-8"?>
<test-data
    xmlns="http://pmd.sourceforge.net/rule-tests"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

    <test-code>
        <description>do while true</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>4,6</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        do {
        } while (true);
        do {
        } while ((true));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>do while true | true</description>
        <expected-problems>8</expected-problems>
        <expected-linenumbers>3,4,5,6,9,10,11,12</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        do { } while (true | true);
        do { } while (true || true);
        do { } while ((true | true));
        do { } while ((true || true));

        boolean otherBool = false;
        do { } while (true | otherBool);
        do { } while (true || otherBool);
        do { } while ((true | otherBool));
        do { } while ((true || otherBool));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>do while false</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>4,6</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        do {
        } while (false);
        do {
        } while ((false));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>do while false | false #3455</description>
        <expected-problems>4</expected-problems>
        <expected-linenumbers>3,5,8,10</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        do {
        } while (false | false);
        do {
        } while ((false | false));

        do {
        } while (false || false);
        do {
        } while ((false || false));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>do while false &amp; false</description>
        <expected-problems>8</expected-problems>
        <expected-linenumbers>3,5,7,9,13,15,17,19</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        do {
        } while (false & false);
        do {
        } while ((false & false));
        do {
        } while (false && false);
        do {
        } while ((false && false));

        boolean otherBool = true;
        do {
        } while (false & otherBool);
        do {
        } while ((false & otherBool));
        do {
        } while (false && otherBool);
        do {
        } while ((false && otherBool));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>do while true &amp;&amp; true</description>
        <expected-problems>8</expected-problems>
        <expected-linenumbers>3,5,7,9,12,14,16,18</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        do {
        } while (true & true);
        do {
        } while ((true & true));
        do {
        } while (true && true);
        do {
        } while ((true && true));

        do {
        } while (true & true & true);
        do {
        } while ((true & true & true));
        do {
        } while (true && true && true);
        do {
        } while ((true && true && true));

        // the following loops are not reported, because they depend on otherBool
        boolean otherBool = true;
        do {
        } while (true & otherBool);
        do {
        } while ((true & otherBool));
        do {
        } while (true && otherBool);
        do {
        } while ((true && otherBool));
    }
}

]]></code>
    </test-code>

    <test-code>
        <description>do while call</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    {
        do {
        } while (call(true));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>while true - allowed</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    {
        while (true) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>while false</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>3,5</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        while (false) {
        }
        while ((false)) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>while false | false</description>
        <expected-problems>8</expected-problems>
        <expected-linenumbers>3,4,5,6,8,9,10,11</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        while (false | false) { }
        while ((false | false)) { }
        while (false || false) { }
        while ((false || false)) { }

        while (false | false | false) { }
        while ((false | false | false)) { }
        while (false || false || false) { }
        while ((false || false || false)) { }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>while call false</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    {
        while (call(false)) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>conditional or with only one boolean literal</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    void bar(boolean arg) {
        while (false | arg) { }
        while (false || arg) { }
        do { } while (false || arg);
    }
}
]]></code>
    </test-code>

    <test-code>
        <description>conditional and with only one boolean literal</description>
        <expected-problems>3</expected-problems>
        <expected-linenumbers>3,4,5</expected-linenumbers>
        <code><![CDATA[
class Foo {
    void bar(boolean arg) {
        while (false & arg) { }
        while (false && arg) { }
        do { } while (false && arg);
    }
}
]]></code>
    </test-code>

    <test-code>
        <description>conditional-and boolean literal</description>
        <expected-problems>28</expected-problems>
        <expected-linenumbers>10,11,12,13,14,15,16,17,18,19,20,21,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39</expected-linenumbers>
        <code><![CDATA[
class Foo {
    {
        // these evaluate to true and are allowed
        while (true & true) { }
        while ((true & true)) { }
        while (true && true) { }
        while ((true && true)) { }

        // these evaluate to false and should be flagged
        while (false & false) { } // line 10
        while ((false & false)) { }
        while (false && false) { }
        while ((false && false)) { }
        while (false & true) { }
        while ((false & true)) { }
        while (false && true) { }
        while ((false && true)) { }
        while (true & false) { }
        while ((true & false)) { }
        while (true && false) { }
        while ((true && false)) { }

        // do-while loops should always be flagged
        do {} while (false & false);
        do {} while ((false & false));
        do {} while (false && false);
        do {} while ((false && false));
        do {} while (true & false);
        do {} while ((true & false));
        do {} while (true && false);
        do {} while ((true && false));
        do {} while (false & true);
        do {} while ((false & true));
        do {} while (false && true);
        do {} while ((false && true));
        do {} while (true & true);
        do {} while ((true & true));
        do {} while (true && true);
        do {} while ((true && true));
    }
}
]]></code>
    </test-code>

    <test-code>
        <description>[java] WhileLoopWithLiteralBoolean - false negative with complex expressions still occurs in PMD 6.52.0 #4250</description>
        <expected-problems>4</expected-problems>
        <expected-linenumbers>3,7,11,15</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void func() {
        do {
            // Loop Body
        } while (false || false || false || false); // This is a false negative

        do {
            // Loop Body
        } while (false | false | false | false | false); // This is a similar false negative.

        do {            //reported: WhileLoopWithLiteralBoolean:    The loop can be simplified.
            // Loop Body
        } while (false && false && false && false);

        do {            //reported: WhileLoopWithLiteralBoolean:    The loop can be simplified.
            // Loop Body
        } while (false & false & false & false & false);
    }
}
]]></code>
    </test-code>

    <test-code>
        <description>False positives without literal booleans</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void func(int x, int y) {
        do {
            System.out.println("Testing");
        } while (x==0 || y==2);
    }
}
]]></code>
    </test-code>
</test-data>
