[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [onionoo/master] Support negative values in IntegerDistribution.
commit 678b18f40a9b15f0939301bd673722a1b20f6a85
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Tue Feb 18 12:51:11 2020 +0100
Support negative values in IntegerDistribution.
Fixes #33360.
---
.../onionoo/server/IntegerDistribution.java | 23 +++++++++++++++++-----
.../onionoo/server/PerformanceMetricsTest.java | 14 +++++++++++++
2 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/torproject/metrics/onionoo/server/IntegerDistribution.java b/src/main/java/org/torproject/metrics/onionoo/server/IntegerDistribution.java
index 85e221a..7be8ca0 100644
--- a/src/main/java/org/torproject/metrics/onionoo/server/IntegerDistribution.java
+++ b/src/main/java/org/torproject/metrics/onionoo/server/IntegerDistribution.java
@@ -7,10 +7,18 @@ import java.util.Arrays;
class IntegerDistribution {
- int[] logValues = new int[64];
+ /**
+ * Counts by power of two with negative values at index 0, values < 2^0 at
+ * index 1, values < 2^1 at index 2, and values >= 2^63 at index 64.
+ */
+ int[] logValues = new int[65];
void addLong(long value) {
- logValues[64 - Long.numberOfLeadingZeros(value)]++;
+ if (value < 0L) {
+ logValues[0]++;
+ } else {
+ logValues[65 - Long.numberOfLeadingZeros(value)]++;
+ }
}
@Override
@@ -27,9 +35,14 @@ class IntegerDistribution {
seenValues += logValues[i];
while (j < permilles.length
&& (seenValues * 1000 > totalValues * permilles[j])) {
- sb.append(j > 0 ? ", " : "").append(".").append(permilles[j])
- .append(i < logValues.length - 1 ? "<" + (1L << i)
- : ">=" + (1L << i - 1));
+ sb.append(j > 0 ? ", " : "").append(".").append(permilles[j]);
+ if (i == 0) {
+ sb.append("<0");
+ } else if (i < logValues.length - 1) {
+ sb.append("<").append(1L << (i - 1));
+ } else {
+ sb.append(">=").append(1L << i - 2);
+ }
j++;
}
if (j == permilles.length) {
diff --git a/src/test/java/org/torproject/metrics/onionoo/server/PerformanceMetricsTest.java b/src/test/java/org/torproject/metrics/onionoo/server/PerformanceMetricsTest.java
index bb3c963..287c287 100644
--- a/src/test/java/org/torproject/metrics/onionoo/server/PerformanceMetricsTest.java
+++ b/src/test/java/org/torproject/metrics/onionoo/server/PerformanceMetricsTest.java
@@ -121,6 +121,20 @@ public class PerformanceMetricsTest {
}
@Test
+ public void testIntegerDistributionMinusOne() {
+ IntegerDistribution id = new IntegerDistribution();
+ id.addLong(-1L);
+ assertEquals(".500<0, .900<0, .990<0, .999<0", id.toString());
+ }
+
+ @Test
+ public void testIntegerDistributionMinLong() {
+ IntegerDistribution id = new IntegerDistribution();
+ id.addLong(Long.MIN_VALUE);
+ assertEquals(".500<0, .900<0, .990<0, .999<0", id.toString());
+ }
+
+ @Test
public void testIntegerDistributionZero() {
IntegerDistribution id = new IntegerDistribution();
id.addLong(0);
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits