// // Sequences.cs // // Author: // Marek Habersack // // Copyright (c) 2010, Marek Habersack // // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, are permitted // provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, this list of // conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, this list of // conditions and the following disclaimer in the documentation and/or other materials // provided with the distribution. // * Neither the name of Marek Habersack nor the names of its contributors may be used to // endorse or promote products derived from this software without specific prior written // permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web.Caching; using System.Xml; using System.Xml.XPath; namespace Tester { static partial class Sequences { public static void Run (StringBuilder sb, string seqDir, string dataDir, string indent) { string[] files = Directory.GetFiles (seqDir, "*.seq"); if (files == null || files.Length == 0) return; int seqNum = 0; Array.Sort (files); foreach (string f in files) RunSequence (sb, indent, dataDir, f, seqNum++); } static void RunSequence (StringBuilder sb, string initialIndent, string dataDir, string file, int seqNum) { Dictionary cacheIndex; List cacheItems; List edSequence; LoadEDSequence (file, out cacheIndex, out cacheItems, out edSequence); if (edSequence == null || edSequence.Count == 0) return; string listName = String.Format ("Sequence_{0:00000}.list", seqNum); string testsName = String.Format ("Sequence_{0:00000}.tests", seqNum); using (var sw = new StreamWriter (Path.Combine (dataDir, listName), false, Encoding.UTF8)) sw.FormatList (cacheItems); string indent = initialIndent + "\t"; int idx; var pq = new PriorityQueueState (); using (var sw = new StreamWriter (Path.Combine (dataDir, testsName), false, Encoding.UTF8)) { foreach (EDSequenceEntry entry in edSequence) { idx = cacheIndex [entry.Item.Guid]; switch (entry.Type) { case EDSequenceEntryType.Enqueue: sw.FormatEnqueue (pq, cacheItems, idx); break; case EDSequenceEntryType.Dequeue: sw.FormatDequeue (pq); break; case EDSequenceEntryType.Disable: sw.FormatDisableItem (pq, cacheItems, idx); break; case EDSequenceEntryType.Peek: sw.FormatPeek (pq); break; case EDSequenceEntryType.Update: sw.FormatUpdate (pq, cacheItems, entry.Item, idx); break; } } sw.FormatQueueSize (pq); while (pq.Queue.Count > 0) sw.FormatDequeue (pq); } sb.SequenceMethodStart (initialIndent, file, seqNum); sb.AppendFormat ("{0}RunTest (\"{1}\", \"{2}\");", indent, testsName, listName); sb.AppendLine (); sb.SequenceMethodEnd (initialIndent); } static List LoadEDSequence (string file, out Dictionary cacheIndex, out List cacheItems, out List edSequence) { var doc = new XPathDocument (file); XPathNavigator nav = doc.CreateNavigator (), current; XPathNodeIterator nodes = nav.Select ("/sequence/entry"); CacheItem item; edSequence = new List (); cacheIndex = new Dictionary (); cacheItems = new List (); while (nodes.MoveNext ()) { current = nodes.Current; item = CreateCacheItem (current, cacheIndex, cacheItems); edSequence.Add (new EDSequenceEntry (item, current.GetRequiredAttribute ("type"))); } return null; } static CacheItem CreateCacheItem (XPathNavigator node, Dictionary cacheIndex, List cacheItems) { Guid guid = node.GetRequiredAttribute ("guid"); int idx; if (cacheIndex.TryGetValue (guid, out idx)) return cacheItems [idx]; bool attrFound; var ret = new CacheItem (); ret.Key = node.GetRequiredAttribute ("key"); ret.AbsoluteExpiration = node.GetRequiredAttribute ("absoluteExpiration"); ret.SlidingExpiration = node.GetRequiredAttribute ("slidingExpiration"); ret.Priority = node.GetRequiredAttribute ("priority"); ret.LastChange = node.GetRequiredAttribute ("lastChange"); ret.ExpiresAt = node.GetRequiredAttribute ("expiresAt"); ret.Disabled = node.GetRequiredAttribute ("disabled"); ret.PriorityQueueIndex = -1; ret.Guid = guid; cacheItems.Add (ret); cacheIndex.Add (guid, cacheItems.Count - 1); return ret; } } }