MinByTest.cs 13 KB

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