[Test] public void Test_Init() { ByteStack bs = new ByteStack(ENC.GetBytes("foo")); Assert.AreEqual("foo", bs.ToString()); bs.Push((byte)'t'); Assert.AreEqual("foot", bs.ToString()); bs = new ByteStack(ENC.GetBytes("f")); bs.Push((byte)'t'); Assert.AreEqual("ft", bs.ToString()); }
[Test] public void Test_Main() { ByteStack bs = new ByteStack(); Assert.AreEqual(0, bs.Count); bs.Push((byte)'a'); Assert.AreEqual(1, bs.Count); byte b = bs.Pop(); Assert.AreEqual(b, (byte)'a'); Assert.AreEqual(0, bs.Count); }
[Test] public void Test_Growth() { ByteStack bs = new ByteStack(4); bs.Push((byte)'b'); Assert.AreEqual("b", bs.ToString()); bs.Push((byte)'c'); Assert.AreEqual("bc", bs.ToString()); bs.Push((byte)'d'); Assert.AreEqual("bcd", bs.ToString()); bs.Push((byte)'e'); Assert.AreEqual("bcde", bs.ToString()); bs.Push((byte)'b'); Assert.AreEqual("bcdeb", bs.ToString()); bs.Push((byte)'c'); Assert.AreEqual("bcdebc", bs.ToString()); bs.Push((byte)'d'); Assert.AreEqual("bcdebcd", bs.ToString()); bs.Push((byte)'e'); Assert.AreEqual("bcdebcde", bs.ToString()); bs.Push((byte)'b'); bs.Push((byte)'c'); bs.Push((byte)'d'); bs.Push((byte)'e'); bs.Push((byte)'b'); bs.Push((byte)'c'); bs.Push((byte)'d'); bs.Push((byte)'e'); Assert.AreEqual("bcdebcdebcdebcde", bs.ToString()); }
//public static NbtDocument ParseValue(ref NbtReader reader) //{ // //} // //public static bool TryParseValue(ref NbtReader reader, out NbtDocument document) //{ //} private static void Parse( ref NbtReader reader, ref MetadataDb database, ref ByteStack <ContainerFrame> stack) { int rowCount = 0; while (reader.Read()) { int location = reader.TagLocation; NbtType type = reader.TagType; NbtFlags flags = reader.TagFlags; PeekStack: ref ContainerFrame frame = ref stack.TryPeek(); if (!Unsafe.IsNullRef(ref frame)) { if (frame.ListEntriesRemaining == 0) { stack.TryPop(); int totalRowCount = rowCount - frame.InitialRowCount; database.SetRowCount(frame.ContainerRow, totalRowCount); goto PeekStack; } else if (frame.ListEntriesRemaining != -1) { frame.ListEntriesRemaining--; } else { frame.CompoundEntryCounter++; } } switch (type) { case NbtType.End: { // Documents with a single End tag (no Compound root) are valid. if (stack.TryPop(out var compoundFrame)) { int totalRowCount = rowCount - compoundFrame.InitialRowCount; int compoundLength = compoundFrame.CompoundEntryCounter - 1; // -1 to exclude End database.SetRowCount(compoundFrame.ContainerRow, totalRowCount); database.SetLength(compoundFrame.ContainerRow, compoundLength); } continue; // Continue to not increment row count } case NbtType.Compound: { int containerRow = database.Append( location, collectionLength: 0, rowCount: 1, type, flags); stack.Push(new ContainerFrame(containerRow, rowCount) { ListEntriesRemaining = -1 }); break; } case NbtType.List: { int listLength = reader.TagCollectionLength; int containerRow = database.Append( location, listLength, rowCount: 1, type, flags); stack.Push(new ContainerFrame(containerRow, rowCount) { ListEntriesRemaining = listLength }); break; } default: database.Append(location, reader.TagCollectionLength, rowCount: 1, type, flags); break; } rowCount++; } database.TrimExcess(); }
public static void RunAutomated() { Console.Title = "Dreary Program test"; Console.WriteLine("Stack trace\n" + Environment.StackTrace); Console.WriteLine("DREARY PROGTEST"); try { ByteStack stack = new ByteStack(16); stack.Push((byte)0xff); PrintByteArray(stack.DumpStack()); Console.WriteLine(stack.PopByte() + " should be " + 0xff); stack.Push((ushort)0xffff); PrintByteArray(stack.DumpStack()); ushort outputs = 0; stack.PopShort(ref outputs); Console.WriteLine(outputs + " should be " + 0xffff); stack.Push(0xffffffff); PrintByteArray(stack.DumpStack()); int outputi = 0; stack.PopInt(ref outputi); Console.WriteLine(outputi + " should be " + 0xffffffff); } catch (Exception e) { Console.WriteLine("test failed: " + e); Environment.Exit(1); } Console.Write(Directory.GetCurrentDirectory() + "\\"); string audiotoplay = "GameSounds/rwd.mp3"; Console.WriteLine("Playing " + audiotoplay); try { AudioSystem.PlayAudio(audiotoplay); } catch (Exception e) { Console.WriteLine("Error playing sound: " + e); Environment.Exit(2); } Console.WriteLine("Testing form"); try { Form1 form = new Form1(); form.Show(); Console.WriteLine("Sleeping for 5s"); Thread.Sleep(5000); form.Close(); form.Dispose(); } catch (Exception e) { Console.WriteLine(e); Environment.Exit(3); } Console.WriteLine("Test done"); Console.WriteLine("TEST DONE"); Environment.Exit(0); }
public static void Run() { Console.Title = "Dreary Program test"; Console.WriteLine("Stack trace\n" + Environment.StackTrace); Console.WriteLine("DREARY PROGTEST"); Console.WriteLine("Select mode"); Console.WriteLine("1 test stack, 2 continue program, 3 net test, 4 audio test, 5 form test, 6 auto test, 0 exit dreary"); bool continueprog = false; bool testing = true; while (testing) { Console.Write('.'); switch (Console.ReadLine()) { case "0": testing = false; break; case "1": try { ByteStack stack = new ByteStack(16); stack.Push((byte)0xff); PrintByteArray(stack.DumpStack()); Console.WriteLine(stack.PopByte() + " should be " + 0xff); stack.Push((ushort)0xffff); PrintByteArray(stack.DumpStack()); ushort outputs = 0; stack.PopShort(ref outputs); Console.WriteLine(outputs + " should be " + 0xffff); stack.Push(0xffffffff); PrintByteArray(stack.DumpStack()); int outputi = 0; stack.PopInt(ref outputi); Console.WriteLine(outputi + " should be " + 0xffffffff); } catch (Exception e) { Console.WriteLine("test failed: " + e); } break; case "2": continueprog = true; testing = false; break; case "3": try { Console.WriteLine("Starting server"); Task serverformthread = new Task(() => { try { Form1 serverform = new Form1(); serverform.Show(); serverform.server = new Net.Server(21212, serverform); serverform.server.Start(); } catch (Exception e) { Console.WriteLine("Server failed " + e); } }); Console.WriteLine("Starting client"); Task clientformthread = new Task(() => { try { Form1 clientform = new Form1(); clientform.Show(); clientform.client = new Net.Client(clientform); clientform.client.Connect("127.0.0.1", 21212); } catch (Exception e) { Console.WriteLine("Client failed " + e); } }); serverformthread.Start(); clientformthread.Start(); } catch (Exception e) { Console.WriteLine("test failed: " + e); } break; case "4": Console.Write(Directory.GetCurrentDirectory() + "\\"); string audiotoplay = Console.ReadLine(); Console.WriteLine("Playing " + audiotoplay); try { AudioSystem.PlayAudio(audiotoplay); } catch (Exception e) { Console.WriteLine("Error playing sound: " + e); } break; case "5": Console.WriteLine("Testing form"); try { Form1 form = new Form1(); form.Show(); Console.WriteLine("Sleeping for 5s"); Thread.Sleep(5000); form.Close(); form.Dispose(); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Test done"); break; case "6": Console.WriteLine("Autotesting (will exit upon finish)"); try { RunAutomated(); } catch (Exception e) { Console.WriteLine(e); } break; default: Console.WriteLine("?"); break; } } Console.WriteLine("TEST DONE"); if (!continueprog) { Environment.Exit(0); } }