Rev 13 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
31 | daniel-mar | 1 | package de.viathinksoft.immortal.gen2.math; |
5 | daniel-mar | 2 | |
3 | import static org.junit.Assert.*; |
||
4 | |||
5 | import java.math.BigInteger; |
||
6 | |||
7 | import org.junit.Test; |
||
8 | |||
31 | daniel-mar | 9 | import de.viathinksoft.immortal.gen2.math.CRTNotSolveableException; |
10 | import de.viathinksoft.immortal.gen2.math.MathUtils2; |
||
11 | import de.viathinksoft.immortal.gen2.math.RemainderNotSmallerThanModulusException; |
||
5 | daniel-mar | 12 | |
13 | public class MathUtils2Test { |
||
14 | |||
15 | @Test |
||
16 | public void powTest() { |
||
17 | BigInteger x = new BigInteger("123"); |
||
18 | |||
19 | for (int y=-3; y<0; y++) { |
||
20 | try { |
||
21 | MathUtils2.pow(x, new BigInteger(""+y)); |
||
22 | fail(); |
||
23 | } catch (ArithmeticException e) { |
||
24 | } |
||
25 | } |
||
26 | |||
27 | for (int y=0; y<=3; y++) { |
||
28 | assertEquals( |
||
29 | x.pow(y), |
||
30 | MathUtils2.pow(x, new BigInteger(""+y)) |
||
31 | ); |
||
32 | } |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @throws CRTNotSolveableException |
||
37 | * @throws RemainderNotSmallerThanModulusException |
||
38 | */ |
||
39 | |||
40 | @Test |
||
13 | daniel-mar | 41 | public void chineseRemainderTest() throws CRTException { |
5 | daniel-mar | 42 | BigInteger x; |
43 | |||
44 | // 45 2 65 3 -> Rest muss kleiner sein als Modul |
||
45 | try { |
||
46 | x = MathUtils2.chineseRemainder(new BigInteger("45"), |
||
47 | new BigInteger("2"), new BigInteger("65"), new BigInteger( |
||
48 | "3")); |
||
49 | fail(); |
||
50 | } catch (RemainderNotSmallerThanModulusException e) { |
||
51 | } |
||
52 | |||
53 | // 1 8 0 125 -> 625 |
||
54 | x = MathUtils2 |
||
55 | .chineseRemainder(new BigInteger("1"), new BigInteger("8"), |
||
56 | new BigInteger("0"), new BigInteger("125")); |
||
57 | assertEquals(new BigInteger("625"), x); |
||
58 | |||
59 | // 1 2 0 4 -> Keine Lösung |
||
60 | try { |
||
61 | x = MathUtils2.chineseRemainder(new BigInteger("1"), |
||
62 | new BigInteger("2"), new BigInteger("0"), new BigInteger( |
||
63 | "4")); |
||
64 | fail(); |
||
65 | } catch (CRTNotSolveableException e) { |
||
66 | } |
||
67 | |||
68 | // 1 6 3 8 -> 19 |
||
69 | x = MathUtils2.chineseRemainder(new BigInteger("1"), |
||
70 | new BigInteger("6"), new BigInteger("3"), new BigInteger("8")); |
||
71 | assertEquals(new BigInteger("19"), x); |
||
72 | |||
73 | // -1 7 -3 8 -> 43 |
||
74 | x = MathUtils2.chineseRemainder(new BigInteger("-1"), new BigInteger( |
||
75 | "7"), new BigInteger("-3"), new BigInteger("8")); |
||
76 | assertEquals(new BigInteger("43"), x); |
||
77 | |||
78 | // -1 -7 -3 -8 -> 43 |
||
79 | x = MathUtils2.chineseRemainder(new BigInteger("-1"), new BigInteger( |
||
80 | "-7"), new BigInteger("-3"), new BigInteger("-8")); |
||
81 | assertEquals(new BigInteger("43"), x); |
||
82 | |||
83 | // 1 -7 3 -8 -> 43 |
||
84 | x = MathUtils2.chineseRemainder(new BigInteger("1"), new BigInteger( |
||
85 | "-7"), new BigInteger("3"), new BigInteger("-8")); |
||
86 | assertEquals(new BigInteger("43"), x); |
||
87 | } |
||
88 | } |