-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"runtime" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
var goroutineSpace = []byte("goroutine ") | ||
|
||
func curGoroutineID() uint64 { | ||
b := make([]byte, 64) | ||
b = b[:runtime.Stack(b, false)] | ||
// Parse the 4707 out of "goroutine 4707 [" | ||
b = bytes.TrimPrefix(b, goroutineSpace) | ||
i := bytes.IndexByte(b, ' ') | ||
if i < 0 { | ||
panic(fmt.Sprintf("No space found in %q", b)) | ||
} | ||
b = b[:i] | ||
n, err := strconv.ParseUint(string(b), 10, 64) | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err)) | ||
} | ||
return n | ||
} | ||
|
||
// 被测代码 | ||
func Add(a, b int) int { | ||
return a + b | ||
} | ||
|
||
func TestAddWithSubtest(t *testing.T) { | ||
//t.Log("g:", curGoroutineID()) | ||
cases := []struct { | ||
name string | ||
a int | ||
b int | ||
r int | ||
}{ | ||
{"2+3", 2, 3, 5}, | ||
{"2+0", 2, 0, 2}, | ||
{"2+(-2)", 2, -2, 0}, | ||
//... ... | ||
} | ||
|
||
for _, caze := range cases { | ||
t.Run(caze.name, func(t *testing.T) { | ||
t.Log("g:", curGoroutineID()) | ||
got := Add(caze.a, caze.b) | ||
if got != caze.r { | ||
t.Errorf("got %d, want %d", got, caze.r) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"runtime" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
var goroutineSpace = []byte("goroutine ") | ||
|
||
func curGoroutineID() uint64 { | ||
b := make([]byte, 64) | ||
b = b[:runtime.Stack(b, false)] | ||
// Parse the 4707 out of "goroutine 4707 [" | ||
b = bytes.TrimPrefix(b, goroutineSpace) | ||
i := bytes.IndexByte(b, ' ') | ||
if i < 0 { | ||
panic(fmt.Sprintf("No space found in %q", b)) | ||
} | ||
b = b[:i] | ||
n, err := strconv.ParseUint(string(b), 10, 64) | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err)) | ||
} | ||
return n | ||
} | ||
|
||
// 被测代码 | ||
func Add(a, b int) int { | ||
return a + b | ||
} | ||
|
||
// 测试代码 | ||
func TestAdd(t *testing.T) { | ||
t.Log("g:", curGoroutineID()) | ||
t.Parallel() | ||
got := Add(2, 3) | ||
if got != 5 { | ||
t.Errorf("Add(2, 3) got %d, want 5", got) | ||
} | ||
} | ||
|
||
func TestAddZero(t *testing.T) { | ||
t.Parallel() | ||
got := Add(2, 0) | ||
if got != 2 { | ||
t.Fatalf("Add(2, 0) got %d, want 2", got) | ||
} | ||
} | ||
|
||
func TestAddOppositeNum(t *testing.T) { | ||
got := Add(2, -2) | ||
t.Parallel() | ||
if got != 0 { | ||
t.Errorf("Add(2, -2) got %d, want 0", got) | ||
} | ||
} | ||
|
||
func TestAddWithTable(t *testing.T) { | ||
t.Log("g:", curGoroutineID()) | ||
t.Parallel() | ||
cases := []struct { | ||
name string | ||
a int | ||
b int | ||
r int | ||
}{ | ||
{"2+3", 2, 3, 5}, | ||
{"2+0", 2, 0, 2}, | ||
{"2+(-2)", 2, -2, 0}, | ||
//... ... | ||
} | ||
|
||
for _, caze := range cases { | ||
got := Add(caze.a, caze.b) | ||
if got != caze.r { | ||
t.Errorf("%s got %d, want %d", caze.name, got, caze.r) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module demo | ||
|
||
go 1.20 |