Oct 29, 2018

Unit testing ternary operators...

...in my compiler makes my head hurt.

i = 1 ? 2 : 3
Assert(i == 2)
i = !1 ? 2 : 3
Assert(i == 3)
i = !1 ? 2 : 3 ? 4 : 5
Assert(i == 4)
i = !1 ? 2 ? 3 : 4 : 5 ? 6 : 7
Assert(i == 6)
i = 1 ? 2 ? 3 : 4 : 5 ? 6 : 7
Assert(i == 3)
i = 1 ? !2 ? 3 : 4 : 5 ? 6 : 7
Assert(i == 4)
i = 1 ? 0 ? 3 : 4 : 5 ? 6 : 7
Assert(i == 4)
i = 1 ? !2 ? 3 ? 4 : 5 : 6 : 7 ? 8 : 9
Assert(i == 6)

Any others I should test?

DrMcCoy

Oct 30, 2018
Maybe add more operators there, a mix of higher and lower precedence than ?:

Marcos

Oct 30, 2018
Mmm aren't 3rd and below just redundant? Having the first 2 tested why would you need the rest?

Ron Gilbert

Oct 30, 2018
@marcos Probably, but I'm also testing the ! operator. I have more variations coming to deal with the issues that @DrMcCoy raised (precedence of other operators). This was my first pass and my head hurt, it's far from the finished test cases.

Neil Martin

Oct 30, 2018
What about placing the ! operator on the answers, changing them to bools.

jmackley

Oct 30, 2018
OR....and I'm just sayin'...never use that hideous syntax ever again.

Neil Martin

Oct 30, 2018
Also, what about !! and !!!

mtve

Oct 30, 2018
1?1?1?1?1?1?1?1?1?1?1?1?1?1?1?1?2:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:0?0:3
1?0?0:1?0?0:1?0?0:1?0?0:1?0?0:1?0?0:1?0?0:1?0?0:4:0:0:0:0:0:0:0:0

Marcos

Oct 31, 2018
@Ron Then, imho, I'd encourage you to split the tests into different sets: the one for ? : op and the one for ! If you pass both independently, you get the work done. And you won't become crazy trying different paths 😀

Marcos

Oct 31, 2018
Oh, and you can have another set for precedence, but at any other lower level like syntax one.

Marcos

Oct 31, 2018
This' a compiler am working on nowadays:

https://github.com/MarcosCobena/GoTo/blob/master/Tests/SemanticAnalyzerTests.cs

It may give you any idea on how to deal with yours.

Someone

Nov 01, 2018
Ron, can't you avoid that syntax? It's very error-prone. If not, Marcos gave already a good tip.

Ron Gilbert

Nov 01, 2018
I've tried other syntaxes and they are all too wordy. Python's has to be the worst.  If you have a better suggestion that is light, I'm open. One of the things about creating your own compile is being able to tweak syntax.

Zak Phoenix McKracken

Nov 02, 2018
I'm surely naive... why are you creating your own compiler? I mean, is it really necessary for your purposes, with all the tools that comes nowadays?

Ron Gilbert

Nov 02, 2018
@zak I'm planning a blog post on why I'm doing this. It's a question I ask myself all the time.

Alien426

Nov 09, 2018
> never use that hideous syntax ever again
@jmackley Ternary is good because it's short and in-line. A regular if-then-else is usually 2-4 lines.

> It's very error-prone.
@Someone In some cases it is less error-prone because at least the "i" variable (in Ron's examples) always gets assigned.

Ternary gets hideous and error-prone when it is nested.

> Python's has to be the worst
@Ron I think it's neat. It doesn't introduce new characters (is "?" used for other things in C?) or change others (":" in a switch case roughly means "if the preceding value is true"). In Python it uses know syntax, just turns it around a bit from regular use. And it forms a sentence: "[on_true] if [expression] else [on_false]". What do "?" and ":" mean in "[expression] ? [on_true] : [on_false]"?
I never wrote a nested ternary in Python (though I have to confess that I use it less often than JavaScript).
Here are the rules for commenting.