diff --git a/numerical/swap.go b/numerical/swap.go new file mode 100644 index 0000000..fbf633f --- /dev/null +++ b/numerical/swap.go @@ -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) +} diff --git a/numerical/swap_test.go b/numerical/swap_test.go new file mode 100644 index 0000000..744d416 --- /dev/null +++ b/numerical/swap_test.go @@ -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") +}