// ----- constructors ------------------------------------------- /// <summary> /// Construct a SimplePofValue instance wrapping the supplied /// binary. /// </summary> /// <param name="valueParent"> /// Parent value within the POF stream. /// </param> /// <param name="ctx"> /// POF context to use when reading or writing properties. /// </param> /// <param name="of"> /// Offset of this value from the beginning of POF stream. /// </param> /// <param name="nType"> /// POF type identifier for this value. /// </param> /// <param name="nIndex"> /// Index of this value within the parent sparse array. /// </param> public NilPofValue(IPofValue valueParent, IPofContext ctx, int of, int nType, int nIndex) : base(valueParent, Binary.NO_BINARY, ctx, of, nType) { m_oValue = null; m_nIndex = nIndex; }
/// <summary> /// Construct a ComplexPofValue instance wrapping the supplied binary. /// </summary> /// <param name="valueParent"> /// Parent value within the POF stream. /// </param> /// <param name="binValue"> /// Binary representation of this value. /// </param> /// <param name="ctx"> /// POF context to use when reading or writing properties. /// </param> /// <param name="of"> /// Offset of this value from the beginning of POF stream. /// </param> /// <param name="nType"> /// POF type identifier for this value. /// </param> /// <param name="ofChildren"> /// Offset of the first child element within this value. /// </param> public ComplexPofValue(IPofValue valueParent, Binary binValue, IPofContext ctx, int of, int nType, int ofChildren) : base(valueParent, binValue, ctx, of, nType) { m_aChildren = new LongSortedList(); m_ofChildren = ofChildren; }
public void TestPofValueAccessor() { // perform the test twice, once with references disable, once with them enabled for (bool isRefEnabled = false; ;) { PortablePerson person = isRefEnabled ? CreatePersonNoChildren() : CreatePerson(); Binary binPerson = Serialize(person, isRefEnabled); IPofValue pv = PofValueParser.Parse(binPerson, GetPofContext(isRefEnabled)); Assert.AreEqual(person.Address, pv.GetChild(1).GetValue()); Assert.AreEqual(person.Name, pv.GetChild(0).GetValue()); Assert.AreEqual(person.DOB, pv.GetChild(2).GetValue()); // test NilPofValue IPofValue nv = pv.GetChild(100); Assert.IsTrue(nv is PofSparseArray.NilPofValue); Assert.IsNull(nv.GetValue()); // test PofNavigationException try { pv.GetChild(0).GetChild(0); Assert.Fail("Should've thrown PofNavigationException"); } catch (PofNavigationException) { } if (isRefEnabled) { break; } isRefEnabled = true; } }
public void TestGetCollection() { // perform the test twice, once with references disable, once with them enabled for (bool isRefEnabled = false; ;) { Stack coll = new Stack(); coll.Push("One"); coll.Push("Two"); coll.Push("Three"); Binary bin = Serialize(coll, isRefEnabled); IPofValue root = PofValueParser.Parse(bin, GetPofContext(isRefEnabled)); ICollection pv = root.GetCollection(null); Assert.AreEqual(coll.ToArray(), pv); Queue res = new Queue(); res.Enqueue("Append"); pv = root.GetCollection(res); Assert.AreSame(res, pv); if (isRefEnabled) { break; } isRefEnabled = true; } }
public void TestNestedPofValueAccessor() { PortablePerson person = CreatePerson(); // perform the test twice, once with references disable, once with them enabled for (bool isRefEnabled = false; ;) { Binary binPerson = Serialize(person, isRefEnabled); IPofValue pv = PofValueParser.Parse(binPerson, GetPofContext(isRefEnabled)); Assert.AreEqual(person.Address.Street, pv.GetChild(1).GetChild(0).GetValue()); Assert.AreEqual(person.Address.City, pv.GetChild(1).GetChild(1).GetValue()); Assert.AreEqual(person.Address.State, pv.GetChild(1).GetChild(2).GetValue()); Assert.AreEqual(person.Address.ZIP, pv.GetChild(1).GetChild(3).GetValue()); if (isRefEnabled) { // test the case where we try to access an object that contains a // uniform collection of user defined objects when object reference is enabled try { pv.GetChild(4); Assert.Fail("Should've thrown NotSupportedException"); } catch (NotSupportedException) { } break; } isRefEnabled = true; } }
public void TestPofUniformSparseArray() { // perform the test twice, once with references disable, once with them enabled for (bool isRefEnabled = false; ;) { TestValue tv = CreateTestValue(isRefEnabled); Binary bin = Serialize(tv, isRefEnabled); IPofValue root = PofValueParser.Parse(bin, GetPofContext(isRefEnabled)); IPofValue pv = root.GetChild(5); Assert.IsTrue(pv is PofUniformSparseArray); Assert.AreEqual(pv.GetChild(2).GetValue(), "two"); Assert.AreEqual(pv.GetChild(4).GetValue(), "four"); if (isRefEnabled) { break; } pv.GetChild(1).SetValue("jedan"); pv.GetChild(3).SetValue("tri"); pv.GetChild(4).SetValue("cetiri"); pv.GetChild(5).SetValue("pet"); Binary binModified = root.ApplyChanges(); TestValue tvModified = (TestValue)Deserialize(binModified); ILongArray arr = tvModified.m_uniformSparseArray; Assert.AreEqual(arr[1], "jedan"); Assert.AreEqual(arr[2], "two"); Assert.AreEqual(arr[3], "tri"); Assert.AreEqual(arr[4], "cetiri"); Assert.AreEqual(arr[5], "pet"); isRefEnabled = true; } }
/// <summary> /// Parses POF-encoded binary and returns an instance of a /// <see cref="IPofValue"/> wrapper for it. /// </summary> /// <param name="valueParent"> /// Parent POF value. /// </param> /// <param name="binValue"> /// POF-encoded binary value. /// </param> /// <param name="ctx"> /// POF context to use. /// </param> /// <param name="of"> /// Offset of the parsed value from the beginning of the POF stream. /// </param> /// <returns> /// An IPofValue instance. /// </returns> internal static IPofValue ParseValue(IPofValue valueParent, Binary binValue, IPofContext ctx, int of) { DataReader reader = binValue.GetReader(); int nType = reader.ReadPackedInt32(); return(InstantiatePofValue(valueParent, nType, binValue, ctx, of, reader)); }
/// <summary> /// Parses POF-encoded binary and returns an instance of a /// <see cref="IPofValue"/> wrapper for it. /// </summary> /// <param name="valueParent"> /// Parent POF value. /// </param> /// <param name="nType"> /// Type identifier of this POF value. /// </param> /// <param name="binValue"> /// POF-encoded binary value. /// </param> /// <param name="ctx"> /// POF context to use. /// </param> /// <param name="of"> /// Offset of the parsed value from the beginning of the POF stream. /// </param> /// <returns> /// An IPofValue instance. /// </returns> internal static IPofValue ParseUniformValue(IPofValue valueParent, int nType, Binary binValue, IPofContext ctx, int of) { AbstractPofValue val = InstantiatePofValue(valueParent, nType, binValue, ctx, of, binValue.GetReader()); val.SetUniformEncoded(); return(val); }
/// <summary> /// Construct a PofValue instance wrapping the supplied binary. /// </summary> /// <param name="valueParent"> /// Parent value within the POF stream. /// </param> /// <param name="binValue"> /// Binary representation of this value. /// </param> /// <param name="ctx"> /// POF context to use when reading or writing properties. /// </param> /// <param name="of"> /// Offset of this value from the beginning of POF stream. /// </param> /// <param name="nType"> /// POF type identifier for this value. /// </param> protected AbstractPofValue(IPofValue valueParent, Binary binValue, IPofContext ctx, int of, int nType) { m_valueParent = valueParent; m_binValue = binValue; m_ctx = ctx; m_nType = nType; m_of = of; }
public void TestPofSparseArray() { // perform the test twice, once with references disable, once with them enabled for (bool isRefEnabled = false; ;) { TestValue tv = CreateTestValue(isRefEnabled); Binary bin = Serialize(tv, isRefEnabled); IPofValue root = PofValueParser.Parse(bin, GetPofContext(isRefEnabled)); IPofValue pv = root.GetChild(4); Assert.IsTrue(pv is PofSparseArray); Assert.AreEqual(pv.GetChild(4).GetValue(), 4); Assert.AreEqual(pv.GetChild(2).GetValue(), "two"); // Test the case where we try to read a reference id before the object is read. // We should get an // IOException: missing identity: 2 // Work around by reading the person object in root.getChild(0) where it first appears // so that root.getChild(4).getValue(), which references the person object will have it. if (isRefEnabled) { try { pv.GetChild(5).GetValue(); Assert.Fail("Should've thrown Exception."); } catch (IOException) { } root.GetChild(0).GetChild(2).GetValue(); } Assert.AreEqual(pv.GetChild(5).GetValue(), isRefEnabled ? CreatePersonNoChildren() : CreatePerson()); if (isRefEnabled) { break; } pv.GetChild(1).SetValue(1); pv.GetChild(2).SetValue("dva"); pv.GetChild(3).SetValue("tri"); pv.GetChild(5).GetChild(0).SetValue("Novak"); Binary binModified = root.ApplyChanges(); TestValue tvModified = (TestValue)Deserialize(binModified); Assert.AreEqual(tvModified.m_sparseArray[1], 1); Assert.AreEqual(tvModified.m_sparseArray[2], "dva"); Assert.AreEqual(tvModified.m_sparseArray[3], "tri"); Assert.AreEqual(((PortablePerson)tvModified.m_sparseArray[5]).Name, "Novak"); isRefEnabled = true; } }
public void TestGetString() { string s = "28162514264337593543950335.1"; Binary bin = Serialize(s); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); string pv = root.GetString(); Assert.IsInstanceOf(s.GetType(), pv); Assert.AreEqual(s, pv); }
/// <summary> /// Locate a child IPofValue contained within this IPofValue. /// </summary> /// <remarks> /// The returned IPofValue could represent a non-existent (null) value. /// </remarks> /// <param name="nIndex"> /// Index of the child value to get. /// </param> /// <returns> /// The the child IPofValue. /// </returns> /// <exception cref="PofNavigationException"> /// If this value is a "terminal" or the child value cannot be located /// for any other reason. /// </exception> public override IPofValue GetChild(int nIndex) { IPofValue valueChild = (IPofValue)m_aChildren[nIndex]; if (valueChild == null) { valueChild = FindChild(nIndex); m_aChildren[nIndex] = valueChild; } return(valueChild); }
public void TestGetInt64() { Int64 l = 0x1234567812345678; Binary bin = Serialize(l); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Int64 pv = root.GetInt64(); Assert.IsInstanceOf(l.GetType(), pv); Assert.AreEqual(l, pv); }
public void TestGetInt32() { Int32 i = 0x12345678; Binary bin = Serialize(i); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Int32 pv = root.GetInt32(); Assert.IsInstanceOf(i.GetType(), pv); Assert.AreEqual(i, pv); }
public void TestPofInt16() { Int16 s = 0x1234; Binary bin = Serialize(s); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Int16 pv = root.GetInt16(); Assert.IsInstanceOf(s.GetType(), pv); Assert.AreEqual(s, pv); }
public void TestGetChar() { Char c = '\x0041'; Binary bin = Serialize(c); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Char pv = root.GetChar(); Assert.IsInstanceOf(c.GetType(), pv); Assert.AreEqual(c, pv); }
/// <summary> /// Locate the <see cref="IPofValue"/> designated by this IPofNavigator /// within the passed IPofValue. /// </summary> /// <param name="valueOrigin"> /// The origin from which navigation starts. /// </param> /// <returns> /// The resulting IPofValue. /// </returns> /// <exception cref="PofNavigationException"> /// If the navigation fails; for example one of the intermediate nodes /// in this path is a "terminal" IPofValue. /// </exception> public IPofValue Navigate(IPofValue valueOrigin) { int[] aiPathElements = GetPathElements(); IPofValue valueCurrent = valueOrigin; for (int i = 0, c = aiPathElements.Length; i < c; i++) { valueCurrent = valueCurrent.GetChild(aiPathElements[i]); } return(valueCurrent); }
public void TestGetDateTime() { DateTime dt = new DateTime((DateTime.Now.Ticks / 10000) * 10000); Binary bin = Serialize(dt); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); DateTime pv = root.GetDateTime(); Assert.IsInstanceOf(dt.GetType(), pv); Assert.AreEqual(dt, pv); }
public void TestGetDouble() { Double d = 0.1234567890123456789d; Binary bin = Serialize(d); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Double pv = root.GetDouble(); Assert.IsInstanceOf(d.GetType(), pv); Assert.AreEqual(d, pv); }
public void TestGetDayTimeInterval() { TimeSpan ts = new TimeSpan(12, 5, 10, 5); Binary bin = Serialize(ts); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); TimeSpan pv = root.GetDayTimeInterval(); Assert.IsInstanceOf(ts.GetType(), pv); Assert.AreEqual(ts, pv); }
public void TestGetDecimal() { Decimal d = Decimal.Parse("28162514264337593543950335.1"); Binary bin = Serialize(d); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Decimal pv = root.GetDecimal(); Assert.IsInstanceOf(d.GetType(), pv); Assert.AreEqual(d, pv); }
public void TestReferencesWithComplexObject() { var ivan = new PortablePersonReference("Ivan", new DateTime(78, 4, 25)); var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3)); var anna = new PortablePersonReference("Anna", new DateTime(80, 4, 12)); var tom = new PortablePerson("Tom", new DateTime(103, 7, 5)); var ellen = new PortablePerson("Ellen", new DateTime(105, 3, 15)); ivan.Children = null; goran.Children = new PortablePerson[2]; goran.Children[0] = tom; goran.Children[1] = ellen; anna.Children = new PortablePerson[2]; anna.Children[0] = tom; anna.Children[1] = ellen; ivan.Siblings = new PortablePersonReference[1]; ivan.Siblings[0] = goran; goran.Siblings = new PortablePersonReference[1]; goran.Siblings[0] = ivan; goran.Spouse = anna; anna.Spouse = goran; IDictionary <CompositeKey, IPortableObject> mapPerson = new Dictionary <CompositeKey, IPortableObject>(); String lastName = "Smith"; CompositeKey key1 = new CompositeKey(lastName, "ivan"), key2 = new CompositeKey(lastName, "goran"); mapPerson.Add(key1, ivan); mapPerson.Add(key2, goran); Binary bin = Serialize(mapPerson, true); IPofValue pv = PofValueParser.Parse(bin, GetPofContext(true)); IDictionary mapResult = pv.GetDictionary(null); Assert.AreEqual(2, mapResult.Count); PortablePersonReference ivanR = (PortablePersonReference)mapResult[key1]; PortablePersonReference goranR = (PortablePersonReference)mapResult[key2]; Assert.AreEqual(goran.Name, goranR.Name); Assert.IsTrue(ivanR.Siblings[0] == goranR); Assert.IsTrue(goranR.Spouse.Children[0] == goranR.Children[0]); bin = Serialize(ivan, true); pv = PofValueParser.Parse(bin, GetPofContext(true)); ivanR = (PortablePersonReference)pv.Root.GetValue(); goranR = ivanR.Siblings[0]; Assert.IsTrue(goranR.Siblings[0] == ivanR); ivanR = (PortablePersonReference)pv.GetValue(PofConstants.T_UNKNOWN); goranR = ivanR.Siblings[0]; Assert.IsTrue(goranR.Siblings[0] == ivanR); }
public void TestGetSingle() { Single f = 0.12345678f; f = 0.1234567890123456789f; Binary bin = Serialize(f); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Single pv = root.GetSingle(); Assert.IsInstanceOf(f.GetType(), pv); Assert.AreEqual(f, pv); }
public void TestCOH5231() { var holder = new BooleanHolder { Boolean1 = false, Boolean2 = true }; Binary bin = Serialize(holder); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); IPofValue bool1 = new SimplePofPath(0).Navigate(root); IPofValue bool2 = new SimplePofPath(1).Navigate(root); Assert.IsFalse(bool1.GetBoolean()); Assert.IsTrue((bool)bool2.GetValue()); }
public void TestGetDate() { DateTime dt = DateTime.Now; Binary bin = Serialize(dt); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); DateTime pv = root.GetDate(); Assert.IsInstanceOf(dt.GetType(), pv); Assert.AreEqual(dt.Date, pv.Date); Assert.AreEqual(pv.Hour, 0); Assert.AreEqual(pv.Minute, 0); Assert.AreEqual(pv.Second, 0); Assert.AreEqual(pv.Millisecond, 0); }
public void TestPofCollection() { // perform the test twice, once with references disable, once with them enabled for (bool isRefEnabled = false; ;) { TestValue tv = CreateTestValue(isRefEnabled); Binary bin = Serialize(tv, isRefEnabled); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); IPofValue pv = root.GetChild(2); Assert.AreEqual(((PofCollection)pv).Length, 3); Assert.AreEqual(pv.GetChild(0).GetValue(), 1); Assert.AreEqual(pv.GetChild(1).GetValue(), "two"); if (isRefEnabled) { IPofValue pv2 = root.GetChild(0).GetChild(2); pv2.GetValue(); } Assert.AreEqual(pv.GetChild(2).GetValue(), isRefEnabled ? CreatePersonNoChildren() : CreatePerson()); try { pv.GetChild(100); Assert.Fail("Should've thrown IndexOutOfRangeException."); } catch (IndexOutOfRangeException) { } if (isRefEnabled) { break; } pv.GetChild(1).SetValue("dva"); pv.GetChild(2).GetChild(0).SetValue("Novak"); Binary binModified = root.ApplyChanges(); TestValue tvModified = (TestValue)Deserialize(binModified); Assert.AreEqual(tvModified.m_col[1], "dva"); Assert.AreEqual(((PortablePerson)tvModified.m_col[2]).Name, "Novak"); isRefEnabled = true; } }
public void TestGetDoubleArray() { Double[] tv = new Double[] { 0, 1.1d, 2.2d, 3.3f, 4.4d }; Binary bin = Serialize(tv); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Double[] pv = root.GetDoubleArray(); Assert.AreEqual(tv, pv); try { pv[5] = pv[5]; Assert.Fail("Should've thrown IndexOutOfRangeException."); } catch (IndexOutOfRangeException) { } }
public void TestGetInt64Array() { Int64[] tv = new Int64[] { 0, 0xf0f0f0f0, 0xffffffff, 0x7fffffffffffffff }; Binary bin = Serialize(tv); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Int64[] pv = root.GetInt64Array(); Assert.AreEqual(tv, pv); try { pv[5] = pv[5]; Assert.Fail("Should've thrown IndexOutOfRangeException."); } catch (IndexOutOfRangeException) { } }
public void TestGetCharArray() { Char[] tv = new char[] { '\x0041', '\x0042', '\x0043', '\x0044' }; Binary bin = Serialize(tv); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Char[] pv = root.GetCharArray(); Assert.AreEqual(tv, pv); try { pv[5] = pv[5]; Assert.Fail("Should've thrown IndexOutOfRangeException."); } catch (IndexOutOfRangeException) { } }
public void TestGetByteArray() { Byte[] tv = new byte[] { 1, 2, 3, 4 }; Binary bin = Serialize(tv); IPofValue root = PofValueParser.Parse(bin, GetPofContext()); Byte[] pv = root.GetByteArray(); Assert.AreEqual(tv, pv); try { pv[5] = pv[5]; Assert.Fail("Should've thrown IndexOutOfRangeException."); } catch (IndexOutOfRangeException) { } }