MtCarsTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace MathPHP\Tests\SampleData;
  3. use MathPHP\SampleData;
  4. class MtCarsTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /** @var SampleData\MtCars */
  7. private $mtCars;
  8. public function setUp(): void
  9. {
  10. $this->mtCars = new SampleData\MtCars();
  11. }
  12. /**
  13. * @test 32 observations
  14. */
  15. public function testDataHas32Observations()
  16. {
  17. // When
  18. $data = $this->mtCars->getData();
  19. // Then
  20. $this->assertCount(32, $data);
  21. }
  22. /**
  23. * @test 11 variables
  24. */
  25. public function testDataHas11Variables()
  26. {
  27. // When
  28. $data = $this->mtCars->getData();
  29. // Then
  30. foreach ($data as $observation) {
  31. $this->assertCount(11, $observation);
  32. }
  33. }
  34. /**
  35. * @test 32 models
  36. */
  37. public function testNumberOfModels()
  38. {
  39. // When
  40. $models = $this->mtCars->getModels();
  41. // Then
  42. $this->assertCount(32, $models);
  43. }
  44. /**
  45. * @test Model names
  46. */
  47. public function testModelNames()
  48. {
  49. // Given
  50. $sampleOfModelNames = ['Mazda RX4', 'Honda Civic', 'Toyota Corolla', 'Volvo 142E'];
  51. $models = $this->mtCars->getModels();
  52. // When
  53. foreach ($sampleOfModelNames as $model) {
  54. // Then
  55. $this->assertTrue(\in_array($model, $models));
  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->mtCars->getLabeledData();
  68. // Then
  69. $this->assertEquals($expectedData, $labeledData[$model]);
  70. }
  71. /**
  72. * @test Model data
  73. * @dataProvider dataProviderForLabeledData
  74. * @param string $model
  75. * @param array $expectedData
  76. */
  77. public function testGetModelData(string $model, array $expectedData)
  78. {
  79. // When
  80. $data = $this->mtCars->getModelData($model);
  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. 'Mazda RX4',
  92. [
  93. 'mpg' => 21,
  94. 'cyl' => 6,
  95. 'disp' => 160,
  96. 'hp' => 110,
  97. 'drat' => 3.9,
  98. 'wt' => 2.62,
  99. 'qsec' => 16.46,
  100. 'vs' => 0,
  101. 'am' => 1,
  102. 'gear' => 4,
  103. 'carb' => 4
  104. ]
  105. ],
  106. [
  107. 'Honda Civic',
  108. [
  109. 'mpg' => 30.4,
  110. 'cyl' => 4,
  111. 'disp' => 75.7,
  112. 'hp' => 52,
  113. 'drat' => 4.93,
  114. 'wt' => 1.615,
  115. 'qsec' => 18.52,
  116. 'vs' => 1,
  117. 'am' => 1,
  118. 'gear' => 4,
  119. 'carb' => 2,
  120. ]
  121. ],
  122. [
  123. 'Volvo 142E',
  124. [
  125. 'mpg' => 21.4,
  126. 'cyl' => 4,
  127. 'disp' => 121,
  128. 'hp' => 109,
  129. 'drat' => 4.11,
  130. 'wt' => 2.78,
  131. 'qsec' => 18.6,
  132. 'vs' => 1,
  133. 'am' => 1,
  134. 'gear' => 4,
  135. 'carb' => 2,
  136. ]
  137. ],
  138. ];
  139. }
  140. /**
  141. * @test 32 mpg observations
  142. */
  143. public function testNumberOfMpgs()
  144. {
  145. // When
  146. $observations = $this->mtCars->getMpg();
  147. // Then
  148. $this->assertCount(32, $observations);
  149. }
  150. /**
  151. * @test 32 cyl observations
  152. */
  153. public function testNumberOfCyl()
  154. {
  155. // When
  156. $observations = $this->mtCars->getCyl();
  157. // Then
  158. $this->assertCount(32, $observations);
  159. }
  160. /**
  161. * @test 32 disp observations
  162. */
  163. public function testNumberOfDisps()
  164. {
  165. // When
  166. $observations = $this->mtCars->getDisp();
  167. // Then
  168. $this->assertCount(32, $observations);
  169. }
  170. /**
  171. * @test 32 hp observations
  172. */
  173. public function testNumberOfHps()
  174. {
  175. // When
  176. $observations = $this->mtCars->getHp();
  177. // Then
  178. $this->assertCount(32, $observations);
  179. }
  180. /**
  181. * @test 32 drat observations
  182. */
  183. public function testNumberOfDrats()
  184. {
  185. // When
  186. $observations = $this->mtCars->getDrat();
  187. // Then
  188. $this->assertCount(32, $observations);
  189. }
  190. /**
  191. * @test 32 wt observations
  192. */
  193. public function testNumberOfWts()
  194. {
  195. // When
  196. $observations = $this->mtCars->getWt();
  197. // Then
  198. $this->assertCount(32, $observations);
  199. }
  200. /**
  201. * @test 32 qsec observations
  202. */
  203. public function testNumberOfQsecs()
  204. {
  205. // When
  206. $observations = $this->mtCars->getQsec();
  207. // Then
  208. $this->assertCount(32, $observations);
  209. }
  210. /**
  211. * @test 32 vs observations
  212. */
  213. public function testNumberOfVss()
  214. {
  215. // When
  216. $observations = $this->mtCars->getVs();
  217. // Then
  218. $this->assertCount(32, $observations);
  219. }
  220. /**
  221. * @test 32 am observations
  222. */
  223. public function testNumberOfAms()
  224. {
  225. // When
  226. $observations = $this->mtCars->getAm();
  227. // Then
  228. $this->assertCount(32, $observations);
  229. }
  230. /**
  231. * @test 32 gear observations
  232. */
  233. public function testNumberOfgears()
  234. {
  235. // When
  236. $observations = $this->mtCars->getGear();
  237. // Then
  238. $this->assertCount(32, $observations);
  239. }
  240. /**
  241. * @test 32 carb observations
  242. */
  243. public function testNumberOfCarbs()
  244. {
  245. // When
  246. $observations = $this->mtCars->getCarb();
  247. // Then
  248. $this->assertCount(32, $observations);
  249. }
  250. }