~ubuntu-branches/ubuntu/trusty/gmetrics/trusty

« back to all changes in this revision

Viewing changes to src/test/groovy/org/gmetrics/metric/coverage/RatioTest.groovy

  • Committer: Package Import Robot
  • Author(s): Miguel Landaeta, Miguel Landaeta, tony mancill
  • Date: 2012-01-18 20:57:50 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120118205750-68fv86p7fs8xz470
Tags: 0.5-1
[Miguel Landaeta]
* New upstream release.
* Remove patch ftbfs_613266.diff since it was merged upstream.
* Update dates in copyright file.

[tony mancill]
* Set DMUA flag.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2011 the original author or authors.
 
3
 *
 
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
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
16
 package org.gmetrics.metric.coverage
 
17
 
 
18
import org.gmetrics.test.AbstractTestCase
 
19
 
 
20
/**
 
21
 * Tests for Ratio
 
22
 *
 
23
 * @author Chris Mair
 
24
 */
 
25
class RatioTest extends AbstractTestCase {
 
26
 
 
27
    void testConstructor_AssignsFields() {
 
28
        def ratio = new Ratio(6, 7)
 
29
        assertRatio(ratio, 6, 7)
 
30
    }
 
31
 
 
32
    void testZERO() {
 
33
        assertRatio(Ratio.ZERO, 0, 0)
 
34
    }
 
35
 
 
36
    void testPlus() {
 
37
        def ratio = new Ratio(6, 7)
 
38
        def sumRatio = ratio + new Ratio(5, 20)
 
39
        assertRatio(sumRatio, 11, 27)
 
40
    }
 
41
 
 
42
    void testPlus_NullRatio_ThrowsException() {
 
43
        def ratio = new Ratio(6, 7)
 
44
        shouldFailWithMessageContaining('ratio') { ratio + null  }
 
45
    }
 
46
 
 
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
 
52
    }
 
53
 
 
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
 
61
    }
 
62
 
 
63
    void testToBigDecimal_DivideByZero() {
 
64
        assert new Ratio(6, 0).toBigDecimal(2, BigDecimal.ROUND_HALF_UP) == 0.0
 
65
    }
 
66
 
 
67
    void testAsType_OtherThanBigDecimal_ThrowsException() {
 
68
        shouldFail { new Ratio(6, 8) as Integer }
 
69
    }
 
70
 
 
71
    private void assertRatio(Ratio ratio, int numerator, int denominator) {
 
72
        assert ratio.numerator == numerator
 
73
        assert ratio.denominator == denominator
 
74
    }
 
75
 
 
76
}