File: set_test.go

package info (click to toggle)
golang-github-xtgo-set 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,690 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
// Copyright 2015 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package set_test

import (
	"testing"

	"github.com/xtgo/set/internal/sliceset"
	"github.com/xtgo/set/internal/testdata"
)

func TestUniq(t *testing.T) {
	for _, tt := range testdata.UniqTests {
		s := sliceset.Set(tt.In).Copy()
		s = s.Uniq()

		if !testdata.IsEqual(s, tt.Out) {
			t.Errorf("Uniq(%v) = %v, want %v", tt.In, s, tt.Out)
		}
	}
}

func TestUnion(t *testing.T)   { testMut(t, "Union") }
func TestInter(t *testing.T)   { testMut(t, "Inter") }
func TestDiff(t *testing.T)    { testMut(t, "Diff") }
func TestSymDiff(t *testing.T) { testMut(t, "SymDiff") }
func TestIsSub(t *testing.T)   { testBool(t, "IsSub") }
func TestIsSuper(t *testing.T) { testBool(t, "IsSuper") }
func TestIsInter(t *testing.T) { testBool(t, "IsInter") }
func TestIsEqual(t *testing.T) { testBool(t, "IsEqual") }

const format = "%s(%v, %v) = %v, want %v"

type (
	mutOp  func(a, b sliceset.Set) sliceset.Set
	boolOp func(a, b sliceset.Set) bool
)

func testMut(t *testing.T, name string) {
	var op mutOp
	testdata.ConvMethod(&op, sliceset.Set(nil), name)

	for _, tt := range testdata.BinTests {
		a := sliceset.Set(tt.A).Copy()
		c := op(a, tt.B)
		want := tt.SelSlice(name)

		if !testdata.IsEqual(c, want) {
			t.Errorf(format, name, tt.A, tt.B, c, want)
		}
	}
}

func testBool(t *testing.T, name string) {
	var op boolOp
	testdata.ConvMethod(&op, sliceset.Set(nil), name)

	for _, tt := range testdata.BinTests {
		ok := op(tt.A, tt.B)
		want := tt.SelBool(name)

		if ok != want {
			t.Errorf(format, name, tt.A, tt.B, ok, want)
		}
	}
}