Esempio n. 1
0
        public void testLong()
        {
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 0;
            }
            long[] lng    = new long[100];
            Random random = new Random();

            for (int i = 0; i < lng.Length; i += 1)
            {
                lng[i] = (long)(long.MaxValue * (2 * (random.Next() - 0.5)));
            }


            lng[0] = long.MaxValue;
            lng[1] = long.MinValue;
            lng[2] = 0;

            bf.TruncationThrow = false;
            bp.FillFields      = true;
            bf.Align           = true;
            offset             = 0;
            for (int i = 0; i < lng.Length; i += 1)
            {
                offset = bf.format(lng[i], buffer, offset, 20);
                if ((i + 1) % 4 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }

            bp.Offset = 0;

            for (int i = 0; i < lng.Length; i += 1)
            {
                Assertion.AssertEquals("Long check", lng[i], bp.GetLong(20));
                if ((i + 1) % 4 == 0)
                {
                    bp.Skip(1);
                }
            }
        }
        public void testInt()
        {
            for (int i = 0; i < buffer.Length; i++)
              buffer[i] = 0;
            bf.Align=true;
            bf.TruncationThrow=false;

            int[] tint = new int[100];

            tint[0] = int.MinValue;
            tint[1] = int.MaxValue;
            tint[2] = 0;
            Random random = new Random();

            for (int i=0; i<tint.Length; i += 1) {
            tint[i] = (int)(int.MaxValue * (2 * (random.NextDouble() - 0.5)));
            }

            cnt = 0;
            offset = 0;
            // Write 100 numbers

               int colSize = 12;
            while (cnt < tint.Length) {

            offset = bf.format(tint[cnt], buffer, offset, colSize);
             cnt += 1;
            if (cnt % 8 == 0) {
            offset = bf.format("\n", buffer, offset, 1);
            }
            }

            // Now see if we can get them back
            bp.Offset=0;
             bp = new ByteParser(buffer);
            for (int i=0; i<tint.Length; i += 1) {
            Console.WriteLine("i= " + i);
            int chk = bp.GetInt(colSize);

            Assertion.AssertEquals("IntegersRA", chk, tint[i]);
            if ((i+1) % 8 == 0) {
            bp.Skip(1);
            }
            }

            // Now do it with left-aligned numbers.
            bf.Align=false;
            bp.FillFields=true;
            offset = 0;
            colSize = 12;
            cnt = 0;
            offset = 0;
            while (cnt < tint.Length) {
            int oldOffset = offset;
            offset = bf.format(tint[cnt], buffer, offset, colSize);
            int nb = colSize - (offset- oldOffset);
            if (nb > 0) {
            offset = bf.alignFill(buffer, offset, nb);
            }
            cnt += 1;
            if (cnt % 8 == 0) {
            offset = bf.format("\n", buffer, offset, 1);
            }
            }

            // Now see if we can get them back
            bp.Offset=0;
            for (int i=0; i<tint.Length; i += 1) {

            int chk = bp.GetInt(colSize);

            Assertion.AssertEquals("IntegersLA", chk, tint[i]);
            if ((i+1) % 8 == 0) {
            bp.Skip(1);
            }
            }

            offset = 0;
            colSize = 12;
            cnt = 0;
            offset = 0;
            while (cnt < tint.Length) {
            offset = bf.format(tint[cnt], buffer, offset, colSize);
            cnt += 1;
            if (cnt % 8 == 0) {
            offset = bf.format("\n", buffer, offset, 1);
            }
            }
            String myStr = null;
            sbyte[] sbytes = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes, 0, buffer.Length);
            unsafe
            {
            // Instruct the Garbage Collector not to move the memory
            fixed (sbyte* buffBytes = sbytes)
            {
            myStr = new String(buffBytes, 0, offset);

            }

            }
            Assertion.AssertEquals("No spaces", -1, myStr.IndexOf(" "));
            bf.Align=false;

            offset = 0;
            colSize = 12;
            cnt = 0;
            offset = 0;
            while (cnt < tint.Length) {
            offset = bf.format(tint[cnt], buffer, offset, colSize);
            offset = bf.format(" ", buffer, offset, 1);
            cnt   += 1;
            }
            String myStr2 = null;
            sbyte[] sbytes2 = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes2, 0, buffer.Length);
            unsafe
            {
            // Instruct the Garbage Collector not to move the memory
            fixed (sbyte* buffBytes2 = sbytes2)
            {
            myStr2 = new String(buffBytes2, 0, offset);

            }

            }
            String[] array = myStr2.Split(' ');

            Assertion.AssertEquals("Split size", 100, array.Length-1);

            for (int i=0; i<array.Length-1; i += 1) {
            Assertion.AssertEquals("Parse token",tint[i], int.Parse(array[i]));
            }

            bf.TruncationThrow=false;

            int val = 1;
            //Arrays.fill(buffer, (byte)' ');

            // array is used, values set, etc.
            //Array.Clear(buffer, ' ', buffer.Length-1);
            for(int i = 0; i < buffer.Length; i++)
             buffer[i] = (byte)' ';
            for (int i=0; i<10; i += 1) {
            offset = bf.format(val, buffer, 0, 6);
            String test = (val+"      ").Substring(0,6);
            sbyte[] sbytes1 = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes1, 0, buffer.Length);
            unsafe
            {
            // Instruct the Garbage Collector not to move the memory
            fixed (sbyte* buffBytes1 = sbytes1)
            {

                if (i < 6)
                {
                    Assertion.AssertEquals("TestTrunc" + i, test, new String(buffBytes1, 0, 6));
                }
                else
                {
                    Assertion.AssertEquals("TestTrunc" + i, "******", new String(buffBytes1, 0, 6));
                }
                val *= 10;
            }
            }
            }

            bf.TruncationThrow=true;
            val = 1;
            for (int i=0; i<10; i += 1) {
            bool thrown = false;
            try {
            offset = bf.format(val, buffer, 0, 6);
            } catch (TruncationException e) {
            thrown = true;
            }
            if (i < 6) {
            Assertion.AssertEquals("TestTruncThrow"+i, false, thrown);
            } else {
            Assertion.AssertEquals("TestTruncThrow"+i, true, thrown);
            }
            val *= 10;
            }
        }
Esempio n. 3
0
        public void testInt()
        {
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = 0;
            }
            bf.Align           = true;
            bf.TruncationThrow = false;

            int[] tint = new int[100];

            tint[0] = int.MinValue;
            tint[1] = int.MaxValue;
            tint[2] = 0;
            Random random = new Random();

            for (int i = 0; i < tint.Length; i += 1)
            {
                tint[i] = (int)(int.MaxValue * (2 * (random.NextDouble() - 0.5)));
            }

            cnt    = 0;
            offset = 0;
            // Write 100 numbers

            int colSize = 12;

            while (cnt < tint.Length)
            {
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                cnt   += 1;
                if (cnt % 8 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }

            // Now see if we can get them back
            bp.Offset = 0;
            bp        = new ByteParser(buffer);
            for (int i = 0; i < tint.Length; i += 1)
            {
                Console.WriteLine("i= " + i);
                int chk = bp.GetInt(colSize);

                Assertion.AssertEquals("IntegersRA", chk, tint[i]);
                if ((i + 1) % 8 == 0)
                {
                    bp.Skip(1);
                }
            }

            // Now do it with left-aligned numbers.
            bf.Align      = false;
            bp.FillFields = true;
            offset        = 0;
            colSize       = 12;
            cnt           = 0;
            offset        = 0;
            while (cnt < tint.Length)
            {
                int oldOffset = offset;
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                int nb = colSize - (offset - oldOffset);
                if (nb > 0)
                {
                    offset = bf.alignFill(buffer, offset, nb);
                }
                cnt += 1;
                if (cnt % 8 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }

            // Now see if we can get them back
            bp.Offset = 0;
            for (int i = 0; i < tint.Length; i += 1)
            {
                int chk = bp.GetInt(colSize);

                Assertion.AssertEquals("IntegersLA", chk, tint[i]);
                if ((i + 1) % 8 == 0)
                {
                    bp.Skip(1);
                }
            }

            offset  = 0;
            colSize = 12;
            cnt     = 0;
            offset  = 0;
            while (cnt < tint.Length)
            {
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                cnt   += 1;
                if (cnt % 8 == 0)
                {
                    offset = bf.format("\n", buffer, offset, 1);
                }
            }
            String myStr = null;

            sbyte[] sbytes = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes, 0, buffer.Length);
            unsafe
            {
                // Instruct the Garbage Collector not to move the memory
                fixed(sbyte *buffBytes = sbytes)
                {
                    myStr = new String(buffBytes, 0, offset);
                }
            }
            Assertion.AssertEquals("No spaces", -1, myStr.IndexOf(" "));
            bf.Align = false;

            offset  = 0;
            colSize = 12;
            cnt     = 0;
            offset  = 0;
            while (cnt < tint.Length)
            {
                offset = bf.format(tint[cnt], buffer, offset, colSize);
                offset = bf.format(" ", buffer, offset, 1);
                cnt   += 1;
            }
            String myStr2 = null;

            sbyte[] sbytes2 = new sbyte[100000];
            Buffer.BlockCopy(buffer, 0, sbytes2, 0, buffer.Length);
            unsafe
            {
                // Instruct the Garbage Collector not to move the memory
                fixed(sbyte *buffBytes2 = sbytes2)
                {
                    myStr2 = new String(buffBytes2, 0, offset);
                }
            }
            String[] array = myStr2.Split(' ');

            Assertion.AssertEquals("Split size", 100, array.Length - 1);

            for (int i = 0; i < array.Length - 1; i += 1)
            {
                Assertion.AssertEquals("Parse token", tint[i], int.Parse(array[i]));
            }


            bf.TruncationThrow = false;

            int val = 1;

            //Arrays.fill(buffer, (byte)' ');

// array is used, values set, etc.
//Array.Clear(buffer, ' ', buffer.Length-1);
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)' ';
            }
            for (int i = 0; i < 10; i += 1)
            {
                offset = bf.format(val, buffer, 0, 6);
                String  test    = (val + "      ").Substring(0, 6);
                sbyte[] sbytes1 = new sbyte[100000];
                Buffer.BlockCopy(buffer, 0, sbytes1, 0, buffer.Length);
                unsafe
                {
                    // Instruct the Garbage Collector not to move the memory
                    fixed(sbyte *buffBytes1 = sbytes1)
                    {
                        if (i < 6)
                        {
                            Assertion.AssertEquals("TestTrunc" + i, test, new String(buffBytes1, 0, 6));
                        }
                        else
                        {
                            Assertion.AssertEquals("TestTrunc" + i, "******", new String(buffBytes1, 0, 6));
                        }
                        val *= 10;
                    }
                }
            }

            bf.TruncationThrow = true;
            val = 1;
            for (int i = 0; i < 10; i += 1)
            {
                bool thrown = false;
                try {
                    offset = bf.format(val, buffer, 0, 6);
                } catch (TruncationException) {
                    thrown = true;
                }
                if (i < 6)
                {
                    Assertion.AssertEquals("TestTruncThrow" + i, false, thrown);
                }
                else
                {
                    Assertion.AssertEquals("TestTruncThrow" + i, true, thrown);
                }
                val *= 10;
            }
        }