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
Marcos
Oct 30, 2018

Ron Gilbert
Oct 30, 2018
Neil Martin
Oct 30, 2018
jmackley
Oct 30, 2018
Neil Martin
Oct 30, 2018
mtve
Oct 30, 2018
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
Marcos
Oct 31, 2018
Marcos
Oct 31, 2018
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 Gilbert
Nov 01, 2018
Zak Phoenix McKracken
Nov 02, 2018

Ron Gilbert
Nov 02, 2018
Alien426
Nov 09, 2018
@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).
Zas
Dec 18, 2018
Assert( i == 7 )
//At least, if it's not my fault. It should check if the ferst tern returns something false as is should.
//Btw, this is a good mind exercise.