test/java/math/BigDecimal/ZeroScalingTests.java

Print this page
rev 10641 : 8057793: BigDecimal is no longer effectively immutable
Summary: Modify MutableBigInteger.divideAndRemainderBurnikelZiegler() to copy the instance (this) to a new MutableBigInteger to use as the dividend.
Reviewed-by: TBD
Contributed-by: robbiexgibson@yahoo.com


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4902952 4905407 4916149
  27  * @summary Tests that the scale of zero is propagated properly and has the proper effect.


  28  * @author Joseph D. Darcy
  29  */
  30 
  31 import java.math.*;
  32 import java.util.*;
  33 
  34 public class ZeroScalingTests {
  35 
  36     static MathContext longEnough = new MathContext(50, RoundingMode.UNNECESSARY);
  37 
  38     static BigDecimal[]  zeros = new BigDecimal[23];
  39     static {
  40         for(int i = 0; i < 21; i++) {
  41             zeros[i] = new BigDecimal(BigInteger.ZERO, i-10);
  42         }
  43         zeros[21] = new BigDecimal(BigInteger.ZERO, Integer.MIN_VALUE);
  44         zeros[22] = new BigDecimal(BigInteger.ZERO, Integer.MAX_VALUE);
  45     }
  46 
  47     static BigDecimal element = BigDecimal.valueOf(100, -2);


 428     }
 429 
 430     static int ulpTests() {
 431         int failures = 0;
 432 
 433         for(BigDecimal zero: zeros) {
 434             BigDecimal result;
 435             BigDecimal expected = BigDecimal.valueOf(1, zero.scale());
 436 
 437             if (! (result=zero.ulp()).equals(expected) ) {
 438                 failures++;
 439                 System.err.println("Unexpected ulp value for zero value " +
 440                                    zero + "; expected " + expected +
 441                                    ", got " + result);
 442             }
 443         }
 444 
 445         return failures;
 446     }
 447 











 448     public static void main(String argv[]) {
 449         int failures = 0;
 450 
 451         failures += addTests();
 452         failures += subtractTests();
 453         failures += multiplyTests();
 454         failures += divideTests();
 455         failures += setScaleTests();
 456         failures += toEngineeringStringTests();
 457         failures += ulpTests();
 458 
 459         if (failures > 0 ) {
 460             throw new RuntimeException("Incurred " + failures + " failures" +
 461                                        " testing the preservation of zero scales.");
 462         }
 463     }
 464 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4902952 4905407 4916149 8057793
  27  * @summary Tests that the scale of zero is propagated properly and has the
  28  * proper effect and that setting the scale to zero does not mutate the
  29  * BigDecimal.
  30  * @author Joseph D. Darcy
  31  */
  32 
  33 import java.math.*;
  34 import java.util.*;
  35 
  36 public class ZeroScalingTests {
  37 
  38     static MathContext longEnough = new MathContext(50, RoundingMode.UNNECESSARY);
  39 
  40     static BigDecimal[]  zeros = new BigDecimal[23];
  41     static {
  42         for(int i = 0; i < 21; i++) {
  43             zeros[i] = new BigDecimal(BigInteger.ZERO, i-10);
  44         }
  45         zeros[21] = new BigDecimal(BigInteger.ZERO, Integer.MIN_VALUE);
  46         zeros[22] = new BigDecimal(BigInteger.ZERO, Integer.MAX_VALUE);
  47     }
  48 
  49     static BigDecimal element = BigDecimal.valueOf(100, -2);


 430     }
 431 
 432     static int ulpTests() {
 433         int failures = 0;
 434 
 435         for(BigDecimal zero: zeros) {
 436             BigDecimal result;
 437             BigDecimal expected = BigDecimal.valueOf(1, zero.scale());
 438 
 439             if (! (result=zero.ulp()).equals(expected) ) {
 440                 failures++;
 441                 System.err.println("Unexpected ulp value for zero value " +
 442                                    zero + "; expected " + expected +
 443                                    ", got " + result);
 444             }
 445         }
 446 
 447         return failures;
 448     }
 449 
 450     // bug 8057793
 451     static int setScaleDoesNotMutateTest() {
 452         BigDecimal total = new BigDecimal("258815507198903607775511093103396443816569106750031264155319238473795838680758514810110764742309284477206138527975952150289602995045050194333030191178778772026538699925775139201970526695485362661420908248887297829319881475178467494779683293036572059595504702727301324759997409522995072582369210284334718757260859794972695026582432867589093687280300148141501712013226636373167978223780290547640482160818746599330924736802844173226042389174403401903999447463440670236056324929325189403433689"
 453                 + ".426167432065785331444814035799717606745777287606858873045971898862329763544687891847664736523584843544347118836628373041412918374550458884706686730726101338872517021688769782894793734049819222924171842793485919753186993388451909096042127903835765393729547730953942175461146061715108701615615142134282261293656760570061554783195726716403304101469782303957325142638493327692352838806741611887655695029948975509680496573999174402058593454203190963443179532640446352828089016874853634851387762579319853267317320515941105912189838719919259277721994880193541634872882180184303434360412344059435559680494807415573269199203376126242271766939666939316648575065702750502798973418978204972336924254702551350654650573582614211506856383897692911422458286912085339575875324832979140870119455620532272318122103640233069115700020760625493816902806241630788230268031695140687964931377988962507263990468276009750998066442971308866347136022907166625330623130307555914930120150437900510530537258665172619821272937026713977709974434967165159545592482710663639966781678268622620229577009317698254134914742098420792313931843709810905414336383757407675429663714210967924767434203021205270369316797752411974617662200898086335322218191674846795163102021505555508444216708745911194321674887527227200297039471799580744303346354057273540730643842091810899490590914195225087593013834388801018488174855060306804024894292757613618190472234110859436472645203753139820658279559340251226992556744343475086923568365637919479462424794554522865559888240039662899509652221329892034706445253487898044421278283079233226845124525434586324657471286953226255430662125870993375281512713207125720748163498642795960457639954616530163959004770092547297392499137383176609646505351001304840762905826237024982330597805063521162285806541220110524989649256399233792799406995068469271941269511818994954109392839548141262324660472253632382325038836831429045617036015122388070240133760858500132713255407855625837956886349324981003917084922808187223285051144454915441134217743066575863563572152133978905444998209075763950909784148142018992367290485890072303179512881131769414783097454103103347826517701720263541869335631166977965013552647906729408522950996105479525445916501155305220090853891226367184989434453290788068397817927893708837722255115237672194162924260945492012622891770365546831236789867922136747819364833843397165107825773447549885351449899330007200651144003961228091210630807333236718793283427788965479074476288255387824982443633190938302785760754436525586544523339170400053128503337395428393881357669568532722167493096151221381017320147344991331421789379785964440840684363041795410525097564979585773948558651896834067324427900848255265001498890329859444233861478388742393060996236783742654761350763876989363052609107226398858310051497856931093693697981165801539060516895227818925342535261227134364063673285588256280386915163875872231395348293505967057794409379709079685798908660258077792158532257603211711587587586356431658240229896344639704");
 454         if (total.setScale(0, RoundingMode.DOWN).equals(total.setScale(0, RoundingMode.DOWN))) {
 455             return 0;
 456         } else {
 457             return 1;
 458         }
 459     }
 460 
 461     public static void main(String argv[]) {
 462         int failures = 0;
 463 
 464         failures += addTests();
 465         failures += subtractTests();
 466         failures += multiplyTests();
 467         failures += divideTests();
 468         failures += setScaleTests();
 469         failures += toEngineeringStringTests();
 470         failures += ulpTests();
 471 
 472         if (failures > 0 ) {
 473             throw new RuntimeException("Incurred " + failures + " failures" +
 474                                        " testing the preservation of zero scales.");
 475         }
 476     }
 477 }