|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.fineract.infrastructure.core.service; |
| 21 | + |
| 22 | +import java.math.BigDecimal; |
| 23 | +import java.math.MathContext; |
| 24 | +import org.junit.jupiter.api.Assertions; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +class MathUtilTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void nullToDefaultReturnsDefaultWhenValueNull() { |
| 31 | + Long result = MathUtil.nullToDefault(null, 5L); |
| 32 | + Assertions.assertEquals(5L, result); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void nullToDefaultReturnsValueWhenNotNull() { |
| 37 | + Long result = MathUtil.nullToDefault(10L, 5L); |
| 38 | + Assertions.assertEquals(10L, result); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void nullToZeroLongReturnsZeroWhenNull() { |
| 43 | + Long result = MathUtil.nullToZero((Long) null); |
| 44 | + Assertions.assertEquals(0L, result); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void zeroToNullReturnsNullWhenZero() { |
| 49 | + Long result = MathUtil.zeroToNull(0L); |
| 50 | + Assertions.assertNull(result); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void negativeToZeroReturnsZeroForNegativeValue() { |
| 55 | + Long result = MathUtil.negativeToZero(-10L); |
| 56 | + Assertions.assertEquals(0L, result); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void negativeToZeroReturnsSameValueForPositive() { |
| 61 | + Long result = MathUtil.negativeToZero(20L); |
| 62 | + Assertions.assertEquals(20L, result); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void isGreaterThanZeroReturnsTrueForPositive() { |
| 67 | + Assertions.assertTrue(MathUtil.isGreaterThanZero(5L)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void isLessThanZeroReturnsTrueForNegative() { |
| 72 | + Assertions.assertTrue(MathUtil.isLessThanZero(-5L)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void addHandlesNullValues() { |
| 77 | + Long result = MathUtil.add(null, 10L); |
| 78 | + Assertions.assertEquals(10L, result); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void addReturnsSumOfValues() { |
| 83 | + Long result = MathUtil.add(10L, 20L); |
| 84 | + Assertions.assertEquals(30L, result); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void subtractReturnsCorrectDifference() { |
| 89 | + Long result = MathUtil.subtract(20L, 5L); |
| 90 | + Assertions.assertEquals(15L, result); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void absReturnsPositiveValue() { |
| 95 | + Long result = MathUtil.abs(-25L); |
| 96 | + Assertions.assertEquals(25L, result); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void bigDecimalAddReturnsCorrectSum() { |
| 101 | + BigDecimal result = MathUtil.add(new BigDecimal("10.5"), new BigDecimal("5.5"), new MathContext(10)); |
| 102 | + |
| 103 | + Assertions.assertEquals(new BigDecimal("16.0"), result); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void bigDecimalSubtractReturnsCorrectDifference() { |
| 108 | + BigDecimal result = MathUtil.subtract(new BigDecimal("20"), new BigDecimal("5"), new MathContext(10)); |
| 109 | + |
| 110 | + Assertions.assertEquals(new BigDecimal("15"), result); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void percentageOfCalculatesCorrectValue() { |
| 115 | + BigDecimal value = new BigDecimal("200"); |
| 116 | + BigDecimal percentage = new BigDecimal("10"); |
| 117 | + |
| 118 | + BigDecimal result = MathUtil.percentageOf(value, percentage, new MathContext(10)); |
| 119 | + |
| 120 | + Assertions.assertEquals(0, result.compareTo(new BigDecimal("20"))); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void percentageOfReturnsZeroWhenValueZero() { |
| 125 | + BigDecimal result = MathUtil.percentageOf(BigDecimal.ZERO, new BigDecimal("10"), new MathContext(10)); |
| 126 | + Assertions.assertEquals(BigDecimal.ZERO, result); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void stripTrailingZerosRemovesExtraZeros() { |
| 131 | + BigDecimal value = new BigDecimal("10.5000"); |
| 132 | + |
| 133 | + BigDecimal result = MathUtil.stripTrailingZeros(value); |
| 134 | + |
| 135 | + Assertions.assertEquals(new BigDecimal("10.5"), result); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void stripTrailingZerosReturnsNullWhenInputNull() { |
| 140 | + Assertions.assertNull(MathUtil.stripTrailingZeros(null)); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments