ItEr45S10CUAsignacionRecursosEspecificosAPlanificacionItEr44S16: Adding class for fixing negative values on interpolation

This commit is contained in:
Óscar González Fernández 2010-01-26 18:16:12 +01:00
parent d7710c45a4
commit e26f57cc84
2 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,46 @@
/*
* This file is part of ###PROJECT_NAME###
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.planner.entities;
/**
* It fills valleys in the discrete function represented by hours. A valley is
* the portion between two maximum points. Besides the last value must be the
* maximum for all the function
* @author Óscar González Fernández <ogonzalez@igalia.com>
*/
public class ValleyFiller {
private ValleyFiller() {
}
public static int[] fillValley(int... hours) {
int[] result = new int[hours.length];
int lastMaximum = Integer.MIN_VALUE;
int globalMaximum = hours[hours.length - 1];
for (int i = 0; i < hours.length; i++) {
if (hours[i] > lastMaximum) {
lastMaximum = hours[i];
}
result[i] = Math.min(lastMaximum, globalMaximum);
}
return result;
}
}

View file

@ -0,0 +1,60 @@
/*
* This file is part of ###PROJECT_NAME###
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.test.planner.entities;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.navalplanner.business.planner.entities.ValleyFiller;
/**
* @author Óscar González Fernández <ogonzalez@igalia.com>
*
*/
public class ValleyFillerTest {
@Test
public void ifIsMonotonicIncreasingDoesntDoAnything() {
int[] param = { 2, 4, 8 };
int[] result = ValleyFiller.fillValley(param);
assertThat(result, equalTo(param));
}
@Test
public void ifHasValleyItFillsIt() {
int[] result = ValleyFiller.fillValley(2, 4, 8, 5, 6, 10);
assertThat(result, equalTo(new int[] { 2, 4, 8, 8, 8, 10 }));
}
@Test
public void ifEndsInValleyEndsAtLastValue() {
int[] result = ValleyFiller.fillValley(2, 4, 8, 5, 6);
assertThat(result, equalTo(new int[] { 2, 4, 6, 6, 6 }));
}
@Test
public void ifHasSeveralValleysWorksOk() {
int[] result = ValleyFiller.fillValley(2, 4, 8, 5, 6, 7, 9, 8, 11);
int[] expectedResult = { 2, 4, 8, 8, 8, 8, 9, 9, 11 };
assertThat(result, equalTo(expectedResult));
}
}