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

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