RandArrayTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Functions;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  5. use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
  6. class RandArrayTest extends AllSetupTeardown
  7. {
  8. public function testRANDARRAYInteger(): void
  9. {
  10. $rows = 3;
  11. $cols = 2;
  12. $min = -5;
  13. $max = 5;
  14. $result = MathTrig\Random::randArray($rows, $cols, $min, $max, true);
  15. self::assertIsArray($result);
  16. self::assertCount($rows, /** @scrutinizer ignore-type */ $result);
  17. self::assertIsArray($result[0]);
  18. self::assertCount($cols, $result[0]);
  19. $values = Functions::flattenArray($result);
  20. array_walk(
  21. $values,
  22. function ($value) use ($min, $max): void {
  23. self::assertIsInt($value);
  24. self::assertTrue($value >= $min && $value <= $max);
  25. }
  26. );
  27. }
  28. public function testRANDARRAYFloat(): void
  29. {
  30. $rows = 3;
  31. $cols = 2;
  32. $min = -2;
  33. $max = 2;
  34. $result = MathTrig\Random::randArray($rows, $cols, $min, $max, false);
  35. self::assertIsArray($result);
  36. self::assertCount($rows, /** @scrutinizer ignore-type */ $result);
  37. self::assertIsArray($result[0]);
  38. self::assertCount($cols, $result[0]);
  39. $values = Functions::flattenArray($result);
  40. array_walk(
  41. $values,
  42. function ($value) use ($min, $max): void {
  43. self::assertIsFloat($value);
  44. self::assertTrue($value >= $min && $value <= $max);
  45. }
  46. );
  47. }
  48. public function testRANDARRAYExceptions(): void
  49. {
  50. $rows = 'THREE';
  51. $cols = 2;
  52. $min = 2;
  53. $max = -2;
  54. $result = MathTrig\Random::randArray($rows, $cols, $min, $max, false);
  55. self::assertSame(ExcelError::VALUE(), $result);
  56. $rows = 3;
  57. $cols = 2;
  58. $min = 2;
  59. $max = -2;
  60. $result = MathTrig\Random::randArray($rows, $cols, $min, $max, false);
  61. self::assertSame(ExcelError::VALUE(), $result);
  62. }
  63. }