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

Feature/add var error #8

Merged
merged 2 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import (
"fmt"
)

var (
ErrorLackingPrefers = errors.New("GaleShapley: Lack of prefers")
)

// Extend Gale-Shapley algorithm for incomplete lists
// Args: each person's preference order
// Returns: stable matching
func GaleShapley(msPrefers, wsPrefers [][]int) (map[int]int, error) {
// Check for lack of participants
if len(msPrefers) == 0 || len(wsPrefers) == 0 {
return nil, errors.New("GaleShapley: Lack of participants")
return nil, ErrorLackingPrefers
}
// Create a list of unmatched men
msUnpaired := make([]int, len(msPrefers))
Expand Down
19 changes: 11 additions & 8 deletions matching_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package matching_test

import (
"errors"
"fmt"
"testing"

"github.com/yutohub/matching"
)

func TestGS4x4(t *testing.T) {
func TestGaleShapley4x4(t *testing.T) {
msPrefers := [][]int{
{0, 1, 2, 3},
{3, 0, 1, 2},
Expand All @@ -33,7 +34,7 @@ func TestGS4x4(t *testing.T) {
}
}

func TestGS3x3(t *testing.T) {
func TestGaleShapley4x3(t *testing.T) {
msPrefers := [][]int{
{2, 3, 1},
{2, 1, 3},
Expand All @@ -59,28 +60,30 @@ func TestGS3x3(t *testing.T) {
}
}

func TestGSLessMsPrefers(t *testing.T) {
func TestGaleShapleyLackingMsPrefers(t *testing.T) {
msPrefers := [][]int{}
wsPrefers := [][]int{{0}, {1}, {3}, {2}}
result, err := matching.GaleShapley(msPrefers, wsPrefers)
fmt.Printf("TestGSLessMsPrefers: %s\n", err)
if result != nil {
t.Error("ERROR: Expect an error and no result returned")
}
if err == nil {
if errors.Is(err, matching.ErrorLackingPrefers) {
fmt.Printf("TestGaleShapleyLackingMsPrefers: %s\n", err)
} else {
t.Error("ERROR: Expect error occurred")
}
}

func TestGSLessWsPrefers(t *testing.T) {
func TestGaleShapleyLackingWsPrefers(t *testing.T) {
msPrefers := [][]int{{0}, {1}, {3}, {2}}
wsPrefers := [][]int{}
result, err := matching.GaleShapley(msPrefers, wsPrefers)
fmt.Printf("TestGSLessWsPrefers: %s\n", err)
if result != nil {
t.Error("ERROR: Expect an error and no result returned")
}
if err == nil {
if errors.Is(err, matching.ErrorLackingPrefers) {
fmt.Printf("TestGaleShapleyLackingWsPrefers: %s\n", err)
} else {
t.Error("ERROR: Expect error occurred")
}
}