MinByTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reactive.Linq;
  8. using Microsoft.Reactive.Testing;
  9. using ReactiveTests.Dummies;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. namespace ReactiveTests.Tests
  12. {
  13. [TestClass]
  14. public class MinByTest : ReactiveTest
  15. {
  16. [TestMethod]
  17. public void MinBy_ArgumentChecking()
  18. {
  19. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.MinBy(default(IObservable<int>), x => x));
  20. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.MinBy(DummyObservable<int>.Instance, default(Func<int, int>)));
  21. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.MinBy(default(IObservable<int>), x => x, Comparer<int>.Default));
  22. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.MinBy(DummyObservable<int>.Instance, default, Comparer<int>.Default));
  23. ReactiveAssert.Throws<ArgumentNullException>(() => Observable.MinBy(DummyObservable<int>.Instance, x => x, null));
  24. }
  25. [TestMethod]
  26. public void MinBy_Empty()
  27. {
  28. var scheduler = new TestScheduler();
  29. var xs = scheduler.CreateHotObservable(
  30. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  31. OnCompleted<KeyValuePair<int, string>>(250)
  32. );
  33. var res = scheduler.Start(() =>
  34. xs.MinBy(x => x.Key)
  35. );
  36. res.Messages.AssertEqual(
  37. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.Count == 0),
  38. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  39. );
  40. xs.Subscriptions.AssertEqual(
  41. Subscribe(200, 250)
  42. );
  43. }
  44. [TestMethod]
  45. public void MinBy_Return()
  46. {
  47. var scheduler = new TestScheduler();
  48. var xs = scheduler.CreateHotObservable(
  49. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  50. OnNext(210, new KeyValuePair<int, string>(2, "a")),
  51. OnCompleted<KeyValuePair<int, string>>(250)
  52. );
  53. var res = scheduler.Start(() =>
  54. xs.MinBy(x => x.Key)
  55. );
  56. res.Messages.AssertEqual(
  57. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.SequenceEqual([
  58. new KeyValuePair<int, string>(2, "a"),
  59. ])),
  60. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  61. );
  62. xs.Subscriptions.AssertEqual(
  63. Subscribe(200, 250)
  64. );
  65. }
  66. [TestMethod]
  67. public void MinBy_Some()
  68. {
  69. var scheduler = new TestScheduler();
  70. var xs = scheduler.CreateHotObservable(
  71. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  72. OnNext(210, new KeyValuePair<int, string>(3, "b")),
  73. OnNext(220, new KeyValuePair<int, string>(2, "c")),
  74. OnNext(230, new KeyValuePair<int, string>(4, "a")),
  75. OnCompleted<KeyValuePair<int, string>>(250)
  76. );
  77. var res = scheduler.Start(() =>
  78. xs.MinBy(x => x.Key)
  79. );
  80. res.Messages.AssertEqual(
  81. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.SequenceEqual([
  82. new KeyValuePair<int, string>(2, "c"),
  83. ])),
  84. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  85. );
  86. xs.Subscriptions.AssertEqual(
  87. Subscribe(200, 250)
  88. );
  89. }
  90. [TestMethod]
  91. public void MinBy_Multiple()
  92. {
  93. var scheduler = new TestScheduler();
  94. var xs = scheduler.CreateHotObservable(
  95. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  96. OnNext(210, new KeyValuePair<int, string>(3, "b")),
  97. OnNext(215, new KeyValuePair<int, string>(2, "d")),
  98. OnNext(220, new KeyValuePair<int, string>(3, "c")),
  99. OnNext(225, new KeyValuePair<int, string>(2, "y")),
  100. OnNext(230, new KeyValuePair<int, string>(4, "a")),
  101. OnNext(235, new KeyValuePair<int, string>(4, "r")),
  102. OnCompleted<KeyValuePair<int, string>>(250)
  103. );
  104. var res = scheduler.Start(() =>
  105. xs.MinBy(x => x.Key)
  106. );
  107. res.Messages.AssertEqual(
  108. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.SequenceEqual([
  109. new KeyValuePair<int, string>(2, "d"),
  110. new KeyValuePair<int, string>(2, "y"),
  111. ])),
  112. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  113. );
  114. xs.Subscriptions.AssertEqual(
  115. Subscribe(200, 250)
  116. );
  117. }
  118. [TestMethod]
  119. public void MinBy_Throw()
  120. {
  121. var ex = new Exception();
  122. var scheduler = new TestScheduler();
  123. var xs = scheduler.CreateHotObservable(
  124. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  125. OnError<KeyValuePair<int, string>>(210, ex)
  126. );
  127. var res = scheduler.Start(() =>
  128. xs.MinBy(x => x.Key)
  129. );
  130. res.Messages.AssertEqual(
  131. OnError<IList<KeyValuePair<int, string>>>(210, ex)
  132. );
  133. xs.Subscriptions.AssertEqual(
  134. Subscribe(200, 210)
  135. );
  136. }
  137. [TestMethod]
  138. public void MinBy_Never()
  139. {
  140. var ex = new Exception();
  141. var scheduler = new TestScheduler();
  142. var xs = scheduler.CreateHotObservable(
  143. OnNext(150, new KeyValuePair<int, string>(1, "z"))
  144. );
  145. var res = scheduler.Start(() =>
  146. xs.MinBy(x => x.Key)
  147. );
  148. res.Messages.AssertEqual(
  149. );
  150. xs.Subscriptions.AssertEqual(
  151. Subscribe(200, 1000)
  152. );
  153. }
  154. [TestMethod]
  155. public void MinBy_Comparer_Empty()
  156. {
  157. var scheduler = new TestScheduler();
  158. var xs = scheduler.CreateHotObservable(
  159. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  160. OnCompleted<KeyValuePair<int, string>>(250)
  161. );
  162. var res = scheduler.Start(() =>
  163. xs.MinBy(x => x.Key, new ReverseComparer<int>(Comparer<int>.Default))
  164. );
  165. res.Messages.AssertEqual(
  166. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.Count == 0),
  167. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  168. );
  169. xs.Subscriptions.AssertEqual(
  170. Subscribe(200, 250)
  171. );
  172. }
  173. [TestMethod]
  174. public void MinBy_Comparer_Return()
  175. {
  176. var scheduler = new TestScheduler();
  177. var xs = scheduler.CreateHotObservable(
  178. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  179. OnNext(210, new KeyValuePair<int, string>(2, "a")),
  180. OnCompleted<KeyValuePair<int, string>>(250)
  181. );
  182. var res = scheduler.Start(() =>
  183. xs.MinBy(x => x.Key, new ReverseComparer<int>(Comparer<int>.Default))
  184. );
  185. res.Messages.AssertEqual(
  186. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.SequenceEqual([
  187. new KeyValuePair<int, string>(2, "a"),
  188. ])),
  189. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  190. );
  191. xs.Subscriptions.AssertEqual(
  192. Subscribe(200, 250)
  193. );
  194. }
  195. [TestMethod]
  196. public void MinBy_Comparer_Some()
  197. {
  198. var scheduler = new TestScheduler();
  199. var xs = scheduler.CreateHotObservable(
  200. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  201. OnNext(210, new KeyValuePair<int, string>(3, "b")),
  202. OnNext(220, new KeyValuePair<int, string>(20, "c")),
  203. OnNext(230, new KeyValuePair<int, string>(4, "a")),
  204. OnCompleted<KeyValuePair<int, string>>(250)
  205. );
  206. var res = scheduler.Start(() =>
  207. xs.MinBy(x => x.Key, new ReverseComparer<int>(Comparer<int>.Default))
  208. );
  209. res.Messages.AssertEqual(
  210. OnNext<IList<KeyValuePair<int, string>>>(250, x => x.SequenceEqual([
  211. new KeyValuePair<int, string>(20, "c"),
  212. ])),
  213. OnCompleted<IList<KeyValuePair<int, string>>>(250)
  214. );
  215. xs.Subscriptions.AssertEqual(
  216. Subscribe(200, 250)
  217. );
  218. }
  219. [TestMethod]
  220. public void MinBy_Comparer_Throw()
  221. {
  222. var ex = new Exception();
  223. var scheduler = new TestScheduler();
  224. var xs = scheduler.CreateHotObservable(
  225. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  226. OnError<KeyValuePair<int, string>>(210, ex)
  227. );
  228. var res = scheduler.Start(() =>
  229. xs.MinBy(x => x.Key, new ReverseComparer<int>(Comparer<int>.Default))
  230. );
  231. res.Messages.AssertEqual(
  232. OnError<IList<KeyValuePair<int, string>>>(210, ex)
  233. );
  234. xs.Subscriptions.AssertEqual(
  235. Subscribe(200, 210)
  236. );
  237. }
  238. [TestMethod]
  239. public void MinBy_Comparer_Never()
  240. {
  241. var ex = new Exception();
  242. var scheduler = new TestScheduler();
  243. var xs = scheduler.CreateHotObservable(
  244. OnNext(150, new KeyValuePair<int, string>(1, "z"))
  245. );
  246. var res = scheduler.Start(() =>
  247. xs.MinBy(x => x.Key, new ReverseComparer<int>(Comparer<int>.Default))
  248. );
  249. res.Messages.AssertEqual(
  250. );
  251. xs.Subscriptions.AssertEqual(
  252. Subscribe(200, 1000)
  253. );
  254. }
  255. [TestMethod]
  256. public void MinBy_SelectorThrows()
  257. {
  258. var ex = new Exception();
  259. var scheduler = new TestScheduler();
  260. var xs = scheduler.CreateHotObservable(
  261. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  262. OnNext(210, new KeyValuePair<int, string>(3, "b")),
  263. OnNext(220, new KeyValuePair<int, string>(2, "c")),
  264. OnNext(230, new KeyValuePair<int, string>(4, "a")),
  265. OnCompleted<KeyValuePair<int, string>>(250)
  266. );
  267. var res = scheduler.Start(() =>
  268. xs.MinBy<KeyValuePair<int, string>, int>(x => { throw ex; })
  269. );
  270. res.Messages.AssertEqual(
  271. OnError<IList<KeyValuePair<int, string>>>(210, ex)
  272. );
  273. xs.Subscriptions.AssertEqual(
  274. Subscribe(200, 210)
  275. );
  276. }
  277. [TestMethod]
  278. public void MinBy_ComparerThrows()
  279. {
  280. var ex = new Exception();
  281. var scheduler = new TestScheduler();
  282. var xs = scheduler.CreateHotObservable(
  283. OnNext(150, new KeyValuePair<int, string>(1, "z")),
  284. OnNext(210, new KeyValuePair<int, string>(3, "b")),
  285. OnNext(220, new KeyValuePair<int, string>(2, "c")),
  286. OnNext(230, new KeyValuePair<int, string>(4, "a")),
  287. OnCompleted<KeyValuePair<int, string>>(250)
  288. );
  289. var res = scheduler.Start(() =>
  290. xs.MinBy(x => x.Key, new ThrowingComparer<int>(ex))
  291. );
  292. res.Messages.AssertEqual(
  293. OnError<IList<KeyValuePair<int, string>>>(220, ex)
  294. );
  295. xs.Subscriptions.AssertEqual(
  296. Subscribe(200, 220)
  297. );
  298. }
  299. private class ReverseComparer<T> : IComparer<T>
  300. {
  301. private readonly IComparer<T> _comparer;
  302. public ReverseComparer(IComparer<T> comparer)
  303. {
  304. _comparer = comparer;
  305. }
  306. public int Compare(T x, T y)
  307. {
  308. return -_comparer.Compare(x, y);
  309. }
  310. }
  311. private class ThrowingComparer<T> : IComparer<T>
  312. {
  313. private readonly Exception _ex;
  314. public ThrowingComparer(Exception ex)
  315. {
  316. _ex = ex;
  317. }
  318. public int Compare(T x, T y)
  319. {
  320. throw _ex;
  321. }
  322. }
  323. }
  324. }