1package util
2
3import (
4	"encoding/json"
5	"fmt"
6	"io/ioutil"
7	"log"
8	"os"
9	"path"
10	"time"
11)
12
13func ExampleBitMask() {
14	var b BitMask
15	b.Add("Justin")
16	b.Add("Naren|Mashiat|Derek|Dieter")
17	v, err := b.Parse("Justin|Derek|Naren")
18	if err != nil {
19		log.Fatal(err)
20	}
21	fmt.Printf("0x%08b\n", v)
22	fmt.Printf("%t %t %t\n", b.IsSet(v, "Justin"), b.IsSet(v, "Naren|Derek"), b.IsSet(v, "Mashiat"))
23	s, err := b.Format(v)
24	fmt.Println(s)
25	// Output: 0x00001011
26	// true true false
27	// justin|naren|derek
28}
29
30func ExampleCopyFile() {
31	s := "This is a test of the CopyFile() function."
32	t1 := path.Join(path.Dir(os.Args[0]), "test1.txt")
33	t2 := path.Join(path.Dir(os.Args[0]), "test2.txt")
34	err := ioutil.WriteFile(t1, []byte(s), os.FileMode(0666))
35	if err != nil {
36		log.Fatal(err)
37	}
38	err = CopyFile(t2, t1)
39	if err != nil {
40		log.Fatal(err)
41	}
42	b, err := ioutil.ReadFile(t2)
43	if err != nil {
44		log.Fatal(err)
45	}
46	err = os.Remove(t1)
47	if err != nil {
48		log.Fatal(err)
49	}
50	err = os.Remove(t2)
51	if err != nil {
52		log.Fatal(err)
53	}
54	fmt.Printf("%s\n", string(b))
55	// Output: This is a test of the CopyFile() function.
56}
57
58func ExampleStopWatch() {
59	var s StopWatch
60	s.Start()
61	time.Sleep(time.Duration(1 * time.Second))
62	s.Stop()
63	d := s.GetElapsed()
64	fmt.Printf("%d\n", Lrint(d.Seconds()))
65	s.Reset()
66	d = s.GetElapsed()
67	fmt.Printf("%d\n", Lrint(d.Seconds()))
68	// Output: 1
69	// 0
70}
71
72func ExampleGaussian() {
73	fmt.Println(Gaussian(0.0, 1.0))
74	fmt.Println(Gaussian(1.0, 1.0))
75	fmt.Println(Gaussian(-1.0, 0.5))
76	// Output: 1
77	// 0.6065306597126334
78	// 0.1353352832366127
79}
80
81func ExampleLrint() {
82	fmt.Println(Lrint(5.01))
83	fmt.Println(Lrint(4.99))
84	fmt.Println(Lrint(4.50))
85	fmt.Println(Lrint(5.50))
86	fmt.Println(Lrint(5.499999999999999))
87	fmt.Println(Lrint(5.4999999999999999))
88	fmt.Println(Lrint(-1.4))
89	// Output: 5
90	// 5
91	// 5
92	// 6
93	// 5
94	// 6
95	// -1
96}
97
98func ExampleClipDuration() {
99	min := time.Duration(5 * time.Second)
100	max := time.Duration(30 * time.Second)
101	fmt.Println(ClipDuration(3*time.Second, min, max))
102	fmt.Println(ClipDuration(10*time.Second, min, max))
103	fmt.Println(ClipDuration(50*time.Second, min, max))
104	// Output: 5s
105	// 10s
106	// 30s
107}
108
109func ExampleClipInt() {
110	min := 0
111	max := 10
112	fmt.Println(ClipInt(-1, min, max))
113	fmt.Println(ClipInt(5, min, max))
114	fmt.Println(ClipInt(15, min, max))
115	// Output: 0
116	// 5
117	// 10
118}
119
120func ExampleMinInt() {
121	fmt.Println(MinInt(5, 6))
122	fmt.Println(MinInt(100, -1))
123	fmt.Println(MinInt(10, 10))
124	// Output: 5
125	// -1
126	// 10
127}
128
129func ExampleMaxInt() {
130	fmt.Println(MaxInt(5, 6))
131	fmt.Println(MaxInt(-1, -100))
132	fmt.Println(MaxInt(10, 10))
133	// Output: 6
134	// -1
135	// 10
136}
137
138func ExampleTwoDimSplit() {
139	opts := TwoDimSplit("FirstName=Justin:LastName=Ruggles:EyeColor=Blue", ":", "=")
140	b, err := json.MarshalIndent(opts, "", "    ")
141	if err != nil {
142		log.Fatal(err)
143	}
144	fmt.Println(string(b))
145	// Output: {
146	//     "EyeColor": "Blue",
147	//     "FirstName": "Justin",
148	//     "LastName": "Ruggles"
149	// }
150}
151
152func ExampleQueue() {
153	q := NewQueue()
154	q.Add(1)
155	q.Add(2)
156	q.Add(3)
157	fmt.Printf("len=%d\n", q.Len())
158	v := q.Remove().(int)
159	fmt.Printf("%d\n", v)
160	fmt.Printf("len=%d\n", q.Len())
161	q.Clear()
162	fmt.Printf("len=%d\n", q.Len())
163	c := time.After(100 * time.Millisecond)
164	d := make(chan int)
165	go func() {
166		v := q.RemoveWait()
167		fmt.Printf("%v\n", v)
168		d <- 0
169	}()
170	<-c
171	q.Close()
172	<-d
173	// Output: len=3
174	// 1
175	// len=2
176	// len=0
177	// <nil>
178}
179
180func ExamplePriorityQueue() {
181	pq := NewPriorityQueueWithWaitLimit(2, 1)
182	pq.Add(5, 1)
183	pq.Add(1, 0)
184	pq.Add(2, 2)
185	pq.Add(3, 1)
186	pq.Add(4, 2)
187	fmt.Printf("len=%d\n", pq.Len())
188	v := pq.PeekP(0).(int)
189	fmt.Printf("%d\n", v)
190	v = pq.RemoveP(0).(int)
191	fmt.Printf("%d\n", v)
192	v = pq.Remove().(int)
193	fmt.Printf("%d\n", v)
194	v = pq.Remove().(int)
195	fmt.Printf("%d\n", v)
196	v = pq.Remove().(int)
197	fmt.Printf("%d\n", v)
198	v = pq.Peek().(int)
199	fmt.Printf("%d\n", v)
200	fmt.Printf("len=%d\n", pq.Len())
201	pq.Clear()
202	fmt.Printf("len=%d\n", pq.Len())
203	c := time.After(100 * time.Millisecond)
204	d := make(chan int)
205	go func() {
206		v := pq.RemoveWait()
207		fmt.Printf("%v\n", v)
208		d <- 0
209	}()
210	<-c
211	pq.Close()
212	<-d
213	// Output: len=5
214	// 1
215	// 1
216	// 2
217	// 5
218	// 4
219	// 3
220	// len=1
221	// len=0
222	// <nil>
223}
224