/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.querydsl.core.util;

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.junit.Test;

public class MathUtilsTest {

    @Test
    public void sum() {
        assertEquals(Integer.valueOf(5), MathUtils.sum(2, 3.0));
    }

    @Test
    public void difference() {
        assertEquals(Integer.valueOf(2), MathUtils.difference(5, 3.0));
    }

    @Test
    public void cast() {
        Integer value = 1;
        assertEquals(BigDecimal.class, MathUtils.cast(value, BigDecimal.class).getClass());
        assertEquals(BigInteger.class, MathUtils.cast(value, BigInteger.class).getClass());
        assertEquals(Double.class, MathUtils.cast(value, Double.class).getClass());
        assertEquals(Float.class, MathUtils.cast(value, Float.class).getClass());
        assertEquals(Integer.class, MathUtils.cast(value, Integer.class).getClass());
        assertEquals(Long.class, MathUtils.cast(value, Long.class).getClass());
        assertEquals(Short.class, MathUtils.cast(value, Short.class).getClass());
        assertEquals(Byte.class, MathUtils.cast(value, Byte.class).getClass());

        assertEquals(BigDecimal.ONE, MathUtils.cast(value, BigDecimal.class));
        assertEquals(BigInteger.ONE, MathUtils.cast(value, BigInteger.class));
        assertEquals(Double.valueOf(1), MathUtils.cast(value, Double.class));
        assertEquals(Float.valueOf(1), MathUtils.cast(value, Float.class));
        assertEquals(Integer.valueOf(1), MathUtils.cast(value, Integer.class));
        assertEquals(Long.valueOf(1), MathUtils.cast(value, Long.class));
        assertEquals(Short.valueOf((short) 1), MathUtils.cast(value, Short.class));
        assertEquals(Byte.valueOf((byte) 1), MathUtils.cast(value, Byte.class));
    }

}

@@ -693,16 +693,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertSame;
 %0A%0Aimport
@@ -1111,776 +1111,438 @@
 cast
-() %7B%0A        Integer value = 1;%0A        assertEquals(BigDecimal.class, MathUtils.cast(value, BigDecimal.class).getClass());%0A        assertEquals(BigInteger.class, MathUtils.cast(value, BigInteger.class).getClass());%0A        assertEquals(Double.class, MathUtils.cast(value, Double.class).getClass());%0A        assertEquals(Float.class, MathUtils.cast(value, Float.class).getClass());%0A        assertEquals(Integer.class, MathUtils.cast(value, Integer.class).getClass());%0A        assertEquals(Long.class, MathUtils.cast(value, Long.class).getClass());%0A        assertEquals(Short.class, MathUtils.cast(value, Short.class).getClass()
+_returns_correct_type() %7B%0A        checkCast(1, BigDecimal.class);%0A        checkCast(1, BigInteger.class);%0A        checkCast(1, Double.class);%0A        checkCast(1, Float.class);%0A        checkCast(1, Integer.class);%0A        checkCast(1, Long.class);%0A        checkCast(1, Short.class);%0A        checkCast(1, Byte.class
 );%0A    
+%7D%0A%0A
     
-assertEquals(Byte.class, MathUtils.cast(value, Byte.class).getClass());%0A%0A        assertEquals(BigDecimal.ONE, MathUtils.cast(value
+@Test%0A    public void cast_returns_argument_as_is_when_compatible() %7B%0A        checkSame(BigDecimal.ONE
 , Bi
@@ -1552,25 +1552,24 @@
 cimal.class)
-)
 ;%0A        as
@@ -1566,28 +1566,25 @@
         
-assertEquals
+checkSame
 (BigInte
@@ -1591,38 +1591,16 @@
 ger.ONE,
- MathUtils.cast(value,
  BigInte
@@ -1601,33 +1601,32 @@
 igInteger.class)
-)
 ;%0A        assert
@@ -1623,60 +1623,28 @@
     
-assertEquals(Double.valueOf(1), MathUtils.cast(value
+checkSame((double) 1
 , Do
@@ -1650,25 +1650,24 @@
 ouble.class)
-)
 ;%0A        as
@@ -1668,74 +1668,119 @@
     
-assertEquals(Float.valueOf
+checkSame((float) 1, Float.class);%0A        checkSame
 (1
-)
 , 
-MathUtils.cast(value, Float
+Integer.class);%0A        checkSame((long) 1, Long
 .class)
-)
 ;%0A  
@@ -1789,79 +1789,177 @@
     
-assertEquals(Integer.valueOf(1), MathUtils.cast(value, Integer.c
+checkSame((short) 1, Short.class);%0A        checkSame((byte) 1, Byte.class);%0A    %7D%0A%0A    private static void checkCast(Number value, Class%3C? extends Number%3E targetC
 lass)
-);
+ %7B
 %0A   
@@ -1967,37 +1967,23 @@
     
-assertEquals(Long.valueOf(1),
+Number target =
  Mat
@@ -2005,20 +2005,20 @@
 ue, 
-Long.c
+targetC
 lass)
-)
 ;%0A  
@@ -2033,82 +2033,50 @@
 sert
-Equals(Short.valueOf((short) 1), MathUtils.cast(value, Short.c
+Same(targetClass, target.getC
 lass
+(
 ));%0A    
     
@@ -2075,82 +2075,184 @@
     
+%7D%0A%0A
     
-assertEquals(Byte.valueOf((byte) 1), MathUtils.cast(value, Byte.class)
+private static %3CN extends Number%3E void checkSame(N value, Class%3CN%3E targetClass) %7B%0A        N target = MathUtils.cast(value, targetClass);%0A        assertSame(value, target
 );%0A 

Make MathUtilsTest more readable