public void TestRounding() {
    double Roundingnumber = 2.55;
    double Roundingnumber1 = -2.55;
    // +2.55 results   -2.55 results
    double result[] = {
      3, -3,
      2, -2,
      3, -2,
      2, -3,
      3, -3,
      3, -3,
      3, -3
    };
    DecimalFormat pat = new DecimalFormat();
    String s = "";
    s = pat.toPattern();
    logln("pattern = " + s);
    int mode;
    int i = 0;
    String message;
    String resultStr;
    for (mode = 0; mode < 7; mode++) {
      pat.setRoundingMode(mode);
      if (pat.getRoundingMode() != mode) {
        errln("SetRoundingMode or GetRoundingMode failed for mode=" + mode);
      }

      // for +2.55 with RoundingIncrement=1.0
      pat.setRoundingIncrement(1.0);
      resultStr = pat.format(Roundingnumber);
      message =
          "round(" + (double) Roundingnumber + "," + mode + ",FALSE) with RoundingIncrement=1.0==>";
      verify(message, resultStr, result[i++]);
      message = "";
      resultStr = "";

      // for -2.55 with RoundingIncrement=1.0
      resultStr = pat.format(Roundingnumber1);
      message =
          "round("
              + (double) Roundingnumber1
              + ","
              + mode
              + ",FALSE) with RoundingIncrement=1.0==>";
      verify(message, resultStr, result[i++]);
      message = "";
      resultStr = "";
    }
  }
Ejemplo n.º 2
0
  public void setUpInfoGroupHopCount() {
    String[] itemListSource = new String[this.listNodeAreaSource.size()];
    String[] itemListDest = new String[this.listNodeAreaDest.size()];
    for (int i = 0; i < this.listNodeAreaSource.size(); i++) {
      NodeTrace node = this.listNodeAreaSource.get(i);
      itemListSource[i] = Integer.toString(node.id);
    }
    fromCombo.setItems(itemListSource);
    for (int i = 0; i < this.listNodeAreaDest.size(); i++) {
      NodeTrace node = this.listNodeAreaDest.get(i);
      itemListDest[i] = Integer.toString(node.id);
    }
    toCombo.setItems(itemListDest);

    table.removeAll();
    int No = 1;
    int maxHopCount = 0;
    int minHopCount = 1000000000;
    double totalHopCount = 0;
    double totalTime = 0;
    LinkedHashMap<Packet, Integer> listHopCountPacket = new LinkedHashMap<Packet, Integer>();
    for (int i = 0; i < Analyze.mParser.getListPacket().size(); i++) {
      Packet packet = Analyze.mParser.getListPacket().get(i);
      for (int j = 0; j < this.listNodeAreaSource.size(); j++)
        for (int k = 0; k < this.listNodeAreaDest.size(); k++) {
          if (this.listNodeAreaSource.get(j).id == Integer.parseInt(packet.sourceID)
              && this.listNodeAreaDest.get(k).id == Integer.parseInt(packet.destID)
              && packet.isSuccess) {
            TableItem tableItem = new TableItem(table, SWT.NONE);
            tableItem.setText(0, Integer.toString(No++));
            tableItem.setText(1, packet.id);
            tableItem.setText(2, Integer.toString(packet.listNode.size() - 1));
            tableItem.setText(3, packet.sourceID + "--" + packet.destID);

            totalHopCount += packet.listNode.size() - 1;
            totalTime +=
                (Double.parseDouble(packet.endTime) - Double.parseDouble(packet.startTime));
            listHopCountPacket.put(packet, packet.listNode.size() - 1);

            if (maxHopCount < packet.listNode.size() - 1) maxHopCount = packet.listNode.size() - 1;
            if (minHopCount > packet.listNode.size() - 1) minHopCount = packet.listNode.size() - 1;
          }
        }
    }
    if (No == 1) {
      MessageBox dialog = new MessageBox(new Shell(), SWT.ICON_QUESTION | SWT.OK);
      dialog.setText("Error");
      dialog.setMessage("Không có packet nào đi từ group1 -> group2");
      dialog.open();
      avgText.setText("0");
      variantText.setText("0");
      maxText.setText("0");
      minText.setText("0");
    } else {
      DecimalFormat df = new DecimalFormat("0.00");
      // System.out.println(No-1);
      String str = df.format(totalHopCount / (No - 1));
      // set mean
      avgText.setText(str);
      // set text variant
      variantText.setText(df.format(variancesHopCount(listHopCountPacket, totalTime)));
      maxText.setText(Integer.toString(maxHopCount));
      minText.setText(Integer.toString(minHopCount));
    }
    //	if(filterByCombo.getSelectionIndex()==0)
    //		resetEditors();
  }
