Skip to content

Commit f0fbbd2

Browse files
committed
Add verify swap using pointers
1 parent bb4e627 commit f0fbbd2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

numerical/swap.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package numerical
2+
3+
import "fmt"
4+
5+
func swap(x, y *int) {
6+
*x, *y = *y, *x
7+
}
8+
9+
func main() {
10+
x := 3
11+
y := 2
12+
fmt.Println(x, y)
13+
swap(&x, &y)
14+
fmt.Println(x, y)
15+
}

numerical/swap_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package numerical
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSwap(t *testing.T) {
10+
x := 5
11+
y := 6
12+
13+
expectedX := 6
14+
expectedY := 5
15+
16+
swap(&x, &y)
17+
assert.Equal(t, x, expectedX, "value should be equal")
18+
assert.Equal(t, y, expectedY, "value should be equal")
19+
}

0 commit comments

Comments
 (0)