WeightedPercentile { public double evaluate(double percentile) { if (percentile >= 50.0) { int numToPass = (int)((100.0 - percentile) / 100.0 * numRawMetrics); int numPassed = 0; for (int i = weightedMetrics.length - 1; i >= 0; i--) { numPassed += weightedMetrics[i].count; if (numPassed >= numToPass) { return weightedMetrics[i].metric; } } } else { int numToPass = (int)(percentile / 100.0 * numRawMetrics); int numPassed = 0; for (int i = 0; i < weightedMetrics.length; i++) { numPassed += weightedMetrics[i].count; if (numPassed >= numToPass) { return weightedMetrics[i].metric; } } } throw new MacroBaseInternalError("WeightedPercentile was implemented incorrectly"); } WeightedPercentile(double[] counts, double[] metrics); double evaluate(double percentile);  }
@Test public void testEvaluate() throws Exception { WeightedPercentile wp = new WeightedPercentile(counts, metrics); double p100 = wp.evaluate(100.0); assertEquals(1000.0, p100, 0.01); double p99 = wp.evaluate(99.0); assertEquals(995.0, p99, 0.01); double p90 = wp.evaluate(90.0); assertEquals(949.0, p90, 0.01); double p50 = wp.evaluate(50.0); assertEquals(707.0, p50, 0.01); double p1 = wp.evaluate(1.0); assertEquals(100.0, p1, 0.01); double p0 = wp.evaluate(0.0); assertEquals(1.0, p0, 0.01); }