public void GetHashCodeTest() { int expected = 1250U.GetHashCode(); int actual = new StackInt(1250).GetHashCode(); Assert.Equal(expected, actual); }
public void ToStringTest() { StackInt si = new StackInt(123); string actual = si.ToString(); Assert.Equal("123", actual); }
public void GetOpCodeTest(uint val, OP expected) { StackInt si = new StackInt(val); OP actual = si.GetOpCode(); Assert.Equal(expected, actual); }
/// <summary> /// Initializes a new instance of <see cref="ReturnOp"/> using the given <see cref="IScript"/>. /// </summary> /// <exception cref="ArgumentNullException"/> /// <param name="scr">Script to use</param> /// <param name="usePushOp"> /// [Default value = true] /// If true, the data will be included after <see cref="OP.RETURN"/> using <see cref="PushDataOp"/> scheme. /// </param> public ReturnOp(IScript scr, bool usePushOp = true) { if (scr == null) { throw new ArgumentNullException(nameof(scr), "Script can not be null."); } byte[] temp = scr.Data; if (usePushOp) { StackInt size = new StackInt(temp.Length); FastStream stream = new FastStream(temp.Length + 2); stream.Write((byte)OP.RETURN); size.WriteToStream(stream); stream.Write(temp); data = stream.ToByteArray(); } else { data = new byte[temp.Length + 1]; data[0] = (byte)OP.RETURN; Buffer.BlockCopy(temp, 0, data, 1, temp.Length); } }
/// <summary> /// Initializes a new instance of <see cref="ReturnOp"/> using the given <see cref="IScript"/>. /// </summary> /// <exception cref="ArgumentNullException"/> /// <param name="scr">Script to use</param> /// <param name="usePushOp"> /// [Default value = true] /// If true, the data will be included after <see cref="OP.RETURN"/> using <see cref="PushDataOp"/> scheme. /// </param> public ReturnOp(IScript scr, bool usePushOp = true) { if (scr == null) { throw new ArgumentNullException(nameof(scr), "Script can not be null."); } byte[] temp = scr.ToByteArray(); if (usePushOp) { StackInt size = new StackInt(temp.Length); Data = ByteArray.ConcatArrays( new byte[1] { (byte)OP.RETURN }, size.ToByteArray(), temp); } else { Data = new byte[temp.Length + 1]; Data[0] = (byte)OP.RETURN; Buffer.BlockCopy(temp, 0, Data, 1, temp.Length); } }
/// <summary> /// Initializes a new instance of <see cref="ReturnOp"/> using the given data. /// </summary> /// <param name="ba">Data to use (can be null)</param> /// <param name="usePushOp"> /// [Default value = true] /// If true, the data will be included after <see cref="OP.RETURN"/> using <see cref="PushDataOp"/> scheme. /// </param> public ReturnOp(byte[] ba, bool usePushOp = true) { if (ba == null || ba.Length == 0) { Data = new byte[1] { (byte)OP.RETURN }; } else if (usePushOp) { StackInt size = new StackInt(ba.Length); Data = ByteArray.ConcatArrays( new byte[1] { (byte)OP.RETURN }, size.ToByteArray(), ba); } else { Data = new byte[ba.Length + 1]; Data[0] = (byte)OP.RETURN; Buffer.BlockCopy(ba, 0, Data, 1, ba.Length); } }
public void TryRead_Fail_NullStreamTest() { bool b = StackInt.TryRead(null, out _, out StackInt actual, out string error); Assert.False(b); Assert.Equal("Stream can not be null.", error); Helper.ComparePrivateField(actual, "value", 0U); }
public void Equals_EdgeTest() { StackInt si = new StackInt(100); object sObj = "StackInt!"; object nl = null; Assert.False(si.Equals(sObj)); Assert.False(si.Equals(nl)); }
public void CompareTo_EdgeTest() { StackInt si = new StackInt(100); object nObj = null; object sObj = "StackInt!"; Assert.Equal(1, si.CompareTo(nObj)); Assert.Throws <ArgumentException>(() => si.CompareTo(sObj)); }
public void Comparison_WithInt_EqualTest(int c, int i, bool expected) { StackInt ci = new StackInt(c); Assert.Equal(expected, ci == i); Assert.Equal(!expected, ci != i); Assert.Equal(expected, i == ci); Assert.Equal(!expected, i != ci); }
public void TryRead_FailStrictTest(byte[] data, int finalPos, string expError) { FastStreamReader stream = new FastStreamReader(data); bool b = StackInt.TryRead(stream, true, out StackInt actual, out string error); Assert.False(b); Assert.Equal(expError, error); Helper.ComparePrivateField(stream, "position", finalPos); Helper.ComparePrivateField(actual, "value", 0U); }
public void TryRead_StrictTest(byte[] data, int finalPos, uint expected) { FastStreamReader stream = new FastStreamReader(data); bool b = StackInt.TryRead(stream, true, out StackInt actual, out string error); Assert.True(b); Assert.Null(error); Helper.ComparePrivateField(stream, "position", finalPos); Helper.ComparePrivateField(actual, "value", expected); }
public void TryReadTest(byte[] data, bool expSuccess, byte[] expBytes, string expErr, int finalPos, uint expected) { var stream = new FastStreamReader(data); bool actSuccess = StackInt.TryRead(stream, out byte[] actBytes, out StackInt actual, out string error); Assert.Equal(expSuccess, actSuccess); Assert.Equal(expBytes, actBytes); Assert.Equal(expErr, error); Helper.ComparePrivateField(stream, "position", finalPos); Helper.ComparePrivateField(actual, "value", expected); }
/// <summary> /// Initializes a new instance of <see cref="PushDataOp"/> using the given <see cref="IScript"/>. /// </summary> /// <exception cref="ArgumentNullException"/> /// <param name="script"> /// Script to use (will be converted to byte array using the <see cref="IScript.ToByteArray"/> function) /// </param> public PushDataOp(IScript script) { if (script == null) { throw new ArgumentNullException(nameof(script), "Script can not be null."); } data = script.ToByteArray(); StackInt size = new StackInt(data.Length); OpValue = size.GetOpCode(); }
public void ToByteArrayTest(byte[] data, int finalOffset, uint val) { StackInt ci = new StackInt(val); FastStream stream = new FastStream(10); ci.WriteToStream(stream); byte[] actual = stream.ToByteArray(); byte[] expected = new byte[finalOffset]; Buffer.BlockCopy(data, 0, expected, 0, finalOffset); Assert.Equal(expected, actual); }
public void AddWithStackIntLengthTest(int init, int add) { var counter = new SizeCounter(init); counter.AddWithStackIntLength(add); var si = new StackInt(add); var stream = new FastStream(5); si.WriteToStream(stream); int expected = stream.GetSize() + add + init; Assert.Equal(expected, counter.Size); }
/// <summary> /// Initializes a new instance of <see cref="PushDataOp"/> using the given integer. /// </summary> /// <param name="num">Integer value to use</param> public PushDataOp(long num) { if (num >= -1 && num <= 16) // We have OP for these { _ = TryConvertToOp((int)num, out _opVal); data = null; } else // There is no OP code, we have to use regular push { data = IntToByteArray(num); StackInt size = new StackInt(data.Length); _opVal = size.GetOpCode(); } }
/// <summary> /// Initializes a new instance of <see cref="PushDataOp"/> using the given integer. /// </summary> /// <param name="num">Integer value to use</param> public PushDataOp(int num) { if (num >= -1 && num <= 16) // We have OP for these { OpValue = OpHelper.IntToOp(num); data = null; } else // There is no OP, we have to use regular push { // TODO: this is wrong!!! data = OpHelper.IntToByteArray(num); StackInt size = new StackInt(data.Length); OpValue = size.GetOpCode(); } }
public void ComparisonOperator_SameTypeTest(StackInt si1, StackInt si2, ValueCompareResult expected) { Assert.Equal(expected.Bigger, si1 > si2); Assert.Equal(expected.BiggerEqual, si1 >= si2); Assert.Equal(expected.Smaller, si1 < si2); Assert.Equal(expected.SmallerEqual, si1 <= si2); Assert.Equal(expected.Equal, si1 == si2); Assert.Equal(!expected.Equal, si1 != si2); Assert.Equal(expected.Equal, si1.Equals(si2)); Assert.Equal(expected.Equal, si1.Equals((object)si2)); Assert.Equal(expected.Compare, si1.CompareTo(si2)); Assert.Equal(expected.Compare, si1.CompareTo((object)si2)); }
/// <summary> /// Reset the game /// </summary> private void Restart() { //initialize the stacks for the towers for (int f = 0; f < towers.Length; f++) { towers[f] = new StackInt(5); } //Add all the disks into the first tower //each disk is represented by a number //1 = the smallest disk, 2 = the 2nd smallest,... for (int i = noDisks; i >= 1; i--) // 5 - 4 - 3 - 2 - 1 { towers[0].Push(i); } Counter = 0; }
/// <summary> /// Initializes a new instance of <see cref="PushDataOp"/> using the given byte array. /// </summary> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException"/> /// <param name="ba">Byte array to use</param> public PushDataOp(byte[] ba) { if (ba == null) { throw new ArgumentNullException(nameof(ba), "Byte array can not be null."); } if (OpHelper.HasOpNum(ba)) // TODO: add isStrict field for this check. { throw new ArgumentException("Short form of data exists which should be used instead."); } data = ba.CloneByteArray(); StackInt size = new StackInt(ba.Length); OpValue = size.GetOpCode(); }
public void Comparison_WithIntTest(int c, int i, bool expected, bool expectedEq) { StackInt ci = new StackInt(c); Assert.Equal(expected, ci > i); Assert.Equal(expectedEq, ci >= i); Assert.Equal(!expected, i > ci); Assert.Equal(!expectedEq, i >= ci); Assert.Equal(!expected, ci < i); Assert.Equal(!expectedEq, ci <= i); Assert.Equal(expected, i < ci); Assert.Equal(expectedEq, i <= ci); }
public void Comparison_EqualTest() { StackInt first = new StackInt(1); StackInt second = new StackInt(1); Assert.False(first > second); Assert.True(first >= second); Assert.False(second < first); Assert.True(second <= first); Assert.True(first == second); Assert.False(first != second); Assert.Equal(0, first.CompareTo(second)); Assert.Equal(0, first.CompareTo((object)second)); Assert.True(first.Equals(second)); Assert.True(first.Equals((object)second)); }
/// <summary> /// Initializes a new instance of <see cref="PushDataOp"/> using the given <see cref="IScript"/>. /// </summary> /// <exception cref="ArgumentNullException"/> /// <param name="script">Script to use</param> public PushDataOp(IScript script) { if (script == null) { throw new ArgumentNullException(nameof(script), "Script can not be null."); } if (script.Data.Length > Constants.MaxScriptItemLength) { throw new ArgumentOutOfRangeException(nameof(script), $"Script byte size to be pushed to the stack can not be bigger than {Constants.MaxScriptItemLength} bytes."); } data = script.Data.CloneByteArray(); StackInt size = new StackInt(data.Length); _opVal = size.GetOpCode(); }
public void Comparison_BigSmall_EqualIntTest() { StackInt ci = new StackInt(1); int i = 1; Assert.False(ci > i); Assert.True(ci >= i); Assert.False(i > ci); Assert.True(i >= ci); Assert.False(ci < i); Assert.True(ci <= i); Assert.False(i < ci); Assert.True(i <= ci); }
public void ComparisonTest() { StackInt big = new StackInt(1); StackInt small = new StackInt(0); Assert.True(big > small); Assert.True(big >= small); Assert.True(small < big); Assert.True(small <= big); Assert.False(big == small); Assert.True(big != small); Assert.Equal(1, big.CompareTo(small)); Assert.Equal(1, big.CompareTo((object)small)); Assert.Equal(-1, small.CompareTo(big)); Assert.Equal(-1, small.CompareTo((object)big)); Assert.False(big.Equals(small)); Assert.False(big.Equals((object)small)); }
public void Cast_FromNumberTest() { uint ui = 0x11223344U; ushort us = 0x1122; byte b = 0x01; int i = 0x11223344; int negi = -1; StackInt c1 = ui; StackInt c2 = us; StackInt c3 = b; StackInt c4 = (StackInt)i; StackInt c5 = (StackInt)negi; Helper.ComparePrivateField(c1, "value", (uint)ui); Helper.ComparePrivateField(c2, "value", (uint)us); Helper.ComparePrivateField(c3, "value", (uint)b); Helper.ComparePrivateField(c4, "value", (uint)i); Helper.ComparePrivateField(c5, "value", (uint)negi); }
public void ComparisonOperator_WithIntTest(StackInt si, int i, ValueCompareResult expected) { Assert.Equal(expected.Bigger, si > i); Assert.Equal(expected.Bigger, i < si); Assert.Equal(expected.BiggerEqual, si >= i); Assert.Equal(expected.BiggerEqual, i <= si); Assert.Equal(expected.Smaller, si < i); Assert.Equal(expected.Smaller, i > si); Assert.Equal(expected.SmallerEqual, si <= i); Assert.Equal(expected.SmallerEqual, i >= si); Assert.Equal(expected.Equal, si == i); Assert.Equal(expected.Equal, i == si); Assert.Equal(!expected.Equal, si != i); Assert.Equal(!expected.Equal, i != si); }
/// <summary> /// Initializes a new instance of <see cref="PushDataOp"/> using the given byte array. /// Throws an <see cref="ArgumentException"/> if there is an OP_number available equal to the value of the byte array. /// </summary> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException"/> /// <param name="ba">Byte array to use</param> public PushDataOp(byte[] ba) { if (ba == null) { throw new ArgumentNullException(nameof(ba), "Byte array can not be null."); } if (HasNumOp(ba)) { throw new ArgumentException("Short form of data exists with OP codes which should be used instead."); } if (ba.Length > Constants.MaxScriptItemLength) { throw new ArgumentOutOfRangeException(nameof(ba), $"Data to be pushed to the stack can not be bigger than {Constants.MaxScriptItemLength} bytes."); } data = ba.CloneByteArray(); StackInt size = new StackInt(ba.Length); _opVal = size.GetOpCode(); }
public void Cast_ToNumberTest() { StackInt c1 = new StackInt(10); StackInt c2 = new StackInt(uint.MaxValue); uint ui1 = c1; uint ui2 = c2; ushort us1 = (ushort)c1; ushort us2 = (ushort)c2; byte b1 = (byte)c1; byte b2 = (byte)c2; int i1 = (int)c1; int i2 = (int)c2; Assert.Equal((uint)10, ui1); Assert.Equal(uint.MaxValue, ui2); Assert.Equal((ushort)10, us1); Assert.Equal(unchecked ((ushort)uint.MaxValue), us2); Assert.Equal((byte)10, b1); Assert.Equal(unchecked ((byte)uint.MaxValue), b2); Assert.Equal(10, i1); Assert.Equal(unchecked ((int)uint.MaxValue), i2); }