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