Search Issue Tracker

By Design

Votes

0

Found in

2019.3.0a7

2019.3.1f1

2020.1

2020.2

Issue ID

1223732

Regression

No

Calling Reinterpret<bool> on NativeArray<byte> fails with 'Expected: True' 'But Was: True' in unit tests

Unity Test Runner

-

How to reproduce:
1. Open user-submitted project (NativeArrayBug.zip)
2. Open Window > General >Test Runner
3. Run All Edit Mode tests

Expected result: test 'ReinterpretByteToBool' passes
Actual result: test 'ReinterpretByteToBool' fails with expected: True, but was: True

Reproducible with: 2019.3.0a7, 2019.3.4f1, 2020.1.0a26, 2020.2.0a1
Could not test with: 2017.4.37f1, 2018.4.18f1, 2019.3.0a6 (NativeArray<byte>' does not contain a definition for 'Reinterpret')

  1. Resolution Note:

    bytes[0] = 255;

    should be changed to

    bytes[0] = 1;

    then test will pass.

    In C# underlying type for boolean is byte, you can't really make true out of 255. But you can do it out of 1.

    Regarding Assert.IsTrue(). What nunit does in this case is something like this:
    Under the hood nunit does this :
    public override ConstraintResult ApplyTo(object actual)
    {
    return new ConstraintResult((IConstraint) this, actual, true.Equals(actual));
    }

    so it is basically an equals operator:
    If you distill this part into code

    bytes[0] = 255;
    NativeArray<bool> bools = bytes.Reinterpret<bool>();
    var b1 = bools[0].Equals(true); // false

    bool[] pureBool = {true};
    pureBool[0] = true;
    var b2 = pureBool[0].Equals(true); // true

    So equals ends up comparing 1 to 255


    The reason why error message looks so weird, is that ToString method doesn't really know how to translates both 255 and 1 into True.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.