|
1 | 1 | package Solution
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "reflect"
|
| 6 | + "runtime" |
| 7 | + "strings" |
5 | 8 | "testing"
|
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
6 | 11 | )
|
7 | 12 |
|
8 |
| -func TestSolution(t *testing.T) { |
9 |
| - // 测试用例 |
10 |
| - cases := []struct { |
11 |
| - name string |
12 |
| - inputs string |
13 |
| - expect int |
14 |
| - }{ |
15 |
| - {"1 test 1", "abcabcbb!", 3}, |
16 |
| - {"2 test 2", "bbbbb", 1}, |
17 |
| - {"3 test 3", "pwwkew", 3}, |
18 |
| - } |
| 13 | +// Solution func Info |
| 14 | +type SolutionFuncType func(string) int |
19 | 15 |
|
20 |
| - // 开始测试 |
21 |
| - for _, c := range cases { |
22 |
| - t.Run(c.name, func(t *testing.T) { |
23 |
| - ret := lengthOfLongestSubstring(c.inputs) |
24 |
| - if !reflect.DeepEqual(ret, c.expect) { |
25 |
| - t.Fatalf("expected: %v, but got: %v, with inputs: %v", |
26 |
| - c.expect, ret, c.inputs) |
27 |
| - } |
28 |
| - }) |
29 |
| - } |
| 16 | +var SolutionFuncList = []SolutionFuncType{ |
| 17 | + lengthOfLongestSubstring_1, |
| 18 | + lengthOfLongestSubstring_2, |
30 | 19 | }
|
31 | 20 |
|
32 |
| -func TestSolution2(t *testing.T) { |
33 |
| - // 测试用例 |
34 |
| - cases := []struct { |
35 |
| - name string |
36 |
| - inputs string |
37 |
| - expect int |
38 |
| - }{ |
39 |
| - {"1 test 1", "abcabcbb!", 3}, |
40 |
| - {"2 test 2", "bbbbb", 1}, |
41 |
| - {"3 test 3", "pwwkew", 3}, |
42 |
| - } |
43 |
| - |
44 |
| - // 开始测试 |
45 |
| - for _, c := range cases { |
46 |
| - t.Run(c.name, func(t *testing.T) { |
47 |
| - ret := lengthOfLongestSubstring2(c.inputs) |
48 |
| - if !reflect.DeepEqual(ret, c.expect) { |
49 |
| - t.Fatalf("expected: %v, but got: %v, with inputs: %v", |
50 |
| - c.expect, ret, c.inputs) |
51 |
| - } |
52 |
| - }) |
53 |
| - } |
| 21 | +// Test case info struct |
| 22 | +type Case struct { |
| 23 | + name string |
| 24 | + input string |
| 25 | + expect int |
54 | 26 | }
|
55 | 27 |
|
56 |
| -const N = 1000 |
57 |
| - |
58 |
| -func BenchmarkMax1(b *testing.B) { |
59 |
| - // b.N = 10000 |
60 |
| - // 测试用例 |
61 |
| - cases := []struct { |
62 |
| - name string |
63 |
| - inputs string |
64 |
| - expect int |
65 |
| - }{ |
66 |
| - {"1 test 1", "abcabcbb!", 3}, |
67 |
| - {"2 test 2", "bbbbb", 1}, |
68 |
| - {"3 test 3", "pwwkew", 3}, |
69 |
| - } |
70 |
| - b.ResetTimer() |
71 |
| - for i := 0; i < b.N; i++ { |
72 |
| - lengthOfLongestSubstring(cases[0].inputs) |
73 |
| - lengthOfLongestSubstring(cases[1].inputs) |
74 |
| - lengthOfLongestSubstring(cases[2].inputs) |
75 |
| - } |
| 28 | +// Test case |
| 29 | +var cases = []Case{ |
| 30 | + {name: "TestCase 1", input: "abcabcbb", expect: 3}, |
| 31 | + {name: "TestCase 2", input: "bbbbb", expect: 1}, |
| 32 | + {name: "TestCase 3", input: "pwwkew", expect: 3}, |
76 | 33 | }
|
77 | 34 |
|
78 |
| -func BenchmarkMax2(b *testing.B) { |
79 |
| - // b.N = 10000 |
80 |
| - // 测试用例 |
81 |
| - cases := []struct { |
82 |
| - name string |
83 |
| - inputs string |
84 |
| - expect int |
85 |
| - }{ |
86 |
| - {"1 test 1", "abcabcbb!", 3}, |
87 |
| - {"2 test 2", "bbbbb", 1}, |
88 |
| - {"3 test 3", "pwwkew", 3}, |
89 |
| - } |
90 |
| - b.ResetTimer() |
91 |
| - for i := 0; i < b.N; i++ { |
92 |
| - lengthOfLongestSubstring2(cases[0].inputs) |
93 |
| - lengthOfLongestSubstring2(cases[1].inputs) |
94 |
| - lengthOfLongestSubstring2(cases[2].inputs) |
| 35 | +// TestSolution Run test case for all solutions |
| 36 | +func TestSolution(t *testing.T) { |
| 37 | + ast := assert.New(t) |
| 38 | + |
| 39 | + for _, f := range SolutionFuncList { |
| 40 | + funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1] |
| 41 | + for _, c := range cases { |
| 42 | + t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) { |
| 43 | + got := f(c.input) |
| 44 | + ast.Equal(c.expect, got, |
| 45 | + "func: %v case: %v ", funcName, c.name) |
| 46 | + }) |
| 47 | + } |
95 | 48 | }
|
96 | 49 | }
|
0 commit comments