示例#1
0
        static void ClipTest(uint[] b, int vals, int bits, uint[] comp, int compsize)
        {
            int   bytes;
            byte *buffer;

            Ogg.oggpack_reset(ref o);

            for (int i = 0; i < vals; i++)
            {
                Ogg.oggpack_write(ref o, b[i], bits > 0 ? bits : iLog(b[i]));
            }

            buffer = Ogg.oggpack_get_buffer(ref o);
            bytes  = Ogg.oggpack_bytes(ref o);

            if (bytes != compsize)
            {
                throw new Exception("wrong number of bytes!");
            }

            for (int i = 0; i < bytes; i++)
            {
                if (buffer[i] != comp[i])
                {
                    for (int j = 0; j < bytes; j++)
                    {
                        Console.WriteLine(buffer[j] + " , " + comp[j]);
                    }

                    throw new Exception("wrote incorrect value!");
                }
            }

            Ogg.oggpack_readinit(ref r, buffer, bytes);

            for (int i = 0; i < vals; i++)
            {
                int tbit = bits > 0 ? bits : iLog(b[i]);

                if (Ogg.oggpack_look(ref r, tbit) == -1)
                {
                    throw new Exception("out of data!");
                }

                if (Ogg.oggpack_look(ref r, tbit) != (b[i] & mask[tbit]))
                {
                    throw new Exception("looked at incorrect value!");
                }

                if (tbit == 1)
                {
                    if (Ogg.oggpack_look1(ref r) != (b[i] & mask[tbit]))
                    {
                        throw new Exception("looked at single bit incorrect value!");
                    }
                }

                if (tbit == 1)
                {
                    if (Ogg.oggpack_read1(ref r) != (b[i] & mask[tbit]))
                    {
                        throw new Exception("read incorrect single bit value!");
                    }
                }
                else if (Ogg.oggpack_read(ref r, tbit) != (b[i] & mask[tbit]))
                {
                    throw new Exception("read incorrect value!");
                }
            }

            if (Ogg.oggpack_bytes(ref r) != bytes)
            {
                throw new Exception("leftover bytes after read!");
            }
        }
