Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CoreExtensionsTests.cs « MonoDevelop.Core « UnitTests « tests « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc0aeb202ab3c07922efabc080000600c163e24a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// CoreExtensionsTests.cs
//
// Author:
//       Marius Ungureanu <marius.ungureanu@xamarin.com>
//
// Copyright (c) 2016 Xamarin, Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Linq;
using NUnit.Framework;

namespace MonoDevelop.Core
{
	[TestFixture]
	public class CoreExtensionsTests
	{
		[SetUp]
		public void SetUp ()
		{
			memoTest1CallCount = 0;
			memoTest2CallCount = 0;
		}

		[Test]
		public void ConcatWorks ()
		{
			string toAdd = "Test String";

			var initial = Enumerable.Repeat (toAdd, 4);
			var expected = Enumerable.Repeat (toAdd, 5);

			Assert.That (expected, Is.EquivalentTo (initial.Concat (toAdd)));
		}

		[TestCase ("item1", "item2", "item1", 0)]
		[TestCase ("item1", "item2", "item2", 1)]
		[TestCase ("item1", "item2", "item3", -1)]
		public void IndexOfWorks (string toAdd1, string toAdd2, string toFind, int expectedIndex)
		{
			var toSearch = Enumerable.Empty<string> ().Concat (toAdd1).Concat (toAdd2);

			Assert.AreEqual (expectedIndex, toSearch.IndexOf (toFind));
		}

		[TestCase ("item1", "item2", "item1", 0)]
		[TestCase ("item1", "item2", "item2", 1)]
		[TestCase ("item1", "item2", "item3", -1)]
		public void FindIndexWorks (string toAdd1, string toAdd2, string toFind, int expectedIndex)
		{
			var toSearch = Enumerable.Empty<string> ().Concat (toAdd1).Concat (toAdd2);

			Assert.AreEqual (expectedIndex, toSearch.FindIndex (i => i == toFind));
		}

		static int memoTest1CallCount;
		static object MemoTest1 ()
		{
			memoTest1CallCount++;
			return new object ();
		}

		[Test]
		public void TestMemoizeMethod1 ()
		{
			var f1 = CoreExtensions.Memoize (MemoTest1);
			var obj1 = f1 ();
			var obj2 = f1 ();
			Assert.AreSame (obj1, obj2);
			Assert.AreEqual (1, memoTest1CallCount);
		}

		[Test]
		public void TestMemoizeMethodsAreDifferent ()
		{
			var f1 = CoreExtensions.Memoize (MemoTest1);
			var f2 = CoreExtensions.Memoize (MemoTest1);

			Assert.AreNotSame (f1(), f2());
			Assert.AreEqual (2, memoTest1CallCount);
		}

		static int memoTest2CallCount;
		const int knownArg = 1;
		static object MemoTest2 (int a)
		{
			memoTest2CallCount++;
			if (a == knownArg)
				return new object ();
			return new object ();
		}

		[Test]
		public void TestMemoizeMethod2 ()
		{
			var f2 = CoreExtensions.Memoize<int, object> (MemoTest2);
			var obj11 = f2 (knownArg);
			var obj12 = f2 (knownArg);
			var obj21 = f2 (knownArg + 1);
			var obj22 = f2 (knownArg + 1);
			Assert.AreSame (obj11, obj12);
			Assert.AreSame (obj21, obj22);
			Assert.AreNotSame (obj11, obj22);
			Assert.AreEqual (2, memoTest2CallCount);
		}

		static int memoTest3CallCount;
		static object MemoTest3 (int a, int b)
		{
			memoTest3CallCount++;
			if (a == knownArg && b == knownArg)
				return new object ();
			
			if (a != knownArg) {
				if (b != knownArg)
					return new object ();
				return new object ();
			}
			return new object ();
		}

		[Test]
		public void TestMemoizeMethod3 ()
		{
			var f2 = CoreExtensions.Memoize<int, int, object> (MemoTest3);
			var obj1 = f2 (knownArg, knownArg);
			var obj2 = f2 (knownArg, knownArg + 1);
			var obj3 = f2 (knownArg + 1, knownArg);
			var obj4 = f2 (knownArg + 1, knownArg + 1);
			Assert.AreNotSame (obj1, obj2);
			Assert.AreNotSame (obj2, obj3);
			Assert.AreNotSame (obj3, obj4);

			// Force this, we already test the general algorithm in memo1 and memo2.
			obj1 = f2 (knownArg, knownArg);
			Assert.AreEqual (4, memoTest3CallCount);
		}
	}
}