-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve interval.go and intervalset.go
interval: * Separate tests from interval_test.go * Improve documentation. * Export and set-like functions that are well defined. * Rename interval.Subtract to interval.SubtractSplit, and add tests. * Handle empty cases first. * Preallocate Elements. intervalset: * Guard Size() from overflow, and use intervalset.CalculateSize(). * Handle empty cases first. * Remove String() method, since it is not obvious; clients should implement. Signed-off-by: Elazar Gershuni <[email protected]>
- Loading branch information
Showing
4 changed files
with
148 additions
and
45 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2023- IBM Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package interval_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/np-guard/models/pkg/interval" | ||
) | ||
|
||
func span(start, end int64) interval.Interval { | ||
return interval.New(start, end) | ||
} | ||
|
||
func empty() interval.Interval { | ||
return interval.New(0, -1) | ||
} | ||
|
||
// To avoid recursion, only use helper functions that are declared earlier in the file. | ||
|
||
func requireEqual(t *testing.T, actual, expected interval.Interval) { | ||
t.Helper() | ||
require.Equal(t, actual.Start(), expected.Start()) | ||
require.Equal(t, actual.End(), expected.End()) | ||
} | ||
|
||
func requireIntersection(t *testing.T, i1, i2, expected interval.Interval) { | ||
t.Helper() | ||
require.Equal(t, !expected.IsEmpty(), i1.Overlap(i2)) | ||
require.Equal(t, !expected.IsEmpty(), i2.Overlap(i1)) | ||
requireEqual(t, i1.Intersect(i2), expected) | ||
requireEqual(t, i2.Intersect(i1), expected) | ||
} | ||
|
||
func requireUnrelated(t *testing.T, i1, i2 interval.Interval) { | ||
t.Helper() | ||
require.False(t, i1.IsSubset(i2)) | ||
require.False(t, i2.IsSubset(i1)) | ||
require.True(t, i1.Intersect(i2).Size() < min(i1.Size(), i2.Size())) | ||
} | ||
|
||
func requireSubset(t *testing.T, small, large interval.Interval) { | ||
t.Helper() | ||
require.True(t, small.IsSubset(large)) | ||
requireIntersection(t, small, large, small) | ||
require.Equal(t, small.Equal(large), large.IsSubset(small)) | ||
} | ||
|
||
func TestInterval_Elements(t *testing.T) { | ||
it1 := span(3, 7) | ||
|
||
require.Equal(t, int64(5), it1.Size()) | ||
require.Equal(t, []int64{3, 4, 5, 6, 7}, it1.Elements()) | ||
} | ||
|
||
func TestInterval_Empty(t *testing.T) { | ||
requireEqual(t, empty(), empty()) | ||
requireEqual(t, span(5, 4), empty()) | ||
|
||
require.Equal(t, int64(0), span(-1, -2).Size()) | ||
require.Equal(t, []int64{}, span(-1, -2).Elements()) | ||
} | ||
|
||
func TestInterval_Intersect(t *testing.T) { | ||
s := span(4, 6) | ||
requireIntersection(t, s, s, span(4, 6)) | ||
|
||
// requireIntersection checks both directions; no need to add tests for all combinations | ||
requireIntersection(t, empty(), empty(), empty()) | ||
requireIntersection(t, span(4, 6), span(3, 7), span(4, 6)) | ||
requireIntersection(t, span(4, 6), span(3, 5), span(4, 5)) | ||
requireIntersection(t, span(4, 6), empty(), empty()) | ||
requireIntersection(t, span(4, 6), span(7, 8), empty()) | ||
} | ||
|
||
func TestInterval_IsSubset(t *testing.T) { | ||
requireSubset(t, empty(), empty()) | ||
requireSubset(t, empty(), span(1, 2)) | ||
requireSubset(t, span(1, 2), span(1, 2)) | ||
requireSubset(t, span(1, 2), span(0, 3)) | ||
|
||
requireUnrelated(t, span(1, 2), span(2, 3)) | ||
requireUnrelated(t, span(1, 2), span(3, 4)) | ||
requireUnrelated(t, span(1, 2), span(5, 6)) | ||
} | ||
|
||
func TestInterval_SubtractSplit(t *testing.T) { | ||
require.Equal(t, []interval.Interval{}, empty().SubtractSplit(empty())) | ||
require.Equal(t, []interval.Interval{}, empty().SubtractSplit(span(1, 3))) | ||
require.Equal(t, []interval.Interval{}, span(1, 3).SubtractSplit(span(1, 3))) | ||
require.Equal(t, []interval.Interval{}, span(1, 3).SubtractSplit(span(0, 3))) | ||
require.Equal(t, []interval.Interval{}, span(1, 3).SubtractSplit(span(1, 4))) | ||
require.Equal(t, []interval.Interval{}, span(1, 3).SubtractSplit(span(0, 4))) | ||
|
||
require.Equal(t, []interval.Interval{span(1, 1), span(3, 3)}, span(1, 3).SubtractSplit(span(2, 2))) | ||
require.Equal(t, []interval.Interval{span(1, 2), span(5, 6)}, span(1, 6).SubtractSplit(span(3, 4))) | ||
|
||
require.Equal(t, []interval.Interval{span(3, 4)}, span(3, 4).SubtractSplit(span(1, 2))) | ||
require.Equal(t, []interval.Interval{span(3, 4)}, span(3, 4).SubtractSplit(span(5, 6))) | ||
} |
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
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