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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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> BeOfType(Type typeCode)
  164. {
  165. Subject.dtype.Should().Be(typeCode);
  166. return new AndConstraint<NDArrayAssertions>(this);
  167. }
  168. public AndConstraint<NDArrayAssertions> NotBeScalar()
  169. {
  170. Subject.shape.IsScalar.Should().BeFalse();
  171. return new AndConstraint<NDArrayAssertions>(this);
  172. }
  173. public AndConstraint<NDArrayAssertions> BeNDim(int ndim)
  174. {
  175. Subject.ndim.Should().Be(ndim);
  176. return new AndConstraint<NDArrayAssertions>(this);
  177. }
  178. public AndConstraint<NDArrayAssertions> Be(NDArray expected)
  179. {
  180. Execute.Assertion
  181. .ForCondition(np.array_equal(Subject, expected))
  182. .FailWith($"Expected the subject and other ndarray to be equals.\n------- Subject -------\n{Subject}\n------- Expected -------\n{expected}");
  183. return new AndConstraint<NDArrayAssertions>(this);
  184. }
  185. public AndConstraint<NDArrayAssertions> AllValuesBe(object val)
  186. {
  187. #region Compute
  188. /*switch (Subject.typecode)
  189. {
  190. case NPTypeCode.Boolean:
  191. {
  192. var iter = Subject.AsIterator<bool>();
  193. var next = iter.MoveNext;
  194. var hasnext = iter.HasNext;
  195. var expected = Convert.ToBoolean(val);
  196. for (int i = 0; hasnext(); i++)
  197. {
  198. var nextval = next();
  199. Execute.Assertion
  200. .ForCondition(expected == nextval)
  201. .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);
  202. }
  203. break;
  204. }
  205. case NPTypeCode.Byte:
  206. {
  207. var iter = Subject.AsIterator<byte>();
  208. var next = iter.MoveNext;
  209. var hasnext = iter.HasNext;
  210. var expected = Convert.ToByte(val);
  211. for (int i = 0; hasnext(); i++)
  212. {
  213. var nextval = next();
  214. Execute.Assertion
  215. .ForCondition(expected == nextval)
  216. .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);
  217. }
  218. break;
  219. }
  220. case NPTypeCode.Int16:
  221. {
  222. var iter = Subject.AsIterator<short>();
  223. var next = iter.MoveNext;
  224. var hasnext = iter.HasNext;
  225. var expected = Convert.ToInt16(val);
  226. for (int i = 0; hasnext(); i++)
  227. {
  228. var nextval = next();
  229. Execute.Assertion
  230. .ForCondition(expected == nextval)
  231. .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);
  232. }
  233. break;
  234. }
  235. case NPTypeCode.UInt16:
  236. {
  237. var iter = Subject.AsIterator<ushort>();
  238. var next = iter.MoveNext;
  239. var hasnext = iter.HasNext;
  240. var expected = Convert.ToUInt16(val);
  241. for (int i = 0; hasnext(); i++)
  242. {
  243. var nextval = next();
  244. Execute.Assertion
  245. .ForCondition(expected == nextval)
  246. .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);
  247. }
  248. break;
  249. }
  250. case NPTypeCode.Int32:
  251. {
  252. var iter = Subject.AsIterator<int>();
  253. var next = iter.MoveNext;
  254. var hasnext = iter.HasNext;
  255. var expected = Convert.ToInt32(val);
  256. for (int i = 0; hasnext(); i++)
  257. {
  258. var nextval = next();
  259. Execute.Assertion
  260. .ForCondition(expected == nextval)
  261. .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);
  262. }
  263. break;
  264. }
  265. case NPTypeCode.UInt32:
  266. {
  267. var iter = Subject.AsIterator<uint>();
  268. var next = iter.MoveNext;
  269. var hasnext = iter.HasNext;
  270. var expected = Convert.ToUInt32(val);
  271. for (int i = 0; hasnext(); i++)
  272. {
  273. var nextval = next();
  274. Execute.Assertion
  275. .ForCondition(expected == nextval)
  276. .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);
  277. }
  278. break;
  279. }
  280. case NPTypeCode.Int64:
  281. {
  282. var iter = Subject.AsIterator<long>();
  283. var next = iter.MoveNext;
  284. var hasnext = iter.HasNext;
  285. var expected = Convert.ToInt64(val);
  286. for (int i = 0; hasnext(); i++)
  287. {
  288. var nextval = next();
  289. Execute.Assertion
  290. .ForCondition(expected == nextval)
  291. .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);
  292. }
  293. break;
  294. }
  295. case NPTypeCode.UInt64:
  296. {
  297. var iter = Subject.AsIterator<ulong>();
  298. var next = iter.MoveNext;
  299. var hasnext = iter.HasNext;
  300. var expected = Convert.ToUInt64(val);
  301. for (int i = 0; hasnext(); i++)
  302. {
  303. var nextval = next();
  304. Execute.Assertion
  305. .ForCondition(expected == nextval)
  306. .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);
  307. }
  308. break;
  309. }
  310. case NPTypeCode.Char:
  311. {
  312. var iter = Subject.AsIterator<char>();
  313. var next = iter.MoveNext;
  314. var hasnext = iter.HasNext;
  315. var expected = Convert.ToChar(val);
  316. for (int i = 0; hasnext(); i++)
  317. {
  318. var nextval = next();
  319. Execute.Assertion
  320. .ForCondition(expected == nextval)
  321. .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);
  322. }
  323. break;
  324. }
  325. case NPTypeCode.Double:
  326. {
  327. var iter = Subject.AsIterator<double>();
  328. var next = iter.MoveNext;
  329. var hasnext = iter.HasNext;
  330. var expected = Convert.ToDouble(val);
  331. for (int i = 0; hasnext(); i++)
  332. {
  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: Double).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n{val}", expected, nextval, i);
  337. }
  338. break;
  339. }
  340. case NPTypeCode.Single:
  341. {
  342. var iter = Subject.AsIterator<float>();
  343. var next = iter.MoveNext;
  344. var hasnext = iter.HasNext;
  345. var expected = Convert.ToSingle(val);
  346. for (int i = 0; hasnext(); i++)
  347. {
  348. var nextval = next();
  349. Execute.Assertion
  350. .ForCondition(expected == nextval)
  351. .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);
  352. }
  353. break;
  354. }
  355. case NPTypeCode.Decimal:
  356. {
  357. var iter = Subject.AsIterator<decimal>();
  358. var next = iter.MoveNext;
  359. var hasnext = iter.HasNext;
  360. var expected = Convert.ToDecimal(val);
  361. for (int i = 0; hasnext(); i++)
  362. {
  363. var nextval = next();
  364. Execute.Assertion
  365. .ForCondition(expected == nextval)
  366. .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);
  367. }
  368. break;
  369. }
  370. default:
  371. throw new NotSupportedException();
  372. }*/
  373. #endregion
  374. return new AndConstraint<NDArrayAssertions>(this);
  375. }
  376. public AndConstraint<NDArrayAssertions> BeOfValuesApproximately(double sensitivity, params object[] values)
  377. {
  378. if (values == null)
  379. throw new ArgumentNullException(nameof(values));
  380. Subject.size.Should().Be((ulong)values.Length, "the method BeOfValuesApproximately also confirms the sizes are matching with given values.");
  381. #region Compute
  382. /*switch (Subject.typecode)
  383. {
  384. case NPTypeCode.Boolean:
  385. {
  386. var iter = Subject.AsIterator<bool>();
  387. var next = iter.MoveNext;
  388. var hasnext = iter.HasNext;
  389. for (int i = 0; i < values.Length; i++)
  390. {
  391. Execute.Assertion
  392. .ForCondition(hasnext())
  393. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  394. var expected = Convert.ToBoolean(values[i]);
  395. var nextval = next();
  396. Execute.Assertion
  397. .ForCondition(expected == nextval)
  398. .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);
  399. }
  400. break;
  401. }
  402. case NPTypeCode.Byte:
  403. {
  404. var iter = Subject.AsIterator<byte>();
  405. var next = iter.MoveNext;
  406. var hasnext = iter.HasNext;
  407. for (int i = 0; i < values.Length; i++)
  408. {
  409. Execute.Assertion
  410. .ForCondition(hasnext())
  411. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  412. var expected = Convert.ToByte(values[i]);
  413. var nextval = next();
  414. Execute.Assertion
  415. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  416. .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);
  417. }
  418. break;
  419. }
  420. case NPTypeCode.Int16:
  421. {
  422. var iter = Subject.AsIterator<short>();
  423. var next = iter.MoveNext;
  424. var hasnext = iter.HasNext;
  425. for (int i = 0; i < values.Length; i++)
  426. {
  427. Execute.Assertion
  428. .ForCondition(hasnext())
  429. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  430. var expected = Convert.ToInt16(values[i]);
  431. var nextval = next();
  432. Execute.Assertion
  433. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  434. .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);
  435. }
  436. break;
  437. }
  438. case NPTypeCode.UInt16:
  439. {
  440. var iter = Subject.AsIterator<ushort>();
  441. var next = iter.MoveNext;
  442. var hasnext = iter.HasNext;
  443. for (int i = 0; i < values.Length; i++)
  444. {
  445. Execute.Assertion
  446. .ForCondition(hasnext())
  447. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  448. var expected = Convert.ToUInt16(values[i]);
  449. var nextval = next();
  450. Execute.Assertion
  451. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  452. .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);
  453. }
  454. break;
  455. }
  456. case NPTypeCode.Int32:
  457. {
  458. var iter = Subject.AsIterator<int>();
  459. var next = iter.MoveNext;
  460. var hasnext = iter.HasNext;
  461. for (int i = 0; i < values.Length; i++)
  462. {
  463. Execute.Assertion
  464. .ForCondition(hasnext())
  465. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  466. var expected = Convert.ToInt32(values[i]);
  467. var nextval = next();
  468. Execute.Assertion
  469. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  470. .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);
  471. }
  472. break;
  473. }
  474. case NPTypeCode.UInt32:
  475. {
  476. var iter = Subject.AsIterator<uint>();
  477. var next = iter.MoveNext;
  478. var hasnext = iter.HasNext;
  479. for (int i = 0; i < values.Length; i++)
  480. {
  481. Execute.Assertion
  482. .ForCondition(hasnext())
  483. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  484. var expected = Convert.ToUInt32(values[i]);
  485. var nextval = next();
  486. Execute.Assertion
  487. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  488. .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);
  489. }
  490. break;
  491. }
  492. case NPTypeCode.Int64:
  493. {
  494. var iter = Subject.AsIterator<long>();
  495. var next = iter.MoveNext;
  496. var hasnext = iter.HasNext;
  497. for (int i = 0; i < values.Length; i++)
  498. {
  499. Execute.Assertion
  500. .ForCondition(hasnext())
  501. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  502. var expected = Convert.ToInt64(values[i]);
  503. var nextval = next();
  504. Execute.Assertion
  505. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  506. .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);
  507. }
  508. break;
  509. }
  510. case NPTypeCode.UInt64:
  511. {
  512. var iter = Subject.AsIterator<ulong>();
  513. var next = iter.MoveNext;
  514. var hasnext = iter.HasNext;
  515. for (int i = 0; i < values.Length; i++)
  516. {
  517. Execute.Assertion
  518. .ForCondition(hasnext())
  519. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  520. var expected = Convert.ToUInt64(values[i]);
  521. var nextval = next();
  522. Execute.Assertion
  523. .ForCondition(Math.Abs((double)(expected - nextval)) <= sensitivity)
  524. .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);
  525. }
  526. break;
  527. }
  528. case NPTypeCode.Char:
  529. {
  530. var iter = Subject.AsIterator<char>();
  531. var next = iter.MoveNext;
  532. var hasnext = iter.HasNext;
  533. for (int i = 0; i < values.Length; i++)
  534. {
  535. Execute.Assertion
  536. .ForCondition(hasnext())
  537. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  538. var expected = Convert.ToChar(values[i]);
  539. var nextval = next();
  540. Execute.Assertion
  541. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  542. .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);
  543. }
  544. break;
  545. }
  546. case NPTypeCode.Double:
  547. {
  548. var iter = Subject.AsIterator<double>();
  549. var next = iter.MoveNext;
  550. var hasnext = iter.HasNext;
  551. for (int i = 0; i < values.Length; i++)
  552. {
  553. Execute.Assertion
  554. .ForCondition(hasnext())
  555. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  556. var expected = Convert.ToDouble(values[i]);
  557. var nextval = next();
  558. Execute.Assertion
  559. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  560. .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);
  561. }
  562. break;
  563. }
  564. case NPTypeCode.Single:
  565. {
  566. var iter = Subject.AsIterator<float>();
  567. var next = iter.MoveNext;
  568. var hasnext = iter.HasNext;
  569. for (int i = 0; i < values.Length; i++)
  570. {
  571. Execute.Assertion
  572. .ForCondition(hasnext())
  573. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  574. var expected = Convert.ToSingle(values[i]);
  575. var nextval = next();
  576. Execute.Assertion
  577. .ForCondition(Math.Abs(expected - nextval) <= sensitivity)
  578. .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);
  579. }
  580. break;
  581. }
  582. case NPTypeCode.Decimal:
  583. {
  584. var iter = Subject.AsIterator<decimal>();
  585. var next = iter.MoveNext;
  586. var hasnext = iter.HasNext;
  587. for (int i = 0; i < values.Length; i++)
  588. {
  589. Execute.Assertion
  590. .ForCondition(hasnext())
  591. .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}.");
  592. var expected = Convert.ToDecimal(values[i]);
  593. var nextval = next();
  594. Execute.Assertion
  595. .ForCondition(Math.Abs(expected - nextval) <= (decimal)sensitivity)
  596. .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);
  597. }
  598. break;
  599. }
  600. default:
  601. throw new NotSupportedException();
  602. }*/
  603. #endregion
  604. return new AndConstraint<NDArrayAssertions>(this);
  605. }
  606. }
  607. }