Source Code Cross Referenced for BigIntegerModPowTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » tests » java » math » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.tests.java.math 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Elena Semukhina
019:         * @version $Revision$
020:         */package org.apache.harmony.tests.java.math;
021:
022:        import junit.framework.TestCase;
023:        import java.math.BigInteger;
024:
025:        /**
026:         * Class:   java.math.BigInteger
027:         * Methods: modPow, modInverse, and gcd 
028:         */
029:        public class BigIntegerModPowTest extends TestCase {
030:            /**
031:             * modPow: non-positive modulus
032:             */
033:            public void testModPowException() {
034:                byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
035:                byte eBytes[] = { 1, 2, 3, 4, 5 };
036:                byte mBytes[] = { 1, 2, 3 };
037:                int aSign = 1;
038:                int eSign = 1;
039:                int mSign = -1;
040:                BigInteger aNumber = new BigInteger(aSign, aBytes);
041:                BigInteger exp = new BigInteger(eSign, eBytes);
042:                BigInteger modulus = new BigInteger(mSign, mBytes);
043:                try {
044:                    aNumber.modPow(exp, modulus);
045:                    fail("ArithmeticException has not been caught");
046:                } catch (ArithmeticException e) {
047:                    assertEquals("Improper exception message",
048:                            "BigInteger: modulus not positive", e.getMessage());
049:                }
050:            }
051:
052:            /**
053:             * modPow: positive exponent
054:             */
055:            public void testModPowPosExp() {
056:                byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75,
057:                        48, -7 };
058:                byte eBytes[] = { 27, -15, 65, 39 };
059:                byte mBytes[] = { -128, 2, 3, 4, 5 };
060:                int aSign = 1;
061:                int eSign = 1;
062:                int mSign = 1;
063:                byte rBytes[] = { 113, 100, -84, -28, -85 };
064:                BigInteger aNumber = new BigInteger(aSign, aBytes);
065:                BigInteger exp = new BigInteger(eSign, eBytes);
066:                BigInteger modulus = new BigInteger(mSign, mBytes);
067:                BigInteger result = aNumber.modPow(exp, modulus);
068:                byte resBytes[] = new byte[rBytes.length];
069:                resBytes = result.toByteArray();
070:                for (int i = 0; i < resBytes.length; i++) {
071:                    assertTrue(resBytes[i] == rBytes[i]);
072:                }
073:                assertEquals("incorrect sign", 1, result.signum());
074:            }
075:
076:            /**
077:             * modPow: negative exponent
078:             */
079:            public void testModPowNegExp() {
080:                byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75,
081:                        48, -7 };
082:                byte eBytes[] = { 27, -15, 65, 39 };
083:                byte mBytes[] = { -128, 2, 3, 4, 5 };
084:                int aSign = 1;
085:                int eSign = -1;
086:                int mSign = 1;
087:                byte rBytes[] = { 12, 118, 46, 86, 92 };
088:                BigInteger aNumber = new BigInteger(aSign, aBytes);
089:                BigInteger exp = new BigInteger(eSign, eBytes);
090:                BigInteger modulus = new BigInteger(mSign, mBytes);
091:                BigInteger result = aNumber.modPow(exp, modulus);
092:                byte resBytes[] = new byte[rBytes.length];
093:                resBytes = result.toByteArray();
094:                for (int i = 0; i < resBytes.length; i++) {
095:                    assertTrue(resBytes[i] == rBytes[i]);
096:                }
097:                assertEquals("incorrect sign", 1, result.signum());
098:            }
099:
100:            /**
101:             * modInverse: non-positive modulus
102:             */
103:            public void testmodInverseException() {
104:                byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
105:                byte mBytes[] = { 1, 2, 3 };
106:                int aSign = 1;
107:                int mSign = -1;
108:                BigInteger aNumber = new BigInteger(aSign, aBytes);
109:                BigInteger modulus = new BigInteger(mSign, mBytes);
110:                try {
111:                    aNumber.modInverse(modulus);
112:                    fail("ArithmeticException has not been caught");
113:                } catch (ArithmeticException e) {
114:                    assertEquals("Improper exception message",
115:                            "BigInteger: modulus not positive", e.getMessage());
116:                }
117:            }
118:
119:            /**
120:             * modInverse: non-invertible number
121:             */
122:            public void testmodInverseNonInvertible() {
123:                byte aBytes[] = { -15, 24, 123, 56, -11, -112, -34, -98, 8, 10,
124:                        12, 14, 25, 125, -15, 28, -127 };
125:                byte mBytes[] = { -12, 1, 0, 0, 0, 23, 44, 55, 66 };
126:                int aSign = 1;
127:                int mSign = 1;
128:                BigInteger aNumber = new BigInteger(aSign, aBytes);
129:                BigInteger modulus = new BigInteger(mSign, mBytes);
130:                try {
131:                    aNumber.modInverse(modulus);
132:                    fail("ArithmeticException has not been caught");
133:                } catch (ArithmeticException e) {
134:                    assertEquals("Improper exception message",
135:                            "BigInteger not invertible.", e.getMessage());
136:                }
137:            }
138:
139:            /**
140:             * modInverse: positive number
141:             */
142:            public void testmodInversePos1() {
143:                byte aBytes[] = { 24, 123, 56, -11, -112, -34, -98, 8, 10, 12,
144:                        14, 25, 125, -15, 28, -127 };
145:                byte mBytes[] = { 122, 45, 36, 100, 122, 45 };
146:                int aSign = 1;
147:                int mSign = 1;
148:                byte rBytes[] = { 47, 3, 96, 62, 87, 19 };
149:                BigInteger aNumber = new BigInteger(aSign, aBytes);
150:                BigInteger modulus = new BigInteger(mSign, mBytes);
151:                BigInteger result = aNumber.modInverse(modulus);
152:                byte resBytes[] = new byte[rBytes.length];
153:                resBytes = result.toByteArray();
154:                for (int i = 0; i < resBytes.length; i++) {
155:                    assertTrue(resBytes[i] == rBytes[i]);
156:                }
157:                assertEquals("incorrect sign", 1, result.signum());
158:            }
159:
160:            /**
161:             * modInverse: positive number (another case: a < 0)
162:             */
163:            public void testmodInversePos2() {
164:                byte aBytes[] = { 15, 24, 123, 56, -11, -112, -34, -98, 8, 10,
165:                        12, 14, 25, 125, -15, 28, -127 };
166:                byte mBytes[] = { 2, 122, 45, 36, 100 };
167:                int aSign = 1;
168:                int mSign = 1;
169:                byte rBytes[] = { 1, -93, 40, 127, 73 };
170:                BigInteger aNumber = new BigInteger(aSign, aBytes);
171:                BigInteger modulus = new BigInteger(mSign, mBytes);
172:                BigInteger result = aNumber.modInverse(modulus);
173:                byte resBytes[] = new byte[rBytes.length];
174:                resBytes = result.toByteArray();
175:                for (int i = 0; i < resBytes.length; i++) {
176:                    assertTrue(resBytes[i] == rBytes[i]);
177:                }
178:                assertEquals("incorrect sign", 1, result.signum());
179:            }
180:
181:            /**
182:             * modInverse: negative number
183:             */
184:            public void testmodInverseNeg1() {
185:                byte aBytes[] = { 15, 24, 123, 56, -11, -112, -34, -98, 8, 10,
186:                        12, 14, 25, 125, -15, 28, -127 };
187:                byte mBytes[] = { 2, 122, 45, 36, 100 };
188:                int aSign = -1;
189:                int mSign = 1;
190:                byte rBytes[] = { 0, -41, 4, -91, 27 };
191:                BigInteger aNumber = new BigInteger(aSign, aBytes);
192:                BigInteger modulus = new BigInteger(mSign, mBytes);
193:                BigInteger result = aNumber.modInverse(modulus);
194:                byte resBytes[] = new byte[rBytes.length];
195:                resBytes = result.toByteArray();
196:                for (int i = 0; i < resBytes.length; i++) {
197:                    assertTrue(resBytes[i] == rBytes[i]);
198:                }
199:                assertEquals("incorrect sign", 1, result.signum());
200:            }
201:
202:            /**
203:             * modInverse: negative number (another case: x < 0)
204:             */
205:            public void testmodInverseNeg2() {
206:                byte aBytes[] = { -15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
207:                        123, 57 };
208:                byte mBytes[] = { 122, 2, 4, 122, 2, 4 };
209:                byte rBytes[] = { 85, 47, 127, 4, -128, 45 };
210:                BigInteger aNumber = new BigInteger(aBytes);
211:                BigInteger modulus = new BigInteger(mBytes);
212:                BigInteger result = aNumber.modInverse(modulus);
213:                byte resBytes[] = new byte[rBytes.length];
214:                resBytes = result.toByteArray();
215:                for (int i = 0; i < resBytes.length; i++) {
216:                    assertTrue(resBytes[i] == rBytes[i]);
217:                }
218:                assertEquals("incorrect sign", 1, result.signum());
219:            }
220:
221:            /**
222:             * gcd: the second number is zero
223:             */
224:            public void testGcdSecondZero() {
225:                byte aBytes[] = { 15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
226:                        123, 57 };
227:                byte bBytes[] = { 0 };
228:                int aSign = 1;
229:                int bSign = 1;
230:                byte rBytes[] = { 15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
231:                        123, 57 };
232:                BigInteger aNumber = new BigInteger(aSign, aBytes);
233:                BigInteger bNumber = new BigInteger(bSign, bBytes);
234:                BigInteger result = aNumber.gcd(bNumber);
235:                byte resBytes[] = new byte[rBytes.length];
236:                resBytes = result.toByteArray();
237:                for (int i = 0; i < resBytes.length; i++) {
238:                    assertTrue(resBytes[i] == rBytes[i]);
239:                }
240:                assertEquals("incorrect sign", 1, result.signum());
241:            }
242:
243:            /**
244:             * gcd: the first number is zero
245:             */
246:            public void testGcdFirstZero() {
247:                byte aBytes[] = { 0 };
248:                byte bBytes[] = { 15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
249:                        123, 57 };
250:                int aSign = 1;
251:                int bSign = 1;
252:                byte rBytes[] = { 15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
253:                        123, 57 };
254:                BigInteger aNumber = new BigInteger(aSign, aBytes);
255:                BigInteger bNumber = new BigInteger(bSign, bBytes);
256:                BigInteger result = aNumber.gcd(bNumber);
257:                byte resBytes[] = new byte[rBytes.length];
258:                resBytes = result.toByteArray();
259:                for (int i = 0; i < resBytes.length; i++) {
260:                    assertTrue(resBytes[i] == rBytes[i]);
261:                }
262:                assertEquals("incorrect sign", 1, result.signum());
263:            }
264:
265:            /**
266:             * gcd: the first number is ZERO
267:             */
268:            public void testGcdFirstZERO() {
269:                byte bBytes[] = { 15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
270:                        123, 57 };
271:                int bSign = 1;
272:                byte rBytes[] = { 15, 24, 123, 57, -15, 24, 123, 57, -15, 24,
273:                        123, 57 };
274:                BigInteger aNumber = BigInteger.ZERO;
275:                BigInteger bNumber = new BigInteger(bSign, bBytes);
276:                BigInteger result = aNumber.gcd(bNumber);
277:                byte resBytes[] = new byte[rBytes.length];
278:                resBytes = result.toByteArray();
279:                for (int i = 0; i < resBytes.length; i++) {
280:                    assertTrue(resBytes[i] == rBytes[i]);
281:                }
282:                assertEquals("incorrect sign", 1, result.signum());
283:            }
284:
285:            /**
286:             * gcd: both numbers are zeros
287:             */
288:            public void testGcdBothZeros() {
289:                byte rBytes[] = { 0 };
290:                BigInteger aNumber = new BigInteger("0");
291:                BigInteger bNumber = BigInteger.valueOf(0L);
292:                BigInteger result = aNumber.gcd(bNumber);
293:                byte resBytes[] = result.toByteArray();
294:                for (int i = 0; i < resBytes.length; i++) {
295:                    assertTrue(resBytes[i] == rBytes[i]);
296:                }
297:                assertEquals("incorrect sign", 0, result.signum());
298:            }
299:
300:            /**
301:             * gcd: the first number is longer
302:             */
303:            public void testGcdFirstLonger() {
304:                byte aBytes[] = { -15, 24, 123, 56, -11, -112, -34, -98, 8, 10,
305:                        12, 14, 25, 125, -15, 28, -127 };
306:                byte bBytes[] = { -12, 1, 0, 0, 0, 23, 44, 55, 66 };
307:                int aSign = 1;
308:                int bSign = 1;
309:                byte rBytes[] = { 7 };
310:                BigInteger aNumber = new BigInteger(aSign, aBytes);
311:                BigInteger bNumber = new BigInteger(bSign, bBytes);
312:                BigInteger result = aNumber.gcd(bNumber);
313:                byte resBytes[] = new byte[rBytes.length];
314:                resBytes = result.toByteArray();
315:                for (int i = 0; i < resBytes.length; i++) {
316:                    assertTrue(resBytes[i] == rBytes[i]);
317:                }
318:                assertEquals("incorrect sign", 1, result.signum());
319:            }
320:
321:            /**
322:             * gcd: the second number is longer
323:             */
324:            public void testGcdSecondLonger() {
325:                byte aBytes[] = { -12, 1, 0, 0, 0, 23, 44, 55, 66 };
326:                byte bBytes[] = { -15, 24, 123, 56, -11, -112, -34, -98, 8, 10,
327:                        12, 14, 25, 125, -15, 28, -127 };
328:                int aSign = 1;
329:                int bSign = 1;
330:                byte rBytes[] = { 7 };
331:                BigInteger aNumber = new BigInteger(aSign, aBytes);
332:                BigInteger bNumber = new BigInteger(bSign, bBytes);
333:                BigInteger result = aNumber.gcd(bNumber);
334:                byte resBytes[] = new byte[rBytes.length];
335:                resBytes = result.toByteArray();
336:                for (int i = 0; i < resBytes.length; i++) {
337:                    assertTrue(resBytes[i] == rBytes[i]);
338:                }
339:                assertEquals("incorrect sign", 1, result.signum());
340:            }
341:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.