2 // Marek Habersack <mhabersack@novell.com>
4 // Copyright (C) 2010 Novell Inc. http://novell.com
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections.Generic;
29 using System.Web.Caching;
31 using NUnit.Framework;
33 namespace MonoTests.System.Web.Caching
36 public partial class CacheItemPriorityQueueTest
38 sealed class TestCacheItem : CacheItem
42 public TestCacheItem (ref int index, object[] data)
44 Key = LoadField <string> (index++, data);
45 AbsoluteExpiration = DateTime.Parse (LoadField <string> (index++, data));
46 SlidingExpiration = TimeSpan.Parse (LoadField <string> (index++, data));
47 Priority = LoadField <CacheItemPriority> (index++, data);
48 LastChange = DateTime.Parse (LoadField <string> (index++, data));
49 ExpiresAt = LoadField <long> (index++, data);
50 Disabled = LoadField <bool> (index++, data);
51 Guid = new Guid (LoadField <string> (index++, data));
54 public override string ToString ()
56 return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
59 T LoadField <T> (int index, object[] data)
61 if (data == null || data.Length <= index)
62 throw new ArgumentOutOfRangeException ("index");
64 object o = data [index];
68 if (o.GetType () != typeof (T))
69 throw new InvalidOperationException (String.Format ("Field at index {0} is not a {1}", index, typeof (T)));
75 void RunTest (string[] tests, object[] list)
77 var queue = new CacheItemPriorityQueue ();
78 var cacheItems = new List <TestCacheItem> ();
81 while (index < list.Length)
82 cacheItems.Add (new TestCacheItem (ref index, list));
84 for (int i = 0; i < tests.Length; i++)
85 RunItem (new CacheItemPriorityQueueTestItem (tests [i]), queue, cacheItems, i);
88 void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
91 string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
93 switch (item.Operation) {
94 case QueueOperation.Enqueue:
95 queue.Enqueue (list [item.ListIndex]);
96 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "1");
97 Assert.AreEqual (item.Guid, ((TestCacheItem)queue.Peek ()).Guid.ToString (), messagePrefix + "2");
100 case QueueOperation.Dequeue:
101 ci = (TestCacheItem)queue.Dequeue ();
103 Assert.IsNull (ci, messagePrefix + "1");
105 Assert.IsNotNull (ci, messagePrefix + "2");
106 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
107 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
109 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
112 case QueueOperation.Disable:
113 ci = list [item.ListIndex];
115 Assert.IsNull (ci, messagePrefix + "1");
117 Assert.IsNotNull (ci, messagePrefix + "2");
118 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
119 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
120 ci.Disabled = item.Disable;
124 case QueueOperation.Peek:
125 ci = (TestCacheItem)queue.Peek ();
127 Assert.IsNull (ci, messagePrefix + "1");
129 Assert.IsNotNull (ci, messagePrefix + "2");
130 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
131 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
133 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
136 case QueueOperation.QueueSize:
137 Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
141 Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);