Skip to content

Commit

Permalink
Merge pull request #163 from traPtitech/fix/calcAvailableTime
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
fuji8 authored Apr 19, 2021
2 parents d9dedee + cd684e2 commit 500d0a6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
43 changes: 33 additions & 10 deletions repository/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (r *Room) CalcAvailableTime(allowTogether bool) []StartEndTime {
if allowTogether && e.AllowTogether {
continue
}
TimeRangesSub(availabletime, StartEndTime{e.TimeStart, e.TimeEnd})
availabletime = TimeRangesSub(availabletime, StartEndTime{e.TimeStart, e.TimeEnd})
}
return availabletime
}
Expand All @@ -224,14 +224,6 @@ func TimeRangesSub(as []StartEndTime, b StartEndTime) (cs []StartEndTime) {
}

func TimeRangeSub(a StartEndTime, b StartEndTime) []StartEndTime {
/*
a: s####e-------
b: -------s####e
-> s####e
*/
if a.TimeStart.Unix() >= b.TimeEnd.Unix() || a.TimeEnd.Unix() <= b.TimeEnd.Unix() {
return []StartEndTime{a}
}

/*
a: ---s#####e---
Expand All @@ -242,12 +234,42 @@ func TimeRangeSub(a StartEndTime, b StartEndTime) []StartEndTime {
return nil
}

/*
a: s####e------- a: -------s####e
b: -------s####e b: s####e-------
-> s####e------- -> -------s####e
*/
if a.TimeEnd.Unix() <= b.TimeStart.Unix() || b.TimeEnd.Unix() <= a.TimeStart.Unix() {
return []StartEndTime{a}
}

/*
a: s###########e
b: ----s####e---
-> s###e----s##e
*/
if a.TimeStart.Unix() < b.TimeStart.Unix() && b.TimeEnd.Unix() < a.TimeEnd.Unix() {
if a.TimeStart.Unix() <= b.TimeStart.Unix() && b.TimeEnd.Unix() <= a.TimeEnd.Unix() {
/*
a: --s######e---
b: --s#####e----
-> --------se---
*/
if a.TimeStart.Unix() == b.TimeStart.Unix() {
return []StartEndTime{
{b.TimeEnd, a.TimeEnd},
}
}

/*
a: --s######e---
b: ----s###e----
-> --s#e--------
*/
if a.TimeEnd.Unix() == b.TimeStart.Unix() {
return []StartEndTime{
{a.TimeStart, b.TimeStart},
}
}
return []StartEndTime{
{a.TimeStart, b.TimeStart},
{b.TimeEnd, a.TimeEnd},
Expand Down Expand Up @@ -275,6 +297,7 @@ func TimeRangeSub(a StartEndTime, b StartEndTime) []StartEndTime {
{b.TimeEnd, a.TimeEnd},
}
}

return nil
}

Expand Down
27 changes: 26 additions & 1 deletion repository/room_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package repository

import (
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -175,6 +176,25 @@ func TestRoom_CalcAvailableTime(t *testing.T) {
},
allowTogether: false,
},
{
name: "Independent error",
fields: fields{
TimeStart: now,
TimeEnd: now.Add(10 * time.Hour),
Events: []Event{
{
TimeStart: now.Add(4 * time.Hour),
TimeEnd: now.Add(12 * time.Hour),
},
},
},
want: []StartEndTime{
{
TimeStart: now,
TimeEnd: now.Add(4 * time.Hour),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -183,7 +203,12 @@ func TestRoom_CalcAvailableTime(t *testing.T) {
TimeEnd: tt.fields.TimeEnd,
Events: tt.fields.Events,
}
r.CalcAvailableTime(true)
got := r.CalcAvailableTime(tt.allowTogether)
if !reflect.DeepEqual(got, tt.want) {
if !(len(got) == 0 && len(tt.want) == 0) {
t.Errorf("r.CalcAvailableTime() = %v, want %v", got, tt.want)
}
}
})
}
}
Expand Down

0 comments on commit 500d0a6

Please sign in to comment.