From d2d580da0acd6e4ad678e7d3676c99316a0da4bc Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 16 Jan 2024 21:58:17 +0530 Subject: [PATCH 1/4] tests: add tests to go/mathutil Signed-off-by: Manik Rana --- go/mathutil/equivalence_relation_test.go | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/go/mathutil/equivalence_relation_test.go b/go/mathutil/equivalence_relation_test.go index 3873562a080..b56df2ff551 100644 --- a/go/mathutil/equivalence_relation_test.go +++ b/go/mathutil/equivalence_relation_test.go @@ -174,3 +174,47 @@ func TestEquivalenceRelation(t *testing.T) { }) } } + +func TestUnknownElementError(t *testing.T) { + err := &UnknownElementError{element: "test_element"} + + assert.Equal(t, "unknown element test_element", err.Error()) +} + +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) +} From 6f70778747c50a0e5d1780bf48b28f574bf457a9 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 16 Jan 2024 23:15:12 +0530 Subject: [PATCH 2/4] tests: increase coverage to 95.2% Signed-off-by: Manik Rana --- go/mathutil/equivalence_relation_test.go | 72 ++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/go/mathutil/equivalence_relation_test.go b/go/mathutil/equivalence_relation_test.go index b56df2ff551..048ef2e42e9 100644 --- a/go/mathutil/equivalence_relation_test.go +++ b/go/mathutil/equivalence_relation_test.go @@ -218,3 +218,75 @@ func TestElementClass(t *testing.T) { 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) + } + }) + } +} From 13e90db9fdbe53204712289750115e581c1ebd57 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Tue, 16 Jan 2024 23:39:14 +0530 Subject: [PATCH 3/4] refactor: use assert.EqualError Signed-off-by: Manik Rana --- go/mathutil/equivalence_relation_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go/mathutil/equivalence_relation_test.go b/go/mathutil/equivalence_relation_test.go index 048ef2e42e9..13d812836c2 100644 --- a/go/mathutil/equivalence_relation_test.go +++ b/go/mathutil/equivalence_relation_test.go @@ -178,13 +178,13 @@ func TestEquivalenceRelation(t *testing.T) { func TestUnknownElementError(t *testing.T) { err := &UnknownElementError{element: "test_element"} - assert.Equal(t, "unknown element test_element", err.Error()) + assert.EqualError(t, err, "unknown element test_element") } func TestUnknownClassError(t *testing.T) { err := &UnknownClassError{class: 42} - assert.Equal(t, "unknown class 42", err.Error()) + assert.EqualError(t, err, "unknown class 42") } func TestAdd(t *testing.T) { @@ -283,7 +283,7 @@ func TestRelated(t *testing.T) { result, err := r.Related(tc.element1, tc.element2) if tc.err != nil { - assert.Error(t, err) + assert.EqualError(t, err, tc.err.Error()) } else { assert.Equal(t, tc.expect, result) } From 5110db04e84b6338a5740716e932241105ff6cc1 Mon Sep 17 00:00:00 2001 From: Manik Rana Date: Wed, 17 Jan 2024 11:47:18 +0530 Subject: [PATCH 4/4] tests: achieve 100% coverage for go/mathutil Signed-off-by: Manik Rana --- go/mathutil/equivalence_relation_test.go | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/go/mathutil/equivalence_relation_test.go b/go/mathutil/equivalence_relation_test.go index 13d812836c2..754e9f792ab 100644 --- a/go/mathutil/equivalence_relation_test.go +++ b/go/mathutil/equivalence_relation_test.go @@ -175,6 +175,41 @@ func TestEquivalenceRelation(t *testing.T) { } } +func TestEquivalenceRelationError(t *testing.T) { + type ttError struct { + name string + element1 string + element2 string + expectedErr string + } + + testsRelateErrorCases := []ttError{ + { + name: "UnknownElementError", + element1: "x", + element2: "b", + expectedErr: "unknown element x", + }, + { + name: "UnknownClassError", + element1: "a", + element2: "y", + expectedErr: "unknown element y", + }, + } + + for _, tc := range testsRelateErrorCases { + t.Run(tc.name, func(t *testing.T) { + r := NewEquivalenceRelation() + r.AddAll([]string{"a", "b", "c"}) + + _, err := r.Relate(tc.element1, tc.element2) + assert.Error(t, err) + assert.EqualError(t, err, tc.expectedErr) + }) + } +} + func TestUnknownElementError(t *testing.T) { err := &UnknownElementError{element: "test_element"}