public void TestEnumerationAndIsLeaf() { SmartPropertyBag animals = new SmartPropertyBag(); animals.AddValue("Horses", 12); animals.AddString("Kingdom", "Animal"); SmartPropertyBag dogs = new SmartPropertyBag(); animals.AddChildSPB("Dogs", dogs); dogs.AddValue("Collies", 14); dogs.AddValue("Chows", 16); animals["Dogs.Collies"] = 18; SmartPropertyBag labs = new SmartPropertyBag(); labs.AddValue("Black", 4); labs.AddValue("Brown", 2); labs.AddValue("Yellow", 1); labs.AddString("Temperament", "Mellow"); labs.AddBoolean("Faithful", false); dogs.AddChildSPB("Labs", labs); DumpEnumerable(animals, 0); Assert.IsTrue(true, "Visual test not successfull"); }
public void TestSubsidiaries() { SmartPropertyBag animals = new SmartPropertyBag(); animals.AddValue("Horses", 12); animals.AddString("Kingdom", "Animal"); SmartPropertyBag dogs = new SmartPropertyBag(); animals.AddChildSPB("Dogs", dogs); dogs.AddValue("Collies", 14); dogs.AddValue("Chows", 16); Debug.WriteLine("Animals.Horses = " + animals["Horses"]); Debug.WriteLine("Animals.Kingdom = " + animals["Kingdom"]); Debug.WriteLine("Animals.Dogs.Collies = " + animals["Dogs.Collies"]); animals["Dogs.Collies"] = 18; Debug.WriteLine("Animals.Dogs.Collies = " + animals["Dogs.Collies"]); SmartPropertyBag labs = new SmartPropertyBag(); labs.AddValue("Black", 4); labs.AddValue("Brown", 2); labs.AddValue("Yellow", 1); labs.AddString("Temperament", "Mellow"); labs.AddBoolean("Faithful", false); dogs.AddChildSPB("Labs", labs); Debug.WriteLine(DiagnosticAids.DumpDictionary("", (IDictionary)animals.Memento.GetDictionary())); Assert.IsTrue((double)animals["Dogs.Labs.Black"] == 4, "Black Lab is not 4"); Assert.IsTrue((double)animals["Dogs.Labs.Brown"] == 2, "Brown Lab is not 2"); Assert.IsTrue((double)animals["Dogs.Labs.Yellow"] == 1, "Yellow Lab is not 1"); Assert.IsTrue("Mellow".Equals((string)labs["Temperament"]), "The Labs temperament is not Mellow"); Assert.IsTrue((double)animals["Dogs.Collies"] == 18, "Collies are not 18"); Assert.IsTrue((double)animals["Dogs.Chows"] == 16, "Chows are not 16"); Assert.IsTrue((double)animals["Horses"] == 12, "Horses are not 12"); Assert.IsTrue("Animal".Equals((string)animals["Kingdom"]), "The Kingdom is not the Animal Kingdom"); Assert.IsTrue(!(bool)labs["Faithful"], "Labs are not not faithful"); Debug.WriteLine("Setting Animals.Dogs.Labs.Black to 19, Animals.Dogs.Labs.Temperament to \"Lovable\"."); Debug.WriteLine("...and Animals.Dogs.Labs.Faithful to \"true\"."); animals["Dogs.Labs.Black"] = 19; animals["Dogs.Labs.Temperament"] = "Lovable"; animals["Dogs.Labs.Faithful"] = true; Debug.WriteLine(DiagnosticAids.DumpDictionary("", (IDictionary)animals.Memento.GetDictionary())); Assert.IsTrue((double)animals["Dogs.Labs.Black"] == 19, "Black Lab is not 19"); Assert.IsTrue((double)animals["Dogs.Labs.Brown"] == 2, "Brown Lab is not 2"); Assert.IsTrue((double)animals["Dogs.Labs.Yellow"] == 1, "Yellow Lab is not 1"); Assert.IsTrue("Lovable".Equals((string)labs["Temperament"]), "The Labs temperament is not Lovable"); Assert.IsTrue((double)animals["Dogs.Collies"] == 18, "Collies are not 18"); Assert.IsTrue((double)animals["Dogs.Chows"] == 16, "Chows are not 16"); Assert.IsTrue((double)animals["Horses"] == 12, "Horses are not 12"); Assert.IsTrue("Animal".Equals((string)animals["Kingdom"]), "The Kingdom is the Animal Kingdom"); Assert.IsTrue((bool)labs["Faithful"], "Labs are faithful"); }
/// <summary> /// Initializes a SPB with the type, name and contents of the field /// depicted in this SmartPropertyBagInitializer. /// </summary> /// <param name="spb">The SmartPropertyBag to be initialized.</param> public void Initialize(SmartPropertyBag spb) { string[] keys = Key.Split('.'); SmartPropertyBag theSpb = spb; int i; for (i = 0; i < keys.Length - 1; i++) { try { SmartPropertyBag parent = theSpb; theSpb = (SmartPropertyBag)theSpb[keys[i]]; if (theSpb == null) { if (ForceCreation) { theSpb = new SmartPropertyBag(); parent.AddChildSPB(keys[i], theSpb); } else { throw new SmartPropertyBagException("Attempt to navigate to spb named " + keys[i] + ", which does not exist."); } } } catch (InvalidCastException) { throw new SmartPropertyBagException("Cannot add a child to sub-property " + keys[i] + " since it is not a SmartPropertyBag."); } } // If the SPB already has an entry with the specified key, set the value. if (theSpb.Contains(keys[i])) { switch (m_initType) { case InitType.Bool: theSpb[keys[i]] = BoolValue; break; case InitType.Double: theSpb[keys[i]] = DoubleValue; break; case InitType.String: theSpb[keys[i]] = StringValue; break; } } else // If it does not have such an entry, and we've been told to force creation, // then we create a new entry and set the value. { if (ForceCreation) { switch (m_initType) { case InitType.Bool: theSpb.AddBoolean(keys[i], BoolValue); break; case InitType.Double: theSpb.AddValue(keys[i], DoubleValue); break; case InitType.String: theSpb.AddString(keys[i], StringValue); break; } } } }
public void TestRepeatedSnapshottingAndRestoration() { SmartPropertyBag animals = new SmartPropertyBag(); animals.AddValue("Horses", 12); animals.AddString("Kingdom", "Animal"); SmartPropertyBag dogs = new SmartPropertyBag(); animals.AddChildSPB("Dogs", dogs); dogs.AddValue("Collies", 14); dogs.AddValue("Chows", 16); Debug.WriteLine("Animals.Horses = " + animals["Horses"]); Debug.WriteLine("Animals.Kingdom = " + animals["Kingdom"]); Debug.WriteLine("Animals.Dogs.Collies = " + animals["Dogs.Collies"]); animals["Dogs.Collies"] = 18; Debug.WriteLine("Animals.Dogs.Collies = " + animals["Dogs.Collies"]); SmartPropertyBag labs = new SmartPropertyBag(); labs.AddValue("Black", 4); labs.AddValue("Brown", 2); labs.AddValue("Yellow", 1); labs.AddString("Temperament", "Mellow"); labs.AddBoolean("Faithful", true); dogs.AddChildSPB("Labs", labs); Highpoint.Sage.Utility.Mementos.IMemento mem = dogs.Memento; DateTime start = DateTime.Now; for (int i = 0; i < 10000; i++) { dogs.Memento = mem; labs["Faithful"] = false; labs["Faithful"] = true; Assert.IsTrue((double)animals["Dogs.Labs.Black"] == 4, "Black Lab is not 4"); Assert.IsTrue((double)animals["Dogs.Labs.Brown"] == 2, "Brown Lab is not 2"); Assert.IsTrue((double)animals["Dogs.Labs.Yellow"] == 1, "Yellow Lab is not 1"); Assert.IsTrue("Mellow".Equals((string)labs["Temperament"]), "The Lab's temperament is not Mellow"); Assert.IsTrue((double)animals["Dogs.Collies"] == 18, "Collies are not 18"); Assert.IsTrue((double)animals["Dogs.Chows"] == 16, "Chows are not 16"); Assert.IsTrue((double)animals["Horses"] == 12, "Horses are not 12"); Assert.IsTrue("Animal".Equals((string)animals["Kingdom"]), "The Kingdom is not the Animal Kingdom"); Assert.IsTrue((bool)labs["Faithful"], "Labs are not faithful"); //Debug.WriteLine(DiagnosticAids.DumpDictionary("",(IDictionary)animals.Memento.GetDictionary())); mem = dogs.Memento; labs["Faithful"] = false; labs["Faithful"] = true; if ((i % 100) == 0) { Debug.WriteLine(((TimeSpan)(DateTime.Now - start)).TotalSeconds); start = DateTime.Now; //if ( i > 7500 ) System.Diagnostics.Debugger.Break(); } } }
public void TestStringsAndBooleans() { SmartPropertyBag spb1 = new SmartPropertyBag(); spb1.AddString("Name", "Habanero"); spb1.AddBoolean("Hot", true); spb1.AddValue("Scovilles", 35000); SmartPropertyBag spb2 = new SmartPropertyBag(); spb2.AddString("Name", "Jalapeno"); spb2.AddBoolean("Hot", false); spb2.AddValue("Scovilles", 22000); spb2.AddAlias("OtherGuysName", spb1, "Name"); spb2.AddAlias("OtherGuysHot", spb1, "Hot"); spb2.AddAlias("OtherGuysScovilles", spb1, "Scovilles"); Debug.WriteLine(spb2["OtherGuysHot"]); spb1["Hot"] = false; Debug.WriteLine(spb2["OtherGuysHot"]); Assert.IsTrue(!(bool)spb2["OtherGuysHot"], "OtherGuysHot is not false"); spb1.AddAlias("OtherGuysName", spb2, "Name"); spb1.AddAlias("OtherGuysHot", spb2, "Hot"); spb1.AddAlias("OtherGuysScovilles", spb2, "Scovilles"); SmartPropertyBag spb0 = new SmartPropertyBag(); spb0.AddChildSPB("Jalapeno", spb2); spb0.AddChildSPB("Habanero", spb1); Highpoint.Sage.Utility.Mementos.IMemento mem1 = spb0.Memento; spb1["Hot"] = false; spb1["Name"] = "Habañero"; spb1["Scovilles"] = 32000; spb2["Hot"] = true; spb2["Name"] = "Jalapeño"; spb2["Scovilles"] = 16000; Assert.IsTrue(!(bool)spb0["Habanero.Hot"], "Habanero.Hot is hot"); Assert.IsTrue("Habañero".Equals((string)spb0["Habanero.Name"]), "Habanero.Name is not Habañero"); Assert.IsTrue((double)spb0["Habanero.Scovilles"] == 32000, "Habanero.Scovilles is not 32000"); Assert.IsTrue((bool)spb0["Jalapeno.Hot"], "Jalapeno.Hot is not hot"); Assert.IsTrue("Jalapeño".Equals((string)spb0["Jalapeno.Name"]), "Jalapeno.Name is not Jalapeño"); Assert.IsTrue((double)spb0["Jalapeno.Scovilles"] == 16000, "Jalapeno.Scovilles is not 16000"); Highpoint.Sage.Utility.Mementos.IMemento mem2 = spb0.Memento; Debug.WriteLine(DiagnosticAids.DumpDictionary("Before", mem1.GetDictionary())); Debug.WriteLine(DiagnosticAids.DumpDictionary("After", mem2.GetDictionary())); spb0["Jalapeno.Hot"] = false; spb0["Habanero.Hot"] = true; spb0["Jalapeno.Scovilles"] = 12000; spb0["Habanero.Scovilles"] = 29000; Highpoint.Sage.Utility.Mementos.IMemento mem3 = spb0.Memento; Assert.IsTrue((bool)spb0["Habanero.Hot"], "Habanero.Hot is not hot"); Assert.IsTrue((double)spb0["Habanero.Scovilles"] == 29000, "Habanero.Scovilles is not 29000"); Assert.IsTrue(!(bool)spb0["Jalapeno.Hot"], "Jalapeno.Hot is not hot"); Assert.IsTrue((double)spb0["Jalapeno.Scovilles"] == 12000, "Jalapeno.Scovilles is not 12000"); Debug.WriteLine(DiagnosticAids.DumpDictionary("After more...", mem3.GetDictionary())); }
public void TestMementoCaching() { m_steve = 12; SmartPropertyBag spb1 = new SmartPropertyBag(); spb1.AddValue("Fred", 12); spb1.AddDelegate("Steve", new SmartPropertyBag.SPBDoubleDelegate(ComputeSteve)); spb1.AddString("Name", "SPB1"); Highpoint.Sage.Utility.Mementos.IMemento mem1 = spb1.Memento; Debug.WriteLine(DiagnosticAids.DumpDictionary("mem1", mem1.GetDictionary())); Highpoint.Sage.Utility.Mementos.IMemento mem2 = spb1.Memento; Debug.WriteLine(DiagnosticAids.DumpDictionary("mem2", mem2.GetDictionary())); Debug.WriteLine("The new snapshot is " + (mem1 == mem2?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem2 == mem1, "Memento 1 is not equal Memento 2"); Assert.IsTrue((double)spb1["Steve"] == 12, "Steve is not 12"); Assert.IsTrue("SPB1".Equals(spb1["Name"]), "Name is not SPB1"); Debug.WriteLine("Changing \"Fred\" to 14 from 12."); spb1["Fred"] = 14; Highpoint.Sage.Utility.Mementos.IMemento mem3 = spb1.Memento; Debug.WriteLine(DiagnosticAids.DumpDictionary("mem3", mem3.GetDictionary())); Debug.WriteLine("The new snapshot is " + (mem2 == mem3?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem2 != mem3, "Memento 2 is not not equal to Memento 3"); Assert.IsTrue((double)spb1["Fred"] == 14, "Fred is not 14"); Debug.WriteLine("Changing \"Name\" to \"spb1\" from \"SPB1\"."); spb1["Name"] = "spb1"; Highpoint.Sage.Utility.Mementos.IMemento mem3a = spb1.Memento; Debug.WriteLine(DiagnosticAids.DumpDictionary("mem3a", mem3a.GetDictionary())); Debug.WriteLine("The new snapshot is " + (mem2 == mem3a?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem2 != mem3a, "Memento 2 is equal to memento 3a"); spb1.AddValue("_Connie", 99); SmartPropertyBag spb2 = new SmartPropertyBag(); spb2.AddValue("_Pete", 14); spb1.AddAlias("Pete", spb2, "_Pete"); Highpoint.Sage.Utility.Mementos.IMemento mem4 = spb1.Memento; Highpoint.Sage.Utility.Mementos.IMemento mem5 = spb1.Memento; Debug.WriteLine("The new snapshot is " + (mem4 == mem5?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem5 == mem4, "Memento 5 is not equal to memento 4"); Debug.WriteLine("\r\nChanging a value in a foreign alias."); spb2["_Pete"] = 16; Highpoint.Sage.Utility.Mementos.IMemento mem6 = spb1.Memento; Debug.WriteLine("The new snapshot is " + (mem5 == mem6?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem5 != mem6, "Memento is equal to memento 6"); Debug.WriteLine("Capturing another snapshot without change."); Highpoint.Sage.Utility.Mementos.IMemento mem7 = spb1.Memento; Debug.WriteLine("The new snapshot is " + (mem6 == mem7?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem6 == mem7, "Memento 6 is not equal to memento 7"); Debug.WriteLine("Changing the data driving a delegate-computed value."); m_steve = 14; Highpoint.Sage.Utility.Mementos.IMemento mem8 = spb1.Memento; Debug.WriteLine("The new snapshot is " + (mem8 == mem7?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem7 != mem8, "Memento 7 is equal to memento 8"); Debug.WriteLine("\r\nWe will now test snapshotting's functionality as it pertains to subsidiaries."); SmartPropertyBag spb1_1 = new SmartPropertyBag(); spb1.AddChildSPB("Sub", spb1_1); spb1_1.AddValue("marine", 12); Highpoint.Sage.Utility.Mementos.IMemento mem9 = spb1.Memento; Debug.WriteLine(DiagnosticAids.DumpDictionary("Main", mem9.GetDictionary())); Debug.WriteLine("Taking another immediately-following snapshot."); Highpoint.Sage.Utility.Mementos.IMemento mem10 = spb1.Memento; Debug.WriteLine("The new snapshot is " + (mem9 == mem10?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem10 == mem9, "Memento 10 is not equal to memento 9"); Debug.WriteLine("Changing a data member in a subsidiary."); spb1_1["marine"] = 21; Highpoint.Sage.Utility.Mementos.IMemento mem11 = spb1.Memento; Debug.WriteLine("The new snapshot is " + (mem10 == mem11?"":"not ") + "equal to the one preceding it.\r\n"); Assert.IsTrue(mem11 != mem10, "Memento 11 is equal to memento 10"); }