Page 1 of 1

bitwise operator with enum?

PostPosted: Sun Dec 07, 2008 10:49 pm
by gauthier
I'm getting error trying to use bitwise operator on combined enum values:
enum Borders
Top = 1
Right = 2
Bottom = 4
Left = 8

class Test
def main is shared
borders = Borders(Top,Left)
assert (borders & Borders.Top) == Borders.Top


Code: Select all
test.cobra(10): error: Cannot apply AMPERSAND to Borders. Try finding a method that performs this function.
Compilation failed - 1 error, 0 warnings


is there any support for bitwise operators on enum without casting every operand to int?

Re: bitwise operator with enum?

PostPosted: Sun Dec 07, 2008 11:51 pm
by Charles
Hmm, it looks like when I implemented the bitwise operators, I did them for ints, but not enums. Sorry about that. For now you will have to use casts.

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 12:29 am
by Charles
Fixed, tested and checked in.

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 12:34 am
by gauthier
Thanks!

the fix is a pretty small delta :)

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 12:39 am
by Charles
Well I probably inadvertently allowed "enum + enum" and "enum * enum" but this quick fix is the lesser of two evils.

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 12:45 am
by gauthier
Can you point me to a sample of compiler unit test that check for expected message? (a beginner one)

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 1:43 am
by Charles
<workspace>\Tests\100-basics\402-branch.cobra

which tests a parsing error. Not that the type of error really matters.

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 1:57 am
by gauthier
Is there a way to run the testfixtures asserting on theses compilator errors?

Re: bitwise operator with enum?

PostPosted: Mon Dec 08, 2008 2:06 am
by Charles
I'm not entirely sure what you're asking.

You can verify that Cobra generates an error with the # .error. directive in the test cases. If the compiler fails to generate an error, then that test will fail. If the error takes place with the given substring included, then the test passes.

See also:

-- <workspace>\Developer\ImplementationNotes.text. Look for "testify".

-- See also "Test Cases" at HowToSubmitAPatch.

-- The implementation is in TestifyRunner.cobra. Although hopefully you don't have to read that just to make new test cases.

If that does not answer your question, let me know.