Merge "Reverse ReferenceWithHistory.dump() order" am: a1afeb6910

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1892637

Change-Id: Ife6269d4967a7d2588bc8f5bcc51eb06a1642a36
This commit is contained in:
Neil Fuller
2021-11-16 09:40:50 +00:00
committed by Automerger Merge Worker
2 changed files with 66 additions and 15 deletions

View File

@@ -25,6 +25,7 @@ import android.util.IndentingPrintWriter;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Iterator;
/** /**
* A class that behaves like the following definition, except it stores the history of values set * A class that behaves like the following definition, except it stores the history of values set
@@ -112,9 +113,11 @@ public final class ReferenceWithHistory<V> {
if (mValues == null) { if (mValues == null) {
ipw.println("{Empty}"); ipw.println("{Empty}");
} else { } else {
int i = mSetCount; int i = mSetCount - mValues.size();
for (TimestampedValue<V> valueHolder : mValues) { Iterator<TimestampedValue<V>> reverseIterator = mValues.descendingIterator();
ipw.print(--i); while (reverseIterator.hasNext()) {
TimestampedValue<V> valueHolder = reverseIterator.next();
ipw.print(i++);
ipw.print("@"); ipw.print("@");
ipw.print(Duration.ofMillis(valueHolder.getReferenceTimeMillis()).toString()); ipw.print(Duration.ofMillis(valueHolder.getReferenceTimeMillis()).toString());
ipw.print(": "); ipw.print(": ");

View File

@@ -17,17 +17,19 @@
package com.android.server.timedetector; package com.android.server.timedetector;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue;
import android.util.IndentingPrintWriter;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.internal.util.IndentingPrintWriter;
import com.android.server.timezonedetector.ReferenceWithHistory; import com.android.server.timezonedetector.ReferenceWithHistory;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Arrays;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class ReferenceWithHistoryTest { public class ReferenceWithHistoryTest {
@@ -41,31 +43,34 @@ public class ReferenceWithHistoryTest {
// Check unset behavior. // Check unset behavior.
compareGet(referenceWithHistory, reference, null); compareGet(referenceWithHistory, reference, null);
assertNotNull(dumpReferenceWithHistory(referenceWithHistory)); assertDumpContent(referenceWithHistory);
compareToString(referenceWithHistory, reference, "null"); compareToString(referenceWithHistory, reference, "null");
// Try setting null. // Try setting null.
setAndCompareReturnValue(referenceWithHistory, reference, null); setAndCompareReturnValue(referenceWithHistory, reference, null);
compareGet(referenceWithHistory, reference, null); compareGet(referenceWithHistory, reference, null);
assertNotNull(dumpReferenceWithHistory(referenceWithHistory)); assertDumpContent(referenceWithHistory, new DumpLine(0, "null"));
compareToString(referenceWithHistory, reference, "null"); compareToString(referenceWithHistory, reference, "null");
// Try setting a non-null value. // Try setting a non-null value.
setAndCompareReturnValue(referenceWithHistory, reference, "Foo"); setAndCompareReturnValue(referenceWithHistory, reference, "Foo");
compareGet(referenceWithHistory, reference, "Foo"); compareGet(referenceWithHistory, reference, "Foo");
assertNotNull(dumpReferenceWithHistory(referenceWithHistory)); assertDumpContent(referenceWithHistory,
new DumpLine(0, "null"), new DumpLine(1, "Foo"));
compareToString(referenceWithHistory, reference, "Foo"); compareToString(referenceWithHistory, reference, "Foo");
// Try setting null again. // Try setting null again.
setAndCompareReturnValue(referenceWithHistory, reference, "Foo"); setAndCompareReturnValue(referenceWithHistory, reference, null);
compareGet(referenceWithHistory, reference, "Foo"); compareGet(referenceWithHistory, reference, null);
assertNotNull(dumpReferenceWithHistory(referenceWithHistory)); assertDumpContent(referenceWithHistory,
compareToString(referenceWithHistory, reference, "Foo"); new DumpLine(1, "Foo"), new DumpLine(2, "null"));
compareToString(referenceWithHistory, reference, "null");
// Try a non-null value again. // Try a non-null value again.
setAndCompareReturnValue(referenceWithHistory, reference, "Bar"); setAndCompareReturnValue(referenceWithHistory, reference, "Bar");
compareGet(referenceWithHistory, reference, "Bar"); compareGet(referenceWithHistory, reference, "Bar");
assertNotNull(dumpReferenceWithHistory(referenceWithHistory)); assertDumpContent(referenceWithHistory,
new DumpLine(2, "null"), new DumpLine(3, "Bar"));
compareToString(referenceWithHistory, reference, "Bar"); compareToString(referenceWithHistory, reference, "Bar");
} }
@@ -132,11 +137,54 @@ public class ReferenceWithHistoryTest {
assertEquals(expected, referenceWithHistory.toString()); assertEquals(expected, referenceWithHistory.toString());
} }
private static String dumpReferenceWithHistory(ReferenceWithHistory<?> referenceWithHistory) { private static void assertDumpContent(
ReferenceWithHistory<?> referenceWithHistory, DumpLine... expectedLines) {
String[] actualLines = dumpReferenceWithHistory(referenceWithHistory);
if (expectedLines.length == 0) {
String expectedEmptyOutput = "{Empty}";
assertEquals(expectedEmptyOutput, 1, actualLines.length);
assertEquals(expectedEmptyOutput, actualLines[0]);
} else {
assertEquals("Expected=" + Arrays.toString(expectedLines)
+ ", actual=" + Arrays.toString(actualLines),
expectedLines.length, actualLines.length);
for (int i = 0; i < expectedLines.length; i++) {
DumpLine expectedLine = expectedLines[i];
String actualLine = actualLines[i];
assertTrue("i=" + i + ", expected=" + expectedLine + ", actual=" + actualLine,
actualLine.startsWith(Integer.toString(expectedLine.mIndex)));
assertTrue("i=" + i + ", expected=" + expectedLine + ", actual=" + actualLine,
actualLine.endsWith(expectedLine.mLine));
}
}
}
private static String[] dumpReferenceWithHistory(ReferenceWithHistory<?> referenceWithHistory) {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
try (IndentingPrintWriter ipw = new IndentingPrintWriter(stringWriter, " ")) { try (IndentingPrintWriter ipw = new IndentingPrintWriter(stringWriter, " ")) {
referenceWithHistory.dump(ipw); referenceWithHistory.dump(ipw);
return stringWriter.toString(); return stringWriter.toString().split("\n");
}
}
/** An expected line of {@link ReferenceWithHistory#dump} output. */
private static class DumpLine {
final int mIndex;
final String mLine;
DumpLine(int index, String line) {
mIndex = index;
mLine = line;
}
@Override
public String toString() {
return "DumpLine{"
+ "mIndex=" + mIndex
+ ", mLine='" + mLine + '\''
+ '}';
} }
} }
} }