Go 语言内置了测试包 testing,可以很方便的为函数编写测试方法

1. 引入 testing 包

import "testing"

2. 源代码文件

假设源代码文件_1_sorts/Sort.go

package _1_sorts

/*
冒泡排序、插入排序、选择排序
 */

//冒泡排序,a是数组,n表示数组大小
func BubbleSort(a []int, n int) {
    if n <= 1 {
        return
    }
    for i := 0; i < n; i++ {
        // 提前退出标志
        flag := false
        for j := 0; j < n-i-1; j++ {
            if a[j] > a[j+1] {
                a[j], a[j+1] = a[j+1], a[j]
                //此次冒泡有数据交换
                flag = true
            }
        }
        // 如果没有交换数据,提前退出
        if !flag {
            break
        }
    }
}

// 插入排序,a表示数组,n表示数组大小
func InsertionSort(a []int, n int) {
    if n <= 1 {
        return
    }
    for i := 1; i < n; i++ {
        value := a[i]
        j := i - 1
        //查找要插入的位置并移动数据
        for ; j >= 0; j-- {
            if a[j] > value {
                a[j+1] = a[j]
            } else {
                break
            }
        }
        a[j+1] = value
    }
}

// 选择排序,a表示数组,n表示数组大小
func SelectionSort(a []int, n int) {
    if n <= 1 {
        return
    }
    for i := 0; i < n; i++ {
        // 查找最小值
        minIndex := i
        for j := i + 1; j < n; j++ {
            if a[j] < a[minIndex] {
                minIndex = j
            }
        }
        // 交换
        a[i], a[minIndex] = a[minIndex], a[i]

    }
}

// 归并排序
func MergeSort(a []int, n int) {
    if n <= 1 {
        return
    }
    mergeSort(a, 0, n-1)
}

func mergeSort(a []int, start, end int) {
    if start >= end {
        return
    }
    mid := (start + end) / 2
    mergeSort(a, start, mid)
    mergeSort(a, mid+1, end)
    merge(a, start, mid, end)
}

func merge(a []int, start, mid, end int) {
    tmpArr := make([]int, end-start+1)

    i := start
    j := mid + 1
    k := 0

    for ; i <= mid && j <= end; k++ {
        if a[i] < a[j] {
            tmpArr[k] = a[i]
            i++
        } else {
            tmpArr[k] = a[j]
            j++
        }
    }

    for ; i <= mid; i++ {
        tmpArr[k] = a[i]
        k++
    }
    for ; j <= end; j++ {
        tmpArr[k] = a[j]
        j++
    }
    copy(a[start:end+1], tmpArr)
}

func QuickSort(a []int, n int) {
    separateSort(a, 0, n-1)
}

func separateSort(a []int, start, end int) {
    if start >= end {
        return
    }
    i := partition(a, start, end)
    separateSort(a, start, i-1)
    separateSort(a, i+1, end)
}

func partition(a []int, start, end int) int {
    // 选取最后一位当对比数字
    pivot := a[end]

    i := start
    for j := start; j < end; j++ {
        if a[j] < pivot {
            if !(i == j) {
                // 交换位置
                a[i], a[j] = a[j], a[i]
            }
            i++
        }
    }
    a[i], a[end] = a[end], a[i]
    return i
}

3. 测试代码文件

  • 测试文件_1_sorts/Sort_test.go文件名后缀必须为_test,文件名前半部分无要求,一般与被测源代码文件相同
  • 测试函数TestBubbleSort函数名必须以Test为前缀,函数名后半部分无要求
  • 测试函数参数必须为test *testing.T
package _1_sorts

import (
    "fmt"
    "math/rand"
    "testing"
)

func createRandomArr(len int) []int {
    arr := make([]int, len, len)
    for i := 0; i < len; i++ {
        arr[i] = rand.Intn(100)
    }
    return arr
}

func TestBubbleSort(t *testing.T) {
    arr := []int{1, 5, 9, 6, 3, 7, 5, 10}
    fmt.Println("排序前:", arr)
    BubbleSort(arr, len(arr))
    fmt.Println("排序后:", arr)
}

func TestInsertionSort(t *testing.T) {
    arr := []int{1, 5, 9, 6, 3, 7, 5, 10}
    fmt.Println("排序前:", arr)
    InsertionSort(arr, len(arr))
    fmt.Println("排序后:", arr)
}

func TestSelectionSort(t *testing.T) {
    arr := []int{1, 5, 9, 6, 3, 7, 5, 10}
    fmt.Println("排序前:", arr)
    SelectionSort(arr, len(arr))
    fmt.Println("排序后:", arr)
}

func TestMergeSort(t *testing.T) {
    a := []int{5, 4}
    MergeSort(a, len(a))
    t.Log(a)

    a = []int{5, 4, 3, 2, 1}
    MergeSort(a, len(a))
    t.Log(a)
}

func TestQuickSort(t *testing.T) {
    a := []int{5, 4}
    QuickSort(a, len(a))
    t.Log(a)

    a = createRandomArr(100)
    QuickSort(a, len(a))
    t.Log(a)
}

4. 运行结果

=== RUN   TestBubbleSort
排序前: [1 5 9 6 3 7 5 10]
排序后: [1 3 5 5 6 7 9 10]
--- PASS: TestBubbleSort (0.00s)
=== RUN   TestInsertionSort
排序前: [1 5 9 6 3 7 5 10]
排序后: [1 3 5 5 6 7 9 10]
--- PASS: TestInsertionSort (0.00s)
=== RUN   TestSelectionSort
排序前: [1 5 9 6 3 7 5 10]
排序后: [1 3 5 5 6 7 9 10]
--- PASS: TestSelectionSort (0.00s)
=== RUN   TestMergeSort
--- PASS: TestMergeSort (0.00s)
    Sort_test.go:41: [4 5]
    Sort_test.go:45: [1 2 3 4 5]
=== RUN   TestQuickSort
--- PASS: TestQuickSort (0.00s)
    Sort_test.go:51: [4 5]
    Sort_test.go:55: [0 0 2 2 2 3 3 5 5 5 6 7 8 10 11 11 13 15 18 18 20 21 23 24 25 26 28 28 28 29 31 31 33 33 33 36 37 37 37 38 40 40 41 41 43 43 45 46 46 47 47 47 47 47 51 52 53 53 55 56 56 56 57 58 59 59 59 61 62 63 63 63 66 66 74 76 77 78 78 81 81 83 85 87 87 87 88 88 89 89 90 90 91 94 94 94 95 96 98 99]
PASS

Process finished with exit code 0

参考文章

  1. Go语言测试框架(testing)用法 | CSDN
  2. Go testing 使用 | 简书