Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using Unity.Mathematics;
  7. namespace Burst.Compiler.IL.Tests.Helpers
  8. {
  9. [Flags]
  10. internal enum DataRange
  11. {
  12. // Standard Test (Zero, Minus100To100, Inf, Nan)
  13. Standard = Zero | Minus100To100 | Inf | NaN | HighIntRange,
  14. // Standard Test (Zero, ZeroExclusiveTo100, Inf, Nan)
  15. StandardPositive = Zero | ZeroExclusiveTo100 | Inf | NaN,
  16. StandardPositiveExclusiveZero = ZeroExclusiveTo100 | Inf | NaN,
  17. // Standard Between -1 and 1 (Zero, MinusOneInclusiveToOneInclusive, Inf, Nan)
  18. Standard11 = Zero | MinusOneInclusiveToOneInclusive | Inf | NaN,
  19. Zero = 1 << 1,
  20. ZeroExclusiveToOneInclusive = 1 << 2,
  21. MinusOneInclusiveToOneInclusive = 1 << 3,
  22. Minus100To100 = 1 << 4,
  23. ZeroExclusiveTo100 = 1 << 5,
  24. Inf = 1 << 6,
  25. NaN = 1 << 7,
  26. HighIntRange = 1 << 8,
  27. ZeroInclusiveToFifteenInclusive = 1 << 9
  28. }
  29. internal static class DataRangeExtensions
  30. {
  31. private const int VectorsCount = 6;
  32. private static bool IsIntegerType(Type type)
  33. {
  34. if (type == typeof(byte)) return true;
  35. if (type == typeof(sbyte)) return true;
  36. if (type == typeof(short)) return true;
  37. if (type == typeof(ushort)) return true;
  38. if (type == typeof(int)) return true;
  39. if (type == typeof(uint)) return true;
  40. if (type == typeof(long)) return true;
  41. if (type == typeof(ulong)) return true;
  42. return false;
  43. }
  44. private static bool IsSignedIntegerType(Type type)
  45. {
  46. if (type == typeof(sbyte)) return true;
  47. if (type == typeof(short)) return true;
  48. if (type == typeof(int)) return true;
  49. if (type == typeof(long)) return true;
  50. return false;
  51. }
  52. public static IEnumerable<object> ExpandRange(this DataRange dataRange, Type type, int seed)
  53. {
  54. if (IsIntegerType(type))
  55. {
  56. var isSigned = IsSignedIntegerType(type);
  57. foreach (var value in ExpandRange(dataRange & ~(DataRange.Inf | DataRange.NaN), typeof(double), seed))
  58. {
  59. var d = (double)value;
  60. if (!isSigned && (d < 0.0))
  61. {
  62. continue;
  63. }
  64. if ((dataRange & DataRange.Zero) == 0 && (int)d == 0)
  65. {
  66. continue;
  67. }
  68. yield return Convert.ChangeType(d, type);
  69. }
  70. if (0 != (dataRange & DataRange.HighIntRange))
  71. {
  72. double rangeLow = 100;
  73. double rangeHigh = 101;
  74. if (type == typeof(byte)) rangeHigh = byte.MaxValue;
  75. if (type == typeof(sbyte)) rangeHigh = sbyte.MaxValue;
  76. if (type == typeof(short)) rangeHigh = short.MaxValue;
  77. if (type == typeof(ushort)) rangeHigh = ushort.MaxValue;
  78. if (type == typeof(int)) rangeHigh = int.MaxValue;
  79. if (type == typeof(uint)) rangeHigh = uint.MaxValue;
  80. if (type == typeof(long)) rangeHigh = long.MaxValue;
  81. if (type == typeof(ulong)) rangeHigh = ulong.MaxValue;
  82. var random = new System.Random(seed);
  83. int total = 8;
  84. if (!isSigned)
  85. {
  86. total *= 2;
  87. }
  88. for (int i = 0; i < total; i++)
  89. {
  90. var next = random.NextDouble();
  91. var d = rangeLow + (rangeHigh - rangeLow) * next;
  92. yield return Convert.ChangeType(d, type);
  93. if (isSigned)
  94. {
  95. yield return Convert.ChangeType(-d, type);
  96. }
  97. }
  98. }
  99. }
  100. else if (type == typeof(bool))
  101. {
  102. yield return true;
  103. yield return false;
  104. }
  105. else if (type == typeof(float))
  106. {
  107. foreach (var value in ExpandRange(dataRange, typeof(double), seed))
  108. {
  109. var d = (double)value;
  110. if (double.IsNaN(d))
  111. {
  112. yield return float.NaN;
  113. }
  114. else if (double.IsPositiveInfinity(d))
  115. {
  116. yield return float.PositiveInfinity;
  117. }
  118. else if (double.IsNegativeInfinity(d))
  119. {
  120. yield return float.NegativeInfinity;
  121. }
  122. else
  123. {
  124. yield return (float)(double)value;
  125. }
  126. }
  127. }
  128. else if (type == typeof(double))
  129. {
  130. if ((dataRange & (DataRange.Minus100To100)) != 0)
  131. {
  132. yield return -100.0;
  133. yield return -77.9;
  134. yield return -50.0;
  135. yield return -36.5;
  136. yield return -9.1;
  137. if ((dataRange & (DataRange.Zero)) != 0)
  138. {
  139. yield return 0.0;
  140. }
  141. yield return 5.1;
  142. yield return 43.5;
  143. yield return 50.0;
  144. yield return 76.8;
  145. yield return 100.0;
  146. if ((dataRange & (DataRange.NaN)) != 0)
  147. {
  148. yield return double.NaN;
  149. }
  150. if ((dataRange & (DataRange.Inf)) != 0)
  151. {
  152. yield return double.PositiveInfinity;
  153. yield return double.NegativeInfinity;
  154. }
  155. }
  156. else if ((dataRange & DataRange.ZeroExclusiveTo100) != 0)
  157. {
  158. foreach (var value in ExpandRange(dataRange | DataRange.Minus100To100, typeof(double), seed))
  159. {
  160. var d = (double)value;
  161. if (double.IsNaN(d) || double.IsInfinity(d))
  162. {
  163. yield return d;
  164. }
  165. else if (d != 0.0)
  166. {
  167. d = d * 0.5 + 50.1;
  168. if (d > 100.0)
  169. {
  170. d = 100.0;
  171. }
  172. yield return d;
  173. }
  174. }
  175. if ((dataRange & (DataRange.Zero)) != 0)
  176. {
  177. yield return 0.0;
  178. }
  179. }
  180. else if ((dataRange & (DataRange.MinusOneInclusiveToOneInclusive)) != 0)
  181. {
  182. foreach (var value in ExpandRange(dataRange | DataRange.Minus100To100, typeof(double), seed))
  183. {
  184. var d = (double)value;
  185. // Return nan/inf as-is
  186. if (double.IsNaN(d) || double.IsInfinity(d) || d == 0.0)
  187. {
  188. yield return d;
  189. }
  190. else
  191. {
  192. yield return d / 100.0;
  193. }
  194. }
  195. }
  196. else if ((dataRange & (DataRange.ZeroExclusiveToOneInclusive)) != 0)
  197. {
  198. foreach (var value in ExpandRange(dataRange | DataRange.ZeroExclusiveTo100, typeof(double), seed))
  199. {
  200. var d = (double)value;
  201. if (double.IsNaN(d) || double.IsInfinity(d) || d == 0.0)
  202. {
  203. yield return d;
  204. }
  205. else
  206. {
  207. yield return d / 100.0;
  208. }
  209. }
  210. }
  211. else if (0 != (dataRange & DataRange.ZeroInclusiveToFifteenInclusive))
  212. {
  213. for (int i = 0; i <= 15; i++)
  214. {
  215. yield return Convert.ChangeType(i, type);
  216. }
  217. }
  218. else
  219. {
  220. throw new NotSupportedException($"Invalid datarange `{dataRange}`: missing either Minus100To100 | MinusOneInclusiveToOneInclusive | ZeroExclusiveToOneInclusive`");
  221. }
  222. }
  223. else if (type.Namespace == "Unity.Mathematics")
  224. {
  225. if (type.IsByRef)
  226. {
  227. type = type.GetElementType();
  228. }
  229. if (type.Name.StartsWith("bool"))
  230. {
  231. var size = (uint)(type.Name["bool".Length] - '0');
  232. var bools = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(bool), seed).OfType<bool>().ToArray();
  233. var indices = Enumerable.Range(0, bools.Length).ToList();
  234. var originalIndices = new List<int>(indices);
  235. var random = new System.Random(seed);
  236. switch (size)
  237. {
  238. case 2:
  239. for (int i = 0; i < VectorsCount; i++)
  240. {
  241. var x = bools[NextIndex(random, indices, originalIndices)];
  242. var y = bools[NextIndex(random, indices, originalIndices)];
  243. yield return new bool2(x, y);
  244. }
  245. break;
  246. case 3:
  247. for (int i = 0; i < VectorsCount; i++)
  248. {
  249. var x = bools[NextIndex(random, indices, originalIndices)];
  250. var y = bools[NextIndex(random, indices, originalIndices)];
  251. var z = bools[NextIndex(random, indices, originalIndices)];
  252. yield return new bool3(x, y, z);
  253. }
  254. break;
  255. case 4:
  256. for (int i = 0; i < VectorsCount; i++)
  257. {
  258. var x = bools[NextIndex(random, indices, originalIndices)];
  259. var y = bools[NextIndex(random, indices, originalIndices)];
  260. var z = bools[NextIndex(random, indices, originalIndices)];
  261. var w = bools[NextIndex(random, indices, originalIndices)];
  262. yield return new bool4(x, y, z, w);
  263. }
  264. break;
  265. default:
  266. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  267. }
  268. }
  269. else if (type.Name.StartsWith("int"))
  270. {
  271. var size = (uint)(type.Name["int".Length] - '0');
  272. var ints = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(int), seed).OfType<int>().ToArray();
  273. var indices = Enumerable.Range(0, ints.Length).ToList();
  274. var originalIndices = new List<int>(indices);
  275. var random = new System.Random(seed);
  276. switch (size)
  277. {
  278. case 2:
  279. for (int i = 0; i < VectorsCount; i++)
  280. {
  281. var x = ints[NextIndex(random, indices, originalIndices)];
  282. var y = ints[NextIndex(random, indices, originalIndices)];
  283. yield return new int2(x, y);
  284. }
  285. break;
  286. case 3:
  287. for (int i = 0; i < VectorsCount; i++)
  288. {
  289. var x = ints[NextIndex(random, indices, originalIndices)];
  290. var y = ints[NextIndex(random, indices, originalIndices)];
  291. var z = ints[NextIndex(random, indices, originalIndices)];
  292. yield return new int3(x, y, z);
  293. }
  294. break;
  295. case 4:
  296. for (int i = 0; i < VectorsCount; i++)
  297. {
  298. var x = ints[NextIndex(random, indices, originalIndices)];
  299. var y = ints[NextIndex(random, indices, originalIndices)];
  300. var z = ints[NextIndex(random, indices, originalIndices)];
  301. var w = ints[NextIndex(random, indices, originalIndices)];
  302. yield return new int4(x, y, z, w);
  303. }
  304. break;
  305. default:
  306. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  307. }
  308. }
  309. else if (type.Name.StartsWith("uint"))
  310. {
  311. var size = (uint)(type.Name["uint".Length] - '0');
  312. var uints = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(uint), seed).OfType<uint>().ToArray();
  313. var indices = Enumerable.Range(0, uints.Length).ToList();
  314. var originalIndices = new List<int>(indices);
  315. var random = new System.Random(seed);
  316. switch (size)
  317. {
  318. case 2:
  319. for (int i = 0; i < VectorsCount; i++)
  320. {
  321. var x = uints[NextIndex(random, indices, originalIndices)];
  322. var y = uints[NextIndex(random, indices, originalIndices)];
  323. yield return new uint2(x, y);
  324. }
  325. break;
  326. case 3:
  327. for (int i = 0; i < VectorsCount; i++)
  328. {
  329. var x = uints[NextIndex(random, indices, originalIndices)];
  330. var y = uints[NextIndex(random, indices, originalIndices)];
  331. var z = uints[NextIndex(random, indices, originalIndices)];
  332. yield return new uint3(x, y, z);
  333. }
  334. break;
  335. case 4:
  336. for (int i = 0; i < VectorsCount; i++)
  337. {
  338. var x = uints[NextIndex(random, indices, originalIndices)];
  339. var y = uints[NextIndex(random, indices, originalIndices)];
  340. var z = uints[NextIndex(random, indices, originalIndices)];
  341. var w = uints[NextIndex(random, indices, originalIndices)];
  342. yield return new uint4(x, y, z, w);
  343. }
  344. break;
  345. default:
  346. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  347. }
  348. }
  349. else if (type.Name.StartsWith("half"))
  350. {
  351. var size = (uint)(type.Name["half".Length] - '0');
  352. var floats = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(float), seed).OfType<float>().ToList();
  353. var originalIndices = Enumerable.Range(0, floats.Count).ToList();
  354. var indices = new List<int>(originalIndices);
  355. // We only put NaN and Inf in the first set of values
  356. if ((dataRange & DataRange.NaN) != 0)
  357. {
  358. indices.Add(floats.Count);
  359. floats.Add(float.NaN);
  360. }
  361. if ((dataRange & DataRange.Inf) != 0)
  362. {
  363. indices.Add(floats.Count);
  364. floats.Add(float.PositiveInfinity);
  365. }
  366. var random = new System.Random(seed);
  367. switch (size)
  368. {
  369. case 2:
  370. for (int i = 0; i < VectorsCount; i++)
  371. {
  372. var x = floats[NextIndex(random, indices, originalIndices)];
  373. var y = floats[NextIndex(random, indices, originalIndices)];
  374. yield return new half2(new float2(x, y));
  375. }
  376. break;
  377. case 3:
  378. for (int i = 0; i < VectorsCount; i++)
  379. {
  380. var x = floats[NextIndex(random, indices, originalIndices)];
  381. var y = floats[NextIndex(random, indices, originalIndices)];
  382. var z = floats[NextIndex(random, indices, originalIndices)];
  383. yield return new half3(new float3(x, y, z));
  384. }
  385. break;
  386. case 4:
  387. for (int i = 0; i < VectorsCount; i++)
  388. {
  389. var x = floats[NextIndex(random, indices, originalIndices)];
  390. var y = floats[NextIndex(random, indices, originalIndices)];
  391. var z = floats[NextIndex(random, indices, originalIndices)];
  392. var w = floats[NextIndex(random, indices, originalIndices)];
  393. yield return new half4(new float4(x, y, z, w));
  394. }
  395. break;
  396. default:
  397. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  398. }
  399. }
  400. else if (type.Name.StartsWith("float"))
  401. {
  402. var size = (uint)(type.Name["float".Length] - '0');
  403. var floats = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(float), seed).OfType<float>().ToList();
  404. var originalIndices = Enumerable.Range(0, floats.Count).ToList();
  405. var indices = new List<int>(originalIndices);
  406. // We only put NaN and Inf in the first set of values
  407. if ((dataRange & DataRange.NaN) != 0)
  408. {
  409. indices.Add(floats.Count);
  410. floats.Add(float.NaN);
  411. }
  412. if ((dataRange & DataRange.Inf) != 0)
  413. {
  414. indices.Add(floats.Count);
  415. floats.Add(float.PositiveInfinity);
  416. }
  417. var random = new System.Random(seed);
  418. switch (size)
  419. {
  420. case 2:
  421. for (int i = 0; i < VectorsCount; i++)
  422. {
  423. var x = floats[NextIndex(random, indices, originalIndices)];
  424. var y = floats[NextIndex(random, indices, originalIndices)];
  425. yield return new float2(x, y);
  426. }
  427. break;
  428. case 3:
  429. for (int i = 0; i < VectorsCount; i++)
  430. {
  431. var x = floats[NextIndex(random, indices, originalIndices)];
  432. var y = floats[NextIndex(random, indices, originalIndices)];
  433. var z = floats[NextIndex(random, indices, originalIndices)];
  434. yield return new float3(x, y, z);
  435. }
  436. break;
  437. case 4:
  438. for (int i = 0; i < VectorsCount; i++)
  439. {
  440. var x = floats[NextIndex(random, indices, originalIndices)];
  441. var y = floats[NextIndex(random, indices, originalIndices)];
  442. var z = floats[NextIndex(random, indices, originalIndices)];
  443. var w = floats[NextIndex(random, indices, originalIndices)];
  444. yield return new float4(x, y, z, w);
  445. }
  446. break;
  447. default:
  448. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  449. }
  450. }
  451. else if (type.Name.StartsWith("double"))
  452. {
  453. var size = (uint)(type.Name["double".Length] - '0');
  454. var doubles = ExpandRange(dataRange & ~(DataRange.NaN | DataRange.Inf), typeof(double), seed).OfType<double>().ToList();
  455. var originalIndices = Enumerable.Range(0, doubles.Count).ToList();
  456. var indices = new List<int>(originalIndices);
  457. // We only put NaN and Inf in the first set of values
  458. if ((dataRange & DataRange.NaN) != 0)
  459. {
  460. indices.Add(doubles.Count);
  461. doubles.Add(double.NaN);
  462. }
  463. if ((dataRange & DataRange.Inf) != 0)
  464. {
  465. indices.Add(doubles.Count);
  466. doubles.Add(double.PositiveInfinity);
  467. }
  468. var random = new System.Random(seed);
  469. switch (size)
  470. {
  471. case 2:
  472. for (int i = 0; i < VectorsCount; i++)
  473. {
  474. var x = doubles[NextIndex(random, indices, originalIndices)];
  475. var y = doubles[NextIndex(random, indices, originalIndices)];
  476. yield return new double2(x, y);
  477. }
  478. break;
  479. case 3:
  480. for (int i = 0; i < VectorsCount; i++)
  481. {
  482. var x = doubles[NextIndex(random, indices, originalIndices)];
  483. var y = doubles[NextIndex(random, indices, originalIndices)];
  484. var z = doubles[NextIndex(random, indices, originalIndices)];
  485. yield return new double3(x, y, z);
  486. }
  487. break;
  488. case 4:
  489. for (int i = 0; i < VectorsCount; i++)
  490. {
  491. var x = doubles[NextIndex(random, indices, originalIndices)];
  492. var y = doubles[NextIndex(random, indices, originalIndices)];
  493. var z = doubles[NextIndex(random, indices, originalIndices)];
  494. var w = doubles[NextIndex(random, indices, originalIndices)];
  495. yield return new double4(x, y, z, w);
  496. }
  497. break;
  498. default:
  499. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  500. }
  501. }
  502. else
  503. {
  504. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  505. }
  506. }
  507. else
  508. {
  509. throw new NotSupportedException($"Unsupported DataRange type `{type}`");
  510. }
  511. }
  512. private static int NextIndex(System.Random random, List<int> indices, List<int> originalIndices)
  513. {
  514. var id = random.Next(0, indices.Count - 1);
  515. var index = indices[id];
  516. indices.RemoveAt(id);
  517. if (indices.Count == 0)
  518. {
  519. indices.AddRange(originalIndices);
  520. }
  521. return index;
  522. }
  523. }
  524. }