Skip to content

Commit

Permalink
tmf: Bug 581427: handle BIG_BANG/BIG_CRUNCH in TmfTimestamp#getDelta
Browse files Browse the repository at this point in the history
TmfTimestamp.BIG_CRUNCH.getDelta(TmfTimestamp.BIG_BANG) leads to an
invalid result due to an overflow.

Time deltas involving any of those two special values are defined as:

  BIG_BANG   - BIG_BANG   == ZERO
  BIG_CRUNCH - BIG_CRUNCH == ZERO

  BIG_BANG   - <anything other than BIG_BANG>   == BIG_BANG
  BIG_CRUNCH - <anything other than BIG_CRUNCH> == BIG_CRUNCH

  <anything other than BIG_BANG> - BIG_BANG     == BIG_CRUNCH
  <anything other than BIG_CRUNCH> - BIG_CRUNCH == BIG_BANG

Also, add tests to cover this.

Bug 581427

Change-Id: Ibb71126af0fee338c74791ed0e4ac8bd5cb87de6
Signed-off-by: Christophe Bedard <bedard.christophe@gmail.com>
Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai>
Reviewed-on: https://git.eclipse.org/r/c/tracecompass/org.eclipse.tracecompass/+/199616
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
  • Loading branch information
christophebedard authored and MatthewKhouzam committed Mar 3, 2023
1 parent 7290218 commit f1dd159
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
Expand Up @@ -117,4 +117,12 @@ public void testToStringFormat() {
assertEquals("toString", "06:55:00.000 000 000", ts3.toString(format));
assertEquals("toString", "-00:00:00.123 450 000", ts4.toString(format));
}

@Test
public void testToStringBigBangBigCrunch() {
ITmfTimestamp bang = new TmfTimestampDelta(TmfTimestamp.BIG_BANG);
ITmfTimestamp crunch = new TmfTimestampDelta(TmfTimestamp.BIG_CRUNCH);
assertEquals("toString", "-9223372036.854 775 808", bang.toString());
assertEquals("toString", "9223372036.854 775 807", crunch.toString());
}
}
Expand Up @@ -234,6 +234,14 @@ public void testToStringInterval() {
assertEquals("toString", "-000.000 012 345", ts9.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
}

@Test
public void testToStringBigBangBigCrunch() {
ITmfTimestamp bang = TmfTimestamp.BIG_BANG;
ITmfTimestamp crunch = TmfTimestamp.BIG_CRUNCH;
assertEquals("toString", "-9223372036.854 775 808", bang.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
assertEquals("toString", "9223372036.854 775 807", crunch.toString(TmfTimestampFormat.getDefaulIntervalFormat()));
}

// ------------------------------------------------------------------------
// normalize
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -694,6 +702,25 @@ public void testDelta() {
exp = TmfTimestamp.create(4, 9);
delta = t0.getDelta(t1);
assertEquals("getDelta", 0, delta.compareTo(exp));

// Delta for BIG_CRUNCH and BIG_BANG
ITmfTimestamp bang = TmfTimestamp.BIG_BANG;
ITmfTimestamp crunch = TmfTimestamp.BIG_CRUNCH;
ITmfTimestamp zero = TmfTimestamp.ZERO;
ITmfTimestamp anyT = TmfTimestamp.create(1, 10);
ITmfTimestamp anyTN = TmfTimestamp.create(-2, 10);
assertEquals(bang.getDelta(bang), zero);
assertEquals(crunch.getDelta(crunch), zero);
assertEquals(crunch.getDelta(bang), crunch);
assertEquals(bang.getDelta(crunch), bang);
assertEquals(bang.getDelta(anyT), bang);
assertEquals(crunch.getDelta(anyT), crunch);
assertEquals(bang.getDelta(anyTN), bang);
assertEquals(crunch.getDelta(anyTN), crunch);
assertEquals(anyT.getDelta(bang), crunch);
assertEquals(anyT.getDelta(crunch), bang);
assertEquals(anyTN.getDelta(bang), crunch);
assertEquals(anyTN.getDelta(crunch), bang);
}

}
Expand Up @@ -358,6 +358,40 @@ public ITmfTimestamp normalize(final long offset, final int scale) {

@Override
public ITmfTimestamp getDelta(final ITmfTimestamp ts) {
/**
* Some of the special instances of TmfTimestamp have a special meaning,
* so the delta between them and other values can be defined as:
*
* <pre>
* BIG_BANG - BIG_BANG == ZERO
* BIG_CRUNCH - BIG_CRUNCH == ZERO
*
* BIG_BANG - <anything other than BIG_BANG> == BIG_BANG
* BIG_CRUNCH - <anything other than BIG_CRUNCH> == BIG_CRUNCH
*
* <anything other than BIG_BANG> - BIG_BANG == BIG_CRUNCH
* <anything other than BIG_CRUNCH> - BIG_CRUNCH == BIG_BANG
* </pre>
*/
if (equals(BIG_BANG)) {
if (ts.equals(BIG_BANG)) {
return ZERO;
}
return BIG_BANG;
}
if (equals(BIG_CRUNCH)) {
if (ts.equals(BIG_CRUNCH)) {
return ZERO;
}
return BIG_CRUNCH;
}
if (ts.equals(BIG_BANG)) {
return BIG_CRUNCH;
}
if (ts.equals(BIG_CRUNCH)) {
return BIG_BANG;
}

final int scale = getScale();
final ITmfTimestamp nts = ts.normalize(0, scale);
final long value = getValue() - nts.getValue();
Expand Down
Expand Up @@ -101,7 +101,7 @@ public String toString() {

@Override
public String toString(TmfTimestampFormat format) {
if (getValue() < 0) {
if (getValue() < 0 && !equals(BIG_BANG)) {
TmfTimestampDelta tmpTs = new TmfTimestampDelta(-getValue(), getScale());
return "-" + tmpTs.toString(format); //$NON-NLS-1$
}
Expand Down

0 comments on commit f1dd159

Please sign in to comment.