File: sets_test.go

package info (click to toggle)
golang-github-tideland-golib 4.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: makefile: 4
file content (147 lines) | stat: -rw-r--r-- 3,598 bytes parent folder | download | duplicates (3)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Tideland Go Library - Collections - Sets - Unit Tests
//
// Copyright (C) 2015 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package collections_test

//--------------------
// IMPORTS
//--------------------

import (
	"errors"
	"testing"

	"github.com/tideland/golib/audit"
	"github.com/tideland/golib/collections"
)

//--------------------
// TESTS
//--------------------

// TestSetsAddRemove tests the core set methods.
func TestSetsAddRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewSet("foo", 42, true)
	assert.Length(set, 3)
	set.Add("foo", "bar", 123)
	assert.Length(set, 5)
	all := set.All()
	assert.Length(all, 5)
	set.Remove("yadda")
	assert.Length(set, 5)
	set.Remove("bar", 42)
	assert.Length(set, 3)
	set.Remove(false, "foo")
	assert.Length(set, 2)
	set.Deflate()
	assert.Length(set, 0)
}

// TestSetsFindAll tests the finding of set values.
func TestSetsFindAll(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewSet("foo", "bar", 42, true, "yadda", 12345)
	vs, err := set.FindAll(func(v interface{}) (bool, error) {
		switch v.(type) {
		case string:
			return true, nil
		default:
			return false, nil
		}
	})
	assert.Nil(err)
	assert.Length(vs, 3)

	vs, err = set.FindAll(func(v interface{}) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot find all matching values: ouch")
	assert.Length(vs, 0)
}

// TestSetsDoAll tests the iteration over set values.
func TestSetsDoAll(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewSet("foo", "bar", 42, true, "yadda", 12345)
	sl := 0
	err := set.DoAll(func(v interface{}) error {
		if s, ok := v.(string); ok {
			sl += len(s)
		}
		return nil
	})
	assert.Nil(err)
	assert.Equal(sl, 11)

	err = set.DoAll(func(v interface{}) error {
		return errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot perform function on all values: ouch")
}

// TestStringSetsAddRemove tests the core set methods.
func TestStringSetsAddRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewStringSet("foo", "42", "true")
	assert.Length(set, 3)
	set.Add("foo", "bar", "123")
	assert.Length(set, 5)
	all := set.All()
	assert.Length(all, 5)
	set.Remove("yadda")
	assert.Length(set, 5)
	set.Remove("bar", "42")
	assert.Length(set, 3)
	set.Remove("false", "foo")
	assert.Length(set, 2)
	set.Deflate()
	assert.Length(set, 0)
}

// TestStringSetsFindAll tests the finding of set values.
func TestStringSetsFindAll(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewStringSet("foo", "bar", "42", "true", "yadda", "12345")
	vs, err := set.FindAll(func(v string) (bool, error) {
		return len(v) == 3, nil
	})
	assert.Nil(err)
	assert.Length(vs, 2)

	vs, err = set.FindAll(func(v string) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot find all matching values: ouch")
	assert.Length(vs, 0)
}

// TestStringSetsDoAll tests the iteration over set values.
func TestStringSetsDoAll(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewStringSet("foo", "bar", "42", "true", "yadda", "12345")
	sl := 0
	err := set.DoAll(func(v string) error {
		sl += len(v)
		return nil
	})
	assert.Nil(err)
	assert.Equal(sl, 22)

	err = set.DoAll(func(v string) error {
		return errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot perform function on all values: ouch")
}

// EOF