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

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