Search in sources :

Example 1 with Decimal

use of de.invesdwin.util.math.decimal.Decimal in project invesdwin-context-matlab by subes.

the class InputsAndResultsTestPercent method testPercent.

public void testPercent() {
    // putPercent
    final Percent putPercent = new Percent(123.123D, PercentScale.RATE);
    // putPercentVector
    final Percent[] putPercentVector = new Percent[3];
    for (int i = 0; i < putPercentVector.length; i++) {
        putPercentVector[i] = new Percent(new Decimal((i + 1) + "." + (i + 1)), PercentScale.RATE);
    }
    // putPercentVectorAsList
    final List<Percent> putPercentVectorAsList = Arrays.asList(putPercentVector);
    // putPercentMatrix
    final Percent[][] putPercentMatrix = new Percent[4][];
    for (int row = 0; row < putPercentMatrix.length; row++) {
        final Percent[] vector = new Percent[3];
        for (int col = 0; col < vector.length; col++) {
            vector[col] = new Percent(new Decimal((row + 1) + "." + (col + 1)), PercentScale.RATE);
        }
        putPercentMatrix[row] = vector;
    }
    // putPercentMatrixAsList
    final List<List<Percent>> putPercentMatrixAsList = new ArrayList<List<Percent>>(putPercentMatrix.length);
    for (final Percent[] vector : putPercentMatrix) {
        putPercentMatrixAsList.add(Arrays.asList(vector));
    }
    new AScriptTaskMatlab<Void>() {

        @Override
        public void populateInputs(final IScriptTaskInputs inputs) {
            inputs.putDecimal("putPercent", putPercent);
            inputs.putDecimalVector("putPercentVector", putPercentVector);
            inputs.putDecimalVectorAsList("putPercentVectorAsList", putPercentVectorAsList);
            inputs.putDecimalMatrix("putPercentMatrix", putPercentMatrix);
            inputs.putDecimalMatrixAsList("putPercentMatrixAsList", putPercentMatrixAsList);
        }

        @Override
        public void executeScript(final IScriptTaskEngine engine) {
            engine.eval(new ClassPathResource(InputsAndResultsTestPercent.class.getSimpleName() + ".sce", InputsAndResultsTestPercent.class));
        }

        @Override
        public Void extractResults(final IScriptTaskResults results) {
            // getPercent
            final Percent getPercent = results.getDecimal("getPercent", Percent.ZERO_PERCENT);
            Assertions.assertThat(putPercent).isEqualTo(getPercent);
            // getPercentVector
            final Percent[] getPercentVector = results.getDecimalVector("getPercentVector", Percent.ZERO_PERCENT);
            Assertions.assertThat(putPercentVector).isEqualTo(getPercentVector);
            // getPercentVectorAsList
            final List<Percent> getPercentVectorAsList = results.getDecimalVectorAsList("getPercentVectorAsList", Percent.ZERO_PERCENT);
            Assertions.assertThat(putPercentVectorAsList).isEqualTo(getPercentVectorAsList);
            // getPercentMatrix
            final Percent[][] getPercentMatrix = results.getDecimalMatrix("getPercentMatrix", Percent.ZERO_PERCENT);
            Assertions.assertThat(putPercentMatrix).isEqualTo(getPercentMatrix);
            // getPercentMatrixAsList
            final List<List<Percent>> getPercentMatrixAsList = results.getDecimalMatrixAsList("getPercentMatrixAsList", Percent.ZERO_PERCENT);
            Assertions.assertThat(putPercentMatrixAsList).isEqualTo(getPercentMatrixAsList);
            return null;
        }
    }.run(runner);
}
Also used : Percent(de.invesdwin.util.math.decimal.scaled.Percent) ArrayList(java.util.ArrayList) IScriptTaskEngine(de.invesdwin.context.integration.script.IScriptTaskEngine) ClassPathResource(org.springframework.core.io.ClassPathResource) IScriptTaskInputs(de.invesdwin.context.integration.script.IScriptTaskInputs) IScriptTaskResults(de.invesdwin.context.integration.script.IScriptTaskResults) Decimal(de.invesdwin.util.math.decimal.Decimal) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with Decimal

use of de.invesdwin.util.math.decimal.Decimal in project invesdwin-context-persistence by subes.

the class SerdeCompressingSoftReferenceTest method testSerialization.

