UsArrestsTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace MathPHP\Tests\SampleData;
  3. use MathPHP\SampleData;
  4. class UsArrestsTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /** @var SampleData\UsArrests */
  7. private $usArrests;
  8. public function setUp(): void
  9. {
  10. $this->usArrests = new SampleData\UsArrests();
  11. }
  12. /**
  13. * @test 50 observations
  14. */
  15. public function testDataHas50Observations()
  16. {
  17. // When
  18. $data = $this->usArrests->getData();
  19. // Then
  20. $this->assertCount(50, $data);
  21. }
  22. /**
  23. * @test 4 variables
  24. */
  25. public function testDataHas4Variables()
  26. {
  27. // When
  28. $data = $this->usArrests->getData();
  29. // Then
  30. foreach ($data as $observation) {
  31. $this->assertCount(4, $observation);
  32. }
  33. }
  34. /**
  35. * @test 50 states
  36. */
  37. public function testNumberOfModels()
  38. {
  39. // When
  40. $models = $this->usArrests->getStates();
  41. // Then
  42. $this->assertCount(50, $models);
  43. }
  44. /**
  45. * @test State names
  46. */
  47. public function testStateNames()
  48. {
  49. // Given
  50. $sampleOfStateNames = ['Alabama', 'Alaska', 'Texas', 'Wyoming'];
  51. $states = $this->usArrests->getStates();
  52. // When
  53. foreach ($sampleOfStateNames as $state) {
  54. // Then
  55. $this->assertTrue(\in_array($state, $states));
  56. }
  57. }
  58. /**
  59. * @test Labeled data
  60. * @dataProvider dataProviderForLabeledData
  61. * @param string $model
  62. * @param array $expectedData
  63. */
  64. public function testLabeledData(string $model, array $expectedData)
  65. {
  66. // When
  67. $labeledData = $this->usArrests->getLabeledData();
  68. // Then
  69. $this->assertEquals($expectedData, $labeledData[$model]);
  70. }
  71. /**
  72. * @test Model data
  73. * @dataProvider dataProviderForLabeledData
  74. * @param string $state
  75. * @param array $expectedData
  76. */
  77. public function testGetStateData(string $state, array $expectedData)
  78. {
  79. // When
  80. $data = $this->usArrests->getStateData($state);
  81. // Then
  82. $this->assertEquals($expectedData, $data);
  83. }
  84. /**
  85. * @return array (model, data)
  86. */
  87. public function dataProviderForLabeledData(): array
  88. {
  89. return [
  90. [
  91. 'Alabama',
  92. [
  93. 'murder' => 13.2,
  94. 'assault' => 236,
  95. 'urbanPop' => 58,
  96. 'rape' => 21.2,
  97. ]
  98. ],
  99. [
  100. 'New York',
  101. [
  102. 'murder' => 11.1,
  103. 'assault' => 254,
  104. 'urbanPop' => 86,
  105. 'rape' => 26.1,
  106. ]
  107. ],
  108. [
  109. 'Wyoming',
  110. [
  111. 'murder' => 6.8,
  112. 'assault' => 161,
  113. 'urbanPop' => 60,
  114. 'rape' => 15.6,
  115. ]
  116. ],
  117. ];
  118. }
  119. /**
  120. * @test 50 murder observations
  121. */
  122. public function testNumberOfMurders()
  123. {
  124. // When
  125. $observations = $this->usArrests->getMurder();
  126. // Then
  127. $this->assertCount(50, $observations);
  128. }
  129. /**
  130. * @test 50 assault observations
  131. */
  132. public function testNumberOfAssaults()
  133. {
  134. // When
  135. $observations = $this->usArrests->getAssault();
  136. // Then
  137. $this->assertCount(50, $observations);
  138. }
  139. /**
  140. * @test 50 urbanPop observations
  141. */
  142. public function testNumberOfUrbanPops()
  143. {
  144. // When
  145. $observations = $this->usArrests->getUrbanPop();
  146. // Then
  147. $this->assertCount(50, $observations);
  148. }
  149. /**
  150. * @test 50 rape observations
  151. */
  152. public function testNumberOfRapes()
  153. {
  154. // When
  155. $observations = $this->usArrests->getRape();
  156. // Then
  157. $this->assertCount(50, $observations);
  158. }
  159. }