示例#2
0
        static public void Test()
        {
            byte *buffer;
            int   bytes = 0;

            /* Test read/write together */
            /* Later we test against pregenerated bitstreams */
            Ogg.oggpack_writeinit(ref o);

            Console.Write("Small preclipped packing (LSb): ");
            ClipTest(testBuffer1, testSize1, 0, one, oneSize);
            Console.WriteLine("ok.");

            Console.Write("Null bit call (LSb): ");
            ClipTest(testBuffer3, testSize3, 0, two, twoSize);
            Console.WriteLine("ok.");

            Console.Write("Large preclipped packing (LSb): ");
            ClipTest(testBuffer2, testSize2, 0, three, threeSize);
            Console.WriteLine("ok.");

            Console.Write("32 bit precliiped packing (LSb): ");
            {
                Ogg.oggpack_reset(ref o);

                for (int i = 0; i < testSize2; i++)
                {
                    Ogg.oggpack_write(ref o, large[i], 32);
                }

                buffer = Ogg.oggpack_get_buffer(ref o);
                bytes  = Ogg.oggpack_bytes(ref o);

                Ogg.oggpack_readinit(ref r, buffer, bytes);

                for (int i = 0; i < testSize2; i++)
                {
                    if (Ogg.oggpack_look(ref r, 32) == -1)
                    {
                        throw new Exception("out of data. failed!");
                    }

                    if (Ogg.oggpack_look(ref r, 32) != large[i])
                    {
                        throw new Exception("read incorrect value! " + Ogg.oggpack_look(ref r, 32) + " != " + large[1]);
                    }

                    Ogg.oggpack_adv(ref r, 32);
                }

                if (Ogg.oggpack_bytes(ref r) != bytes)
                {
                    throw new Exception("leftover bytes after read!");
                }

                Console.WriteLine("ok.");
            }

            Console.Write("Small unclipped packing (LSb): ");
            ClipTest(testBuffer1, testSize1, 7, four, fourSize);
            Console.WriteLine("ok.");

            Console.Write("Large unclipped packing (LSb): ");
            ClipTest(testBuffer2, testSize2, 17, five, fiveSize);
            Console.WriteLine("ok.");

            Console.Write("Single bit unclipped packing (LSb): ");
            ClipTest(testBuffer3, testSize3, 1, six, sixSize);
            Console.WriteLine("ok.");

            Console.Write("Testing read past end (LSb): ");
            {
                byte *temp = (byte *)Ogg._ogg_malloc(8);

                try
                {
                    for (int i = 0; i < 8; i++)
                    {
                        temp[i] = 0;
                    }

                    Ogg.oggpack_readinit(ref r, temp, 8);

                    for (int i = 0; i < 64; i++)
                    {
                        if (Ogg.oggpack_read(ref r, 1) != 0)
                        {
                            throw new Exception("failed; got -1 prematurely.");
                        }
                    }

                    if (Ogg.oggpack_look(ref r, 1) != -1 || Ogg.oggpack_read(ref r, 1) != -1)
                    {
                        throw new Exception("failed; read past end without -1");
                    }

                    for (int i = 0; i < 8; i++)
                    {
                        temp[i] = 0;
                    }

                    Ogg.oggpack_readinit(ref r, temp, 8);

                    if (Ogg.oggpack_read(ref r, 30) != 0 || Ogg.oggpack_read(ref r, 16) != 0)
                    {
                        throw new Exception("failed 2; got -1 prematurely.");
                    }

                    if (Ogg.oggpack_look(ref r, 18) != 0 || Ogg.oggpack_look(ref r, 18) != 0)
                    {
                        throw new Exception("failed 3; got -1 prematurely.");
                    }

                    if (Ogg.oggpack_look(ref r, 19) != -1 || Ogg.oggpack_look(ref r, 19) != -1)
                    {
                        throw new Exception("failed 3; got -1 prematurely.");
                    }

                    if (Ogg.oggpack_look(ref r, 32) != -1 || Ogg.oggpack_look(ref r, 32) != -1)
                    {
                        throw new Exception("failed 3; got -1 prematurely.");
                    }

                    Ogg.oggpack_writeclear(ref o);
                    Console.WriteLine("ok.");
                }
                finally
                {
                    Ogg._ogg_free(temp);
                }
            }

            /********** lazy, cut-n-paste retest with MSb packing ***********/

            /* Test read/write together */
            /* Later we test against pregenerated bitstreams */

            Ogg.oggpackB_writeinit(ref o);

            Console.Write("Small preclipped packing (MSb): ");
            ClipTestB(testBuffer1, testSize1, 0, oneB, oneSize);
            Console.WriteLine("ok.");

            Console.Write("Null bit call (LSb): ");
            ClipTestB(testBuffer3, testSize3, 0, twoB, twoSize);
            Console.WriteLine("ok.");

            Console.Write("Large preclipped packing (LSb): ");
            ClipTestB(testBuffer2, testSize2, 0, threeB, threeSize);
            Console.WriteLine("ok.");

            Console.Write("32 bit precliiped packing (LSb): ");
            {
                Ogg.oggpackB_reset(ref o);

                for (int i = 0; i < testSize2; i++)
                {
                    Ogg.oggpackB_write(ref o, large[i], 32);
                }

                buffer = Ogg.oggpackB_get_buffer(ref o);
                bytes  = Ogg.oggpackB_bytes(ref o);

                Ogg.oggpackB_readinit(ref r, buffer, bytes);

                for (int i = 0; i < testSize2; i++)
                {
                    if (Ogg.oggpackB_look(ref r, 32) == -1)
                    {
                        throw new Exception("out of data. failed!");
                    }

                    if (Ogg.oggpackB_look(ref r, 32) != large[i])
                    {
                        throw new Exception("read incorrect value! " + Ogg.oggpackB_look(ref r, 32) + " != " + large[1]);
                    }

                    Ogg.oggpackB_adv(ref r, 32);
                }

                if (Ogg.oggpackB_bytes(ref r) != bytes)
                {
                    throw new Exception("leftover bytes after read!");
                }

                Console.WriteLine("ok.");
            }

            Console.Write("Small unclipped packing (LSb): ");
            ClipTestB(testBuffer1, testSize1, 7, fourB, fourSize);
            Console.WriteLine("ok.");

            Console.Write("Large unclipped packing (LSb): ");
            ClipTestB(testBuffer2, testSize2, 17, fiveB, fiveSize);
            Console.WriteLine("ok.");

            Console.Write("Single bit unclipped packing (LSb): ");
            ClipTestB(testBuffer3, testSize3, 1, sixB, sixSize);
            Console.WriteLine("ok.");

            Console.Write("Testing read past end (LSb): ");
            {
                byte *temp = (byte *)Ogg._ogg_malloc(8);

                try
                {
                    for (int i = 0; i < 8; i++)
                    {
                        temp[i] = 0;
                    }

                    Ogg.oggpackB_readinit(ref r, temp, 8);

                    for (int i = 0; i < 64; i++)
                    {
                        if (Ogg.oggpackB_read(ref r, 1) != 0)
                        {
                            throw new Exception("failed; got -1 prematurely.");
                        }
                    }

                    if (Ogg.oggpackB_look(ref r, 1) != -1 || Ogg.oggpackB_read(ref r, 1) != -1)
                    {
                        throw new Exception("failed; read past end without -1");
                    }

                    for (int i = 0; i < 8; i++)
                    {
                        temp[i] = 0;
                    }

                    Ogg.oggpackB_readinit(ref r, temp, 8);

                    if (Ogg.oggpackB_read(ref r, 30) != 0 || Ogg.oggpackB_read(ref r, 16) != 0)
                    {
                        throw new Exception("failed 2; got -1 prematurely.");
                    }

                    if (Ogg.oggpackB_look(ref r, 18) != 0 || Ogg.oggpackB_look(ref r, 18) != 0)
                    {
                        throw new Exception("failed 3; got -1 prematurely.");
                    }

                    if (Ogg.oggpackB_look(ref r, 19) != -1 || Ogg.oggpackB_look(ref r, 19) != -1)
                    {
                        throw new Exception("failed 3; got -1 prematurely.");
                    }

                    if (Ogg.oggpackB_look(ref r, 32) != -1 || Ogg.oggpackB_look(ref r, 32) != -1)
                    {
                        throw new Exception("failed 3; got -1 prematurely.");
                    }

                    Ogg.oggpackB_writeclear(ref o);
                    Console.WriteLine("ok.");
                }
                finally
                {
                    Ogg._ogg_free(temp);
                }
            }
        }