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

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