Created
January 25, 2021 07:23
-
-
Save ayzu/c9ebf7979df346c2f54cc8d1996f10c8 to your computer and use it in GitHub Desktop.
Tests for Merge Intervals algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package merge_intervals | |
import ( | |
"reflect" | |
"testing" | |
) | |
func TestMerge(t *testing.T) { | |
type args struct { | |
arr1 []Slot | |
arr2 []Slot | |
} | |
tests := []struct { | |
name string | |
args args | |
want []Slot | |
}{ | |
{ | |
name: "merge all", | |
args: args{ | |
arr1: []Slot{ | |
{1, 3}, | |
{4, 5}, | |
}, | |
arr2: []Slot{ | |
{3, 4}, | |
}, | |
}, | |
want: []Slot{ | |
{1, 5}, | |
}, | |
}, | |
{ | |
name: "merge none", | |
args: args{ | |
arr1: []Slot{ | |
{1, 3}, | |
{4, 5}, | |
}, | |
arr2: []Slot{ | |
{13, 14}, | |
}, | |
}, | |
want: []Slot{ | |
{1, 3}, | |
{4, 5}, | |
{13, 14}, | |
}, | |
}, | |
{ | |
name: "merge first", | |
args: args{ | |
arr1: []Slot{ | |
{1, 3}, | |
{14, 15}, | |
}, | |
arr2: []Slot{ | |
{2, 5}, | |
}, | |
}, | |
want: []Slot{ | |
{1, 5}, | |
{14, 15}, | |
}, | |
}, | |
{ | |
name: "merge last", | |
args: args{ | |
arr1: []Slot{ | |
{1, 3}, | |
{14, 15}, | |
}, | |
arr2: []Slot{ | |
{15, 16}, | |
}, | |
}, | |
want: []Slot{ | |
{1, 3}, | |
{14, 16}, | |
}, | |
}, | |
{ | |
name: "merge middle", | |
args: args{ | |
arr1: []Slot{ | |
{3, 3}, | |
{4, 5}, | |
{8, 10}, | |
}, | |
arr2: []Slot{ | |
{1, 2}, | |
{4, 7}, | |
{12, 20}, | |
}, | |
}, | |
want: []Slot{ | |
{1, 2}, | |
{3, 3}, | |
{4, 7}, | |
{8, 10}, | |
{12, 20}, | |
}, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
if got := Merge(tt.args.arr1, tt.args.arr2); !reflect.DeepEqual(got, tt.want) { | |
t.Errorf("Merge() = %v, want %v", got, tt.want) | |
} | |
}) | |
} | |
} | |
func TestAvailableSlots(t *testing.T) { | |
type args struct { | |
schedule [][]Slot | |
} | |
tests := []struct { | |
name string | |
args args | |
want []Slot | |
}{ | |
{ | |
name: "0", | |
args: args{ | |
schedule: [][]Slot{ | |
{{9, 12}}, | |
{{1, 3}, {5, 6}}, | |
{{2, 3}, {6, 8}}, | |
{{4, 6}}, | |
}, | |
}, | |
want: []Slot{{3, 4}, {8, 9}}, | |
}, | |
{ | |
name: "1", | |
args: args{ | |
schedule: [][]Slot{ | |
{{9, 12}}, | |
{{1, 3}, {5, 6}}, | |
{{2, 3}, {6, 8}}, | |
{{4, 6}}, | |
}, | |
}, | |
want: []Slot{{3, 4}, {8, 9}}, | |
}, | |
{ | |
name: "2", | |
args: args{ | |
schedule: [][]Slot{ | |
{{9, 10}}, | |
{{1, 3}, {5, 6}}, | |
{{2, 3}, {6, 8}}, | |
{{4, 6}}, | |
}, | |
}, | |
want: []Slot{{3, 4}, {8, 9}, {10, 12}}, | |
}, | |
{ | |
name: "3", | |
args: args{ | |
schedule: [][]Slot{ | |
{{9, 10}}, | |
{{2, 3}, {5, 6}}, | |
{{2, 3}, {6, 8}}, | |
{{4, 6}}, | |
}, | |
}, | |
want: []Slot{{1, 2}, {3, 4}, {8, 9}, {10, 12}}, | |
}, | |
{ | |
name: "4", | |
args: args{ | |
schedule: [][]Slot{ | |
{{9, 10}}, | |
{{2, 3}, {5, 6}}, | |
{{6, 8}}, | |
{{4, 6}}, | |
}, | |
}, | |
want: []Slot{{1, 2}, {3, 4}, {8, 9}, {10, 12}}, | |
}, | |
{ | |
name: "5", | |
args: args{ | |
schedule: [][]Slot{ | |
{{9, 10}}, | |
{{2, 3}, {5, 6}}, | |
{{6, 8}}, | |
}, | |
}, | |
want: []Slot{{1, 2}, {3, 5}, {8, 9}, {10, 12}}, | |
}, | |
{ | |
name: "6", | |
args: args{ | |
schedule: [][]Slot{ | |
{{2, 3}, {5, 6}}, | |
{{6, 8}}, | |
}, | |
}, | |
want: []Slot{{1, 2}, {3, 5}, {8, 12}}, | |
}, | |
{ | |
name: "7", | |
args: args{ | |
schedule: [][]Slot{ | |
{{2, 3}, {5, 6}}, | |
}, | |
}, | |
want: []Slot{{1, 2}, {3, 5}, {6, 12}}, | |
}, | |
{ | |
name: "8", | |
args: args{ | |
schedule: [][]Slot{ | |
{{1, 3}, {5, 6}}, | |
}, | |
}, | |
want: []Slot{{3, 5}, {6, 12}}, | |
}, | |
{ | |
name: "9", | |
args: args{ | |
schedule: [][]Slot{}, | |
}, | |
want: []Slot{{1, 12}}, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
if got := AvailableSlots(tt.args.schedule); !reflect.DeepEqual(got, tt.want) { | |
t.Errorf("input: %v AvailableSlots() = %v, want %v", tt.args.schedule, got, tt.want) | |
} | |
if got := AvailableSlots1(tt.args.schedule); !reflect.DeepEqual(got, tt.want) { | |
t.Errorf("input: %v AvailableSlots1() = %v, want %v", tt.args.schedule, got, tt.want) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment