[asp.net] Rewrote CacheItemPriorityQueue tests generator to produce simpler test...
[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                 sealed class TestCacheItem : CacheItem
39                 {
40                         public Guid Guid;
41
42                         public TestCacheItem (ref int index, object[] data)
43                         {
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));
52                         }
53
54                         public override string ToString ()
55                         {
56                                 return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
57                         }
58
59                         T LoadField <T> (int index, object[] data)
60                         {
61                                 if (data == null || data.Length <= index)
62                                         throw new ArgumentOutOfRangeException ("index");
63                 
64                                 object o = data [index];
65                                 if (o == null)
66                                         return default (T);
67                 
68                                 if (o.GetType () != typeof (T))
69                                         throw new InvalidOperationException (String.Format ("Field at index {0} is not a {1}", index, typeof (T)));
70                 
71                                 return (T)o;
72                         }
73                 }
74                 
75                 void RunTest (string[] tests, object[] list)
76                 {
77                         var queue = new CacheItemPriorityQueue ();
78                         var cacheItems = new List <TestCacheItem> ();
79                         int index = 0;
80
81                         while (index < list.Length)
82                                 cacheItems.Add (new TestCacheItem (ref index, list));
83
84                         for (int i = 0; i < tests.Length; i++)
85                                 RunItem (new CacheItemPriorityQueueTestItem (tests [i]), queue, cacheItems, i);
86                 }
87
88                 void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
89                 {
90                         TestCacheItem ci;
91                         string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
92                         
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");
98                                         break;
99                                         
100                                 case QueueOperation.Dequeue:
101                                         ci = (TestCacheItem)queue.Dequeue ();
102                                         if (item.IsNull)
103                                                 Assert.IsNull (ci, messagePrefix + "1");
104                                         else {
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");
108                                         }
109                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
110                                         break;
111                                         
112                                 case QueueOperation.Disable:
113                                         ci = list [item.ListIndex];
114                                         if (item.IsNull)
115                                                 Assert.IsNull (ci, messagePrefix + "1");
116                                         else {
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;
121                                         }
122                                         break;
123
124                                 case QueueOperation.Peek:
125                                         ci = (TestCacheItem)queue.Peek ();
126                                         if (item.IsNull)
127                                                 Assert.IsNull (ci, messagePrefix + "1");
128                                         else {
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");
132                                         }
133                                         Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
134                                         break;
135
136                                 case QueueOperation.QueueSize:
137                                         Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
138                                         break;
139                                         
140                                 default:
141                                         Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);
142                                         break;
143                         }
144                 }
145         }
146 }