You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FluentExtension.cs 54 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. using FluentAssertions;
  2. using FluentAssertions.Execution;
  3. using FluentAssertions.Primitives;
  4. using NumSharp;
  5. using NumSharp.Backends;
  6. using NumSharp.Utilities;
  7. using System;
  8. using System.Diagnostics;
  9. using System.Linq;
  10. using System.Runtime.CompilerServices;
  11. namespace TensorFlowNET.UnitTest
  12. {
  13. [DebuggerStepThrough]
  14. public static class FluentExtension
  15. {
  16. public static ShapeAssertions Should(this Shape shape)
  17. {
  18. return new ShapeAssertions(shape);
  19. }
  20. public static NDArrayAssertions Should(this NDArray arr)
  21. {
  22. return new NDArrayAssertions(arr);
  23. }
  24. public static NDArrayAssertions Should(this UnmanagedStorage arr)
  25. {
  26. return new NDArrayAssertions(arr);
  27. }
  28. public static string ToString(this Array arr, bool flat)
  29. {
  30. return new NDArray(arr).ToString(flat);
  31. }
  32. }
  33. [DebuggerStepThrough]
  34. public class ShapeAssertions : ReferenceTypeAssertions<Shape, ShapeAssertions>
  35. {
  36. public ShapeAssertions(Shape instance)
  37. {
  38. Subject = instance;
  39. }
  40. protected override string Identifier => "shape";
  41. public AndConstraint<ShapeAssertions> BeOfSize(int size, string because = null, params object[] becauseArgs)
  42. {
  43. Subject.Size.Should().Be(size, because, becauseArgs);
  44. return new AndConstraint<ShapeAssertions>(this);
  45. }
  46. public AndConstraint<ShapeAssertions> NotBeOfSize(int size, string because = null, params object[] becauseArgs)
  47. {
  48. Subject.Size.Should().NotBe(size, because, becauseArgs);
  49. return new AndConstraint<ShapeAssertions>(this);
  50. }
  51. public AndConstraint<ShapeAssertions> BeShaped(params int[] dimensions)
  52. {
  53. if (dimensions == null)
  54. throw new ArgumentNullException(nameof(dimensions));
  55. if (dimensions.Length == 0)
  56. throw new ArgumentException("Value cannot be an empty collection.", nameof(dimensions));
  57. Subject.Dimensions.Should().BeEquivalentTo(dimensions);
  58. return new AndConstraint<ShapeAssertions>(this);
  59. }
  60. public AndConstraint<ShapeAssertions> Be(Shape shape, string because = null, params object[] becauseArgs)
  61. {
  62. Execute.Assertion
  63. .BecauseOf(because, becauseArgs)
  64. .ForCondition(Subject.Equals(shape))
  65. .FailWith($"Expected shape to be {shape.ToString()} but got {Subject.ToString()}");
  66. return new AndConstraint<ShapeAssertions>(this);
  67. }
  68. public AndConstraint<ShapeAssertions> BeEquivalentTo(int? size = null, int? ndim = null, ITuple shape = null)
  69. {
  70. if (size.HasValue)
  71. {
  72. BeOfSize(size.Value, null);
  73. }
  74. if (ndim.HasValue)
  75. HaveNDim(ndim.Value);
  76. if (shape != null)
  77. for (int i = 0; i < shape.Length; i++)
  78. {
  79. Subject.Dimensions[i].Should().Be((int)shape[i]);
  80. }
  81. return new AndConstraint<ShapeAssertions>(this);
  82. }
  83. public AndConstraint<ShapeAssertions> NotBe(Shape shape, string because = null, params object[] becauseArgs)
  84. {
  85. Execute.Assertion
  86. .BecauseOf(because, becauseArgs)
  87. .ForCondition(!Subject.Equals(shape))
  88. .FailWith($"Expected shape to be {shape.ToString()} but got {Subject.ToString()}");
  89. return new AndConstraint<ShapeAssertions>(this);
  90. }
  91. public AndConstraint<ShapeAssertions> HaveNDim(int ndim)
  92. {
  93. Subject.Dimensions.Length.Should().Be(ndim);
  94. return new AndConstraint<ShapeAssertions>(this);
  95. }
  96. public AndConstraint<ShapeAssertions> BeSliced()
  97. {
  98. Subject.IsSliced.Should().BeTrue();
  99. return new AndConstraint<ShapeAssertions>(this);
  100. }
  101. public AndConstraint<ShapeAssertions> BeScalar()
  102. {
  103. Subject.IsScalar.Should().BeTrue();
  104. return new AndConstraint<ShapeAssertions>(this);
  105. }
  106. public AndConstraint<ShapeAssertions> BeBroadcasted()
  107. {
  108. Subject.IsBroadcasted.Should().BeTrue();
  109. return new AndConstraint<ShapeAssertions>(this);
  110. }
  111. public AndConstraint<ShapeAssertions> NotBeSliced()
  112. {
  113. Subject.IsSliced.Should().BeFalse();
  114. return new AndConstraint<ShapeAssertions>(this);
  115. }
  116. public AndConstraint<ShapeAssertions> NotBeScalar()
  117. {
  118. Subject.IsScalar.Should().BeFalse();
  119. return new AndConstraint<ShapeAssertions>(this);
  120. }
  121. public AndConstraint<ShapeAssertions> NotBeBroadcasted()
  122. {
  123. Subject.IsBroadcasted.Should().BeFalse();
  124. return new AndConstraint<ShapeAssertions>(this);
  125. }
  126. public AndConstraint<ShapeAssertions> BeNDim(int ndim)
  127. {
  128. Subject.Dimensions.Length.Should().Be(ndim);
  129. return new AndConstraint<ShapeAssertions>(this);
  130. }
  131. }
  132. //[DebuggerStepThrough]
  133. public class NDArrayAssertions : ReferenceTypeAssertions<NDArray, NDArrayAssertions>
  134. {
  135. public NDArrayAssertions(NDArray instance)
  136. {
  137. Subject = instance;
  138. }
  139. public NDArrayAssertions(UnmanagedStorage instance)
  140. {
  141. Subject = new NDArray(instance);
  142. }
  143. protected override string Identifier => "shape";
  144. public AndConstraint<NDArrayAssertions> BeOfSize(int size, string because = null, params object[] becauseArgs)
  145. {
  146. Subject.size.Should().Be(size, because, becauseArgs);
  147. return new AndConstraint<NDArrayAssertions>(this);
  148. }
  149. public AndConstraint<NDArrayAssertions> BeShaped(params int[] dimensions)
  150. {
  151. if (dimensions == null)
  152. throw new ArgumentNullException(nameof(dimensions));
  153. if (dimensions.Length == 0)
  154. throw new ArgumentException("Value cannot be an empty collection.", nameof(dimensions));
  155. Subject.Unsafe.Storage.Shape.Dimensions.Should().BeEquivalentTo(dimensions);
  156. return new AndConstraint<NDArrayAssertions>(this);
  157. }
  158. public AndConstraint<NDArrayAssertions> BeShaped(int? size = null, int? ndim = null, ITuple shape = null)
  159. {
  160. if (size.HasValue)
  161. {
  162. BeOfSize(size.Value, null);
  163. }
  164. if (ndim.HasValue)
  165. HaveNDim(ndim.Value);
  166. if (shape != null)
  167. for (int i = 0; i < shape.Length; i++)
  168. {
  169. Subject.Unsafe.Storage.Shape.Dimensions[i].Should().Be((int)shape[i]);
  170. }
  171. return new AndConstraint<NDArrayAssertions>(this);
  172. }
  173. public AndConstraint<NDArrayAssertions> NotBeShaped(Shape shape, string because = null, params object[] becauseArgs)
  174. {
  175. Execute.Assertion
  176. .BecauseOf(because, becauseArgs)
  177. .ForCondition(!Subject.Unsafe.Storage.Shape.Equals(shape))
  178. .FailWith($"Expected shape to be {shape.ToString()} but got {Subject.ToString()}");
  179. return new AndConstraint<NDArrayAssertions>(this);
  180. }
  181. public AndConstraint<NDArrayAssertions> HaveNDim(int ndim)
  182. {
  183. Subject.Unsafe.Storage.Shape.Dimensions.Length.Should().Be(ndim);
  184. return new AndConstraint<NDArrayAssertions>(this);
  185. }
  186. public AndConstraint<NDArrayAssertions> BeBroadcasted()
  187. {
  188. Subject.Unsafe.Storage.Shape.IsBroadcasted.Should().BeTrue();
  189. return new AndConstraint<NDArrayAssertions>(this);
  190. }
  191. public AndConstraint<NDArrayAssertions> NotBeBroadcasted()
  192. {
  193. Subject.Unsafe.Storage.Shape.IsBroadcasted.Should().BeFalse();
  194. return new AndConstraint<NDArrayAssertions>(this);
  195. }
  196. public AndConstraint<NDArrayAssertions> BeSliced()
  197. {
  198. Subject.Unsafe.Storage.Shape.IsSliced.Should().BeTrue();
  199. return new AndConstraint<NDArrayAssertions>(this);
  200. }
  201. public AndConstraint<NDArrayAssertions> BeScalar()
  202. {
  203. Subject.Unsafe.Storage.Shape.IsScalar.Should().BeTrue();
  204. return new AndConstraint<NDArrayAssertions>(this);
  205. }
  206. public AndConstraint<NDArrayAssertions> BeScalar(object value)
  207. {
  208. Subject.Unsafe.Storage.Shape.IsScalar.Should().BeTrue();
  209. Subject.GetValue().Should().Be(value);
  210. return new AndConstraint<NDArrayAssertions>(this);
  211. }
  212. public AndConstraint<NDArrayAssertions> BeOfType(NPTypeCode typeCode)
  213. {
  214. Subject.typecode.Should().Be(typeCode);
  215. return new AndConstraint<NDArrayAssertions>(this);
  216. }
  217. public AndConstraint<NDArrayAssertions> BeOfType(Type typeCode)
  218. {
  219. Subject.dtype.Should().Be(typeCode);
  220. return new AndConstraint<NDArrayAssertions>(this);
  221. }
  222. public AndConstraint<NDArrayAssertions> BeOfType<T>()
  223. {
  224. Subject.typecode.Should().Be(InfoOf<T>.NPTypeCode);
  225. return new AndConstraint<NDArrayAssertions>(this);
  226. }
  227. public AndConstraint<NDArrayAssertions> NotBeSliced()
  228. {
  229. Subject.Unsafe.Storage.Shape.IsSliced.Should().BeFalse();
  230. return new AndConstraint<NDArrayAssertions>(this);
  231. }
  232. public AndConstraint<NDArrayAssertions> NotBeScalar()
  233. {
  234. Subject.Unsafe.Storage.Shape.IsScalar.Should().BeFalse();
  235. return new AndConstraint<NDArrayAssertions>(this);
  236. }
  237. public AndConstraint<NDArrayAssertions> BeNDim(int ndim)
  238. {
  239. Subject.Unsafe.Storage.Shape.Dimensions.Length.Should().Be(ndim);
  240. return new AndConstraint<NDArrayAssertions>(this);
  241. }
  242. public AndConstraint<NDArrayAssertions> Be(NDArray expected)
  243. {
  244. Execute.Assertion
  245. .ForCondition(np.array_equal(Subject, expected))
  246. .FailWith($"Expected the subject and other ndarray to be equals.\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{expected.ToString(false)}");
  247. return new AndConstraint<NDArrayAssertions>(this);
  248. }
  249. public AndConstraint<NDArrayAssertions> BeOfValues(params object[] values)
  250. {
  251. if (values == null)
  252. throw new ArgumentNullException(nameof(values));
  253. Subject.size.Should().Be(values.Length, "the method BeOfValues also confirms the sizes are matching with given values.");
  254. #if _REGEN
  255. #region Compute
  256. switch (Subject.typecode)
  257. {
  258. %foreach supported_dtypes,supported_dtypes_lowercase%
  259. case NPTypeCode.#1:
  260. {
  261. var iter = Subject.AsIterator<#2>();
  262. var next = iter.MoveNext;
  263. var hasnext = iter.HasNext;
  264. for (int i = 0; i < values.Length; i++)
  265. {
  266. Execute.Assertion
  267. .ForCondition(hasnext())
  268. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  269. var expected = Convert.To#1(values[i]);
  270. var nextval = next();
  271. Execute.Assertion
  272. .ForCondition(expected == nextval)
  273. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: #1).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  274. }
  275. break;
  276. }
  277. %
  278. default:
  279. throw new NotSupportedException();
  280. }
  281. #endregion
  282. #else
  283. #region Compute
  284. switch (Subject.typecode)
  285. {
  286. case NPTypeCode.Boolean:
  287. {
  288. var iter = Subject.AsIterator<bool>();
  289. var next = iter.MoveNext;
  290. var hasnext = iter.HasNext;
  291. for (int i = 0; i < values.Length; i++)
  292. {
  293. Execute.Assertion
  294. .ForCondition(hasnext())
  295. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  296. var expected = Convert.ToBoolean(values[i]);
  297. var nextval = next();
  298. Execute.Assertion
  299. .ForCondition(expected == nextval)
  300. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  301. }
  302. break;
  303. }
  304. case NPTypeCode.Byte:
  305. {
  306. var iter = Subject.AsIterator<byte>();
  307. var next = iter.MoveNext;
  308. var hasnext = iter.HasNext;
  309. for (int i = 0; i < values.Length; i++)
  310. {
  311. Execute.Assertion
  312. .ForCondition(hasnext())
  313. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  314. var expected = Convert.ToByte(values[i]);
  315. var nextval = next();
  316. Execute.Assertion
  317. .ForCondition(expected == nextval)
  318. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Byte).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  319. }
  320. break;
  321. }
  322. case NPTypeCode.Int16:
  323. {
  324. var iter = Subject.AsIterator<short>();
  325. var next = iter.MoveNext;
  326. var hasnext = iter.HasNext;
  327. for (int i = 0; i < values.Length; i++)
  328. {
  329. Execute.Assertion
  330. .ForCondition(hasnext())
  331. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  332. var expected = Convert.ToInt16(values[i]);
  333. var nextval = next();
  334. Execute.Assertion
  335. .ForCondition(expected == nextval)
  336. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Int16).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  337. }
  338. break;
  339. }
  340. case NPTypeCode.UInt16:
  341. {
  342. var iter = Subject.AsIterator<ushort>();
  343. var next = iter.MoveNext;
  344. var hasnext = iter.HasNext;
  345. for (int i = 0; i < values.Length; i++)
  346. {
  347. Execute.Assertion
  348. .ForCondition(hasnext())
  349. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  350. var expected = Convert.ToUInt16(values[i]);
  351. var nextval = next();
  352. Execute.Assertion
  353. .ForCondition(expected == nextval)
  354. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: UInt16).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  355. }
  356. break;
  357. }
  358. case NPTypeCode.Int32:
  359. {
  360. var iter = Subject.AsIterator<int>();
  361. var next = iter.MoveNext;
  362. var hasnext = iter.HasNext;
  363. for (int i = 0; i < values.Length; i++)
  364. {
  365. Execute.Assertion
  366. .ForCondition(hasnext())
  367. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  368. var expected = Convert.ToInt32(values[i]);
  369. var nextval = next();
  370. Execute.Assertion
  371. .ForCondition(expected == nextval)
  372. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Int32).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  373. }
  374. break;
  375. }
  376. case NPTypeCode.UInt32:
  377. {
  378. var iter = Subject.AsIterator<uint>();
  379. var next = iter.MoveNext;
  380. var hasnext = iter.HasNext;
  381. for (int i = 0; i < values.Length; i++)
  382. {
  383. Execute.Assertion
  384. .ForCondition(hasnext())
  385. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  386. var expected = Convert.ToUInt32(values[i]);
  387. var nextval = next();
  388. Execute.Assertion
  389. .ForCondition(expected == nextval)
  390. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: UInt32).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  391. }
  392. break;
  393. }
  394. case NPTypeCode.Int64:
  395. {
  396. var iter = Subject.AsIterator<long>();
  397. var next = iter.MoveNext;
  398. var hasnext = iter.HasNext;
  399. for (int i = 0; i < values.Length; i++)
  400. {
  401. Execute.Assertion
  402. .ForCondition(hasnext())
  403. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  404. var expected = Convert.ToInt64(values[i]);
  405. var nextval = next();
  406. Execute.Assertion
  407. .ForCondition(expected == nextval)
  408. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Int64).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  409. }
  410. break;
  411. }
  412. case NPTypeCode.UInt64:
  413. {
  414. var iter = Subject.AsIterator<ulong>();
  415. var next = iter.MoveNext;
  416. var hasnext = iter.HasNext;
  417. for (int i = 0; i < values.Length; i++)
  418. {
  419. Execute.Assertion
  420. .ForCondition(hasnext())
  421. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  422. var expected = Convert.ToUInt64(values[i]);
  423. var nextval = next();
  424. Execute.Assertion
  425. .ForCondition(expected == nextval)
  426. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: UInt64).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  427. }
  428. break;
  429. }
  430. case NPTypeCode.Char:
  431. {
  432. var iter = Subject.AsIterator<char>();
  433. var next = iter.MoveNext;
  434. var hasnext = iter.HasNext;
  435. for (int i = 0; i < values.Length; i++)
  436. {
  437. Execute.Assertion
  438. .ForCondition(hasnext())
  439. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  440. var expected = Convert.ToChar(values[i]);
  441. var nextval = next();
  442. Execute.Assertion
  443. .ForCondition(expected == nextval)
  444. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Char).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  445. }
  446. break;
  447. }
  448. case NPTypeCode.Double:
  449. {
  450. var iter = Subject.AsIterator<double>();
  451. var next = iter.MoveNext;
  452. var hasnext = iter.HasNext;
  453. for (int i = 0; i < values.Length; i++)
  454. {
  455. Execute.Assertion
  456. .ForCondition(hasnext())
  457. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  458. var expected = Convert.ToDouble(values[i]);
  459. var nextval = next();
  460. Execute.Assertion
  461. .ForCondition(expected == nextval)
  462. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Double).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  463. }
  464. break;
  465. }
  466. case NPTypeCode.Single:
  467. {
  468. var iter = Subject.AsIterator<float>();
  469. var next = iter.MoveNext;
  470. var hasnext = iter.HasNext;
  471. for (int i = 0; i < values.Length; i++)
  472. {
  473. Execute.Assertion
  474. .ForCondition(hasnext())
  475. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  476. var expected = Convert.ToSingle(values[i]);
  477. var nextval = next();
  478. Execute.Assertion
  479. .ForCondition(expected == nextval)
  480. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Single).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  481. }
  482. break;
  483. }
  484. case NPTypeCode.Decimal:
  485. {
  486. var iter = Subject.AsIterator<decimal>();
  487. var next = iter.MoveNext;
  488. var hasnext = iter.HasNext;
  489. for (int i = 0; i < values.Length; i++)
  490. {
  491. Execute.Assertion
  492. .ForCondition(hasnext())
  493. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  494. var expected = Convert.ToDecimal(values[i]);
  495. var nextval = next();
  496. Execute.Assertion
  497. .ForCondition(expected == nextval)
  498. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Decimal).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  499. }
  500. break;
  501. }
  502. default:
  503. throw new NotSupportedException();
  504. }
  505. #endregion
  506. #endif
  507. return new AndConstraint<NDArrayAssertions>(this);
  508. }
  509. public AndConstraint<NDArrayAssertions> AllValuesBe(object val)
  510. {
  511. #if _REGEN
  512. #region Compute
  513. switch (Subject.typecode)
  514. {
  515. %foreach supported_dtypes,supported_dtypes_lowercase%
  516. case NPTypeCode.#1:
  517. {
  518. var iter = Subject.AsIterator<#2>();
  519. var next = iter.MoveNext;
  520. var hasnext = iter.HasNext;
  521. var expected = Convert.To#1(val);
  522. for (int i = 0; hasnext(); i++)
  523. {
  524. var nextval = next();
  525. Execute.Assertion
  526. .ForCondition(expected == nextval)
  527. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: #1).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  528. }
  529. break;
  530. }
  531. %
  532. default:
  533. throw new NotSupportedException();
  534. }
  535. #endregion
  536. #else
  537. #region Compute
  538. switch (Subject.typecode)
  539. {
  540. case NPTypeCode.Boolean:
  541. {
  542. var iter = Subject.AsIterator<bool>();
  543. var next = iter.MoveNext;
  544. var hasnext = iter.HasNext;
  545. var expected = Convert.ToBoolean(val);
  546. for (int i = 0; hasnext(); i++)
  547. {
  548. var nextval = next();
  549. Execute.Assertion
  550. .ForCondition(expected == nextval)
  551. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  552. }
  553. break;
  554. }
  555. case NPTypeCode.Byte:
  556. {
  557. var iter = Subject.AsIterator<byte>();
  558. var next = iter.MoveNext;
  559. var hasnext = iter.HasNext;
  560. var expected = Convert.ToByte(val);
  561. for (int i = 0; hasnext(); i++)
  562. {
  563. var nextval = next();
  564. Execute.Assertion
  565. .ForCondition(expected == nextval)
  566. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Byte).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  567. }
  568. break;
  569. }
  570. case NPTypeCode.Int16:
  571. {
  572. var iter = Subject.AsIterator<short>();
  573. var next = iter.MoveNext;
  574. var hasnext = iter.HasNext;
  575. var expected = Convert.ToInt16(val);
  576. for (int i = 0; hasnext(); i++)
  577. {
  578. var nextval = next();
  579. Execute.Assertion
  580. .ForCondition(expected == nextval)
  581. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Int16).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  582. }
  583. break;
  584. }
  585. case NPTypeCode.UInt16:
  586. {
  587. var iter = Subject.AsIterator<ushort>();
  588. var next = iter.MoveNext;
  589. var hasnext = iter.HasNext;
  590. var expected = Convert.ToUInt16(val);
  591. for (int i = 0; hasnext(); i++)
  592. {
  593. var nextval = next();
  594. Execute.Assertion
  595. .ForCondition(expected == nextval)
  596. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: UInt16).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  597. }
  598. break;
  599. }
  600. case NPTypeCode.Int32:
  601. {
  602. var iter = Subject.AsIterator<int>();
  603. var next = iter.MoveNext;
  604. var hasnext = iter.HasNext;
  605. var expected = Convert.ToInt32(val);
  606. for (int i = 0; hasnext(); i++)
  607. {
  608. var nextval = next();
  609. Execute.Assertion
  610. .ForCondition(expected == nextval)
  611. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Int32).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  612. }
  613. break;
  614. }
  615. case NPTypeCode.UInt32:
  616. {
  617. var iter = Subject.AsIterator<uint>();
  618. var next = iter.MoveNext;
  619. var hasnext = iter.HasNext;
  620. var expected = Convert.ToUInt32(val);
  621. for (int i = 0; hasnext(); i++)
  622. {
  623. var nextval = next();
  624. Execute.Assertion
  625. .ForCondition(expected == nextval)
  626. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: UInt32).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  627. }
  628. break;
  629. }
  630. case NPTypeCode.Int64:
  631. {
  632. var iter = Subject.AsIterator<long>();
  633. var next = iter.MoveNext;
  634. var hasnext = iter.HasNext;
  635. var expected = Convert.ToInt64(val);
  636. for (int i = 0; hasnext(); i++)
  637. {
  638. var nextval = next();
  639. Execute.Assertion
  640. .ForCondition(expected == nextval)
  641. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Int64).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  642. }
  643. break;
  644. }
  645. case NPTypeCode.UInt64:
  646. {
  647. var iter = Subject.AsIterator<ulong>();
  648. var next = iter.MoveNext;
  649. var hasnext = iter.HasNext;
  650. var expected = Convert.ToUInt64(val);
  651. for (int i = 0; hasnext(); i++)
  652. {
  653. var nextval = next();
  654. Execute.Assertion
  655. .ForCondition(expected == nextval)
  656. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: UInt64).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  657. }
  658. break;
  659. }
  660. case NPTypeCode.Char:
  661. {
  662. var iter = Subject.AsIterator<char>();
  663. var next = iter.MoveNext;
  664. var hasnext = iter.HasNext;
  665. var expected = Convert.ToChar(val);
  666. for (int i = 0; hasnext(); i++)
  667. {
  668. var nextval = next();
  669. Execute.Assertion
  670. .ForCondition(expected == nextval)
  671. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Char).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  672. }
  673. break;
  674. }
  675. case NPTypeCode.Double:
  676. {
  677. var iter = Subject.AsIterator<double>();
  678. var next = iter.MoveNext;
  679. var hasnext = iter.HasNext;
  680. var expected = Convert.ToDouble(val);
  681. for (int i = 0; hasnext(); i++)
  682. {
  683. var nextval = next();
  684. Execute.Assertion
  685. .ForCondition(expected == nextval)
  686. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Double).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  687. }
  688. break;
  689. }
  690. case NPTypeCode.Single:
  691. {
  692. var iter = Subject.AsIterator<float>();
  693. var next = iter.MoveNext;
  694. var hasnext = iter.HasNext;
  695. var expected = Convert.ToSingle(val);
  696. for (int i = 0; hasnext(); i++)
  697. {
  698. var nextval = next();
  699. Execute.Assertion
  700. .ForCondition(expected == nextval)
  701. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Single).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  702. }
  703. break;
  704. }
  705. case NPTypeCode.Decimal:
  706. {
  707. var iter = Subject.AsIterator<decimal>();
  708. var next = iter.MoveNext;
  709. var hasnext = iter.HasNext;
  710. var expected = Convert.ToDecimal(val);
  711. for (int i = 0; hasnext(); i++)
  712. {
  713. var nextval = next();
  714. Execute.Assertion
  715. .ForCondition(expected == nextval)
  716. .FailWith($"Expected NDArray's {2}th value to be {0}, but found {1} (dtype: Decimal).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  717. }
  718. break;
  719. }
  720. default:
  721. throw new NotSupportedException();
  722. }
  723. #endregion
  724. #endif
  725. return new AndConstraint<NDArrayAssertions>(this);
  726. }
  727. public AndConstraint<NDArrayAssertions> BeOfValuesApproximately(double sensitivity, params object[] values)
  728. {
  729. if (values == null)
  730. throw new ArgumentNullException(nameof(values));
  731. Subject.size.Should().Be(values.Length, "the method BeOfValuesApproximately also confirms the sizes are matching with given values.");
  732. #if _REGEN
  733. #region Compute
  734. switch (Subject.typecode)
  735. {
  736. %foreach supported_dtypes,supported_dtypes_lowercase%
  737. case NPTypeCode.#1:
  738. {
  739. var iter = Subject.AsIterator<#2>();
  740. var next = iter.MoveNext;
  741. var hasnext = iter.HasNext;
  742. for (int i = 0; i < values.Length; i++)
  743. {
  744. Execute.Assertion
  745. .ForCondition(hasnext())
  746. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  747. var expected = Convert.To#1(values[i]);
  748. var nextval = next();
  749. Execute.Assertion
  750. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  751. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  752. }
  753. break;
  754. }
  755. %
  756. default:
  757. throw new NotSupportedException();
  758. }
  759. #endregion
  760. #else
  761. #region Compute
  762. switch (Subject.typecode)
  763. {
  764. case NPTypeCode.Boolean:
  765. {
  766. var iter = Subject.AsIterator<bool>();
  767. var next = iter.MoveNext;
  768. var hasnext = iter.HasNext;
  769. for (int i = 0; i < values.Length; i++)
  770. {
  771. Execute.Assertion
  772. .ForCondition(hasnext())
  773. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  774. var expected = Convert.ToBoolean(values[i]);
  775. var nextval = next();
  776. Execute.Assertion
  777. .ForCondition(expected == nextval)
  778. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  779. }
  780. break;
  781. }
  782. case NPTypeCode.Byte:
  783. {
  784. var iter = Subject.AsIterator<byte>();
  785. var next = iter.MoveNext;
  786. var hasnext = iter.HasNext;
  787. for (int i = 0; i < values.Length; i++)
  788. {
  789. Execute.Assertion
  790. .ForCondition(hasnext())
  791. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  792. var expected = Convert.ToByte(values[i]);
  793. var nextval = next();
  794. Execute.Assertion
  795. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  796. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  797. }
  798. break;
  799. }
  800. case NPTypeCode.Int16:
  801. {
  802. var iter = Subject.AsIterator<short>();
  803. var next = iter.MoveNext;
  804. var hasnext = iter.HasNext;
  805. for (int i = 0; i < values.Length; i++)
  806. {
  807. Execute.Assertion
  808. .ForCondition(hasnext())
  809. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  810. var expected = Convert.ToInt16(values[i]);
  811. var nextval = next();
  812. Execute.Assertion
  813. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  814. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  815. }
  816. break;
  817. }
  818. case NPTypeCode.UInt16:
  819. {
  820. var iter = Subject.AsIterator<ushort>();
  821. var next = iter.MoveNext;
  822. var hasnext = iter.HasNext;
  823. for (int i = 0; i < values.Length; i++)
  824. {
  825. Execute.Assertion
  826. .ForCondition(hasnext())
  827. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  828. var expected = Convert.ToUInt16(values[i]);
  829. var nextval = next();
  830. Execute.Assertion
  831. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  832. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  833. }
  834. break;
  835. }
  836. case NPTypeCode.Int32:
  837. {
  838. var iter = Subject.AsIterator<int>();
  839. var next = iter.MoveNext;
  840. var hasnext = iter.HasNext;
  841. for (int i = 0; i < values.Length; i++)
  842. {
  843. Execute.Assertion
  844. .ForCondition(hasnext())
  845. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  846. var expected = Convert.ToInt32(values[i]);
  847. var nextval = next();
  848. Execute.Assertion
  849. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  850. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  851. }
  852. break;
  853. }
  854. case NPTypeCode.UInt32:
  855. {
  856. var iter = Subject.AsIterator<uint>();
  857. var next = iter.MoveNext;
  858. var hasnext = iter.HasNext;
  859. for (int i = 0; i < values.Length; i++)
  860. {
  861. Execute.Assertion
  862. .ForCondition(hasnext())
  863. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  864. var expected = Convert.ToUInt32(values[i]);
  865. var nextval = next();
  866. Execute.Assertion
  867. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  868. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  869. }
  870. break;
  871. }
  872. case NPTypeCode.Int64:
  873. {
  874. var iter = Subject.AsIterator<long>();
  875. var next = iter.MoveNext;
  876. var hasnext = iter.HasNext;
  877. for (int i = 0; i < values.Length; i++)
  878. {
  879. Execute.Assertion
  880. .ForCondition(hasnext())
  881. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  882. var expected = Convert.ToInt64(values[i]);
  883. var nextval = next();
  884. Execute.Assertion
  885. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  886. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  887. }
  888. break;
  889. }
  890. case NPTypeCode.UInt64:
  891. {
  892. var iter = Subject.AsIterator<ulong>();
  893. var next = iter.MoveNext;
  894. var hasnext = iter.HasNext;
  895. for (int i = 0; i < values.Length; i++)
  896. {
  897. Execute.Assertion
  898. .ForCondition(hasnext())
  899. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  900. var expected = Convert.ToUInt64(values[i]);
  901. var nextval = next();
  902. Execute.Assertion
  903. .ForCondition(Math.Abs((double)(expected - nextval)) <= sensitivity)
  904. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  905. }
  906. break;
  907. }
  908. case NPTypeCode.Char:
  909. {
  910. var iter = Subject.AsIterator<char>();
  911. var next = iter.MoveNext;
  912. var hasnext = iter.HasNext;
  913. for (int i = 0; i < values.Length; i++)
  914. {
  915. Execute.Assertion
  916. .ForCondition(hasnext())
  917. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  918. var expected = Convert.ToChar(values[i]);
  919. var nextval = next();
  920. Execute.Assertion
  921. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  922. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  923. }
  924. break;
  925. }
  926. case NPTypeCode.Double:
  927. {
  928. var iter = Subject.AsIterator<double>();
  929. var next = iter.MoveNext;
  930. var hasnext = iter.HasNext;
  931. for (int i = 0; i < values.Length; i++)
  932. {
  933. Execute.Assertion
  934. .ForCondition(hasnext())
  935. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  936. var expected = Convert.ToDouble(values[i]);
  937. var nextval = next();
  938. Execute.Assertion
  939. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  940. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  941. }
  942. break;
  943. }
  944. case NPTypeCode.Single:
  945. {
  946. var iter = Subject.AsIterator<float>();
  947. var next = iter.MoveNext;
  948. var hasnext = iter.HasNext;
  949. for (int i = 0; i < values.Length; i++)
  950. {
  951. Execute.Assertion
  952. .ForCondition(hasnext())
  953. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  954. var expected = Convert.ToSingle(values[i]);
  955. var nextval = next();
  956. Execute.Assertion
  957. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  958. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  959. }
  960. break;
  961. }
  962. case NPTypeCode.Decimal:
  963. {
  964. var iter = Subject.AsIterator<decimal>();
  965. var next = iter.MoveNext;
  966. var hasnext = iter.HasNext;
  967. for (int i = 0; i < values.Length; i++)
  968. {
  969. Execute.Assertion
  970. .ForCondition(hasnext())
  971. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  972. var expected = Convert.ToDecimal(values[i]);
  973. var nextval = next();
  974. Execute.Assertion
  975. .ForCondition(Math.Abs(expected - nextval) <= (decimal)sensitivity)
  976. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  977. }
  978. break;
  979. }
  980. default:
  981. throw new NotSupportedException();
  982. }
  983. #endregion
  984. #endif
  985. return new AndConstraint<NDArrayAssertions>(this);
  986. }
  987. }
  988. }