summaryrefslogtreecommitdiff
path: root/each_test.go
blob: f64e55bd3376b08b6262276194d70e11b1302219 (plain) (blame)
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
package collection

import (
	"sync/atomic"
	"testing"
)

func TestEachSync(t *testing.T) {
	collection := []int{1, 2, 3, 4, 5, 6}
	want := 21
	sum := 0
	EachSync(collection, func(v int, _ int) {
		sum += v
	})
	if sum != want {
		t.Errorf("Expected %d, got %d", want, sum)
	}
}

func TestEach(t *testing.T) {
	collection := []int{1, 2, 3, 4, 5, 6}
	want := int64(21)
	sum := int64(0)
	Each(collection, func(v int, _ int) {
		atomic.AddInt64(&sum, int64(v))
	})
	if sum != want {
		t.Errorf("Expected %d, got %d", want, sum)
	}
}