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 51 kB

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