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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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(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(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(Type typeCode)
  170. {
  171. Subject.dtype.Should().Be(typeCode);
  172. return new AndConstraint<NDArrayAssertions>(this);
  173. }
  174. public AndConstraint<NDArrayAssertions> NotBeScalar()
  175. {
  176. Subject.shape.IsScalar.Should().BeFalse();
  177. return new AndConstraint<NDArrayAssertions>(this);
  178. }
  179. public AndConstraint<NDArrayAssertions> BeNDim(int ndim)
  180. {
  181. Subject.ndim.Should().Be(ndim);
  182. return new AndConstraint<NDArrayAssertions>(this);
  183. }
  184. public AndConstraint<NDArrayAssertions> Be(NDArray expected)
  185. {
  186. Execute.Assertion
  187. .ForCondition(np.array_equal(Subject, expected))
  188. .FailWith($"Expected the subject and other ndarray to be equals.\n------- Subject -------\n{Subject}\n------- Expected -------\n{expected}");
  189. return new AndConstraint<NDArrayAssertions>(this);
  190. }
  191. public AndConstraint<NDArrayAssertions> BeOfValues(params object[] values)
  192. {
  193. if (values == null)
  194. throw new ArgumentNullException(nameof(values));
  195. Subject.size.Should().Be((ulong)values.Length, "the method BeOfValues also confirms the sizes are matching with given values.");
  196. #if _REGEN
  197. #region Compute
  198. switch (Subject.typecode)
  199. {
  200. %foreach supported_dtypes,supported_dtypes_lowercase%
  201. case NPTypeCode.#1:
  202. {
  203. var iter = Subject.AsIterator<#2>();
  204. var next = iter.MoveNext;
  205. var hasnext = iter.HasNext;
  206. for (int i = 0; i < values.Length; i++)
  207. {
  208. Execute.Assertion
  209. .ForCondition(hasnext())
  210. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  211. var expected = Convert.To#1(values[i]);
  212. var nextval = next();
  213. Execute.Assertion
  214. .ForCondition(expected == nextval)
  215. .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);
  216. }
  217. break;
  218. }
  219. %
  220. default:
  221. throw new NotSupportedException();
  222. }
  223. #endregion
  224. #else
  225. #region Compute
  226. switch (Subject.dtype)
  227. {
  228. case TF_DataType.TF_BOOL:
  229. {
  230. var iter = Subject.AsIterator<bool>();
  231. var hasnext = iter.HasNext;
  232. for (int i = 0; i < values.Length; i++)
  233. {
  234. Execute.Assertion
  235. .ForCondition(hasnext())
  236. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  237. var expected = Convert.ToBoolean(values[i]);
  238. /*var nextval = iter.MoveNext();
  239. Execute.Assertion
  240. .ForCondition(expected == nextval)
  241. .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);*/
  242. }
  243. break;
  244. }
  245. case TF_DataType.TF_INT8:
  246. {
  247. var iter = Subject.AsIterator<byte>();
  248. /*var next = iter.MoveNext;
  249. var hasnext = iter.HasNext;
  250. for (int i = 0; i < values.Length; i++)
  251. {
  252. Execute.Assertion
  253. .ForCondition(hasnext())
  254. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  255. var expected = Convert.ToByte(values[i]);
  256. var nextval = next();
  257. Execute.Assertion
  258. .ForCondition(expected == nextval)
  259. .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);
  260. }*/
  261. break;
  262. }
  263. case TF_DataType.TF_INT16:
  264. {
  265. var iter = Subject.AsIterator<short>();
  266. /*var next = iter.MoveNext;
  267. var hasnext = iter.HasNext;
  268. for (int i = 0; i < values.Length; i++)
  269. {
  270. Execute.Assertion
  271. .ForCondition(hasnext())
  272. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  273. var expected = Convert.ToInt16(values[i]);
  274. var nextval = next();
  275. Execute.Assertion
  276. .ForCondition(expected == nextval)
  277. .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);
  278. }*/
  279. break;
  280. }
  281. case TF_DataType.TF_UINT16:
  282. {
  283. var iter = Subject.AsIterator<ushort>();
  284. /*var next = iter.MoveNext;
  285. var hasnext = iter.HasNext;
  286. for (int i = 0; i < values.Length; i++)
  287. {
  288. Execute.Assertion
  289. .ForCondition(hasnext())
  290. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  291. var expected = Convert.ToUInt16(values[i]);
  292. var nextval = next();
  293. Execute.Assertion
  294. .ForCondition(expected == nextval)
  295. .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);
  296. }*/
  297. break;
  298. }
  299. case TF_DataType.TF_INT32:
  300. {
  301. var iter = Subject.AsIterator<int>();
  302. /*var next = iter.MoveNext;
  303. var hasnext = iter.HasNext;
  304. for (int i = 0; i < values.Length; i++)
  305. {
  306. Execute.Assertion
  307. .ForCondition(hasnext())
  308. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  309. var expected = Convert.ToInt32(values[i]);
  310. var nextval = next();
  311. Execute.Assertion
  312. .ForCondition(expected == nextval)
  313. .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);
  314. }*/
  315. break;
  316. }
  317. case TF_DataType.TF_UINT32:
  318. {
  319. var iter = Subject.AsIterator<uint>();
  320. /*var next = iter.MoveNext;
  321. var hasnext = iter.HasNext;
  322. for (int i = 0; i < values.Length; i++)
  323. {
  324. Execute.Assertion
  325. .ForCondition(hasnext())
  326. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  327. var expected = Convert.ToUInt32(values[i]);
  328. var nextval = next();
  329. Execute.Assertion
  330. .ForCondition(expected == nextval)
  331. .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);
  332. }*/
  333. break;
  334. }
  335. case TF_DataType.TF_INT64:
  336. {
  337. var iter = Subject.AsIterator<long>();
  338. /*var next = iter.MoveNext;
  339. var hasnext = iter.HasNext;
  340. for (int i = 0; i < values.Length; i++)
  341. {
  342. Execute.Assertion
  343. .ForCondition(hasnext())
  344. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  345. var expected = Convert.ToInt64(values[i]);
  346. var nextval = next();
  347. Execute.Assertion
  348. .ForCondition(expected == nextval)
  349. .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);
  350. }*/
  351. break;
  352. }
  353. case TF_DataType.TF_UINT64:
  354. {
  355. var iter = Subject.AsIterator<ulong>();
  356. /*var next = iter.MoveNext;
  357. var hasnext = iter.HasNext;
  358. for (int i = 0; i < values.Length; i++)
  359. {
  360. Execute.Assertion
  361. .ForCondition(hasnext())
  362. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  363. var expected = Convert.ToUInt64(values[i]);
  364. var nextval = next();
  365. Execute.Assertion
  366. .ForCondition(expected == nextval)
  367. .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);
  368. }*/
  369. break;
  370. }
  371. case TF_DataType.TF_UINT8:
  372. {
  373. var iter = Subject.AsIterator<char>();
  374. /*var next = iter.MoveNext;
  375. var hasnext = iter.HasNext;
  376. for (int i = 0; i < values.Length; i++)
  377. {
  378. Execute.Assertion
  379. .ForCondition(hasnext())
  380. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  381. var expected = Convert.ToChar(values[i]);
  382. var nextval = next();
  383. Execute.Assertion
  384. .ForCondition(expected == nextval)
  385. .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);
  386. }*/
  387. break;
  388. }
  389. case TF_DataType.TF_DOUBLE:
  390. {
  391. var iter = Subject.AsIterator<double>();
  392. /*var next = iter.MoveNext;
  393. var hasnext = iter.HasNext;
  394. for (int i = 0; i < values.Length; i++)
  395. {
  396. Execute.Assertion
  397. .ForCondition(hasnext())
  398. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  399. var expected = Convert.ToDouble(values[i]);
  400. var nextval = next();
  401. Execute.Assertion
  402. .ForCondition(expected == nextval)
  403. .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);
  404. }*/
  405. break;
  406. }
  407. case TF_DataType.TF_FLOAT:
  408. {
  409. var iter = Subject.AsIterator<float>();
  410. /*var next = iter.MoveNext;
  411. var hasnext = iter.HasNext;
  412. for (int i = 0; i < values.Length; i++)
  413. {
  414. Execute.Assertion
  415. .ForCondition(hasnext())
  416. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  417. var expected = Convert.ToSingle(values[i]);
  418. var nextval = next();
  419. Execute.Assertion
  420. .ForCondition(expected == nextval)
  421. .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);
  422. }*/
  423. break;
  424. }
  425. default:
  426. throw new NotSupportedException();
  427. }
  428. #endregion
  429. #endif
  430. return new AndConstraint<NDArrayAssertions>(this);
  431. }
  432. public AndConstraint<NDArrayAssertions> AllValuesBe(object val)
  433. {
  434. #region Compute
  435. /*switch (Subject.typecode)
  436. {
  437. case NPTypeCode.Boolean:
  438. {
  439. var iter = Subject.AsIterator<bool>();
  440. var next = iter.MoveNext;
  441. var hasnext = iter.HasNext;
  442. var expected = Convert.ToBoolean(val);
  443. for (int i = 0; hasnext(); i++)
  444. {
  445. var nextval = next();
  446. Execute.Assertion
  447. .ForCondition(expected == nextval)
  448. .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);
  449. }
  450. break;
  451. }
  452. case NPTypeCode.Byte:
  453. {
  454. var iter = Subject.AsIterator<byte>();
  455. var next = iter.MoveNext;
  456. var hasnext = iter.HasNext;
  457. var expected = Convert.ToByte(val);
  458. for (int i = 0; hasnext(); i++)
  459. {
  460. var nextval = next();
  461. Execute.Assertion
  462. .ForCondition(expected == nextval)
  463. .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);
  464. }
  465. break;
  466. }
  467. case NPTypeCode.Int16:
  468. {
  469. var iter = Subject.AsIterator<short>();
  470. var next = iter.MoveNext;
  471. var hasnext = iter.HasNext;
  472. var expected = Convert.ToInt16(val);
  473. for (int i = 0; hasnext(); i++)
  474. {
  475. var nextval = next();
  476. Execute.Assertion
  477. .ForCondition(expected == nextval)
  478. .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);
  479. }
  480. break;
  481. }
  482. case NPTypeCode.UInt16:
  483. {
  484. var iter = Subject.AsIterator<ushort>();
  485. var next = iter.MoveNext;
  486. var hasnext = iter.HasNext;
  487. var expected = Convert.ToUInt16(val);
  488. for (int i = 0; hasnext(); i++)
  489. {
  490. var nextval = next();
  491. Execute.Assertion
  492. .ForCondition(expected == nextval)
  493. .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);
  494. }
  495. break;
  496. }
  497. case NPTypeCode.Int32:
  498. {
  499. var iter = Subject.AsIterator<int>();
  500. var next = iter.MoveNext;
  501. var hasnext = iter.HasNext;
  502. var expected = Convert.ToInt32(val);
  503. for (int i = 0; hasnext(); i++)
  504. {
  505. var nextval = next();
  506. Execute.Assertion
  507. .ForCondition(expected == nextval)
  508. .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);
  509. }
  510. break;
  511. }
  512. case NPTypeCode.UInt32:
  513. {
  514. var iter = Subject.AsIterator<uint>();
  515. var next = iter.MoveNext;
  516. var hasnext = iter.HasNext;
  517. var expected = Convert.ToUInt32(val);
  518. for (int i = 0; hasnext(); i++)
  519. {
  520. var nextval = next();
  521. Execute.Assertion
  522. .ForCondition(expected == nextval)
  523. .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);
  524. }
  525. break;
  526. }
  527. case NPTypeCode.Int64:
  528. {
  529. var iter = Subject.AsIterator<long>();
  530. var next = iter.MoveNext;
  531. var hasnext = iter.HasNext;
  532. var expected = Convert.ToInt64(val);
  533. for (int i = 0; hasnext(); i++)
  534. {
  535. var nextval = next();
  536. Execute.Assertion
  537. .ForCondition(expected == nextval)
  538. .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);
  539. }
  540. break;
  541. }
  542. case NPTypeCode.UInt64:
  543. {
  544. var iter = Subject.AsIterator<ulong>();
  545. var next = iter.MoveNext;
  546. var hasnext = iter.HasNext;
  547. var expected = Convert.ToUInt64(val);
  548. for (int i = 0; hasnext(); i++)
  549. {
  550. var nextval = next();
  551. Execute.Assertion
  552. .ForCondition(expected == nextval)
  553. .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);
  554. }
  555. break;
  556. }
  557. case NPTypeCode.Char:
  558. {
  559. var iter = Subject.AsIterator<char>();
  560. var next = iter.MoveNext;
  561. var hasnext = iter.HasNext;
  562. var expected = Convert.ToChar(val);
  563. for (int i = 0; hasnext(); i++)
  564. {
  565. var nextval = next();
  566. Execute.Assertion
  567. .ForCondition(expected == nextval)
  568. .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);
  569. }
  570. break;
  571. }
  572. case NPTypeCode.Double:
  573. {
  574. var iter = Subject.AsIterator<double>();
  575. var next = iter.MoveNext;
  576. var hasnext = iter.HasNext;
  577. var expected = Convert.ToDouble(val);
  578. for (int i = 0; hasnext(); i++)
  579. {
  580. var nextval = next();
  581. Execute.Assertion
  582. .ForCondition(expected == nextval)
  583. .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);
  584. }
  585. break;
  586. }
  587. case NPTypeCode.Single:
  588. {
  589. var iter = Subject.AsIterator<float>();
  590. var next = iter.MoveNext;
  591. var hasnext = iter.HasNext;
  592. var expected = Convert.ToSingle(val);
  593. for (int i = 0; hasnext(); i++)
  594. {
  595. var nextval = next();
  596. Execute.Assertion
  597. .ForCondition(expected == nextval)
  598. .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);
  599. }
  600. break;
  601. }
  602. case NPTypeCode.Decimal:
  603. {
  604. var iter = Subject.AsIterator<decimal>();
  605. var next = iter.MoveNext;
  606. var hasnext = iter.HasNext;
  607. var expected = Convert.ToDecimal(val);
  608. for (int i = 0; hasnext(); i++)
  609. {
  610. var nextval = next();
  611. Execute.Assertion
  612. .ForCondition(expected == nextval)
  613. .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);
  614. }
  615. break;
  616. }
  617. default:
  618. throw new NotSupportedException();
  619. }*/
  620. #endregion
  621. return new AndConstraint<NDArrayAssertions>(this);
  622. }
  623. public AndConstraint<NDArrayAssertions> BeOfValuesApproximately(double sensitivity, params object[] values)
  624. {
  625. if (values == null)
  626. throw new ArgumentNullException(nameof(values));
  627. Subject.size.Should().Be((ulong)values.Length, "the method BeOfValuesApproximately also confirms the sizes are matching with given values.");
  628. #region Compute
  629. /*switch (Subject.typecode)
  630. {
  631. case NPTypeCode.Boolean:
  632. {
  633. var iter = Subject.AsIterator<bool>();
  634. var next = iter.MoveNext;
  635. var hasnext = iter.HasNext;
  636. for (int i = 0; i < values.Length; i++)
  637. {
  638. Execute.Assertion
  639. .ForCondition(hasnext())
  640. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  641. var expected = Convert.ToBoolean(values[i]);
  642. var nextval = next();
  643. Execute.Assertion
  644. .ForCondition(expected == nextval)
  645. .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);
  646. }
  647. break;
  648. }
  649. case NPTypeCode.Byte:
  650. {
  651. var iter = Subject.AsIterator<byte>();
  652. var next = iter.MoveNext;
  653. var hasnext = iter.HasNext;
  654. for (int i = 0; i < values.Length; i++)
  655. {
  656. Execute.Assertion
  657. .ForCondition(hasnext())
  658. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  659. var expected = Convert.ToByte(values[i]);
  660. var nextval = next();
  661. Execute.Assertion
  662. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  663. .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);
  664. }
  665. break;
  666. }
  667. case NPTypeCode.Int16:
  668. {
  669. var iter = Subject.AsIterator<short>();
  670. var next = iter.MoveNext;
  671. var hasnext = iter.HasNext;
  672. for (int i = 0; i < values.Length; i++)
  673. {
  674. Execute.Assertion
  675. .ForCondition(hasnext())
  676. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  677. var expected = Convert.ToInt16(values[i]);
  678. var nextval = next();
  679. Execute.Assertion
  680. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  681. .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);
  682. }
  683. break;
  684. }
  685. case NPTypeCode.UInt16:
  686. {
  687. var iter = Subject.AsIterator<ushort>();
  688. var next = iter.MoveNext;
  689. var hasnext = iter.HasNext;
  690. for (int i = 0; i < values.Length; i++)
  691. {
  692. Execute.Assertion
  693. .ForCondition(hasnext())
  694. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  695. var expected = Convert.ToUInt16(values[i]);
  696. var nextval = next();
  697. Execute.Assertion
  698. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  699. .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);
  700. }
  701. break;
  702. }
  703. case NPTypeCode.Int32:
  704. {
  705. var iter = Subject.AsIterator<int>();
  706. var next = iter.MoveNext;
  707. var hasnext = iter.HasNext;
  708. for (int i = 0; i < values.Length; i++)
  709. {
  710. Execute.Assertion
  711. .ForCondition(hasnext())
  712. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  713. var expected = Convert.ToInt32(values[i]);
  714. var nextval = next();
  715. Execute.Assertion
  716. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  717. .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);
  718. }
  719. break;
  720. }
  721. case NPTypeCode.UInt32:
  722. {
  723. var iter = Subject.AsIterator<uint>();
  724. var next = iter.MoveNext;
  725. var hasnext = iter.HasNext;
  726. for (int i = 0; i < values.Length; i++)
  727. {
  728. Execute.Assertion
  729. .ForCondition(hasnext())
  730. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  731. var expected = Convert.ToUInt32(values[i]);
  732. var nextval = next();
  733. Execute.Assertion
  734. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  735. .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);
  736. }
  737. break;
  738. }
  739. case NPTypeCode.Int64:
  740. {
  741. var iter = Subject.AsIterator<long>();
  742. var next = iter.MoveNext;
  743. var hasnext = iter.HasNext;
  744. for (int i = 0; i < values.Length; i++)
  745. {
  746. Execute.Assertion
  747. .ForCondition(hasnext())
  748. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  749. var expected = Convert.ToInt64(values[i]);
  750. var nextval = next();
  751. Execute.Assertion
  752. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  753. .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);
  754. }
  755. break;
  756. }
  757. case NPTypeCode.UInt64:
  758. {
  759. var iter = Subject.AsIterator<ulong>();
  760. var next = iter.MoveNext;
  761. var hasnext = iter.HasNext;
  762. for (int i = 0; i < values.Length; i++)
  763. {
  764. Execute.Assertion
  765. .ForCondition(hasnext())
  766. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  767. var expected = Convert.ToUInt64(values[i]);
  768. var nextval = next();
  769. Execute.Assertion
  770. .ForCondition(Math.Abs((double)(expected - nextval)) <= sensitivity)
  771. .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);
  772. }
  773. break;
  774. }
  775. case NPTypeCode.Char:
  776. {
  777. var iter = Subject.AsIterator<char>();
  778. var next = iter.MoveNext;
  779. var hasnext = iter.HasNext;
  780. for (int i = 0; i < values.Length; i++)
  781. {
  782. Execute.Assertion
  783. .ForCondition(hasnext())
  784. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  785. var expected = Convert.ToChar(values[i]);
  786. var nextval = next();
  787. Execute.Assertion
  788. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  789. .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);
  790. }
  791. break;
  792. }
  793. case NPTypeCode.Double:
  794. {
  795. var iter = Subject.AsIterator<double>();
  796. var next = iter.MoveNext;
  797. var hasnext = iter.HasNext;
  798. for (int i = 0; i < values.Length; i++)
  799. {
  800. Execute.Assertion
  801. .ForCondition(hasnext())
  802. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  803. var expected = Convert.ToDouble(values[i]);
  804. var nextval = next();
  805. Execute.Assertion
  806. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  807. .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);
  808. }
  809. break;
  810. }
  811. case NPTypeCode.Single:
  812. {
  813. var iter = Subject.AsIterator<float>();
  814. var next = iter.MoveNext;
  815. var hasnext = iter.HasNext;
  816. for (int i = 0; i < values.Length; i++)
  817. {
  818. Execute.Assertion
  819. .ForCondition(hasnext())
  820. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  821. var expected = Convert.ToSingle(values[i]);
  822. var nextval = next();
  823. Execute.Assertion
  824. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  825. .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);
  826. }
  827. break;
  828. }
  829. case NPTypeCode.Decimal:
  830. {
  831. var iter = Subject.AsIterator<decimal>();
  832. var next = iter.MoveNext;
  833. var hasnext = iter.HasNext;
  834. for (int i = 0; i < values.Length; i++)
  835. {
  836. Execute.Assertion
  837. .ForCondition(hasnext())
  838. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  839. var expected = Convert.ToDecimal(values[i]);
  840. var nextval = next();
  841. Execute.Assertion
  842. .ForCondition(Math.Abs(expected - nextval) <= (decimal)sensitivity)
  843. .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);
  844. }
  845. break;
  846. }
  847. default:
  848. throw new NotSupportedException();
  849. }*/
  850. #endregion
  851. return new AndConstraint<NDArrayAssertions>(this);
  852. }
  853. }
  854. }