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
50 public int QueueCount;
51 public QueueOperation Operation;
52 public bool IsDisabled;
55 public int OperationCount;
59 sealed class TestCacheItem : CacheItem
63 public TestCacheItem ()
65 Guid = Guid.NewGuid ();
68 public override string ToString ()
70 return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
74 void RunTest (List <TestItem> tests, List <TestCacheItem> list)
76 var queue = new CacheItemPriorityQueue ();
78 foreach (TestItem item in tests)
79 RunItem (item, queue, list);
82 void RunItem (TestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list)
85 string messagePrefix = String.Format ("{0}-{1:00000}-", item.Operation, item.OperationCount);
87 switch (item.Operation) {
88 case QueueOperation.Enqueue:
89 queue.Enqueue (list [item.ListIndex]);
90 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "1");
91 Assert.AreEqual (item.Guid, ((TestCacheItem)queue.Peek ()).Guid.ToString (), messagePrefix + "2");
94 case QueueOperation.Dequeue:
95 ci = (TestCacheItem)queue.Dequeue ();
97 Assert.IsNull (ci, messagePrefix + "1");
99 Assert.IsNotNull (ci, messagePrefix + "2");
100 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
101 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
103 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
106 case QueueOperation.Disable:
107 ci = list [item.ListIndex];
109 Assert.IsNull (ci, messagePrefix + "1");
111 Assert.IsNotNull (ci, messagePrefix + "2");
112 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
113 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
114 ci.Disabled = item.Disable;
118 case QueueOperation.Peek:
119 ci = (TestCacheItem)queue.Peek ();
121 Assert.IsNull (ci, messagePrefix + "1");
123 Assert.IsNotNull (ci, messagePrefix + "2");
124 Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
125 Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
127 Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
130 case QueueOperation.QueueSize:
131 Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
135 Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);