-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitrate_example_test.go
151 lines (132 loc) · 3.94 KB
/
bitrate_example_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2020 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package infounit_test
import (
"fmt"
"github.com/tunabay/go-infounit"
)
//
func ExampleBitRate_Convert_siPrefix() {
x := infounit.BitRate(123456789.0)
fmt.Printf("%f\n", x.Convert(infounit.KilobitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.MegabitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.GigabitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.TerabitPerSecond))
// Output:
// 123456.789000
// 123.456789
// 0.123457
// 0.000123
}
//
func ExampleBitRate_Convert_binaryPrefix() {
x := infounit.BitRate(123456789.0)
fmt.Printf("%f\n", x.Convert(infounit.KibibitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.MebibitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.GibibitPerSecond))
fmt.Printf("%f\n", x.Convert(infounit.TebibitPerSecond))
// Output:
// 120563.270508
// 117.737569
// 0.114978
// 0.000112
}
//
func ExampleBitRate_ConvertRound_siPrefix() {
x := infounit.BitRate(123456789.0)
fmt.Printf("%f\n", x.ConvertRound(infounit.KilobitPerSecond, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.KilobitPerSecond, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.MegabitPerSecond, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.MegabitPerSecond, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.TerabitPerSecond, 6))
// Output:
// 123456.790000
// 123456.789000
// 123.000000
// 123.457000
// 0.000123
}
//
func ExampleBitRate_ConvertRound_binaryPrefix() {
x := infounit.BitRate(123456789.0)
fmt.Printf("%f\n", x.ConvertRound(infounit.KibibitPerSecond, 2))
fmt.Printf("%f\n", x.ConvertRound(infounit.KibibitPerSecond, 5))
fmt.Printf("%f\n", x.ConvertRound(infounit.MebibitPerSecond, 0))
fmt.Printf("%f\n", x.ConvertRound(infounit.MebibitPerSecond, 3))
fmt.Printf("%f\n", x.ConvertRound(infounit.TebibitPerSecond, 6))
// Output:
// 120563.270000
// 120563.270510
// 118.000000
// 117.738000
// 0.000112
}
//
func ExampleBitRate_Format_errorf() {
limit := infounit.KilobitPerSecond * 100
check := func(rate infounit.BitRate) error {
if limit < rate {
return fmt.Errorf("too fast: %v", rate)
}
return nil
}
for i := 0; i < 8; i++ {
rate := infounit.BitRate(uint64(1 << (6 * i)))
if err := check(rate); err != nil {
fmt.Println("error:", err)
continue
}
fmt.Println("rate:", rate)
}
// Output:
// rate: 1.0 bit/s
// rate: 64.0 bit/s
// rate: 4.1 kbit/s
// error: too fast: 262.1 kbit/s
// error: too fast: 16.8 Mbit/s
// error: too fast: 1.1 Gbit/s
// error: too fast: 68.7 Gbit/s
// error: too fast: 4.4 Tbit/s
}
//
func ExampleBitRate_Format_printf() {
x := infounit.KilobitPerSecond * 123.456 // 123.456 kbit/s
// Default format
fmt.Printf("%v\n", x) // default format, same as "% .1s"
fmt.Printf("%#v\n", x) // Go syntax format
// SI prefix
fmt.Printf("%s\n", x) // default precision
fmt.Printf("% s\n", x) // with space
fmt.Printf("%014.2s\n", x) // zero padding, width 14, precision 2
fmt.Printf("%#.2s\n", x) // long unit
fmt.Printf("%# .2s\n", x) // long unit, with space
fmt.Printf("[%16s]\n", x) // width 16
fmt.Printf("[%-16s]\n", x) // width 16, left-aligned
fmt.Printf("% a\n", x) // non-standard abbreviation "bps"
// Binary prefix
fmt.Printf("%S\n", x) // default precision
fmt.Printf("% S\n", x) // with space
fmt.Printf("%.1S\n", x) // precision 1
fmt.Printf("%# .2S\n", x) // precision 2, long unit, with space
fmt.Printf("% A\n", x) // non-standard abbreviation "bps"
// Float
fmt.Printf("%f\n", x) // floating point value in bit/s
// Output:
// 123.5 kbit/s
// BitRate(123456)
// 123.456kbit/s
// 123.456 kbit/s
// 00123.46kbit/s
// 123.46kilobits per second
// 123.46 kilobits per second
// [ 123.456kbit/s]
// [123.456kbit/s ]
// 123.456 kbps
// 120.5625Kibit/s
// 120.5625 Kibit/s
// 120.6Kibit/s
// 120.56 kibibits per second
// 120.5625 Kibps
// 123456.000000
}