text: //=========================================================================
  // Copyright (C) 2012 The Elastos Open Source Project
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //      http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  //=========================================================================
  
  #include <elautoptr.h>
  #include "elastos/coredef.h"
  #include "elastos/core/StringUtils.h"
  #include "elastos/core/StringBuilder.h"
  #include "elastos/utility/etl/List.h"
  #include "elastos/utility/Arrays.h"
  
  using namespace Elastos;
  using Elastos::Core::StringUtils;
  using Elastos::Core::Math;
  using Elastos::Core::INumber;
  using Elastos::Math::IBigInteger;
  using Elastos::Math::CBigInteger;
  using Elastos::Math::IBigDecimal;
  using Elastos::Math::CBigDecimal;
  using Elastos::Math::IBigDecimalHelper;
  using Elastos::Math::CBigDecimalHelper;
  using Elastos::Core::EIID_IComparable;
  using Elastos::Core::EIID_INumber;
  
  namespace Elastos {
  namespace Math {
  
  static void assertEquals(const char *hintMessage, Int32 expecting, Int32 toVerify)
  {
      printf("expecting: %d, toVerify: %d. %s\n", expecting, toVerify, hintMessage);
      assert(expecting == toVerify);
  }
  
  #if 0
  static void assertEquals(const char *hintMessage, Double expecting, Double toVerify)
  {
      printf("expecting: %f, toVerify: %f. %s\n", expecting, toVerify, hintMessage);
      assert(expecting == toVerify);
  }
  
  static void assertEquals(const char *hintMessage, String expecting, String toVerify)
  {
      printf("expecting: %s, toVerify: %s. %s\n", expecting.string(), toVerify.string(), hintMessage);
      assert(expecting.Equals(toVerify) == TRUE);
  }
  #endif
  
  #if 0
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You under the Apache License, Version 2.0
   *  (the "License"); you may not use this file except in compliance with
   *  the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  /**
   * @author Elena Semukhina
   */
  
  package org.apache.harmony.tests.java.math;
  
  import junit.framework.TestCase;
  import java.math.*;
  
  /**
   * Class:  java.math.BigDecimal
   * Methods: movePointLeft, movePointRight, scale, setScale, unscaledValue *
   */
  public class BigDecimalScaleOperationsTest extends TestCase {
  #endif
  
      /**
       * Check the default scale
       */
  #if 0
      public void testScaleDefault() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int cScale = 0;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a));
          assertTrue("incorrect scale", aNumber.scale() == cScale);
      }
  #endif
  void testScaleDefault()
  {
      String a = String("1231212478987482988429808779810457634781384756794987");
      int cScale = 0;
  
      AutoPtr<IBigInteger> bA;
      ECode ec = CBigInteger::New(a, (IBigInteger**)&bA);
      if (FAILED(ec)) {
          printf(" Failed to create CBigInteger. Error %08X\n", ec);
          return;
      }
  
      AutoPtr<IBigDecimal> aNumber;
      ec = CBigDecimal::New((IBigInteger*)bA, (IBigDecimal**)&aNumber);
      if (FAILED(ec)) {
          printf(" Failed to create CBigDecimal. Error %08X\n", ec);
          return;
      }
  
      Int32 r;
      aNumber->GetScale(&r);
  
      assertEquals("testScaleDefault() incorrect scale", cScale, r);
  }
  
      /**
       * Check a negative scale
       */
  #if 0
      public void testScaleNeg() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = -10;
          int cScale = -10;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          assertTrue("incorrect scale", aNumber.scale() == cScale);
      }
  #endif
  void testScaleNeg()
  {
      String a = String("1231212478987482988429808779810457634781384756794987");
      int aScale = -10;
      int cScale = -10;
  
      AutoPtr<IBigInteger> bA;
      ECode ec = CBigInteger::New(a, (IBigInteger**)&bA);
      if (FAILED(ec)) {
          printf(" Failed to create CBigInteger. Error %08X\n", ec);
          return;
      }
  
      AutoPtr<IBigDecimal> aNumber;
      ec = CBigDecimal::New((IBigInteger*)bA, aScale, (IBigDecimal**)&aNumber);
      if (FAILED(ec)) {
          printf(" Failed to create CBigDecimal. Error %08X\n", ec);
          return;
      }
  
      Int32 r;
      aNumber->GetScale(&r);
  
      assertEquals("testScaleNeg() incorrect scale", cScale, r);
  }
  
      /**
       * Check a positive scale
       */
  #if 0
      public void testScalePos() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 10;
          int cScale = 10;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          assertTrue("incorrect scale", aNumber.scale() == cScale);
      }
  #endif
  void testScalePos()
  {
      String a = String("1231212478987482988429808779810457634781384756794987");
      int aScale = 10;
      int cScale = 10;
  
      AutoPtr<IBigInteger> bA;
      ECode ec = CBigInteger::New(a, (IBigInteger**)&bA);
      if (FAILED(ec)) {
          printf(" Failed to create CBigInteger. Error %08X\n", ec);
          return;
      }
  
      AutoPtr<IBigDecimal> aNumber;
      ec = CBigDecimal::New((IBigInteger*)bA, aScale, (IBigDecimal**)&aNumber);
      if (FAILED(ec)) {
          printf(" Failed to create CBigDecimal. Error %08X\n", ec);
          return;
      }
  
      Int32 r;
      aNumber->GetScale(&r);
  
      assertEquals("testScalePos() incorrect scale", cScale, r);
  }
  
      /**
       * Check the zero scale
       */
  #if 0
      public void testScaleZero() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 0;
          int cScale = 0;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          assertTrue("incorrect scale", aNumber.scale() == cScale);
      }
  #endif
  void testScaleZero()
  {
      String a = String("1231212478987482988429808779810457634781384756794987");
      int aScale = 0;
      int cScale = 0;
  
      AutoPtr<IBigInteger> bA;
      ECode ec = CBigInteger::New(a, (IBigInteger**)&bA);
      if (FAILED(ec) || bA == NULL) {
          printf(" Failed to create CBigInteger. Error %08X\n", ec);
      }
  
      AutoPtr<IBigDecimal> aNumber;
      ec = CBigDecimal::New((IBigInteger*)bA, aScale, (IBigDecimal**)&aNumber);
      if (FAILED(ec)) {
          printf(" Failed to create CBigDecimal. Error %08X\n", ec);
          return;
      }
  
      Int32 r;
      aNumber->GetScale(&r);
  
      assertEquals("testScaleZero() incorrect scale", cScale, r);
  }
  
      /**
       * Check the unscaled value
       */
  #if 0
      public void testUnscaledValue() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 100;
          BigInteger bNumber = new BigInteger(a);
          BigDecimal aNumber = new BigDecimal(bNumber, aScale);
          assertTrue("incorrect unscaled value", aNumber.unscaledValue().equals(bNumber));
      }
  #endif
  void testUnscaledValue()
  {
      String a = String("1231212478987482988429808779810457634781384756794987");
      int aScale = 100;
  
      AutoPtr<IBigInteger> bA;
      ECode ec = CBigInteger::New(a, (IBigInteger**)&bA);
      if (FAILED(ec)) {
          printf(" Failed to create CBigInteger. Error %08X\n", ec);
          return;
      }
  
      AutoPtr<IBigDecimal> aNumber;
      ec = CBigDecimal::New((IBigInteger*)bA, aScale, (IBigDecimal**)&aNumber);
      if (FAILED(ec)) {
          printf(" Failed to create CBigDecimal. Error %08X\n", ec);
          return;
      }
  
      AutoPtr<IBigInteger> rInteger;
      aNumber->GetUnscaledValue((IBigInteger**)&rInteger);
  
      AutoPtr<IComparable> comp;
      comp = IComparable::Probe(rInteger);
      if (comp == NULL) {
          printf(" Failed to Probe IComparable. Error\n");
          return;
      }
  
      Int32 r;
      comp->CompareTo(rInteger, &r);
      if (r != 0) {
          printf("incorrect value\n");
      }
  
      assertEquals("testUnscaledValue() incorrect scale", 0, r);
  
      aNumber->GetScale(&r);
  }
  
      /**
       * Set a greater new scale
       */
  #if 0
      public void testSetScaleGreater() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 18;
          int newScale = 28;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertEquals("incorrect value", 0, bNumber.compareTo(aNumber));
      }
  #endif
  
      /**
       * Set a less new scale; this.scale == 8; newScale == 5.
       */
  #if 0
      public void testSetScaleLess() {
          String a = "2.345726458768760000E+10";
          int newScale = 5;
          BigDecimal aNumber = new BigDecimal(a);
          BigDecimal bNumber = aNumber.setScale(newScale);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertEquals("incorrect value", 0, bNumber.compareTo(aNumber));
      }
  #endif
  
      /**
       * Verify an exception when setting a new scale
       */
  #if 0
      public void testSetScaleException() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          try {
              aNumber.setScale(newScale);
              fail("ArithmeticException has not been caught");
          } catch (ArithmeticException e) {
              assertEquals("Improper exception message", "Rounding necessary", e.getMessage());
          }
      }
  #endif
  
      /**
       * Set the same new scale
       */
  #if 0
      public void testSetScaleSame() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 18;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.equals(aNumber));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundUp() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478139";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_UP);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundDown() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478138";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_DOWN);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundCeiling() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478139";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_CEILING);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundFloor() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478138";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_FLOOR);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundHalfUp() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478138";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_HALF_UP);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundHalfDown() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478138";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_HALF_DOWN);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
      /**
       * Set a new scale
       */
  #if 0
      public void testSetScaleRoundHalfEven() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478138";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_HALF_EVEN);
          assertTrue("incorrect scale", bNumber.scale() == newScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
  
      /**
       * SetScale(int, RoundingMode)
       */
  #if 0
      public void testSetScaleIntRoundingMode() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 28;
          int newScale = 18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal result = aNumber.setScale(newScale, RoundingMode.HALF_EVEN);
          String res = "123121247898748298842980.877981045763478138";
          int resScale = 18;
          assertEquals("incorrect value", res, result.toString());
          assertEquals("incorrect scale", resScale, result.scale());
      }
  #endif
  
  
      /**
       * Move the decimal point to the left; the shift value is positive
       */
  #if 0
      public void testMovePointLeftPos() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 28;
          int shift = 18;
          int resScale = 46;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.movePointLeft(shift);
          assertTrue("incorrect scale", bNumber.scale() == resScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a));
      }
  #endif
  
  
      /**
       * Move the decimal point to the left; the shift value is positive
       */
  #if 0
      public void testMovePointLeftNeg() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 28;
          int shift = -18;
          int resScale = 10;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.movePointLeft(shift);
          assertTrue("incorrect scale", bNumber.scale() == resScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a));
      }
  #endif
  
      /**
       * Move the decimal point to the right; the shift value is positive
       */
  #if 0
      public void testMovePointRightPosGreater() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 28;
          int shift = 18;
          int resScale = 10;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.movePointRight(shift);
          assertTrue("incorrect scale", bNumber.scale() == resScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a));
      }
  #endif
  
  
      /**
       * Move the decimal point to the right; the shift value is positive
       */
  #if 0
      public void testMovePointRightPosLess() {
          String a = "1231212478987482988429808779810457634781384756794987";
          String b = "123121247898748298842980877981045763478138475679498700";
          int aScale = 28;
          int shift = 30;
          int resScale = 0;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.movePointRight(shift);
          assertTrue("incorrect scale", bNumber.scale() == resScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b));
      }
  #endif
  
  
      /**
       * Move the decimal point to the right; the shift value is positive
       */
  #if 0
      public void testMovePointRightNeg() {
          String a = "1231212478987482988429808779810457634781384756794987";
          int aScale = 28;
          int shift = -18;
          int resScale = 46;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          BigDecimal bNumber = aNumber.movePointRight(shift);
          assertTrue("incorrect scale", bNumber.scale() == resScale);
          assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a));
      }
  #endif
  
      /**
       * Move the decimal point to the right when the scale overflows
       */
  #if 0
      public void testMovePointRightException() {
          String a = "12312124789874829887348723648726347429808779810457634781384756794987";
          int aScale = Integer.MAX_VALUE; //2147483647
          int shift = -18;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          try {
              aNumber.movePointRight(shift);
              fail("ArithmeticException has not been caught");
          } catch (ArithmeticException e) {
          }
      }
  #endif
  
      /**
       * precision()
       */
  #if 0
      public void testPrecision() {
          String a = "12312124789874829887348723648726347429808779810457634781384756794987";
          int aScale = 14;
          BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
          int prec = aNumber.precision();
          assertEquals(68, prec);
      }
  #endif
  
  
  //==============================================================================
  
  EXTERN_C int mainBigDecimalScaleOperationsTest(int argc, char *argv[])
  {
      printf("\n==== libcore/math/BigDecimalScaleOperationsTest ====\n");
      testScaleDefault();
      testScaleNeg();
      testScalePos();
      testScaleZero();
      testUnscaledValue();
      printf("\n==== end of libcore/math/BigDecimalScaleOperationsTest ====\n");
  
      return 0;
  }
  
  }
  }
  
  
  