Beispiel #1
0
func main() {
	rand.Seed(time.Now().UnixNano())
	queue := make(chan points.Points) //This channel will be used to send an endless stream of points

	k := 2                        // K closest poins
	go points.Generate(queue, 20) // Points generator. It will work 20 milliseconds and exit.

	res := points.Reduce(queue, k) // Generates the result
	fmt.Printf("Result: %+v\n", res)
}
Beispiel #2
0
func TestReduce(t *testing.T) {
	expected := []points.Points{{2, 2, 8}, {1, 2, 5}}
	queue := make(chan points.Points)
	k := 2
	go func() {
		queue <- points.Points{2, 2, 0}
		queue <- points.Points{2, 3, 0}
		queue <- points.Points{1, 2, 0}
		close(queue)

	}()
	res := points.Reduce(queue, k)
	assert(expected, res, k, t)

	expected = []points.Points{{2, 2, 8}, {2, 3, 13}, {10, 2, 104}, {0, 0, 0}}
	queue = make(chan points.Points)
	k = 4
	go func() {
		queue <- points.Points{2, 2, 0}
		queue <- points.Points{2, 3, 0}
		queue <- points.Points{10, 2, 0}
		queue <- points.Points{1, 20, 0}
		queue <- points.Points{100, 22, 0}
		queue <- points.Points{11, 2, 0}
		queue <- points.Points{1100, 2, 0}
		queue <- points.Points{0, 0, 0}
		close(queue)

	}()
	res = points.Reduce(queue, k)
	assert(expected, res, k, t)

	// his method does not work with slice of structs for some reason
	// if !reflect.DeepEqual(res, expected) {
	// 	t.Error("Expected: ", expected, " Got:", res)
	// }

}