summaryrefslogtreecommitdiffstats
path: root/slice_test.go
blob: f8a1d0b37f100e1513fceb56198e37cf13217f9e (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
package share_test

import (
	"testing"

	"git.samanthony.xyz/share"
)

func TestConstSlice(t *testing.T) {
	orig := []string{"foo", "bar", "baz"}
	shared := share.NewConstSlice(orig)
	verifySame(shared, orig, t)
	go func() {
		defer shared.Close()
		verifySame(shared, orig, t)
	}()
}

func verifySame[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))
	}
}