2
* Copyright 2011 the original author or authors.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.gmetrics.metric.coverage
18
import org.gmetrics.test.AbstractTestCase
25
class RatioTest extends AbstractTestCase {
27
void testConstructor_AssignsFields() {
28
def ratio = new Ratio(6, 7)
29
assertRatio(ratio, 6, 7)
33
assertRatio(Ratio.ZERO, 0, 0)
37
def ratio = new Ratio(6, 7)
38
def sumRatio = ratio + new Ratio(5, 20)
39
assertRatio(sumRatio, 11, 27)
42
void testPlus_NullRatio_ThrowsException() {
43
def ratio = new Ratio(6, 7)
44
shouldFailWithMessageContaining('ratio') { ratio + null }
47
void testAsBigDecimal() {
48
assert new Ratio(6, 8) as BigDecimal == 0.75
49
assert new Ratio(1, 5) as BigDecimal == 0.2
50
assert new Ratio(5, 5) as BigDecimal == 1.0
51
assert new Ratio(0, 8) as BigDecimal == 0.0
54
void testToBigDecimal() {
55
assert new Ratio(6, 8).toBigDecimal(2, BigDecimal.ROUND_HALF_UP) == 0.75
56
assert new Ratio(6, 8).toBigDecimal(1, BigDecimal.ROUND_HALF_UP) == 0.8
57
assert new Ratio(745, 1000).toBigDecimal(2, BigDecimal.ROUND_HALF_DOWN) == 0.74
58
assert new Ratio(1, 5).toBigDecimal(1, BigDecimal.ROUND_HALF_UP) == 0.2
59
assert new Ratio(5, 5).toBigDecimal(3, BigDecimal.ROUND_UNNECESSARY) == 1.000
60
assert new Ratio(0, 8).toBigDecimal(3, BigDecimal.ROUND_HALF_DOWN) == 0.0
63
void testToBigDecimal_DivideByZero() {
64
assert new Ratio(6, 0).toBigDecimal(2, BigDecimal.ROUND_HALF_UP) == 0.0
67
void testAsType_OtherThanBigDecimal_ThrowsException() {
68
shouldFail { new Ratio(6, 8) as Integer }
71
private void assertRatio(Ratio ratio, int numerator, int denominator) {
72
assert ratio.numerator == numerator
73
assert ratio.denominator == denominator