package tapp;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
/**
* Basically this is a mathematical issue.
*
*/
public class OptimalTaxTest {
static class Example {
final int[] fixedBase;
final int[] percents;
final int index;
final double result;
public Example(int[] fixedBase, int[] percents, int index, double result) {
super();
this.fixedBase = fixedBase;
this.percents = percents;
this.index = index;
this.result = result;
}
}
Example[] examples = new Example[] {
new Example(
new int[] { 10, 5, 3 },
new int[] { 0, 10, 20 },
0,
50.0),
new Example(
new int[] { 435, 3325, 2345, 0 },
new int[] { 45, 33, 13, 100 },
2,
5968.75),
new Example(
new int[] { 1, 0, 0, 0 },
new int[] { 9, 6, 7, 8 },
0,
-1.0) };
@Before
public void setUp() {
}
static class OptimalTax {
static double optimalIncome(int[] fixedBase, int[] percents, int index) {
if(percents.length != fixedBase.length)
throw new IllegalArgumentException("size");
int size = fixedBase.length;
if(index<0 || index>=size)
throw new IllegalArgumentException("index");
//compare the gradient
for(int k=0; k<size; ++k) {
if(k == index)
continue;
if(percents[k] <= percents[index])
return -1.0;
}
//find the cross points
double maxCross = -1;
for(int k=0; k<size; ++k) {
if(k == index)
continue;
double cross = 1.0 *
(fixedBase[k] - fixedBase[index])
/ (0.01 * (percents[index] - percents[k]));
if(cross > maxCross)
maxCross = cross;
}
return maxCross;
}
}
@Test
public void testAlgorithm() {
for (Example ex : examples) {
double optimalIncome = OptimalTax.optimalIncome(ex.fixedBase,
ex.percents, ex.index);
assertEquals(ex.result, optimalIncome, 0.0001);