Skip to content

Instantly share code, notes, and snippets.

@henrikw henrikw/gist:8999044
Last active Aug 29, 2015

Embed
What would you like to do?
package com.example.testphone.node.generator;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author Henrik Warne
*/
public class TestScalingValue extends TestCase {
public void test_setTimeAnchors_zeroValues_gives100() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<Integer>());
scalingValue.setTimeAnchors(0, 1000);
assertEquals(2, scalingValue.valueList.size());
assertEquals(100, scalingValue.valueList.get(0).scalePercentage);
assertEquals(0, scalingValue.valueList.get(0).anchorTimeStamp);
assertEquals(100, scalingValue.valueList.get(1).scalePercentage);
assertEquals(1000, scalingValue.valueList.get(1).anchorTimeStamp);
}
public void test_setTimeAnchors_oneValues_duplicated() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(60)));
scalingValue.setTimeAnchors(0, 1000);
assertEquals(2, scalingValue.valueList.size());
assertEquals(60, scalingValue.valueList.get(0).scalePercentage);
assertEquals(0, scalingValue.valueList.get(0).anchorTimeStamp);
assertEquals(60, scalingValue.valueList.get(1).scalePercentage);
assertEquals(1000, scalingValue.valueList.get(1).anchorTimeStamp);
}
public void test_setTimeAnchors_twoValues() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(100,0)));
scalingValue.setTimeAnchors(0, 1000);
assertEquals(2, scalingValue.valueList.size());
assertEquals(100, scalingValue.valueList.get(0).scalePercentage);
assertEquals(0, scalingValue.valueList.get(0).anchorTimeStamp);
assertEquals(0, scalingValue.valueList.get(1).scalePercentage);
assertEquals(1000, scalingValue.valueList.get(1).anchorTimeStamp);
}
public void test_setTimeAnchors_threeValues() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(0, 100, 60)));
scalingValue.setTimeAnchors(0, 1000);
assertEquals(3, scalingValue.valueList.size());
assertEquals(0, scalingValue.valueList.get(0).scalePercentage);
assertEquals(0, scalingValue.valueList.get(0).anchorTimeStamp);
assertEquals(100, scalingValue.valueList.get(1).scalePercentage);
assertEquals(500, scalingValue.valueList.get(1).anchorTimeStamp);
assertEquals(60, scalingValue.valueList.get(2).scalePercentage);
assertEquals(1000, scalingValue.valueList.get(2).anchorTimeStamp);
}
public void testGetCurrentRate_timeAnchorsNotSet_exception() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(0, 100, 60)));
try {
scalingValue.getCurrentRate(0, 10.0);
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
public void testGetNextIndex() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(0, 100, 60)));
scalingValue.setTimeAnchors(0, 1000);
assertEquals(1, scalingValue.getNextIndex(0));
assertEquals(1, scalingValue.getNextIndex(499));
assertEquals(2, scalingValue.getNextIndex(500));
assertEquals(2, scalingValue.getNextIndex(999));
assertEquals(2, scalingValue.getNextIndex(1000));
}
public void testGetScalingFactor() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(0, 100, 60)));
scalingValue.setTimeAnchors(0, 1000);
long now;
now = 250;
int i;
ScalingValue.Value prev;
ScalingValue.Value next;
i = scalingValue.getNextIndex(now);
prev = scalingValue.valueList.get(i-1);
next = scalingValue.valueList.get(i);
assertEquals(0.5, scalingValue.getScalingFactor(prev, next, now));
now = 750;
i = scalingValue.getNextIndex(now);
prev = scalingValue.valueList.get(i-1);
next = scalingValue.valueList.get(i);
assertEquals(0.8, scalingValue.getScalingFactor(prev, next, now));
}
public void test_getCurrentRate_twoValues() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(100,0)));
scalingValue.setTimeAnchors(0, 1000);
assertEquals(10.0, scalingValue.getCurrentRate(0, 10.0));
assertEquals(8.0, scalingValue.getCurrentRate(200, 10.0));
assertEquals(5.0, scalingValue.getCurrentRate(500, 10.0));
assertEquals(4.0, scalingValue.getCurrentRate(600, 10.0));
assertEquals(2.0, scalingValue.getCurrentRate(800, 10.0));
}
public void test_getCurrentRate_threeValues() {
ScalingValue scalingValue = new ScalingValue(new ArrayList<>(Arrays.asList(100,0,100,0,100)));
scalingValue.setTimeAnchors(4000, 1000);
assertEquals(10.0, scalingValue.getCurrentRate(0, 10.0));
assertEquals(10.0, scalingValue.getCurrentRate(4000, 10.0));
assertEquals(15.0, scalingValue.getCurrentRate(4125, 30.0));
assertEquals(0.0, scalingValue.getCurrentRate(4250, 10.0));
assertEquals(5.0, scalingValue.getCurrentRate(4375, 10.0));
assertEquals(10.0, scalingValue.getCurrentRate(4500, 10.0));
assertEquals(0.0, scalingValue.getCurrentRate(4750, 10.0));
assertEquals(10.0, scalingValue.getCurrentRate(5000, 10.0));
assertEquals(10.0, scalingValue.getCurrentRate(6000, 10.0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.