Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Stream have diffrent size while debugging

Posted on 2009-07-01
1
264 Views
Last Modified: 2012-05-07
Hi,

I have a TestMethod to compare 2 streams using their bytes. here is part of the code (see code)

When debug this test then it pass, but when i just run it then i get the length differ fail assertion.

what is wrong in this code?
byte[] actual;
 byte[] expectedbytes;
 
using (MemoryStream actualstream = GenerateDocument(document, null, MpFunctionCode.NWPOF))
                {
                    actualstream.Position = 0;
                    actual = actualstream.ToArray();
                    actualstream.Close();
                }
 
using (Stream expected = typeof(GeneratorTest).Assembly.GetManifestResourceStream("Generator.UnitTests.resources.ExpectedOutput.pdf"))
            {
                expected.Position = 0;
                expectedbytes = WriteToByte(expected, (int)expected.Length);
                expected.Close();
            }
 
AssertEqual(actual, expectedbytes);
 
 
private static void AssertEqual(byte[] actual, byte[] expectedbytes)
        {
            if (actual.Length != expectedbytes.Length)
                Assert.Fail("Length differ:    Actual: " + actual.Length + "        Expected: " + expectedbytes.Length);
 
            for (int i = 0; i < expectedbytes.Length; i++)
            {
                if (expectedbytes[i] != actual[i])
                    Assert.Fail("Not the same: " + actual[i] + " " + expectedbytes[i] + " index: " + i);
            }
        }
 
 
private static byte[] WriteToByte(Stream stream, int initialLength)
        {
            stream.Position = 0;
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }
 
            byte[] buffer = new byte[initialLength];
            int read = 0;
 
            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;
 
                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();
 
                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }
 
                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
       
            return ret;
        }

Open in new window

0
Comment
Question by:kimos123
1 Comment
 

Accepted Solution

by:
kimos123 earned 0 total points
ID: 24761059
nvm delete this
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question