Exercises in the " Iteration " chapter
Modify the test code so that the caller can specify the number of character repetitions, and then fix the code
First follow the steps in the " Iteration " chapter to complete the test code. Get the final result:
In order for the caller to specify the number of character repetitions, there is no doubt that one more parameter needs to be added to represent the number of repetitions.
The results are as follows:
Write an ExampleRepeat to complete your function documentation
Import the fmt library in the repeat_test.go file and add the following code:
func ExampleRepeat() {
str := Repeat("a", 10)
fmt.Println(str)
//Output: aaaaaaaaaa
}
The execution go test -v
results are as follows:
Take a look at the strings package. Find functions you think might be useful and write some tests for them. Investing time in learning the standard library will slowly pay off.
By consulting the documentation, there are still quite a few functions in the strings package, here are the four functions Contains, Index, Fields, and Join. First create a new string_test.go file, imitate the previous test, and complete the code:
package iteration
import "testing"
import "fmt"
import "strings"
func TestString(t *testing.T) {
test1 := strings.Contains("abcde", "bc")
expected1 := true
if test1 != expected1 {
t.Errorf("expected '%v' but got '%v'", expected1, test1)
}
test2 := strings.Index("abcde", "bc")
expected2 := 1
if test2 != expected2 {
t.Errorf("expected '%v' but got '%v'", expected2, test2)
}
}
func ExampleString() {
str1 := strings.Fields("a b c")
fmt.Println(str1)
str2 := strings.Join([]string{
"a","b","c"}," ")
fmt.Println(str2)
/*Output:
[a b c]
a b c*/
}
Execute again to go test -v
get the following results:
Merge Sort TDD Practice Report
Write test first
Create a new mergeSort_test.go file and write a test:
package iteration
import "testing"
func TestSort(t *testing.T) {
arr := []int{
5,6,1,3,9,7,4,2,8,10}
MergeSort(arr, 0, 10)
expected := [10]int{
1,2,3,4,5,6,7,8,9,10}
for i:=0;i<10;i++ {
if arr[i] != expected[i] {
t.Errorf("expected '%v' but got '%v'", expected[i], arr[i])
}
}
}
Try to run the test
Execute go test
instructions.
Use the least amount of code first to make the failed test run first
Create a new mergeSort.go file and fill in the simplest code:
package iteration
func MergeSort(arr []int, l, r int) {
}
Execute the go test
instruction again and get the result:
Complete the code so that it can pass the test
Improve the MergeSort function, the code is as follows:
func MergeSort(arr []int, l, r int) {
if r==l+1 {
return
}
var mid int = (l+r)/2
MergeSort(arr, l, mid)
MergeSort(arr, mid, r)
arr_temp := [10]int{
0}
i, j, n := l, mid, 0
for i<mid && j<r {
if arr[i]<arr[j] {
arr_temp[n] = arr[i]
n++
i++
} else {
arr_temp[n] = arr[j]
n++
j++
}
}
for i<mid {
arr_temp[n] = arr[i]
n++
i++
}
for j<r {
arr_temp[n] = arr[j]
n++
j++
}
for i=0; i<n; i++ {
arr[l+i] = arr_temp[i]
}
}
Execute the go test
command again to pass the test:
Refactor
Change the original pointer transfer to value transfer, so that the iterative assignment step at the end of the original function can be omitted; at the same time, a return value can be added to the function, and the return value is directly the corresponding string.
First modify the original test code:
func TestSort(t *testing.T) {
arr := MergeSort([10]int{
5,6,1,3,9,7,4,2,8,10}, 0, 10)
expected := [10]int{
1,2,3,4,5,6,7,8,9,10}
for i:=0;i<10;i++ {
if arr[i] != expected[i] {
t.Errorf("expected '%v' but got '%v'", expected[i], arr[i])
}
}
}
Then modify the MergeSort function:
func MergeSort(arr [10]int, l, r int) [10]int {
if r==l+1 {
return arr
}
var mid int = (l+r)/2
arr = MergeSort(arr, l, mid)
arr = MergeSort(arr, mid, r)
arr_temp := arr
i, j, n := l, mid, 0
for i<mid && j<r {
if arr[i]<arr[j] {
arr_temp[l+n] = arr[i]
n++
i++
} else {
arr_temp[l+n] = arr[j]
n++
j++
}
}
for i<mid {
arr_temp[l+n] = arr[i]
n++
i++
}
for j<r {
arr_temp[l+n] = arr[j]
n++
j++
}
return arr_temp
}
Experiments show that if the parameter is []int, it is equivalent to pointer transfer; and if the parameter is [10]int, it is equivalent to value transfer. Therefore, the parameters are also different from the original function.
Executing the go test
instruction, the test can still pass:
Benchmarks
Add the test code in the mergeSort_test.go file:
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
MergeSort([10]int{
5,6,1,3,9,7,4,2,8,10}, 0, 10)
}
}
Perform go test -bench=.
benchmark test results: