Exemplo n.º 1
0
        internal static void test(string s, char type, string pfx, string sfx, int w, int p, bool alt, bool zeropad, bool left, bool blank, bool sign)
        {
            PrintfFormat fmt = null;

            try
            {
                fmt = new PrintfFormat(s);
            }
            catch (Exception)
            {
                Assert.Fail("bad expression");
            }

            if (fmt != null)
            {
                Assert.AreEqual(type, fmt.type, "type is '" + fmt.type + "' vs. '" + type + "'");
                Assert.AreEqual(pfx, fmt.prefix, "prefix is '" + fmt.prefix + "' vs. '" + pfx + "'");
                Assert.AreEqual(sfx, fmt.suffix, "suffix is '" + fmt.suffix + "' vs. '" + sfx + "'");
                Assert.AreEqual(w, fmt.width, "width is '" + fmt.width + "' vs. '" + w + "'");
                Assert.AreEqual(p, fmt.prec, "prec is '" + fmt.prec + "' vs. '" + p + "'");
                Assert.AreEqual(alt, fmt.alternate, "alternate is '" + fmt.alternate + "' vs. '" + alt + "'");
                Assert.AreEqual(zeropad, fmt.zeropad, "zeropad is '" + fmt.zeropad + "' vs. '" + zeropad + "'");
                Assert.AreEqual(left, fmt.leftAdjust, "leftAdjust is '" + fmt.leftAdjust + "' vs. '" + left + "'");
                Assert.AreEqual(blank, fmt.addBlank, "addBlank is '" + fmt.addBlank + "' vs. '" + blank + "'");
                Assert.AreEqual(sign, fmt.addSign, "addSign is '" + fmt.addSign + "' vs. '" + sign + "'");
            }
        }
Exemplo n.º 2
0
        // ~ Methods

///
///	 <summary> * Tests the class PrintfFormat. If everything is OK, the string "Passed" is
///	 * printed, and the program exits with status 0. Otherwise, diagnostics and
///	 * a stack trace are printed, and the program exits with status 1.
///	 *  </summary>
///	 * <param name="args">
///	 *            Program arguments.<br>
///	 *            "-help" prints a usage message.<br>
///	 *            "-timing" compares how long it takes to output a double
///	 *            compared to the regular Java string conversion. </param>
///
        static void Main(string[] args)
        {
            bool testTiming = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].Equals("-timing"))
                {
                    testTiming = true;
                }
                else if (args[i].Equals("-help"))
                {
                    printUsage();
                    Environment.Exit(0);
                }
                else
                {
                    printUsage();
                    Environment.Exit(1);
                }
            }

            if (testTiming)
            {
                try
                {
                    int    i;
                    int    k;
                    int    nsamp = 10;
                    int    niter = 1000;
                    long   t0;
                    long   t1;
                    double texec;

                    // call once to init
                    t0 = System.DateTime.Now.Ticks;

                    for (i = 0; i < niter; i++)
                    {
                        for (k = 0; k < nsamp; k++)
                        {
                            Convert.ToString(k * 1.111111 * Math.Pow(10, k));
                        }
                    }

                    t1    = System.DateTime.Now.Ticks;
                    texec = (1000 * (t1 - t0)) / (double)(niter * nsamp);
                    Console.WriteLine("Typical time for Double.toString(): " + texec + " usec");

                    PrintfFormat fmt = new PrintfFormat("%g");
                    t0 = System.DateTime.Now.Ticks;

                    for (i = 0; i < niter; i++)
                    {
                        for (k = 0; k < nsamp; k++)
                        {
                            fmt.tostr(k * 1.111111 * Math.Pow(10, k));
                        }
                    }

                    t1    = System.DateTime.Now.Ticks;
                    texec = (1000 * (t1 - t0)) / (double)(niter * nsamp);
                    Console.WriteLine("Typical time for PrintfFormat.tostr(double): " + texec + " usec");
                }
                catch (Exception e)
                {
                    SupportClass.WriteStackTrace(e, Console.Error);
                    Environment.Exit(1);
                }
            }
            else
            {
                try
                {
                    testLots();
                }
                catch (Exception e)
                {
                    SupportClass.WriteStackTrace(e, Console.Error);
                    Environment.Exit(1);
                }

                Console.WriteLine("\nPassed\n");
                Environment.Exit(0);
            }
        }
Exemplo n.º 3
0
 ///
 ///      <summary> * Prints a char in accordance with the supplied
 ///      * PrintfFormat object.
 ///      * </summary>
 ///      * <param name="fmt"> Formatting object </param>
 ///      * <param name="x"> Char to output </param>
 ///      * <seealso cref= PrintfFormat </seealso>
 ///
 public virtual void printf(PrintfFormat fmt, char x)
 {
     Write(fmt.tostr(x));
 }