Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2012 AndroidPlot.com
      3  *
      4  *    Licensed under the Apache License, Version 2.0 (the "License");
      5  *    you may not use this file except in compliance with the License.
      6  *    You may obtain a copy of the License at
      7  *
      8  *        http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  *    Unless required by applicable law or agreed to in writing, software
     11  *    distributed under the License is distributed on an "AS IS" BASIS,
     12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  *    See the License for the specific language governing permissions and
     14  *    limitations under the License.
     15  */
     16 
     17 package com.androidplot.util;
     18 
     19 import org.junit.Ignore;
     20 import org.junit.Test;
     21 
     22 import static junit.framework.Assert.assertEquals;
     23 import static org.junit.Assert.fail;
     24 
     25 public class ValPixConverterTest {
     26     @org.junit.Before
     27     public void setUp() throws Exception {
     28 
     29     }
     30 
     31     @org.junit.After
     32     public void tearDown() throws Exception {
     33 
     34     }
     35 
     36     /*
     37     @org.junit.Test
     38     public void testIndexToPix() throws Exception {
     39         int sizeInPix = 100;
     40         int itemCount = 10;
     41         assertEquals(10.0f, ValPixConverter.indexToPix(1, itemCount, sizeInPix));
     42 
     43         try {
     44            ValPixConverter.indexToPix(100, 10, 100);
     45             fail("IndexOutOfBoundsException expected.");
     46         } catch(IndexOutOfBoundsException ex) {
     47 
     48         }
     49 
     50     }
     51     */
     52 
     53     @org.junit.Test
     54     public void testValToPix() throws Exception {
     55         int sizeInPix = 100;
     56         int min = 0;
     57         int max = 100;
     58 
     59         int value = 50;
     60 
     61         assertEquals(50.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     62 
     63         // should be closer to the top:
     64         // (remember that 0,0 is the top left pixel)
     65         value = 75;
     66         assertEquals(25.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     67 
     68         // should be at the very top:
     69         value = 100;
     70         assertEquals(0.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     71 
     72         // should be at the very top:
     73         value = 0;
     74         assertEquals(100.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     75 
     76         // should be smack in the middle:
     77         min = -100;
     78         value = 0;
     79         assertEquals(50.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     80 
     81         // should be at the very bottom:
     82         value = 100;
     83         assertEquals(0.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     84 
     85         // should be in the middle:
     86         value = 0;
     87         assertEquals(50.0f, ValPixConverter.valToPix(value, min, max, sizeInPix, true));
     88 
     89         min = -100;
     90         max = 100;
     91         sizeInPix = 200;
     92         assertEquals(0f, ValPixConverter.valToPix(-100, min, max, sizeInPix, false));
     93         assertEquals(200f, ValPixConverter.valToPix(100, min, max, sizeInPix, false));
     94     }
     95 
     96     @org.junit.Test
     97     public void testpixToVal() throws Exception {
     98         int sizeInPix = 100;
     99         int min = 0;
    100         int max = 100;
    101 
    102         double value = 50;
    103 
    104         float pixel = ValPixConverter.valToPix(value, min, max, sizeInPix, true);
    105         assertEquals(value, ValPixConverter.pixToVal(pixel, min, max, sizeInPix, true));
    106 
    107         value = 75;
    108         pixel = ValPixConverter.valToPix(value, min, max, sizeInPix, true);
    109         assertEquals(value, ValPixConverter.pixToVal(pixel, min, max, sizeInPix, true));
    110 
    111 
    112         min = -100;
    113         value = 50;
    114         pixel = ValPixConverter.valToPix(value, min, max, sizeInPix, true);
    115         assertEquals(value, ValPixConverter.pixToVal(pixel, min, max, sizeInPix, true));
    116 
    117         min = -100;
    118         value = 60;
    119         pixel = ValPixConverter.valToPix(value, min, max, sizeInPix, true);
    120         assertEquals(value, ValPixConverter.pixToVal(pixel, min, max, sizeInPix, true));
    121 
    122         min =  20;
    123         value = 50;
    124         pixel = ValPixConverter.valToPix(value, min, max, sizeInPix, true);
    125         assertEquals(value, ValPixConverter.pixToVal(pixel, min, max, sizeInPix, true));
    126 
    127         try {
    128             ValPixConverter.pixToVal(-5, 0, 0, 0, true);
    129             fail("IllegalArgumentException expected.");
    130         } catch(IllegalArgumentException ex) {
    131 
    132         }
    133 
    134 
    135     }
    136 
    137 
    138     @Test
    139     public void testValPerPix() {
    140         //double result = ;
    141         assertEquals(1.0, ValPixConverter.valPerPix(0, 100, 100));
    142         double expected = 200d/100;
    143         assertEquals(expected, ValPixConverter.valPerPix(100, 300, 100));
    144         expected = 50d/100;
    145         assertEquals(expected, ValPixConverter.valPerPix(0, 50, 100));
    146         expected = 200d/100;
    147         assertEquals(expected, ValPixConverter.valPerPix(-100, 100, 100));
    148     }
    149 }
    150