Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add tests to go/mathutil #14969

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions go/mathutil/equivalence_relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,119 @@ func TestEquivalenceRelation(t *testing.T) {
})
}
}

func TestUnknownElementError(t *testing.T) {
err := &UnknownElementError{element: "test_element"}

assert.Equal(t, "unknown element test_element", err.Error())
mattlord marked this conversation as resolved.
Show resolved Hide resolved
}

func TestUnknownClassError(t *testing.T) {
err := &UnknownClassError{class: 42}

assert.Equal(t, "unknown class 42", err.Error())
}

func TestAdd(t *testing.T) {
r := NewEquivalenceRelation()
initialElements := []string{"a", "b", "c"}

for _, element := range initialElements {
r.Add(element)
}

for _, element := range initialElements {
class, err := r.ElementClass(element)
require.NoError(t, err)
assert.Contains(t, r.classElementsMap[class], element)
}

classCounter := r.classCounter
r.Add("a")
assert.Equal(t, classCounter, r.classCounter)
}

func TestElementClass(t *testing.T) {
r := NewEquivalenceRelation()
element := "test_element"

_, err := r.ElementClass(element)
assert.Error(t, err)

r.Add(element)
class, err := r.ElementClass(element)
require.NoError(t, err)
assert.Greater(t, class, -1)
}

func TestRelated(t *testing.T) {
type tt struct {
name string
relations []string
element1 string
element2 string
expect bool
err error
}

tests := []tt{
{
name: "related, same class",
relations: []string{"ab"},
element1: "a",
element2: "b",
expect: true,
err: nil,
},
{
name: "related, different classes",
relations: []string{"ab, cd"},
element1: "a",
element2: "c",
expect: false,
err: nil,
},
{
name: "related, unknown element",
relations: []string{"ab"},
element1: "x",
element2: "b",
expect: false,
err: &UnknownElementError{element: "x"},
},
{
name: "related, unknown element 2",
relations: []string{"ab"},
element1: "a",
element2: "y",
expect: false,
err: &UnknownElementError{element: "y"},
},
{
name: "related, both elements unknown",
relations: []string{"ab"},
element1: "x",
element2: "y",
expect: false,
err: &UnknownElementError{element: "x"},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := NewEquivalenceRelation()
r.AddAll([]string{"a", "b", "c", "d"})
for _, relation := range tc.relations {
_, err := r.Relate(relation[0:1], relation[1:2])
require.NoError(t, err)
}

result, err := r.Related(tc.element1, tc.element2)
if tc.err != nil {
assert.Error(t, err)
} else {
assert.Equal(t, tc.expect, result)
}
})
}
}
Loading