1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Elena Semukhina 19 * @version $Revision$ 20 */ 21 22 package org.apache.harmony.math.tests.java.math; 23 24 import dalvik.annotation.TestTargetClass; 25 import dalvik.annotation.TestTargets; 26 import dalvik.annotation.TestLevel; 27 import dalvik.annotation.TestTargetNew; 28 29 import java.math.BigInteger; 30 31 import junit.framework.TestCase; 32 @TestTargetClass(BigInteger.class) 33 /** 34 * Class: java.math.BigInteger 35 * Methods: modPow, modInverse, and gcd 36 */ 37 public class BigIntegerModPowTest extends TestCase { 38 /** 39 * modPow: non-positive modulus 40 */ 41 @TestTargetNew( 42 level = TestLevel.PARTIAL_COMPLETE, 43 notes = "This is a complete subset of tests for modPow method.", 44 method = "modPow", 45 args = {java.math.BigInteger.class, java.math.BigInteger.class} 46 ) 47 public void testModPowException() { 48 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 49 byte eBytes[] = {1, 2, 3, 4, 5}; 50 byte mBytes[] = {1, 2, 3}; 51 int aSign = 1; 52 int eSign = 1; 53 int mSign = -1; 54 BigInteger aNumber = new BigInteger(aSign, aBytes); 55 BigInteger exp = new BigInteger(eSign, eBytes); 56 BigInteger modulus = new BigInteger(mSign, mBytes); 57 try { 58 aNumber.modPow(exp, modulus); 59 fail("ArithmeticException has not been caught"); 60 } catch (ArithmeticException e) { 61 assertEquals("Improper exception message", "BigInteger: modulus not positive", e.getMessage()); 62 } 63 } 64 65 /** 66 * modPow: positive exponent 67 */ 68 @TestTargetNew( 69 level = TestLevel.PARTIAL_COMPLETE, 70 notes = "This is a complete subset of tests for modPow method.", 71 method = "modPow", 72 args = {java.math.BigInteger.class, java.math.BigInteger.class} 73 ) 74 public void testModPowPosExp() { 75 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75, 48, -7}; 76 byte eBytes[] = {27, -15, 65, 39}; 77 byte mBytes[] = {-128, 2, 3, 4, 5}; 78 int aSign = 1; 79 int eSign = 1; 80 int mSign = 1; 81 byte rBytes[] = {113, 100, -84, -28, -85}; 82 BigInteger aNumber = new BigInteger(aSign, aBytes); 83 BigInteger exp = new BigInteger(eSign, eBytes); 84 BigInteger modulus = new BigInteger(mSign, mBytes); 85 BigInteger result = aNumber.modPow(exp, modulus); 86 byte resBytes[] = new byte[rBytes.length]; 87 resBytes = result.toByteArray(); 88 for(int i = 0; i < resBytes.length; i++) { 89 assertTrue(resBytes[i] == rBytes[i]); 90 } 91 assertEquals("incorrect sign", 1, result.signum()); 92 } 93 94 /** 95 * modPow: negative exponent 96 */ 97 @TestTargetNew( 98 level = TestLevel.PARTIAL_COMPLETE, 99 notes = "This is a complete subset of tests for modPow method.", 100 method = "modPow", 101 args = {java.math.BigInteger.class, java.math.BigInteger.class} 102 ) 103 public void testModPowNegExp() { 104 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75, 48, -7}; 105 byte eBytes[] = {27, -15, 65, 39}; 106 byte mBytes[] = {-128, 2, 3, 4, 5}; 107 int aSign = 1; 108 int eSign = -1; 109 int mSign = 1; 110 byte rBytes[] = {12, 118, 46, 86, 92}; 111 BigInteger aNumber = new BigInteger(aSign, aBytes); 112 BigInteger exp = new BigInteger(eSign, eBytes); 113 BigInteger modulus = new BigInteger(mSign, mBytes); 114 BigInteger result = aNumber.modPow(exp, modulus); 115 byte resBytes[] = new byte[rBytes.length]; 116 resBytes = result.toByteArray(); 117 for(int i = 0; i < resBytes.length; i++) { 118 assertTrue(resBytes[i] == rBytes[i]); 119 } 120 assertEquals("incorrect sign", 1, result.signum()); 121 } 122 123 /** 124 * modInverse: non-positive modulus 125 */ 126 @TestTargetNew( 127 level = TestLevel.PARTIAL_COMPLETE, 128 notes = "This is a complete subset of tests for modInverse method.", 129 method = "modInverse", 130 args = {java.math.BigInteger.class} 131 ) 132 public void testmodInverseException() { 133 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 134 byte mBytes[] = {1, 2, 3}; 135 int aSign = 1; 136 int mSign = -1; 137 BigInteger aNumber = new BigInteger(aSign, aBytes); 138 BigInteger modulus = new BigInteger(mSign, mBytes); 139 try { 140 aNumber.modInverse(modulus); 141 fail("ArithmeticException has not been caught"); 142 } catch (ArithmeticException e) { 143 assertEquals("Improper exception message", "BigInteger: modulus not positive", e.getMessage()); 144 } 145 } 146 147 /** 148 * modInverse: non-invertible number 149 */ 150 @TestTargetNew( 151 level = TestLevel.PARTIAL_COMPLETE, 152 notes = "This is a complete subset of tests for modInverse method.", 153 method = "modInverse", 154 args = {java.math.BigInteger.class} 155 ) 156 public void testmodInverseNonInvertible() { 157 byte aBytes[] = {-15, 24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127}; 158 byte mBytes[] = {-12, 1, 0, 0, 0, 23, 44, 55, 66}; 159 int aSign = 1; 160 int mSign = 1; 161 BigInteger aNumber = new BigInteger(aSign, aBytes); 162 BigInteger modulus = new BigInteger(mSign, mBytes); 163 try { 164 aNumber.modInverse(modulus); 165 fail("ArithmeticException has not been caught"); 166 } catch (ArithmeticException e) { 167 assertEquals("Improper exception message", "BigInteger not invertible.", e.getMessage()); 168 } 169 } 170 171 /** 172 * modInverse: positive number 173 */ 174 @TestTargetNew( 175 level = TestLevel.PARTIAL_COMPLETE, 176 notes = "This is a complete subset of tests for modInverse method.", 177 method = "modInverse", 178 args = {java.math.BigInteger.class} 179 ) 180 public void testmodInversePos1() { 181 byte aBytes[] = {24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127}; 182 byte mBytes[] = {122, 45, 36, 100, 122, 45}; 183 int aSign = 1; 184 int mSign = 1; 185 byte rBytes[] = {47, 3, 96, 62, 87, 19}; 186 BigInteger aNumber = new BigInteger(aSign, aBytes); 187 BigInteger modulus = new BigInteger(mSign, mBytes); 188 BigInteger result = aNumber.modInverse(modulus); 189 byte resBytes[] = new byte[rBytes.length]; 190 resBytes = result.toByteArray(); 191 for(int i = 0; i < resBytes.length; i++) { 192 assertTrue(resBytes[i] == rBytes[i]); 193 } 194 assertEquals("incorrect sign", 1, result.signum()); 195 } 196 197 /** 198 * modInverse: positive number (another case: a < 0) 199 */ 200 @TestTargetNew( 201 level = TestLevel.PARTIAL_COMPLETE, 202 notes = "This is a complete subset of tests for modInverse method.", 203 method = "modInverse", 204 args = {java.math.BigInteger.class} 205 ) 206 public void testmodInversePos2() { 207 byte aBytes[] = {15, 24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127}; 208 byte mBytes[] = {2, 122, 45, 36, 100}; 209 int aSign = 1; 210 int mSign = 1; 211 byte rBytes[] = {1, -93, 40, 127, 73}; 212 BigInteger aNumber = new BigInteger(aSign, aBytes); 213 BigInteger modulus = new BigInteger(mSign, mBytes); 214 BigInteger result = aNumber.modInverse(modulus); 215 byte resBytes[] = new byte[rBytes.length]; 216 resBytes = result.toByteArray(); 217 for(int i = 0; i < resBytes.length; i++) { 218 assertTrue(resBytes[i] == rBytes[i]); 219 } 220 assertEquals("incorrect sign", 1, result.signum()); 221 } 222 223 /** 224 * modInverse: negative number 225 */ 226 @TestTargetNew( 227 level = TestLevel.PARTIAL_COMPLETE, 228 notes = "This is a complete subset of tests for modInverse method.", 229 method = "modInverse", 230 args = {java.math.BigInteger.class} 231 ) 232 public void testmodInverseNeg1() { 233 byte aBytes[] = {15, 24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127}; 234 byte mBytes[] = {2, 122, 45, 36, 100}; 235 int aSign = -1; 236 int mSign = 1; 237 byte rBytes[] = {0, -41, 4, -91, 27}; 238 BigInteger aNumber = new BigInteger(aSign, aBytes); 239 BigInteger modulus = new BigInteger(mSign, mBytes); 240 BigInteger result = aNumber.modInverse(modulus); 241 byte resBytes[] = new byte[rBytes.length]; 242 resBytes = result.toByteArray(); 243 for(int i = 0; i < resBytes.length; i++) { 244 assertTrue(resBytes[i] == rBytes[i]); 245 } 246 assertEquals("incorrect sign", 1, result.signum()); 247 } 248 249 /** 250 * modInverse: negative number (another case: x < 0) 251 */ 252 @TestTargetNew( 253 level = TestLevel.PARTIAL_COMPLETE, 254 notes = "This is a complete subset of tests for modInverse method.", 255 method = "modInverse", 256 args = {java.math.BigInteger.class} 257 ) 258 public void testmodInverseNeg2() { 259 byte aBytes[] = {-15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 260 byte mBytes[] = {122, 2, 4, 122, 2, 4}; 261 byte rBytes[] = {85, 47, 127, 4, -128, 45}; 262 BigInteger aNumber = new BigInteger(aBytes); 263 BigInteger modulus = new BigInteger(mBytes); 264 BigInteger result = aNumber.modInverse(modulus); 265 byte resBytes[] = new byte[rBytes.length]; 266 resBytes = result.toByteArray(); 267 for(int i = 0; i < resBytes.length; i++) { 268 assertTrue(resBytes[i] == rBytes[i]); 269 } 270 assertEquals("incorrect sign", 1, result.signum()); 271 } 272 273 /** 274 * gcd: the second number is zero 275 */ 276 @TestTargetNew( 277 level = TestLevel.PARTIAL_COMPLETE, 278 notes = "This is a complete subset of tests for gcd method.", 279 method = "gcd", 280 args = {java.math.BigInteger.class} 281 ) 282 public void testGcdSecondZero() { 283 byte aBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 284 byte bBytes[] = {0}; 285 int aSign = 1; 286 int bSign = 1; 287 byte rBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 288 BigInteger aNumber = new BigInteger(aSign, aBytes); 289 BigInteger bNumber = new BigInteger(bSign, bBytes); 290 BigInteger result = aNumber.gcd(bNumber); 291 byte resBytes[] = new byte[rBytes.length]; 292 resBytes = result.toByteArray(); 293 for(int i = 0; i < resBytes.length; i++) { 294 assertTrue(resBytes[i] == rBytes[i]); 295 } 296 assertEquals("incorrect sign", 1, result.signum()); 297 } 298 299 /** 300 * gcd: the first number is zero 301 */ 302 @TestTargetNew( 303 level = TestLevel.PARTIAL_COMPLETE, 304 notes = "This is a complete subset of tests for gcd method.", 305 method = "gcd", 306 args = {java.math.BigInteger.class} 307 ) 308 public void testGcdFirstZero() { 309 byte aBytes[] = {0}; 310 byte bBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 311 int aSign = 1; 312 int bSign = 1; 313 byte rBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 314 BigInteger aNumber = new BigInteger(aSign, aBytes); 315 BigInteger bNumber = new BigInteger(bSign, bBytes); 316 BigInteger result = aNumber.gcd(bNumber); 317 byte resBytes[] = new byte[rBytes.length]; 318 resBytes = result.toByteArray(); 319 for(int i = 0; i < resBytes.length; i++) { 320 assertTrue(resBytes[i] == rBytes[i]); 321 } 322 assertEquals("incorrect sign", 1, result.signum()); 323 } 324 325 /** 326 * gcd: the first number is ZERO 327 */ 328 @TestTargetNew( 329 level = TestLevel.PARTIAL_COMPLETE, 330 notes = "This is a complete subset of tests for gcd method.", 331 method = "gcd", 332 args = {java.math.BigInteger.class} 333 ) 334 public void testGcdFirstZERO() { 335 byte bBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 336 int bSign = 1; 337 byte rBytes[] = {15, 24, 123, 57, -15, 24, 123, 57, -15, 24, 123, 57}; 338 BigInteger aNumber = BigInteger.ZERO; 339 BigInteger bNumber = new BigInteger(bSign, bBytes); 340 BigInteger result = aNumber.gcd(bNumber); 341 byte resBytes[] = new byte[rBytes.length]; 342 resBytes = result.toByteArray(); 343 for(int i = 0; i < resBytes.length; i++) { 344 assertTrue(resBytes[i] == rBytes[i]); 345 } 346 assertEquals("incorrect sign", 1, result.signum()); 347 } 348 349 /** 350 * gcd: both numbers are zeros 351 */ 352 @TestTargetNew( 353 level = TestLevel.PARTIAL_COMPLETE, 354 notes = "This is a complete subset of tests for gcd method.", 355 method = "gcd", 356 args = {java.math.BigInteger.class} 357 ) 358 public void testGcdBothZeros() { 359 byte rBytes[] = {0}; 360 BigInteger aNumber = new BigInteger("0"); 361 BigInteger bNumber = BigInteger.valueOf(0L); 362 BigInteger result = aNumber.gcd(bNumber); 363 byte resBytes[] = result.toByteArray(); 364 for(int i = 0; i < resBytes.length; i++) { 365 assertTrue(resBytes[i] == rBytes[i]); 366 } 367 assertEquals("incorrect sign", 0, result.signum()); 368 } 369 370 /** 371 * gcd: the first number is longer 372 */ 373 @TestTargetNew( 374 level = TestLevel.PARTIAL_COMPLETE, 375 notes = "This is a complete subset of tests for gcd method.", 376 method = "gcd", 377 args = {java.math.BigInteger.class} 378 ) 379 public void testGcdFirstLonger() { 380 byte aBytes[] = {-15, 24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127}; 381 byte bBytes[] = {-12, 1, 0, 0, 0, 23, 44, 55, 66}; 382 int aSign = 1; 383 int bSign = 1; 384 byte rBytes[] = {7}; 385 BigInteger aNumber = new BigInteger(aSign, aBytes); 386 BigInteger bNumber = new BigInteger(bSign, bBytes); 387 BigInteger result = aNumber.gcd(bNumber); 388 byte resBytes[] = new byte[rBytes.length]; 389 resBytes = result.toByteArray(); 390 for(int i = 0; i < resBytes.length; i++) { 391 assertTrue(resBytes[i] == rBytes[i]); 392 } 393 assertEquals("incorrect sign", 1, result.signum()); 394 } 395 396 /** 397 * gcd: the second number is longer 398 */ 399 @TestTargetNew( 400 level = TestLevel.PARTIAL_COMPLETE, 401 notes = "This is a complete subset of tests for gcd method.", 402 method = "gcd", 403 args = {java.math.BigInteger.class} 404 ) 405 public void testGcdSecondLonger() { 406 byte aBytes[] = {-12, 1, 0, 0, 0, 23, 44, 55, 66}; 407 byte bBytes[] = {-15, 24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127}; 408 int aSign = 1; 409 int bSign = 1; 410 byte rBytes[] = {7}; 411 BigInteger aNumber = new BigInteger(aSign, aBytes); 412 BigInteger bNumber = new BigInteger(bSign, bBytes); 413 BigInteger result = aNumber.gcd(bNumber); 414 byte resBytes[] = new byte[rBytes.length]; 415 resBytes = result.toByteArray(); 416 for(int i = 0; i < resBytes.length; i++) { 417 assertTrue(resBytes[i] == rBytes[i]); 418 } 419 assertEquals("incorrect sign", 1, result.signum()); 420 } 421 } 422