/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.commons.math.analysis.function;

import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NonMonotonousSequenceException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NoDataException;

import org.junit.Assert;
import org.junit.Test;

/**
 * Test for class {@link StepFunction}.
 */
public class StepFunctionTest {
    private final double EPS = Math.ulp(1d);

    @Test
    public void testPreconditions() {
        try {
            final UnivariateRealFunction f = new StepFunction(null,
                                                              new double[] {0, -1, -2});
        } catch (NullArgumentException e) {
            // Expected.
        }
        try {
            final UnivariateRealFunction f = new StepFunction(new double[] {0, 1},
                                                              null);
        } catch (NullArgumentException e) {
            // Expected.
        }

        try {
            final UnivariateRealFunction f = new StepFunction(new double[] {0},
                                                              new double[] {});
        } catch (NoDataException e) {
            // Expected.
        }

        try {
            final UnivariateRealFunction f = new StepFunction(new double[] {},
                                                              new double[] {0});
        } catch (NoDataException e) {
            // Expected.
        }

        try {
            final UnivariateRealFunction f = new StepFunction(new double[] {0, 1},
                                                              new double[] {0, -1, -2});
        } catch (DimensionMismatchException e) {
            // Expected.
        }

        try {
            final UnivariateRealFunction f = new StepFunction(new double[] {1, 0, 1},
                                                              new double[] {0, -1, -2});
        } catch (NonMonotonousSequenceException e) {
            // Expected.
        }
    }

    @Test
    public void testSomeValues() {
        final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 };
        final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 };

        final UnivariateRealFunction f = new StepFunction(x, y);

        Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS);
        Assert.assertEquals(4, f.value(-10), EPS);
        Assert.assertEquals(-1, f.value(-0.4), EPS);
        Assert.assertEquals(-5.5, f.value(0), EPS);
        Assert.assertEquals(0.4, f.value(2), EPS);
        Assert.assertEquals(5.8, f.value(10), EPS);
        Assert.assertEquals(51.2, f.value(30), EPS);
        Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS);
    }
}

@@ -1354,32 +1354,70 @@
 (1d);%0A%0A    @Test
+(expected=NullArgumentException.class)
 %0A    public void
@@ -1434,16 +1434,17 @@
 nditions
+1
 () %7B%0A   
@@ -1452,143 +1452,30 @@
     
-try %7B%0A            final UnivariateRealFunction f = new StepFunction(null,%0A                                                             
+new StepFunction(null,
  new
@@ -1498,37 +1498,46 @@
 , -2%7D);%0A    
+%7D%0A%0A
     
-%7D catch (
+@Test(expected=
 NullArgument
@@ -1549,60 +1549,52 @@
 tion
- e) %7B%0A            // Expected.%0A        %7D%0A        try
+.class)%0A    public void testPreconditions2()
  %7B%0A 
@@ -1604,45 +1604,8 @@
     
-  
-  final UnivariateRealFunction f = 
 new 
@@ -1641,70 +1641,8 @@
  1%7D,
-%0A                                                             
  nul
@@ -1653,239 +1653,134 @@
     
+%7D%0A%0A
     
-%7D catch (NullArgumentException e) %7B%0A            // Expected.%0A        %7D%0A%0A        try %7B%0A            final UnivariateRealFunction f = new StepFunction(new double%5B%5D %7B0%7D,%0A                                                             
+@Test(expected=NoDataException.class)%0A    public void testPreconditions3() %7B%0A        new StepFunction(new double%5B%5D %7B0%7D,
  new
@@ -1802,232 +1802,133 @@
     
+%7D%0A%0A
     
-%7D catch (NoDataException e) %7B%0A            // Expected.%0A        %7D%0A%0A        try %7B%0A            final UnivariateRealFunction f = new StepFunction(new double%5B%5D %7B%7D,%0A                                                             
+@Test(expected=NoDataException.class)%0A    public void testPreconditions4() %7B%0A        new StepFunction(new double%5B%5D %7B%7D,
  new
@@ -1951,236 +1951,148 @@
     
+%7D%0A%0A
     
-%7D catch (NoDataException e) %7B%0A            // Expected.%0A        %7D%0A%0A        try %7B%0A            final UnivariateRealFunction f = new StepFunction(new double%5B%5D %7B0, 1%7D,%0A                                                             
+@Test(expected=DimensionMismatchException.class)%0A    public void testPreconditions5() %7B%0A        new StepFunction(new double%5B%5D %7B0, 1%7D,
  new
@@ -2123,100 +2123,104 @@
     
+%7D%0A%0A
     
-%7D catch (DimensionMismatchException e) %7B%0A            // Expected.%0A        %7D%0A%0A        try
+@Test(expected=NonMonotonousSequenceException.class)%0A    public void testPreconditions6()
  %7B%0A 
@@ -2230,45 +2230,8 @@
     
- 
-   final UnivariateRealFunction f = 
 new 
@@ -2270,185 +2270,35 @@
  1%7D,
-%0A                                                              new double%5B%5D %7B0, -1, -2%7D);%0A        %7D catch (NonMonotonousSequenceException e) %7B%0A            // Expected.%0A        %7D
+ new double%5B%5D %7B0, -1, -2%7D);
 %0A   

Tidy up test