天天看點

go 單元測試和benchmark測試

Go語言的表格驅動測試

main.go

package main

import (
    "fmt"
)

func isOneBitCharacter(bits []int) bool {

    var result bool

    var idx int

    for idx =; idx < len(bits); {

        if bits[idx] == {
            idx++
            result = true
        } else {
            idx = idx +
            result = false
        }
    }

    return result
}

func main() {

    var bits = []int,,,}
    fmt.Println(isOneBitCharacter(bits))
}
           

main_test.go

package main

import "testing"

func Benchmark_isOneBitCharacter(b *testing.B) {

    type args struct {
        bits []int
    }
    tests := struct {
        name string
        args args
        want bool /*預期結果*/
    }{"1", args{[]int,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}}, false}

    for idx :=; idx < b.N; idx++ {
        b.Run(tests.name, func(b *testing.B) {
            if got := isOneBitCharacter(tests.args.bits); got != tests.want {
                b.Errorf("isOneBitCharacter() = %v, want %v", got, tests.want)
            }
        })
    }

}

func Test_isOneBitCharacter(t *testing.T) {
    type args struct {
        bits []int
    }
    tests := []struct {
        name string
        args args
        want bool /*預期結果*/
    }{
        // TODO: Add test cases.
        {"1", args{[]int,,,}}, false},
        {"2", args{[]int,,,}}, true},
        {"3", args{[]int,,}}, true},
        {"4", args{[]int{}}, false},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := isOneBitCharacter(tt.args.bits); got != tt.want {
                t.Errorf("isOneBitCharacter() = %v, want %v", got, tt.want)
            }
        })
    }
}
           

引用

https://blog.csdn.net/x356982611/article/details/80443252