-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc_test.go
102 lines (87 loc) · 2.02 KB
/
misc_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
package gota
import (
"bytes"
"io"
"testing"
"time"
)
func TestStatistic_AddSentBytes(t *testing.T) {
s := &Statistic{}
sent := uint64(1024)
s.AddSentBytes(sent)
if s.SentBytes != 1024 {
t.Errorf("Error SentBytes: %d", s.SentBytes)
}
}
func TestStatistic_AddReceivedBytes(t *testing.T) {
s := &Statistic{}
sent := uint64(1024)
s.AddReceivedBytes(sent)
if s.ReceivedBytes != 1024 {
t.Errorf("Error ReceivedBytes: %d", s.ReceivedBytes)
}
}
func TestStatistic_SendSpeedSecond(t *testing.T) {
s := NewStatistic(3)
s.AddSentBytes(10)
s.AddSentBytes(10)
time.Sleep(1 * time.Second)
s.AddSentBytes(20)
s.AddSentBytes(10)
time.Sleep(1 * time.Second)
s.AddSentBytes(30)
s.AddSentBytes(10)
ss := s.SendSpeedSecond(3)
if ss != 30 {
t.Errorf("Error speed: %d", ss)
}
}
func TestStatistic_ReceiveSpeedSecond(t *testing.T) {
s := NewStatistic(3)
s.AddReceivedBytes(10)
s.AddReceivedBytes(10)
time.Sleep(1 * time.Second)
s.AddReceivedBytes(20)
s.AddReceivedBytes(10)
time.Sleep(1 * time.Second)
s.AddReceivedBytes(30)
s.AddReceivedBytes(10)
ss := s.ReceiveSpeedSecond(3)
if ss != 30 {
t.Errorf("Error speed: %d", ss)
}
}
func TestReadNBytes(t *testing.T) {
str := "Hello world, I'm Jim"
lens := 20
var testBuf bytes.Buffer
testBuf.Write([]byte(str))
full, err := ReadNBytes(&testBuf, lens)
if err != nil && err != io.EOF {
t.Errorf("ReadNBytes Error: %s", err)
}
if string(full) != str {
t.Error("ReadNBytes Error, different string")
}
testBuf.Write([]byte(str))
partial, err := ReadNBytes(&testBuf, 5)
t.Logf("%s", partial)
if err != nil {
t.Errorf("ReadNBytes Error: %s", err)
}
if string(partial) != str[:5] {
t.Error("ReadNBytes Error, different string")
}
}
func TestWriteNBytes(t *testing.T) {
str := "Hello world, I'm Jim"
lens := 20
var testWrite bytes.Buffer
err := WriteNBytes(&testWrite, lens, []byte(str))
if err != nil && err != io.EOF {
t.Errorf("WriteNBytes Error: %s", err)
}
if string(testWrite.Bytes()) != str {
t.Errorf("WriteNBytes Error, different string")
}
}