Skip to content

Commit

Permalink
fix(timespan): improve check for a time contained in a timespan (#787)
Browse files Browse the repository at this point in the history
In checking whether a time t is contained in an interval (a, b),
consider it true only if a <= t < b. Previously this allowed for
a <= t <= b, but that is not accurate since timespans are composed
of half-open intervals (closed on the left) with a universe of
[0, infinity).
  • Loading branch information
johnnylam88 committed Jan 4, 2021
1 parent acf8948 commit f5b7783
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/tools/TimeSpan.ts
Expand Up @@ -144,7 +144,7 @@ export class OvaleTimeSpan implements LuaArray<number | undefined> {
HasTime(atTime: number) {
const A = this;
for (let i = 1; i <= lualength(A); i += 2) {
if (A[i] <= atTime && atTime <= A[i + 1]) {
if (A[i] <= atTime && atTime < A[i + 1]) {
return true;
}
}
Expand Down
199 changes: 127 additions & 72 deletions src/tools/timespan.spec.ts
@@ -1,72 +1,127 @@
import { test, expect } from "@jest/globals";
import { huge } from "@wowts/math";
import { newFromArgs } from "./TimeSpan";

test("intersectInterval with one interval which is within the first", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(5, 10);

// Assert
expect(result[1]).toEqual(5);
expect(result[2]).toEqual(10);
});

test("intersectInterval with one interval which overlaps the first", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(5, 15);

// Assert
expect(result[1]).toEqual(5);
expect(result[2]).toEqual(10);
});

test("intersectInterval with one interval which overlaps the start of the first", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(-5, 5);

// Assert
expect(result[1]).toEqual(0);
expect(result[2]).toEqual(5);
});

test("intersectInterval with one interval which does not overlap the end", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(11, 15);

// Assert
expect(result[1]).toBeUndefined();
});

test("intersectInterval with one interval which does not overlap the start", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(-5, -1);

// Assert
expect(result[1]).toBeUndefined();
});

test("intersectInterval with nothing", () => {
// Arrange
const timeSpan = newFromArgs();

// Act
const result = timeSpan.IntersectInterval(1, huge);

// Assert
expect(result[1]).toBeUndefined();
});
import { test, expect } from "@jest/globals";
import { huge } from "@wowts/math";
import { newFromArgs } from "./TimeSpan";

test("HasTime with point to left of interval", () => {
// Arrange
const timeSpan = newFromArgs(10, 20);

// Act
const bool = timeSpan.HasTime(5);

// Assert
expect(bool).toBe(false);
});

test("HasTime with point on left endpoint of interval", () => {
// Arrange
const timeSpan = newFromArgs(10, 20);

// Act
const bool = timeSpan.HasTime(10);

// Assert
expect(bool).toBe(true);
});

test("HasTime with point inside interval", () => {
// Arrange
const timeSpan = newFromArgs(10, 20);

// Act
const bool = timeSpan.HasTime(15);

// Assert
expect(bool).toBe(true);
});

test("HasTime with point on right endpoint of interval", () => {
// Arrange
const timeSpan = newFromArgs(10, 20);

// Act
const bool = timeSpan.HasTime(20);

// Assert
expect(bool).toBe(false);
});

test("HasTime with point to right of interval", () => {
// Arrange
const timeSpan = newFromArgs(10, 20);

// Act
const bool = timeSpan.HasTime(25);

// Assert
expect(bool).toBe(false);
});

test("intersectInterval with one interval which is within the first", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(5, 10);

// Assert
expect(result[1]).toEqual(5);
expect(result[2]).toEqual(10);
});

test("intersectInterval with one interval which overlaps the first", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(5, 15);

// Assert
expect(result[1]).toEqual(5);
expect(result[2]).toEqual(10);
});

test("intersectInterval with one interval which overlaps the start of the first", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(-5, 5);

// Assert
expect(result[1]).toEqual(0);
expect(result[2]).toEqual(5);
});

test("intersectInterval with one interval which does not overlap the end", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(11, 15);

// Assert
expect(result[1]).toBeUndefined();
});

test("intersectInterval with one interval which does not overlap the start", () => {
// Arrange
const timeSpan = newFromArgs(0, 10);

// Act
const result = timeSpan.IntersectInterval(-5, -1);

// Assert
expect(result[1]).toBeUndefined();
});

test("intersectInterval with nothing", () => {
// Arrange
const timeSpan = newFromArgs();

// Act
const result = timeSpan.IntersectInterval(1, huge);

// Assert
expect(result[1]).toBeUndefined();
});

0 comments on commit f5b7783

Please sign in to comment.