Ejemplo n.º 3
0
  public LocaleValueProvider() {
    Builder<String, Object> builder = ImmutableMap.builder();

    AuraLocale al = Aura.getLocalizationAdapter().getAuraLocale();

    Locale userLocale = al.getLocale();
    Locale lang = al.getLanguageLocale();
    Locale dateLocale = al.getDateLocale();

    builder.put(USER_LOCALE_LANGUAGE, userLocale.getLanguage());
    builder.put(USER_LOCALE_COUNTRY, userLocale.getCountry());
    builder.put(LANGUAGE, lang.getLanguage());
    builder.put(COUNTRY, lang.getCountry());
    builder.put(VARIANT, lang.getVariant());
    builder.put(LANGUAGE_LOCALE, lang.toString());

    try {
      builder.put(MONTH_NAME, this.getNameOfMonths(al));
      builder.put(WEEKDAY_NAME, this.getNameOfWeekdays(al));
      builder.put(TODAY_LABEL, this.getLabelForToday());
    } catch (QuickFixException qfe) {
      // Ignore
    }

    builder.put(
        FIRST_DAY_OF_WEEK, Calendar.getInstance(al.getTimeZone(), userLocale).getFirstDayOfWeek());

    // using java DateFormat because the year pattern "MMM d, y" (although valid) isn't understood
    // by moment.js
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, dateLocale);
    DateFormat datetimeFormat =
        DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, dateLocale);
    DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, dateLocale);
    try {
      SimpleDateFormat sdf = (SimpleDateFormat) dateFormat;
      builder.put(DATE_FORMAT, sdf.toPattern());

      SimpleDateFormat sdtf = (SimpleDateFormat) datetimeFormat;
      builder.put(DATETIME_FORMAT, sdtf.toPattern());

      SimpleDateFormat stf = (SimpleDateFormat) timeFormat;
      builder.put(TIME_FORMAT, stf.toPattern());
    } catch (ClassCastException cce) {
      builder.put(DATE_FORMAT, DEFAULT_DATE_FORMAT);
      builder.put(DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
      builder.put(TIME_FORMAT, DEFAULT_TIME_FORMAT);
    }

    String timezoneId = al.getTimeZone().getID();
    String availableTimezoneId = Aura.getConfigAdapter().getAvailableTimezone(timezoneId);
    builder.put(TIME_ZONE, availableTimezoneId);
    builder.put(TIME_ZONE_FILE_NAME, availableTimezoneId.replace("/", "-"));

    builder.put(IS_EASTERN_NAME_STYLE, al.isEasternNameStyle());

    // DecimalFormat is expected
    DecimalFormat df = (DecimalFormat) DecimalFormat.getNumberInstance(al.getNumberLocale());

    // Patterns are not localized; the "." means "locale decimal" not "dot"
    builder.put(NUMBER_FORMAT, df.toPattern());
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    builder.put(DECIMAL, dfs.getDecimalSeparator());
    builder.put(GROUPING, dfs.getGroupingSeparator());
    builder.put(ZERO_DIGIT, dfs.getZeroDigit());

    df = (DecimalFormat) DecimalFormat.getPercentInstance(al.getNumberLocale());

    // Don't localize the patterns
    builder.put(PERCENT_FORMAT, df.toPattern());

    df = (DecimalFormat) DecimalFormat.getCurrencyInstance(al.getCurrencyLocale());

    // Don't localize the patterns
    builder.put(CURRENCY_FORMAT, df.toPattern());
    DecimalFormatSymbols cdfs = df.getDecimalFormatSymbols();
    Currency cur = cdfs.getCurrency();
    builder.put(CURRENCY_CODE, cur != null ? cur.getCurrencyCode() : "");
    builder.put(CURRENCY, cdfs.getCurrencySymbol());

    data = builder.build();
  }
  // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
  public void TestAPI() {

    logln("DecimalFormat API test---");
    logln("");
    Locale.setDefault(Locale.ENGLISH);

    // ======= Test constructors

    logln("Testing DecimalFormat constructors");

    DecimalFormat def = new DecimalFormat();

    final String pattern = new String("#,##0.# FF");
    final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
    final CurrencyPluralInfo infoInput = new CurrencyPluralInfo(ULocale.FRENCH);

    DecimalFormat pat = null;
    try {
      pat = new DecimalFormat(pattern);
    } catch (IllegalArgumentException e) {
      errln("ERROR: Could not create DecimalFormat (pattern)");
    }

    DecimalFormat cust1 = null;
    try {
      cust1 = new DecimalFormat(pattern, symbols);
    } catch (IllegalArgumentException e) {
      errln("ERROR: Could not create DecimalFormat (pattern, symbols)");
    }

    DecimalFormat cust2 = null;
    try {
      cust2 = new DecimalFormat(pattern, symbols, infoInput, NumberFormat.PLURALCURRENCYSTYLE);
    } catch (IllegalArgumentException e) {
      errln("ERROR: Could not create DecimalFormat (pattern, symbols, infoInput, style)");
    }

    // ======= Test clone(), assignment, and equality

    logln("Testing clone() and equality operators");

    Format clone = (Format) def.clone();
    if (!def.equals(clone)) {
      errln("ERROR: Clone() failed");
    }

    // ======= Test various format() methods

    logln("Testing various format() methods");

    //        final double d = -10456.0037; // this appears as -10456.003700000001 on NT
    //        final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
    final double d = -10456.00370000000000; // this works!
    final long l = 100000000;
    logln("" + Double.toString(d) + " is the double value");

    StringBuffer res1 = new StringBuffer();
    StringBuffer res2 = new StringBuffer();
    StringBuffer res3 = new StringBuffer();
    StringBuffer res4 = new StringBuffer();
    FieldPosition pos1 = new FieldPosition(0);
    FieldPosition pos2 = new FieldPosition(0);
    FieldPosition pos3 = new FieldPosition(0);
    FieldPosition pos4 = new FieldPosition(0);

    res1 = def.format(d, res1, pos1);
    logln("" + Double.toString(d) + " formatted to " + res1);

    res2 = pat.format(l, res2, pos2);
    logln("" + l + " formatted to " + res2);

    res3 = cust1.format(d, res3, pos3);
    logln("" + Double.toString(d) + " formatted to " + res3);

    res4 = cust1.format(l, res4, pos4);
    logln("" + l + " formatted to " + res4);

    // ======= Test parse()

    logln("Testing parse()");

    String text = new String("-10,456.0037");
    ParsePosition pos = new ParsePosition(0);
    String patt = new String("#,##0.#");
    pat.applyPattern(patt);
    double d2 = pat.parse(text, pos).doubleValue();
    if (d2 != d) {
      errln(
          "ERROR: Roundtrip failed (via parse("
              + Double.toString(d2)
              + " != "
              + Double.toString(d)
              + ")) for "
              + text);
    }
    logln(text + " parsed into " + (long) d2);

    // ======= Test getters and setters

    logln("Testing getters and setters");

    final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
    def.setDecimalFormatSymbols(syms);
    if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
      errln("ERROR: set DecimalFormatSymbols() failed");
    }

    String posPrefix;
    pat.setPositivePrefix("+");
    posPrefix = pat.getPositivePrefix();
    logln("Positive prefix (should be +): " + posPrefix);
    if (posPrefix != "+") {
      errln("ERROR: setPositivePrefix() failed");
    }

    String negPrefix;
    pat.setNegativePrefix("-");
    negPrefix = pat.getNegativePrefix();
    logln("Negative prefix (should be -): " + negPrefix);
    if (negPrefix != "-") {
      errln("ERROR: setNegativePrefix() failed");
    }

    String posSuffix;
    pat.setPositiveSuffix("_");
    posSuffix = pat.getPositiveSuffix();
    logln("Positive suffix (should be _): " + posSuffix);
    if (posSuffix != "_") {
      errln("ERROR: setPositiveSuffix() failed");
    }

    String negSuffix;
    pat.setNegativeSuffix("~");
    negSuffix = pat.getNegativeSuffix();
    logln("Negative suffix (should be ~): " + negSuffix);
    if (negSuffix != "~") {
      errln("ERROR: setNegativeSuffix() failed");
    }

    long multiplier = 0;
    pat.setMultiplier(8);
    multiplier = pat.getMultiplier();
    logln("Multiplier (should be 8): " + multiplier);
    if (multiplier != 8) {
      errln("ERROR: setMultiplier() failed");
    }

    int groupingSize = 0;
    pat.setGroupingSize(2);
    groupingSize = pat.getGroupingSize();
    logln("Grouping size (should be 2): " + (long) groupingSize);
    if (groupingSize != 2) {
      errln("ERROR: setGroupingSize() failed");
    }

    pat.setDecimalSeparatorAlwaysShown(true);
    boolean tf = pat.isDecimalSeparatorAlwaysShown();
    logln("DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
    if (tf != true) {
      errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
    }

    String funkyPat;
    funkyPat = pat.toPattern();
    logln("Pattern is " + funkyPat);

    String locPat;
    locPat = pat.toLocalizedPattern();
    logln("Localized pattern is " + locPat);

    pat.setCurrencyPluralInfo(infoInput);
    if (!infoInput.equals(pat.getCurrencyPluralInfo())) {
      errln("ERROR: set/get CurrencyPluralInfo() failed");
    }

    // ======= Test applyPattern()

    logln("Testing applyPattern()");

    String p1 = new String("#,##0.0#;(#,##0.0#)");
    logln("Applying pattern " + p1);
    pat.applyPattern(p1);
    String s2;
    s2 = pat.toPattern();
    logln("Extracted pattern is " + s2);
    if (!s2.equals(p1)) {
      errln("ERROR: toPattern() result did not match pattern applied");
    }

    String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
    logln("Applying pattern " + p2);
    pat.applyLocalizedPattern(p2);
    String s3;
    s3 = pat.toLocalizedPattern();
    logln("Extracted pattern is " + s3);
    if (!s3.equals(p2)) {
      errln("ERROR: toLocalizedPattern() result did not match pattern applied");
    }

    // ======= Test getStaticClassID()

    //        logln("Testing instanceof()");

    //        try {
    //           NumberFormat test = new DecimalFormat();

    //            if (! (test instanceof DecimalFormat)) {
    //                errln("ERROR: instanceof failed");
    //            }
    //        }
    //        catch (Exception e) {
    //            errln("ERROR: Couldn't create a DecimalFormat");
    //        }

  }