📄 bplustest.cs
字号:
throw new ApplicationException("when walking found "+currentkey+" when expecting "+testkey);
}
currentkey = bpt.NextKey(testkey);
}
}
public static void BplusTreeLongTest()
{
Console.WriteLine("TESTING BPLUSTREELONG -- LOTS OF OUTPUT to Debug.WriteLine(...)");
for (int nodesize=2; nodesize<6; nodesize++)
{
allinserts = new Hashtable();
System.IO.Stream mstream = new System.IO.MemoryStream();
int keylength = 10+nodesize;
BplusDotNet.BplusTreeLong bpt = BplusDotNet.BplusTreeLong.InitializeInStream(mstream, keylength, nodesize);
bpt = restart(bpt);
//bpt["d"] = 15;
inserttest(bpt, "d", 15);
deletetest(bpt, "d", 15);
inserttest(bpt, "d", 15);
inserttest(bpt, "", 99);
bpt.SerializationCheck();
//bpt["ab"] = 55;
inserttest(bpt, "ab", 55);
//bpt["b"] = -5;
inserttest(bpt, "b", -5);
deletetest(bpt, "b", 0);
inserttest(bpt, "b", -5);
//return;
//bpt["c"] = 34;
inserttest(bpt, "c", 34);
//bpt["a"] = 8;
inserttest(bpt, "a", 8);
commit(bpt);
Debug.WriteLine("<h1>after commit</h1>\r\n");
Debug.WriteLine(bpt.toHtml());
//bpt["a"] = 800;
inserttest(bpt, "a", 800);
//bpt["ca"]= -999;
inserttest(bpt, "ca", -999);
//bpt["da"]= -999;
inserttest(bpt, "da", -999);
//bpt["ea"]= -9991;
inserttest(bpt, "ea", -9991);
//bpt["aa"]= -9992;
inserttest(bpt, "aa", -9992);
//bpt["ba"]= -9995;
inserttest(bpt, "ba", -9995);
commit(bpt);
//bpt["za"]= -9997;
inserttest(bpt, "za", -9997);
//bpt[" a"]= -9999;
inserttest(bpt, " a", -9999);
commit(bpt);
deletetest(bpt, "d", 0);
deletetest(bpt, "da", 0);
deletetest(bpt, "ca", 0);
bpt = restart(bpt);
inserttest(bpt, "aaa", 88);
Console.WriteLine(" now doing torture test for "+nodesize);
Debug.WriteLine("<h1>now doing torture test for "+nodesize+"</h1>");
if (full)
{
for (int i=0; i<33; i++)
{
for (int k=0; k<10; k++)
{
int m = (i*5+k*23)%77;
string s = "b"+m;
inserttest(bpt, s, m);
if (i%2==1 || k%3==1)
{
deletetest(bpt, s, m);
}
}
int j = i%3;
if (j==0)
{
abort(bpt);
}
else if (j==1)
{
commit(bpt);
}
else
{
bpt = restart(bpt);
}
}
}
commit(bpt);
deletetest(bpt, "za", 0);
deletetest(bpt, "ea", 0);
deletetest(bpt, "c", 0);
deletetest(bpt, "ba", 0);
deletetest(bpt, "b", 0);
deletetest(bpt, "ab", 0);
abort(bpt);
inserttest(bpt, "dog", 1);
commit(bpt);
deletetest(bpt, "dog", 1);
inserttest(bpt, "pig", 2);
abort(bpt);
inserttest(bpt, "cat", 3);
bpt.Recover(true);
}
}
public static String CompatKey(int i, int j, int k, int l)
{
String seed = "i="+i+" j="+j+" k="+k+" ";
String result = seed;
// for (int ii=0; ii<l; ii++)
// {
// result += seed;
// }
return ""+l+result;
}
public static String CompatValue(int i, int j, int k, int l)
{
String result = CompatKey(k,j,l,i)+CompatKey(l,k,j,i);
return result+result;
}
public static void CompatTest()
{
if (tempdirectory==null)
{
Console.WriteLine(" compatibility test requires temp directory to be defined: please edit test source file");
return;
}
string myTreeFileName = tempdirectory+"\\CsharpTree.dat";
string myBlocksFileName = tempdirectory+"\\CsharpBlocks.dat";
string otherTreeFileName = tempdirectory+"\\JavaTree.dat";
string otherBlocksFileName = tempdirectory+"\\JavaBlocks.dat";
Hashtable map = new Hashtable();
Console.WriteLine(" creating "+myTreeFileName+" and "+myBlocksFileName);
if (System.IO.File.Exists(myTreeFileName))
{
Console.WriteLine(" deleting existing files");
System.IO.File.Delete(myTreeFileName);
System.IO.File.Delete(myBlocksFileName);
}
BplusDotNet.hBplusTree myTree = BplusDotNet.hBplusTree.Initialize(myTreeFileName, myBlocksFileName, 6);
for (int i=0; i<10; i++)
{
Console.WriteLine(" "+i);
for (int j=0; j<10; j++)
{
for (int k=0; k<10; k++)
{
for (int l=0; l<10; l++)
{
String TheKey = CompatKey(i,j,k,l);
String TheValue = CompatValue(i,j,k,l);
map[TheKey] = TheValue;
myTree[TheKey] = TheValue;
}
}
}
}
myTree.Commit();
myTree.Shutdown();
Console.WriteLine(" trying to test "+otherTreeFileName+" and "+otherBlocksFileName);
if (!System.IO.File.Exists(otherTreeFileName))
{
Console.WriteLine(" file not created yet :(");
//return;
}
else
{
int count = 0;
BplusDotNet.hBplusTree otherTree = BplusDotNet.hBplusTree.ReadOnly(otherTreeFileName, otherBlocksFileName);
foreach (DictionaryEntry D in map)
{
if ( (count%1000)==1)
{
Console.WriteLine(" ... "+count);
}
String TheKey = (String) D.Key;
String TheValue = (String) D.Value;
String OtherValue = otherTree[TheKey];
if (!OtherValue.Equals(TheValue) )
{
throw new Exception(" Values don't match "+TheValue+" "+OtherValue);
}
count++;
}
}
otherTreeFileName = tempdirectory+"\\PyTree.dat";
otherBlocksFileName = tempdirectory+"\\PyBlocks.dat";
Console.WriteLine(" trying to test "+otherTreeFileName+" and "+otherBlocksFileName);
if (!System.IO.File.Exists(otherTreeFileName))
{
Console.WriteLine(" file not created yet :(");
//return;
}
else
{
int count = 0;
BplusDotNet.hBplusTree otherTree = BplusDotNet.hBplusTree.ReadOnly(otherTreeFileName, otherBlocksFileName);
foreach (DictionaryEntry D in map)
{
if ( (count%1000)==1)
{
Console.WriteLine(" ... "+count);
}
String TheKey = (String) D.Key;
String TheValue = (String) D.Value;
String OtherValue = otherTree[TheKey];
if (!OtherValue.Equals(TheValue) )
{
throw new Exception(" Values don't match "+TheValue+" "+OtherValue);
}
count++;
}
}
Console.WriteLine(" compatibility test ok");
}
public static void LinkedFileTest()
{
Console.WriteLine("TESTING LINKED FILE");
System.IO.Stream mstream = new System.IO.MemoryStream();
// make a bunch of sample data
int asize = 200;
//int asize = 2;
int maxsizing = 53;
int prime = 17;
int buffersize = 33;
string seedData = "a wop bop a loo bop";
byte[][] stuff = new byte[asize][];
for (int i=0; i<asize; i++)
{
stuff[i] = makeSampleData(seedData, (i*prime)%maxsizing);
}
// store them off
BplusDotNet.LinkedFile lf = BplusDotNet.LinkedFile.InitializeLinkedFileInStream(mstream, buffersize, prime);
lf.checkStructure();
long[] seeks = new long[asize];
for (int i=0; i<asize; i++)
{
seeks[i] = lf.StoreNewChunk(stuff[i], 0, stuff[i].Length);
// allocated it again and delete it off to mix things up...
long dummy = lf.StoreNewChunk(stuff[i], 0, stuff[i].Length);
lf.ReleaseBuffers(dummy);
lf.checkStructure();
}
// delete the last one
lf.ReleaseBuffers(seeks[asize-1]);
lf.checkStructure();
lf.Flush();
// create new handle
lf = BplusDotNet.LinkedFile.SetupFromExistingStream(mstream, prime);
// read them back and check (except for last)
for (int i=0; i<asize-1; i++)
{
byte[] retrieved = lf.GetChunk(seeks[i]);
testByteArrays(retrieved, stuff[i]);
// delete every so often
if (i%prime==1)
{
lf.ReleaseBuffers(seeks[i]);
lf.checkStructure();
}
}
lf.checkStructure();
Debug.WriteLine("");
Debug.WriteLine("linked file tests ok");
}
public static byte[] makeSampleData(string testdata, int sizing)
{
if (testdata.Length<1 || sizing<1)
{
return new byte[0];
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i=0; i<sizing; i++)
{
char c = testdata[i % testdata.Length];
sb.Append(testdata);
sb.Append(c);
}
string result = sb.ToString();
return System.Text.UTF8Encoding.ASCII.GetBytes(result);
}
public static void testBufferFile()
{
Console.WriteLine("TESTING BUFFERFILE");
int buffersize = 17;
int writesize = 10;
System.IO.Stream mstream = new System.IO.MemoryStream();
int offset = 55;
BplusDotNet.BufferFile bf = BplusDotNet.BufferFile.InitializeBufferFileInStream(mstream, buffersize, offset);
byte[] testheader = bf.makeHeader();
byte[] inputarray = makeSampleData("THIS IS SOME sample data off the cuff...", 100);
byte[] outputarray = new byte[inputarray.Length];
int position = 0;
// shove in the testdata in reverse order
for (int i=inputarray.Length; i>writesize; i-=writesize)
{
Debug.Write(" "+position);
//Console.Write(" "+position);
bf.setBuffer(position, inputarray, i-writesize, writesize);
position++;
}
bf.setBuffer(position, inputarray, 0, writesize);
// extract it again
bf = BplusDotNet.BufferFile.SetupFromExistingStream(mstream, offset);
position = 0;
Debug.WriteLine("");
//Console.WriteLine("");
for (int i=inputarray.Length; i>writesize; i-=writesize)
{
Debug.Write(" "+position);
//Console.Write(" "+position);
bf.getBuffer(position, outputarray, i-writesize, writesize);
position++;
}
bf.getBuffer(position, outputarray, 0, writesize);
testByteArrays(inputarray, outputarray);
Debug.WriteLine("");
Debug.WriteLine(" buffer file test ok");
}
public static void testByteArrays(byte[] a, byte[] b)
{
if (a.Length!=b.Length)
{
throw new Exception("array lengths don't match "+a.Length+" "+b.Length);
}
for (int i=0; i<b.Length; i++)
{
if (a[i]!=b[i])
{
throw new Exception("first error at "+i+" "+a[i]+" "+b[i]);
}
}
}
public static void intTests()
{
int bsize = 13;
byte[] buffer = new byte[bsize];
int[] ints = {1, 566, -55, 32888, 4201010, 87878, -8989898};
int index = 99;
foreach (int theInt in ints)
{
index = Math.Abs(index) % (bsize-4);
BplusDotNet.BufferFile.Store(theInt, buffer, index);
int otherInt = BplusDotNet.BufferFile.Retrieve(buffer, index);
if (theInt!=otherInt)
{
throw new Exception("encode/decode int failed "+theInt+"!="+otherInt);
}
index = (index+theInt);
}
Debug.WriteLine("encode/decode of ints ok");
}
public static void shortTests()
{
int bsize = 13;
byte[] buffer = new byte[bsize];
short[] shorts = {1, 566, -32766, 32, 32755, 80, -8989};
int index = 99;
foreach (short theInt in shorts)
{
index = Math.Abs(index) % (bsize-4);
BplusDotNet.BufferFile.Store(theInt, buffer, index);
short otherInt = BplusDotNet.BufferFile.RetrieveShort(buffer, index);
if (theInt!=otherInt)
{
throw new Exception("encode/decode int failed "+theInt+"!="+otherInt);
}
index = (index+theInt);
}
Debug.WriteLine("encode/decode of longs ok");
}
public static void longTests()
{
int bsize = 17;
byte[] buffer = new byte[bsize];
long[] longs = {1, 566, -55, 32888, 4201010, 87878, -8989898, 0xefaefabbccddee, -0xefaefabbccddee};
int index = 99;
foreach (long theLong in longs)
{
index = Math.Abs(index) % (bsize-8);
BplusDotNet.BufferFile.Store(theLong, buffer, index);
long otherLong = BplusDotNet.BufferFile.RetrieveLong(buffer, index);
if (theLong!=otherLong)
{
throw new Exception("encode/decode int failed "+theLong+"!="+otherLong);
}
index = (index+((int)(theLong&0xffffff)));
}
Debug.WriteLine("encode/decode of longs ok");
}
public static void byteStringTest()
{
byte[] testbytes = new byte[128];
for (byte i=0; i<128; i++)
{
testbytes[i] = i;
}
string teststring = BplusDotNet.BplusTree.BytesToString(testbytes);
if (teststring.Length!=128)
{
throw new Exception("test string changed length "+teststring.Length);
}
testbytes = BplusDotNet.BplusTree.StringToBytes(teststring);
if (testbytes.Length!=128)
{
throw new Exception("test string changed length "+teststring.Length);
}
//string test = BplusDotNet.hBplusTreeBytes.PrefixForByteCount1("thisThing", 5);
//Debug.WriteLine("hash of thisThing is for len 5 '"+test+"'");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -