Merge pull request #35 from ridwanfathin/swap

Add verify swap using pointers
master
0xAX 2 years ago committed by GitHub
commit 8b7a3eee46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,15 @@
package numerical
import "fmt"
func swap(x, y *int) {
*x, *y = *y, *x
}
func main() {
x := 3
y := 2
fmt.Println(x, y)
swap(&x, &y)
fmt.Println(x, y)
}

@ -0,0 +1,19 @@
package numerical
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSwap(t *testing.T) {
x := 5
y := 6
expectedX := 6
expectedY := 5
swap(&x, &y)
assert.Equal(t, x, expectedX, "value should be equal")
assert.Equal(t, y, expectedY, "value should be equal")
}
Loading…
Cancel
Save