Add verify swap using pointers

pull/35/head
ridwanfathin 4 years ago
parent bb4e627657
commit f0fbbd2ff2

@ -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