Friend of mine ask me a question: how to switch values of two variables (Integer type to be strict) but without using another one?
Use this spock spec as problem definition:
You can find working solution on https://github.com/piotrpietrzak/gatchaman/tree/swicher
Use this spock spec as problem definition:
@Unroll |
def "should switch variables (#a,#b) without additional variable"() { |
expect: |
new Switcher(a, b).switchVariables().toString() == result |
where: |
a | b | result |
1 | 2 | "a=2 b=1" |
10 | 20 | "a=20 b=10" |
-10 | -20 | "a=-20 b=-10" |
20 | 10 | "a=10 b=20" |
-20 | -10 | "a=-10 b=-20" |
0 | 10 | "a=10 b=0" |
10 | 0 | "a=0 b=10" |
-10 | 0 | "a=0 b=-10" |
0 | 0 | "a=0 b=0" |
0 | -1 | "a=-1 b=0" |
10 | 10 | "a=10 b=10" |
Integer.MAX_VALUE | Integer.MAX_VALUE | "a=" + Integer.MAX_VALUE + " b=" + Integer.MAX_VALUE |
Integer.MIN_VALUE | Integer.MAX_VALUE | "a=" + Integer.MAX_VALUE + " b=" + Integer.MIN_VALUE |
Integer.MIN_VALUE | Integer.MIN_VALUE | "a=" + Integer.MIN_VALUE + " b=" + Integer.MIN_VALUE |
1 | Integer.MAX_VALUE | "a=" + Integer.MAX_VALUE + " b=1" |
Integer.MIN_VALUE | 1 | "a=1 b=" + Integer.MIN_VALUE |
-1 | Integer.MIN_VALUE | "a=" + Integer.MIN_VALUE + " b=-1" |
} |
Have fun!