File: utils_test.go

package info (click to toggle)
golang-github-gatherstars-com-jwz 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,744 kB
  • sloc: makefile: 3
file content (172 lines) | stat: -rw-r--r-- 4,012 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package jwz

import (
	"fmt"
)

func ExampleSort() {

	// Emails := loadEmails() - your function to load emails into a slice
	//

	// Create a threader and thread using the slice of Threadable in the slice called Emails
	//
	threader := NewThreader()
	sliceRoot, err := threader.ThreadSlice(Emails)
	if err != nil {
		fmt.Printf("func ThreadSlice() error = %#v", err)
	}

	// Count with no dummies
	//
	var n, ns int
	Count(sliceRoot, &n)

	// Sort it Rodney!
	//
	x := Sort(sliceRoot, func(t1 Threadable, t2 Threadable) bool {

		// Sort by date, inline function
		//
		return t1.GetDate().Before(t2.GetDate())
	})

	Count(x, &ns)
	if n != ns {
		fmt.Printf("Count before sort: %d, Count after sort: %d\n", n, ns)
	}

	fmt.Printf("First node subject: %s\n", x.Subject())

	// Output: First node subject: 2002-02-01 05:44:14 +0000 UTC : Please help a newbie compile mplayer :-)
}

func ExampleWalk_depth() {

	// Emails := loadEmails() - your function to load emails into a slice
	//

	// Create a threader and thread using the slice of Threadable in the slice called Emails
	//
	threader := NewThreader()
	sliceRoot, err := threader.ThreadSlice(Emails)
	if err != nil {
		fmt.Printf("func ThreadSlice() error = %#v", err)
	}
	var c int
	_ = Walk(true, sliceRoot, func(t Threadable, u interface{}) (bool, error) {

		c := u.(*int)
		if !t.IsDummy() {
			*c++
		}
		return false, nil
	},
		&c)

	fmt.Printf("Walker walked %d depth first\n", c)

	// Output: Walker walked 2387 depth first
}

func ExampleWalk_breadth() {

	// Emails := loadEmails() - your function to load emails into a slice
	//

	// Create a threader and thread using the slice of Threadable in the slice called Emails
	//
	threader := NewThreader()
	sliceRoot, err := threader.ThreadSlice(Emails)
	if err != nil {
		fmt.Printf("func ThreadSlice() error = %#v", err)
	}
	var c int

	// Walk the tree breadth first and call our anonymous function on each Threadable
	//
	_ = Walk(false, sliceRoot, func(t Threadable, u interface{}) (bool, error) {

		c := u.(*int)
		if !t.IsDummy() {
			*c++
		}
		return false, nil
	},
		&c)

	fmt.Printf("Walker walked %d depth first\n", c)

	// Output: Walker walked 2387 depth first
}

type searcher struct {
	messageID string
	e         Threadable
}

func ExampleWalk_search() {

	// Emails := loadEmails() - your function to load emails into a slice
	//

	// Create a threader and thread using the slice of Threadable in the slice called Emails
	//
	threader := NewThreader()
	sliceRoot, err := threader.ThreadSlice(Emails)
	if err != nil {
		fmt.Printf("func ThreadSlice() error = %#v", err)
	}

	// Construct our search
	//
	var param = &searcher{
		messageID: "<008701c24a16$e3443830$0200a8c0@JMHALL>",
	}

	// Walk the tree breadth first and call our anonymous function on each Threadable
	//
	_ = Walk(false, sliceRoot,
		func(t Threadable, u interface{}) (bool, error) {

			params := u.(*searcher)
			if !t.IsDummy() {
				if t.MessageThreadID() == params.messageID {

					// We found the email we wanted, so we can stop here
					//
					params.e = t
					return true, nil
				}
			}
			return false, nil
		},
		param)

	fmt.Printf("Walker found the email %s with subject %s\n", param.messageID, param.e.Subject())

	// Walker found the email <008701c24a16$e3443830$0200a8c0@JMHALL> with subject 2002-08-22 20:02:45 +0000 UTC : Property rights in the 3rd World (De Soto's Mystery of Capital)
}

func ExampleCount() {

	// Emails := loadEmails() - your function to load emails into a slice
	//

	// Create a threader and thread using the slice of Threadable in the slice called Emails
	//
	threader := NewThreader()
	sliceRoot, err := threader.ThreadSlice(Emails)
	if err != nil {
		fmt.Printf("func ThreadSlice() error = %#v", err)
		return
	}

	// Find out how many non dummy Threadables are in the tree - in other words, how many
	// actual emails are there in the tree?
	//
	var nc int
	Count(sliceRoot, &nc)
	fmt.Printf("There are %d test emails", nc)
	// Output: There are 2387 test emails
}