Merge branch 'myeisha-xml-fixes'
[mono.git] / mcs / class / System.Web / Test / System.Web.Caching / CacheItemPriorityQueueTest.cs
1 // Authors:
2 //      Marek Habersack <mhabersack@novell.com>
3 //
4 // Copyright (C) 2010 Novell Inc. http://novell.com
5 //
6
7 //
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:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
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.
26 //
27 using System;
28 using System.Collections.Generic;
29 using System.Web.Caching;
30
31 using NUnit.Framework;
32
33 namespace MonoTests.System.Web.Caching
34 {
35         [TestFixture]
36         public partial class CacheItemPriorityQueueTest
37         {
38                 enum QueueOperation
39                 {
40                         Enqueue,
41                         Dequeue,
42                         Disable,
43                         Peek,
44                         QueueSize
45                 }
46                 
47                 sealed class TestItem
48                 {
49                         public int ListIndex;
50                         public int QueueCount;
51                         public QueueOperation Operation;
52                         public bool IsDisabled;
53                         public bool IsNull;
54                         public bool Disable;
55                         public int OperationCount;
56                         public string Guid;
57                 }
58
59                 sealed class TestCacheItem : CacheItem
60                 {
61                         public Guid Guid;
62
63                         public TestCacheItem ()
64                         {
65                                 Guid = Guid.NewGuid ();
66                         }
67
68                         public override string ToString ()
69                         {
70                                 return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
71                         }
72                 }
73                 
74                 void RunTest (List <TestItem> tests, List <TestCacheItem> list)
75                 {
76                         var queue = new CacheItemPriorityQueue ();
77
78                         foreach (TestItem item in tests)
79                                 RunItem (item, queue, list);
80                 }
81
82                 void RunItem (TestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list)
83                 {
84                         TestCacheItem ci;
85                         string messagePrefix = String.Format ("{0}-{1:00000}-", item.Operation, item.OperationCount);
86                         
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");
92                                         break;
93                                         
94                                 case QueueOperation.Dequeue:
95                                         ci = (TestCacheItem)queue.Dequeue ();
96                                         if (item.IsNull)
97                                                 Assert.IsNull (ci, messagePrefix + "1");
98                                         else {
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");
102                                         }
103                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
104                                         break;
105                                         
106                                 case QueueOperation.Disable:
107                                         ci = list [item.ListIndex];
108                                         if (item.IsNull)
109                                                 Assert.IsNull (ci, messagePrefix + "1");
110                                         else {
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;
115                                         }
116                                         break;
117
118                                 case QueueOperation.Peek:
119                                         ci = (TestCacheItem)queue.Peek ();
120                                         if (item.IsNull)
121                                                 Assert.IsNull (ci, messagePrefix + "1");
122                                         else {
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");
126                                         }
127                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
128                                         break;
129
130                                 case QueueOperation.QueueSize:
131                                         Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
132                                         break;
133                                         
134                                 default:
135                                         Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);
136                                         break;
137                         }
138                 }
139         }
140 }