Пример #1
0
        /// <summary>
        /// Simple test of some ParseUtil methods.
        /// </summary>
        /// <returns>bool - true for all passed, false otherwise</returns>
        public static bool TestSelf()
        {
            Logger log = new Logger("ParseUtil: TestSelf");

            log.Info("Starting...");

            StreamTokenizer tokenizer = new StreamTokenizer();

            tokenizer.Verbosity = VerbosityLevel.Warn;

            // FindMatch
            ArrayList alist = new ArrayList();

            tokenizer.TokenizeString("{ [ ] '}' }", alist);
            foreach (Token t in alist)
            {
                log.Debug("Token = {0}", t);
            }

            Token[] tarray = (Token[])alist.ToArray(typeof(Token));
            int     i      = 0;

            if (!FindMatch(tarray, ref i, '{'))
            {
                log.Error("FindMatch failed to match { char");
                return(false);
            }

            if (i != 4)
            {
                log.Error("FindMatch got the wrong answer {0}", i);
                return(false);
            }
            else
            {
                log.Info("FindMatch worked.");
            }

            //
            // try BuildArray
            //
            ArrayList tokens = new ArrayList();

            tokenizer.TokenizeString("1 2 3 4 5", tokens);
            foreach (Token t in tokens)
            {
                log.Debug("Token = {0}", t);
            }

            i = 0;
            Int16[] shorts = (short[])BuildArray(tokens, ref i, typeof(Int16), null,
                                                 -1, log);

            if (shorts == null)
            {
                log.Error("Unable to BuildArray of shorts.");
                return(false);
            }

            log.Info("Parsed shorts:");
            foreach (Int16 s in shorts)
            {
                log.Write("{0}, ", s);
            }
            log.WriteLine(String.Empty);

            //
            // try BuildArray of floats, char terminated
            //
            tokens.Clear();
            tokenizer.TokenizeString("1 2 ; 3 4", tokens);
            foreach (Token t in tokens)
            {
                log.Debug("Token = {0}", t);
            }

            i = 0;
            Single[] floats = (float[])BuildArray(tokens, ref i, typeof(Single),
                                                  new CharToken(';'), -1, log);

            if (floats == null)
            {
                log.Error("Unable to BuildArray of floats.");
                return(false);
            }

            log.Info("Parsed floats:");
            foreach (float f in floats)
            {
                log.Write("{0}, ", f);
            }
            log.WriteLine(String.Empty);

            if (i != 2)
            {
                log.Error("BuildArray left i = {0} which is incorrect");
                return(false);
            }

            //
            // try BuildArray on high-precision floats
            //
            tokens.Clear();
            float f1 = 1.23456f;
            float f2 = 2.34567f;

            tokenizer.TokenizeString(String.Format("{0:f6} {1:f6}", f1, f2), tokens);
            foreach (Token t in tokens)
            {
                log.Debug("Token = {0}", t);
            }

            i      = 0;
            floats = (float[])BuildArray(tokens, ref i, typeof(Single),
                                         null, -1, log);

            if (floats == null)
            {
                log.Error("Unable to BuildArray of floats.");
                return(false);
            }

            log.Info("Parsed floats:");
            foreach (float f in floats)
            {
                log.Write("{0}, ", f);
            }
            log.WriteLine(String.Empty);

            if (floats[0] != f1)
            {
                log.Error("BuildArray produced float {0:f6} instead of {1:f6}",
                          floats[0], f1);
                return(false);
            }

            //
            // try BuildArray of chars, maxLength terminated
            //
            log.Info("Chars, terminated by maxLength");
            tokens.Clear();
            tokenizer.TokenizeString("12 2 ; 3 4", tokens);
            foreach (Token t in tokens)
            {
                log.Debug("Token = {0}", t);
            }

            i = 0;
            char[] chars = (char[])BuildArray(tokens, ref i, typeof(Char),
                                              null, 3, log);

            if (chars == null)
            {
                log.Error("Unable to BuildArray of chars.");
                return(false);
            }

            log.Info("Parsed chars:");
            foreach (char f in chars)
            {
                log.Write("{0}, ", f);
            }
            log.WriteLine(String.Empty);

            if (i != 4)
            {
                log.Error("BuildArray left i = {0} which is incorrect", i);
                return(false);
            }

            //
            // try BuildArray of hex numbers
            //
            log.Info("Hex numbers");
            tokens.Clear();
            tokenizer.Settings.ParseHexNumbers = true;
            tokenizer.TokenizeString("0xfff, 0xffe", tokens);
            foreach (Token t in tokens)
            {
                log.Debug("Token = {0}", t);
            }

            i = 0;
            ushort[] ushorts = (ushort[])BuildArray(tokens, ref i, typeof(ushort),
                                                    null, 3, log);

            if (ushorts == null)
            {
                log.Error("Unable to BuildArray of ushorts.");
                return(false);
            }

            log.Info("Parsed ushorts:");
            foreach (ushort us in ushorts)
            {
                log.Write("{0}, ", us);
            }
            log.WriteLine(String.Empty);

//			if (i != 4)
//			{
//				log.Error("BuildArray left i = {0} which is incorrect", i);
//				return(false);
//			}

            log.Info("All PASSED");
            return(true);
        }