aboutsummaryrefslogtreecommitdiff
path: root/ClojureCLR/Clojure/Clojure.Tests/LibTests/NumbersTests.cs
blob: e5c869fc70f032452fc5467067049a6e86be2043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 *   Copyright (c) David Miller. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using Rhino.Mocks;

using clojure.lang;
using java.math;

namespace Clojure.Tests.LibTests
{
    [TestFixture]
    public class NumbersTests : AssertionHelper
    {
        #region Helpers

        private void ExpectInt32(object x)
        {
            Expect(x, TypeOf(typeof(Int32)));
        }

        private void ExpectSameObject(object x, object y)
        {
            Expect(x, SameAs(y));
        }

        private void ExpectEqualObject(object x, object y)
        {
            Expect(x, EqualTo(y));
        }

        #endregion

        #region reduce tests

        [Test]
        public void ReduceOnBigIntReducesSmallerValues()
        {
            BigInteger b1 = new BigInteger("123");
            BigInteger b2 = new BigInteger("0");
            BigInteger b3 = new BigInteger(Int32.MaxValue.ToString());
            BigInteger b4 = new BigInteger(Int32.MinValue.ToString());
                
            ExpectInt32(Numbers.reduce(b1));
            ExpectInt32(Numbers.reduce(b2));
            ExpectInt32(Numbers.reduce(b3));
            ExpectInt32(Numbers.reduce(b4));
        }

        [Test]
        public void ReduceOnBigIntReturnsLargerValues()
        {
            BigInteger b1 = new BigInteger("100000000000000000000", 16);
            BigInteger b2 = b1.negate();
            BigInteger b3 = new BigInteger("123456789012345678901234567890");
            BigInteger b4 = b3.negate();

            ExpectSameObject(b1, Numbers.reduce(b1));
            ExpectSameObject(b2, Numbers.reduce(b2));
            ExpectSameObject(b3, Numbers.reduce(b3));
            ExpectSameObject(b4, Numbers.reduce(b4));
        }

        [Test]
        public void ReduceOnLongReducesSmallerValues()
        {
            long b1 = 123;
            long b2 = 0;
            long b3 = Int32.MaxValue;
            long b4 = Int32.MinValue;

            ExpectInt32(Numbers.reduce(b1));
            ExpectInt32(Numbers.reduce(b2));
            ExpectInt32(Numbers.reduce(b3));
            ExpectInt32(Numbers.reduce(b4));
        }


        [Test]
        public void ReduceOnLongReturnsLargerValues()
        {
            long b1 = ((long)Int32.MaxValue) + 1;
            long b2 = ((long)Int32.MinValue) - 1;
            long b3 = 123456789000;
            long b4 = -b3;

            ExpectEqualObject(b1, Numbers.reduce(b1));
            ExpectEqualObject(b2, Numbers.reduce(b2));
            ExpectEqualObject(b3, Numbers.reduce(b3));
            ExpectEqualObject(b4, Numbers.reduce(b4));
        }

        #endregion

        #region divide tests

        [Test]
        [ExpectedException(typeof(ArithmeticException))]
        public void DivideByZeroFails()
        {
            object o = Numbers.BIDivide(Numbers.BigIntegerOne, Numbers.BigIntegerZero);
        }

        [Test]
        public void DivideReducesToIntOnDenomOne()
        {
            object o = Numbers.BIDivide(new BigInteger("75"), new BigInteger("25"));
            Expect(o, EqualTo(3));
        }

        [Test]
        public void DivideReturnsReducedRatio()
        {
            object o = Numbers.BIDivide(new BigInteger("42"), new BigInteger("30"));
            
            Expect(o, TypeOf(typeof(Ratio)));
            
            Ratio r = o as Ratio;
            Expect(r.Numerator, EqualTo(new BigInteger("7")));
            Expect(r.Denominator, EqualTo(new BigInteger("5")));
        }

        #endregion
    }
}