@Test
public void testSerialization() {
    final SerdeCompressingSoftReference<Decimal> ref = new SerdeCompressingSoftReference<Decimal>(new Decimal("100"), new RemoteFastSerializingSerde<Decimal>(false, Decimal.class));
    Assertions.assertThat(ref.get()).isNotNull();
    ref.clear();
    Assertions.assertThat(ref.get()).isNotNull();
    ref.close();
    Assertions.assertThat(ref.get()).isNull();
}
Also used : Decimal(de.invesdwin.util.math.decimal.Decimal) ATest(de.invesdwin.context.test.ATest) Test(org.junit.Test)

Example 3 with Decimal

use of de.invesdwin.util.math.decimal.Decimal in project invesdwin-context-persistence by subes.

the class SerdeCompressingSoftReferenceTest method testOutOfMemory.

@Test
@Ignore("manual test")
public void testOutOfMemory() {
    final Set<SerdeCompressingSoftReference<Decimal>> refs = new LinkedHashSet<SerdeCompressingSoftReference<Decimal>>();
    Decimal curValue = Decimal.ZERO;
    while (true) {
        curValue = curValue.add(Decimal.ONE);
        final SerdeCompressingSoftReference<Decimal> ref = new SerdeCompressingSoftReference<Decimal>(curValue, new RemoteFastSerializingSerde<Decimal>(false, Decimal.class));
        refs.add(ref);
        Assertions.assertThat(ref.get()).isNotNull();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Decimal(de.invesdwin.util.math.decimal.Decimal) Ignore(org.junit.Ignore) ATest(de.invesdwin.context.test.ATest) Test(org.junit.Test)

Example 4 with Decimal

use of de.invesdwin.util.math.decimal.Decimal in project invesdwin-context-persistence by subes.

the class TimedDecimalSerde method fromBytes.

@Override
public TimedDecimal fromBytes(final byte[] bytes) {
    final ByteBuffer buffer = ByteBuffer.wrap(bytes);
    final FDate time = FDates.extractFDate(buffer);
    final Decimal percent = Decimal.extractDecimal(buffer);
    final TimedDecimal timedMoney = new TimedDecimal(time, percent);
    return timedMoney;
}
Also used : TimedDecimal(de.invesdwin.util.math.decimal.TimedDecimal) Decimal(de.invesdwin.util.math.decimal.Decimal) ByteBuffer(java.nio.ByteBuffer) TimedDecimal(de.invesdwin.util.math.decimal.TimedDecimal) FDate(de.invesdwin.util.time.fdate.FDate)

Example 5 with Decimal

use of de.invesdwin.util.math.decimal.Decimal in project invesdwin-context-matlab by subes.

the class InputsAndResultsTestEmptyMatrixValue method testEmptyMatrixValue.

// CHECKSTYLE:OFF NCSS
public void testEmptyMatrixValue() {
    // CHECKSTYLE:ON
    final boolean[][] putBooleanMatrix = new boolean[2][];
    for (int i = 0; i < putBooleanMatrix.length; i++) {
        putBooleanMatrix[i] = new boolean[0];
    }
    final List<List<Boolean>> putBooleanMatrixAsList = Booleans.asListMatrix(putBooleanMatrix);
    final byte[][] putByteMatrix = new byte[2][];
    for (int i = 0; i < putByteMatrix.length; i++) {
        putByteMatrix[i] = new byte[0];
    }
    final List<List<Byte>> putByteMatrixAsList = Bytes.asListMatrix(putByteMatrix);
    final char[][] putCharacterMatrix = new char[2][];
    for (int i = 0; i < putCharacterMatrix.length; i++) {
        putCharacterMatrix[i] = new char[0];
    }
    final List<List<Character>> putCharacterMatrixAsList = Characters.asListMatrix(putCharacterMatrix);
    final Decimal[][] putDecimalMatrix = new Decimal[2][];
    for (int i = 0; i < putDecimalMatrix.length; i++) {
        putDecimalMatrix[i] = new Decimal[0];
    }
    final List<List<Decimal>> putDecimalMatrixAsList = Decimal.asListMatrix(putDecimalMatrix);
    final double[][] putDoubleMatrix = new double[2][];
    for (int i = 0; i < putDoubleMatrix.length; i++) {
        putDoubleMatrix[i] = new double[0];
    }
    final List<List<Double>> putDoubleMatrixAsList = Doubles.asListMatrix(putDoubleMatrix);
    final float[][] putFloatMatrix = new float[2][];
    for (int i = 0; i < putFloatMatrix.length; i++) {
        putFloatMatrix[i] = new float[0];
    }
    final List<List<Float>> putFloatMatrixAsList = Floats.asListMatrix(putFloatMatrix);
    final int[][] putIntegerMatrix = new int[2][];
    for (int i = 0; i < putIntegerMatrix.length; i++) {
        putIntegerMatrix[i] = new int[0];
    }
    final List<List<Integer>> putIntegerMatrixAsList = Integers.asListMatrix(putIntegerMatrix);
    final long[][] putLongMatrix = new long[2][];
    for (int i = 0; i < putLongMatrix.length; i++) {
        putLongMatrix[i] = new long[0];
    }
    final List<List<Long>> putLongMatrixAsList = Longs.asListMatrix(putLongMatrix);
    final Percent[][] putPercentMatrix = new Percent[2][];
    for (int i = 0; i < putPercentMatrix.length; i++) {
        putPercentMatrix[i] = new Percent[0];
    }
    final List<List<Percent>> putPercentMatrixAsList = Percent.asListMatrix(putPercentMatrix);
    final short[][] putShortMatrix = new short[2][];
    for (int i = 0; i < putShortMatrix.length; i++) {
        putShortMatrix[i] = new short[0];
    }
    final List<List<Short>> putShortMatrixAsList = Shorts.asListMatrix(putShortMatrix);
    final String[][] putStringMatrix = new String[2][];
    for (int i = 0; i < putStringMatrix.length; i++) {
        putStringMatrix[i] = new String[0];
    }
    final List<List<String>> putStringMatrixAsList = Strings.asListMatrix(putStringMatrix);
    new AScriptTaskMatlab<Void>() {

        @Override
        public void populateInputs(final IScriptTaskInputs inputs) {
            inputs.putBooleanMatrix("putBooleanMatrix", putBooleanMatrix);
            inputs.putBooleanMatrixAsList("putBooleanMatrixAsList", putBooleanMatrixAsList);
            inputs.putByteMatrix("putByteMatrix", putByteMatrix);
            inputs.putByteMatrixAsList("putByteMatrixAsList", putByteMatrixAsList);
            inputs.putCharacterMatrix("putCharacterMatrix", putCharacterMatrix);
            inputs.putCharacterMatrixAsList("putCharacterMatrixAsList", putCharacterMatrixAsList);
            inputs.putDecimalMatrix("putDecimalMatrix", putDecimalMatrix);
            inputs.putDecimalMatrixAsList("putDecimalMatrixAsList", putDecimalMatrixAsList);
            inputs.putDoubleMatrix("putDoubleMatrix", putDoubleMatrix);
            inputs.putDoubleMatrixAsList("putDoubleMatrixAsList", putDoubleMatrixAsList);
            inputs.putFloatMatrix("putFloatMatrix", putFloatMatrix);
            inputs.putFloatMatrixAsList("putFloatMatrixAsList", putFloatMatrixAsList);
            inputs.putIntegerMatrix("putIntegerMatrix", putIntegerMatrix);
            inputs.putIntegerMatrixAsList("putIntegerMatrixAsList", putIntegerMatrixAsList);
            inputs.putLongMatrix("putLongMatrix", putLongMatrix);
            inputs.putLongMatrixAsList("putLongMatrixAsList", putLongMatrixAsList);
            inputs.putDecimalMatrix("putPercentMatrix", putPercentMatrix);
            inputs.putDecimalMatrixAsList("putPercentMatrixAsList", putPercentMatrixAsList);
            inputs.putShortMatrix("putShortMatrix", putShortMatrix);
            inputs.putShortMatrixAsList("putShortMatrixAsList", putShortMatrixAsList);
            inputs.putStringMatrix("putStringMatrix", putStringMatrix);
            inputs.putStringMatrixAsList("putStringMatrixAsList", putStringMatrixAsList);
        }

        @Override
        public void executeScript(final IScriptTaskEngine engine) {
            try {
                final String script = IOUtils.toString(new ClassPathResource(InputsAndResultsTestEmptyMatrixValue.class.getSimpleName() + ".m", InputsAndResultsTestNull.class).getInputStream(), Charset.defaultCharset());
                if (!engine.getResults().getBoolean("exist('OCTAVE_VERSION', 'builtin') > 0")) {
                    // workaround for matlab and isempty not counting empty cells there
                    final StringBuilder sb = new StringBuilder();
                    // we can actually create functions in scripts without separate files: http://de.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
                    sb.append("ismatlabempty = @(x) all(cellfun(@isempty, x));\n");
                    sb.append("\n");
                    sb.append(script.replace("isempty", "ismatlabempty"));
                    engine.eval(sb.toString());
                } else {
                    engine.eval(script);
                }
            } catch (final IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public Void extractResults(final IScriptTaskResults results) {
            Assertions.checkEquals(putBooleanMatrix, results.getBooleanMatrix("getBooleanMatrix"));
            Assertions.checkEquals(putBooleanMatrixAsList, results.getBooleanMatrixAsList("getBooleanMatrixAsList"));
            Assertions.checkEquals(putByteMatrix, results.getByteMatrix("getByteMatrix"));
            Assertions.checkEquals(putByteMatrixAsList, results.getByteMatrixAsList("getByteMatrixAsList"));
            Assertions.checkEquals(putCharacterMatrix, results.getCharacterMatrix("getCharacterMatrix"));
            Assertions.checkEquals(putCharacterMatrixAsList, results.getCharacterMatrixAsList("getCharacterMatrixAsList"));
            Assertions.checkEquals(putDecimalMatrix, results.getDecimalMatrix("getDecimalMatrix"));
            Assertions.checkEquals(putDecimalMatrixAsList, results.getDecimalMatrixAsList("getDecimalMatrixAsList"));
            Assertions.checkEquals(putDoubleMatrix, results.getDoubleMatrix("getDoubleMatrix"));
            Assertions.checkEquals(putDoubleMatrixAsList, results.getDoubleMatrixAsList("getDoubleMatrixAsList"));
            Assertions.checkEquals(putFloatMatrix, results.getFloatMatrix("getFloatMatrix"));
            Assertions.checkEquals(putFloatMatrixAsList, results.getFloatMatrixAsList("getFloatMatrixAsList"));
            Assertions.checkEquals(putIntegerMatrix, results.getIntegerMatrix("getIntegerMatrix"));
            Assertions.checkEquals(putIntegerMatrixAsList, results.getIntegerMatrixAsList("getIntegerMatrixAsList"));
            Assertions.checkEquals(putLongMatrix, results.getLongMatrix("getLongMatrix"));
            Assertions.checkEquals(putLongMatrixAsList, results.getLongMatrixAsList("getLongMatrixAsList"));
            Assertions.checkEquals(putPercentMatrix, results.getDecimalMatrix("getPercentMatrix", Percent.ZERO_PERCENT));
            Assertions.checkEquals(putPercentMatrixAsList, results.getDecimalMatrixAsList("getPercentMatrixAsList", Percent.ZERO_PERCENT));
            Assertions.checkEquals(putShortMatrix, results.getShortMatrix("getShortMatrix"));
            Assertions.checkEquals(putShortMatrixAsList, results.getShortMatrixAsList("getShortMatrixAsList"));
            Assertions.checkEquals(putStringMatrix, results.getStringMatrix("getStringMatrix"));
            Assertions.checkEquals(putStringMatrixAsList, results.getStringMatrixAsList("getStringMatrixAsList"));
            return null;
        }
    }.run(runner);
}
Also used : IScriptTaskInputs(de.invesdwin.context.integration.script.IScriptTaskInputs) Decimal(de.invesdwin.util.math.decimal.Decimal) List(java.util.List) Percent(de.invesdwin.util.math.decimal.scaled.Percent) IOException(java.io.IOException) IScriptTaskEngine(de.invesdwin.context.integration.script.IScriptTaskEngine) ClassPathResource(org.springframework.core.io.ClassPathResource) IScriptTaskResults(de.invesdwin.context.integration.script.IScriptTaskResults)

Aggregations

Decimal (de.invesdwin.util.math.decimal.Decimal)9 ClassPathResource (org.springframework.core.io.ClassPathResource)6 IScriptTaskEngine (de.invesdwin.context.integration.script.IScriptTaskEngine)5 IScriptTaskInputs (de.invesdwin.context.integration.script.IScriptTaskInputs)5 IScriptTaskResults (de.invesdwin.context.integration.script.IScriptTaskResults)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Percent (de.invesdwin.util.math.decimal.scaled.Percent)3 ATest (de.invesdwin.context.test.ATest)2 Test (org.junit.Test)2 JavaOctaveScriptTaskRunnerMatlab (de.invesdwin.context.matlab.runtime.javaoctave.JavaOctaveScriptTaskRunnerMatlab)1 TimedDecimal (de.invesdwin.util.math.decimal.TimedDecimal)1 FDate (de.invesdwin.util.time.fdate.FDate)1 File (java.io.File)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedHashSet (java.util.LinkedHashSet)1 Ignore (org.junit.Ignore)1 Test (org.junit.jupiter.api.Test)1