// PERFORM_MUTATION // public void performMutation(DataElement obj, int num) { logger.Debug("performMutation(num=" + num + ")"); Dom.Array objAsArray = (Dom.Array)obj; //if (num == 0) // return; if (num < objAsArray.Count) { // remove some items for (int i = objAsArray.Count - 1; i >= num; --i) { if (objAsArray[i] == null) { break; } objAsArray.RemoveAt(i); } } else if (num > objAsArray.Count) { // add some items, but do it by replicating // the last item over and over to save memory objAsArray.ExpandTo(num); } }
public void SlurpArray() { string xml = @" <Peach> <DataModel name='TheDataModel'> <String name='str' value='Hello'/> <Block name='blk' minOccurs='1'> <String name='val'/> </Block> </DataModel> <StateModel name='TheState' initialState='State1'> <State name='State1'> <Action type='output'> <DataModel ref='TheDataModel'/> </Action> </State> </StateModel> <Test name='Default'> <StateModel ref='TheState'/> <Publisher class='Console'/> </Test> </Peach> "; PitParser parser = new PitParser(); Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml))); Dom.Array array = dom.tests[0].stateModel.states["State1"].actions[0].dataModel[1] as Dom.Array; array.ExpandTo(50); PeachXPathNavigator navi = new PeachXPathNavigator(dom); var iter = navi.Select("//str"); if (!iter.MoveNext()) { Assert.Fail(); } DataElement valueElement = ((PeachXPathNavigator)iter.Current).currentNode as DataElement; if (valueElement == null) { Assert.Fail(); } if (iter.MoveNext()) { Assert.Fail(); } iter = navi.Select("//val"); if (!iter.MoveNext()) { Assert.Fail(); } int count = 0; do { var setElement = ((PeachXPathNavigator)iter.Current).currentNode as DataElement; if (setElement == null) { Assert.Fail(); } setElement.DefaultValue = valueElement.DefaultValue; ++count; }while (iter.MoveNext()); // When Array.ExpandTo() is used, it duplicates the element and adds that // same element over and over, so there are really only 2 unique elements in the array... Assert.AreEqual(2, count); Assert.AreEqual(50, array.Count); int hash = 0; for (int i = 0; i < array.Count; ++i) { if (i <= 1) { hash = array[i].GetHashCode(); } Assert.AreEqual(hash, array[i].GetHashCode()); var b = array[i] as Block; Assert.NotNull(b); Assert.AreEqual("Hello", (string)b[0].DefaultValue); } }