Skip to content

Commit 5d50f6f

Browse files
sebx59HARDEMAN Sebastien
andauthored
BAEL-5559 - Swap two variables using Java (eugenp#12113)
* BAEL-5559 - Swap two variables using Java initial commit * Updating tests names Co-authored-by: HARDEMAN Sebastien <[email protected]>
1 parent 1dccedc commit 5d50f6f

1 file changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package com.baeldung.math.swap;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SwappingVariablesUnitTest {
7+
8+
@Test
9+
public void givenTwoStrings_whenSwappingInMethod_thenFails() {
10+
11+
String a = "a";
12+
String b = "b";
13+
14+
swap(a, b);
15+
16+
Assertions.assertFalse(a.equals("b"));
17+
Assertions.assertFalse(b.equals("a"));
18+
}
19+
20+
@Test
21+
public void givenTwoWrappers_whenSwappingInMethod_thenSuccess() {
22+
23+
Wrapper a = new Wrapper("a");
24+
Wrapper b = new Wrapper("b");
25+
26+
swap(a, b);
27+
28+
Assertions.assertTrue(a.string.equals("b"));
29+
Assertions.assertTrue(b.string.equals("a"));
30+
}
31+
32+
@Test
33+
public void givenTwoIntegers_whenSwappingUsingAdditionSubstraction_thenSuccess() {
34+
35+
int a = 5;
36+
int b = 10;
37+
38+
a = a + b;
39+
b = a - b;
40+
a = a - b;
41+
42+
Assertions.assertTrue(a == 10);
43+
Assertions.assertTrue(b == 5);
44+
}
45+
46+
@Test
47+
public void givenTwoIntegers_whenSwappingUsingMultiplicationDivision_thenSuccess() {
48+
49+
int a = 5;
50+
int b = 10;
51+
52+
a = a * b;
53+
b = a / b;
54+
a = a / b;
55+
56+
Assertions.assertTrue(a == 10);
57+
Assertions.assertTrue(b == 5);
58+
}
59+
60+
@Test
61+
public void givenTwoIntegers_whenSwappingWithOverflow_thenFails() {
62+
63+
int a = Integer.MAX_VALUE;
64+
int b = 10;
65+
66+
a = a * b;
67+
b = a / b;
68+
a = a / b;
69+
70+
Assertions.assertTrue(a == 10);
71+
Assertions.assertFalse(b == Integer.MAX_VALUE);
72+
}
73+
74+
@Test
75+
public void givenTwoChars_whenSwappingWithCast_thenSuccess() {
76+
77+
char a = 'a';
78+
char b = 'b';
79+
80+
a = (char)(a * b);
81+
b = (char)(a / b);
82+
a = (char)(a / b);
83+
84+
Assertions.assertTrue(a == 'b');
85+
Assertions.assertTrue(b == 'a');
86+
}
87+
88+
@Test
89+
public void givenTwoIntegers_whenSwappingUsingXor_thenSuccess() {
90+
91+
int a = 5;
92+
int b = 10;
93+
94+
a = a ^ b;
95+
b = a ^ b;
96+
a = a ^ b;
97+
98+
Assertions.assertTrue(a == 10);
99+
Assertions.assertTrue(b == 5);
100+
}
101+
102+
@Test
103+
public void givenTwoIntegers_whenSwappingUsingSingleLineXor_thenSuccess() {
104+
105+
int a = 5;
106+
int b = 10;
107+
108+
a = a ^ b ^ (b = a);
109+
110+
Assertions.assertTrue(a == 10);
111+
Assertions.assertTrue(b == 5);
112+
}
113+
114+
@Test
115+
public void givenTwoIntegers_whenSwappingUsingAdditionSubstractionSingleLine_thenSuccess() {
116+
117+
int a = 5;
118+
int b = 10;
119+
120+
b = (a + b) - (a = b);
121+
122+
Assertions.assertTrue(a == 10);
123+
Assertions.assertTrue(b == 5);
124+
}
125+
126+
/**
127+
* Illustrates that swapping in a method doesn't work
128+
*/
129+
private void swap(String a, String b) {
130+
131+
String temp = b;
132+
b = a;
133+
a = temp;
134+
}
135+
136+
/**
137+
* Illustrates swapping in a method with Wrapper class
138+
*/
139+
private void swap(Wrapper a, Wrapper b) {
140+
141+
String temp = b.string;
142+
b.string = a.string;
143+
a.string = temp;
144+
}
145+
146+
class Wrapper {
147+
148+
public String string;
149+
150+
public Wrapper(String string) {
151+
152+
this.string = string;
153+
}
154+
155+
156+
}
157+
}

0 commit comments

Comments
 (0)