summaryrefslogtreecommitdiffstats
path: root/slice_test.go
blob: af44eafa07d2bfae9828d51307d736cb6c8c6a94 (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
31
32
33
34
35
36
37
38
39
40
41
42
package share_test

import (
	"sync"
	"testing"

	"github.com/sam-rba/share"
)

func TestConstSlice(t *testing.T) {
	orig := []string{"foo", "bar", "baz"}

	shared := share.NewConstSlice(orig)
	defer shared.Close()

	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		verifySameSlice(shared, orig, t)
		wg.Done()
	}()
	go func() {
		verifySameSlice(shared, orig, t)
		wg.Done()
	}()
	wg.Wait()
}

func verifySameSlice[T comparable](cs share.ConstSlice[T], s []T, t *testing.T) {
	i := 0
	for elem := range cs.Elems() {
		if i < len(s) {
			if elem != s[i] {
				t.Errorf("ConstSlice[%d] = %v; expected %v", i, elem, s[i])
			}
		}
		i++
	}
	if i != len(s) {
		t.Errorf("ConstSlice.Elems() returned %d elements; expected %d", i, len(s))
	}
}