Skip to content

Instantly share code, notes, and snippets.

@meklarian
Created June 6, 2013 21:27
Show Gist options
  • Save meklarian/5725123 to your computer and use it in GitHub Desktop.
Save meklarian/5725123 to your computer and use it in GitHub Desktop.
ReadWriteVerifyTest - big file naive remaining space error checking test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ReadWriteVerifyTest
{
class Program
{
static long ExamineBlock(FileStream fs, long startPos, long length, byte pattern)
{
long errors = 0;
fs.Seek(startPos, SeekOrigin.Begin);
// Write
for (long l = 0; l < length; l++)
{
fs.WriteByte(pattern);
}
fs.Seek(-length, SeekOrigin.Current);
for (long l = 0; l < length; l++)
{
int i = fs.ReadByte();
if (i != pattern) { errors++; }
}
return errors;
}
static long TestBlock(FileStream fs, long block, long blockSize, long maxSize)
{
long blockStart = block * blockSize;
long blockEnd = blockStart + blockSize;
if (blockEnd > maxSize) { blockEnd = maxSize; }
if (blockStart > maxSize) { blockStart = maxSize; }
long testBlockSize = blockEnd - blockStart;
if (testBlockSize <= 0) { return 0; }
long errors = 0;
long subErrors = 0;
byte testPattern = 0;
subErrors = 0;
testPattern = 0xFF;
subErrors = ExamineBlock(fs, blockStart, testBlockSize, testPattern);
if (subErrors > 0)
{
Console.WriteLine("block {0} failed on test pattern 0x{1:X2} with {2} errors.", block, testPattern, subErrors);
}
errors += subErrors;
subErrors = 0;
testPattern = 0x00;
subErrors = ExamineBlock(fs, blockStart, testBlockSize, testPattern);
if (subErrors > 0)
{
Console.WriteLine("block {0} failed on test pattern 0x{1:X2} with {2} errors.", block, testPattern, subErrors);
}
errors += subErrors;
subErrors = 0;
testPattern = 0x55;
subErrors = ExamineBlock(fs, blockStart, testBlockSize, testPattern);
if (subErrors > 0)
{
Console.WriteLine("block {0} failed on test pattern 0x{1:X2} with {2} errors.", block, testPattern, subErrors);
}
errors += subErrors;
subErrors = 0;
testPattern = 0xAA;
subErrors = ExamineBlock(fs, blockStart, testBlockSize, testPattern);
if (subErrors > 0)
{
Console.WriteLine("block {0} failed on test pattern 0x{1:X2} with {2} errors.", block, testPattern, subErrors);
}
errors += subErrors;
return errors;
}
static void DoTest(string location, long maxSize, long blockSize)
{
string filename = Path.Combine(location, Path.GetRandomFileName());
using (FileStream fs = File.Create(filename))
{
//fs.SetLength(maxSize);
fs.Flush();
long blocks = (maxSize / blockSize) + ((maxSize % blockSize) > 0 ? 1 : 0);
for (long block = 0; block < blocks; block++)
{
TestBlock(fs, block, blockSize, maxSize);
}
fs.Close();
}
}
[STAThread]
static void Main(string[] args)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if(DialogResult.OK != dlg.ShowDialog()){return;}
string location = dlg.SelectedPath;
string root = Path.GetPathRoot(location);
DriveInfo inf = new DriveInfo(root);
long space = inf.AvailableFreeSpace;
long blockSize = 128 * 1024;
DoTest(location, space, blockSize);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment