-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_test.go
120 lines (106 loc) · 3.36 KB
/
profile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2014 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package util
import (
"testing"
"time"
)
func TestSortByName(t *testing.T) {
Prof.data["A"] = ProfileEntry{Calls: 1, Tottime: time.Duration(5)}
Prof.data["B"] = ProfileEntry{Calls: 1, Tottime: time.Duration(4)}
Prof.data["C"] = ProfileEntry{Calls: 1, Tottime: time.Duration(3)}
Prof.data["D"] = ProfileEntry{Calls: 1, Tottime: time.Duration(2)}
results := Prof.SortByName()
if results[0].Name != "A" {
t.Errorf("TestSortByName expected A, but got %s", results[0].Name)
}
if results[1].Name != "B" {
t.Errorf("TestSortByName expected B, but got %s", results[1].Name)
}
if results[2].Name != "C" {
t.Errorf("TestSortByName expected C, but got %s", results[2].Name)
}
if results[3].Name != "D" {
t.Errorf("TestSortByName expected D, but got %s", results[3].Name)
}
}
func TestSortByTime(t *testing.T) {
Prof.data["A"] = ProfileEntry{Calls: 1, Tottime: time.Duration(1)}
Prof.data["C"] = ProfileEntry{Calls: 1, Tottime: time.Duration(2)}
Prof.data["D"] = ProfileEntry{Calls: 1, Tottime: time.Duration(3)}
Prof.data["B"] = ProfileEntry{Calls: 1, Tottime: time.Duration(4)}
results := Prof.SortByTotalTime()
if results[0].Name != "A" {
t.Errorf("TestSortByTime expected A, but got %s", results[0].Name)
}
if results[1].Name != "C" {
t.Errorf("TestSortByTime expected C, but got %s", results[1].Name)
}
if results[2].Name != "D" {
t.Errorf("TestSortByTime expected D, but got %s", results[2].Name)
}
if results[3].Name != "B" {
t.Errorf("TestSortByTime expected B, but got %s", results[3].Name)
}
}
func TestSortByAvgTime(t *testing.T) {
Prof.data["A"] = ProfileEntry{Calls: 1, Tottime: time.Duration(1)}
Prof.data["C"] = ProfileEntry{Calls: 1, Tottime: time.Duration(2)}
Prof.data["D"] = ProfileEntry{Calls: 1, Tottime: time.Duration(3)}
Prof.data["B"] = ProfileEntry{Calls: 1, Tottime: time.Duration(4)}
results := Prof.SortByAvgTime()
if results[0].Name != "A" {
t.Errorf("TestSortByTime expected A, but got %s", results[0].Name)
}
if results[1].Name != "C" {
t.Errorf("TestSortByTime expected C, but got %s", results[1].Name)
}
if results[2].Name != "D" {
t.Errorf("TestSortByTime expected D, but got %s", results[2].Name)
}
if results[3].Name != "B" {
t.Errorf("TestSortByTime expected B, but got %s", results[3].Name)
}
}
func TestEnter(t *testing.T) {
profiler := &Profiler{}
pt := profiler.Enter("some name")
if &pt == nil {
t.Errorf("Returned a nil ProfileToken")
}
}
func TestExit(t *testing.T) {
ptoken := &ProfToken{}
ptoken.Exit()
}
func TestLess(t *testing.T) {
ps := &prsorter{nil, func(i, j int) bool {
return i < j
}}
result := ps.Less(4, 5)
if !result {
t.Errorf("Less returned incorrect result for 4 < 5")
}
}
func TestLen(t *testing.T) {
ps := &prsorter{nil, func(i, j int) bool {
return i < j
}}
if ps.Len() != 0 {
t.Errorf("Incorrect length of Data (nil data should be 0 len)")
}
}
func TestSwap(t *testing.T) {
pr0 := &ProfileResult{"pr1", *&ProfileEntry{}}
pr1 := &ProfileResult{"pr2", *&ProfileEntry{}}
prList := []ProfileResult{*pr0, *pr1}
ps := &prsorter{prList, func(i, j int) bool { return i < j }}
ps.Swap(0, 1)
if ps.data[0] != *pr1 || ps.data[1] != *pr0 {
t.Errorf("prsorter swapped incorrectly!")
}
}
func TestString(t *testing.T) {
Prof.String()
}