Debian Codegraph @

external/rx/Ix/NET/Tests/Tests.Imperative.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 using System;
3 using System.Text;
4 using System.Collections.Generic;
5 using System.Linq;
6 #if NUNIT
7 using NUnit.Framework;
8 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
9 using TestMethodAttribute = NUnit.Framework.TestAttribute;
10 using TestInitializeAttribute = NUnit.Framework.SetUpAttribute;
11 #else
12 using Microsoft.VisualStudio.TestTools.UnitTesting;
13 #endif
14
15 namespace Tests
16 {
17 public partial class Tests
18 {
19 [TestMethod]
20 public void While_Arguments()
21 {
22 AssertThrows<ArgumentNullException>(() => EnumerableEx.While<int>(null, new[] { 1 }));
23 AssertThrows<ArgumentNullException>(() => EnumerableEx.While<int>(() => true, null));
24 }
25
26 [TestMethod]
27 public void While1()
28 {
29 var x = 5;
30 var res = EnumerableEx.While(() => x > 0, EnumerableEx.Defer(() => new[] { x }).Do(_ => x--)).ToList();
31 Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 5, 4, 3, 2, 1 }));
32 }
33
34 [TestMethod]
35 public void While2()
36 {
37 var x = 0;
38 var res = EnumerableEx.While(() => x > 0, EnumerableEx.Defer(() => new[] { x }).Do(_ => x--)).ToList();
39 Assert.IsTrue(Enumerable.SequenceEqual(res, new int[0]));
40 }
41
42 [TestMethod]
43 public void DoWhile_Arguments()
44 {
45 AssertThrows<ArgumentNullException>(() => EnumerableEx.DoWhile<int>(new[] { 1 }, null));
46 AssertThrows<ArgumentNullException>(() => EnumerableEx.DoWhile<int>(null, () => true));
47 }
48
49 [TestMethod]
50 public void DoWhile1()
51 {
52 var x = 5;
53 var res = EnumerableEx.DoWhile(EnumerableEx.Defer(() => new[] { x }).Do(_ => x--), () => x > 0).ToList();
54 Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 5, 4, 3, 2, 1 }));
55 }
56
57 [TestMethod]
58 public void DoWhile2()
59 {
60 var x = 0;
61 var res = EnumerableEx.DoWhile(EnumerableEx.Defer(() => new[] { x }).Do(_ => x--), () => x > 0).ToList();
62 Assert.IsTrue(Enumerable.SequenceEqual(res, new[] { 0 }));
63 }
64
65 [TestMethod]
66 public void If_Arguments()
67 {
68 AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(null, new[] { 1 }));
69 AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, null));
70 AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(null, new[] { 1 }, new[] { 1 }));
71 AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, null, new[] { 1 }));
72 AssertThrows<ArgumentNullException>(() => EnumerableEx.If<int>(() => true, new[] { 1 }, null));
73 }
74
75 [TestMethod]
76 public void If1()
77 {
78 var x = 5;
79 var res = EnumerableEx.If(() => x > 0, new[] { +1 }, new[] { -1 });
80
81 Assert.AreEqual(+1, res.Single());
82
83 x = -x;
84 Assert.AreEqual(-1, res.Single());
85 }
86
87 [TestMethod]
88 public void If2()
89 {
90 var x = 5;
91 var res = EnumerableEx.If(() => x > 0, new[] { +1 });
92
93 Assert.AreEqual(+1, res.Single());
94
95 x = -x;
96 Assert.IsTrue(res.IsEmpty());
97 }
98
99 [TestMethod]
100 public void Case_Arguments()
101 {
102 AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(null, new Dictionary<int, IEnumerable<int>>()));
103 AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(() => 1, null));
104 AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(null, new Dictionary<int, IEnumerable<int>>(), new[] { 1 }));
105 AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(() => 1, null, new[] { 1 }));
106 AssertThrows<ArgumentNullException>(() => EnumerableEx.Case<int, int>(() => 1, new Dictionary<int, IEnumerable<int>>(), null));
107 }
108
109 [TestMethod]
110 public void Case1()
111 {
112 var x = 1;
113 var d = 'd';
114 var res = EnumerableEx.Case<int, char>(() => x, new Dictionary<int, IEnumerable<char>>
115 {
116 { 0, new[] { 'a' } },
117 { 1, new[] { 'b' } },
118 { 2, new[] { 'c' } },
119 { 3, EnumerableEx.Defer(() => new[] { d }) },
120 });
121
122 Assert.AreEqual('b', res.Single());
123 Assert.AreEqual('b', res.Single());
124
125 x = 0;
126 Assert.AreEqual('a', res.Single());
127
128 x = 2;
129 Assert.AreEqual('c', res.Single());
130
131 x = 3;
132 Assert.AreEqual('d', res.Single());
133
134 d = 'e';
135 Assert.AreEqual('e', res.Single());
136
137 x = 4;
138 Assert.IsTrue(res.IsEmpty());
139 }
140
141 [TestMethod]
142 public void Case2()
143 {
144 var x = 1;
145 var d = 'd';
146 var res = EnumerableEx.Case<int, char>(() => x, new Dictionary<int, IEnumerable<char>>
147 {
148 { 0, new[] { 'a' } },
149 { 1, new[] { 'b' } },
150 { 2, new[] { 'c' } },
151 { 3, EnumerableEx.Defer(() => new[] { d }) },
152 }, new[] { 'z' });
153
154 Assert.AreEqual('b', res.Single());
155 Assert.AreEqual('b', res.Single());
156
157 x = 0;
158 Assert.AreEqual('a', res.Single());
159
160 x = 2;
161 Assert.AreEqual('c', res.Single());
162
163 x = 3;
164 Assert.AreEqual('d', res.Single());
165
166 d = 'e';
167 Assert.AreEqual('e', res.Single());
168
169 x = 4;
170 Assert.AreEqual('z', res.Single());
171 }
172
173 [TestMethod]
174 public void For_Arguments()
175 {
176 AssertThrows<ArgumentNullException>(() => EnumerableEx.For<int, int>(null, x => new[] { 1 }));
177 AssertThrows<ArgumentNullException>(() => EnumerableEx.For<int, int>(new[] { 1 }, null));
178 }
179
180 [TestMethod]
181 public void For()
182 {
183 var res = EnumerableEx.For(new[] { 1, 2, 3 }, x => Enumerable.Range(0, x)).ToList();
184 Assert.IsTrue(res.SequenceEqual(new[] { 0, 0, 1, 0, 1, 2 }));
185 }
186 }
187 }