PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/question/type/ddmarker/tests/shapes_test.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 165 lines | 109 code | 25 blank | 31 comment | 1 complexity | 979fb3aadfbf661bf544bc113763edd4 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Unit tests for the drag-and-drop words shape code.
  18. *
  19. * @package qtype_ddmarker
  20. * @copyright 2012 The Open University
  21. * @author Jamie Pratt <me@jamiep.org>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/question/type/ddmarker/shapes.php');
  27. /**
  28. * Unit tests for shape code
  29. *
  30. * @copyright 2012 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class qtype_ddmarker_shapes_test extends basic_testcase {
  34. public function test_polygon_valdiation_test_ok() {
  35. $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10; 20, 20; 10, 20');
  36. $this->assertFalse($shape->get_coords_interpreter_error()); // No errors.
  37. }
  38. public function test_polygon_valdiation_test_only_two_points() {
  39. $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10');
  40. $this->assertEquals(get_string('formerror_polygonmusthaveatleastthreepoints', 'qtype_ddmarker',
  41. array('shape' => 'polygon', 'coordsstring' => get_string('shape_polygon_coords', 'qtype_ddmarker'))),
  42. $shape->get_coords_interpreter_error());
  43. }
  44. public function test_polygon_valdiation_test_invalid_point() {
  45. $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, ; 20, 20; 10, 20');
  46. $this->assertEquals(get_string('formerror_onlyusewholepositivenumbers', 'qtype_ddmarker',
  47. array('shape' => 'polygon', 'coordsstring' => get_string('shape_polygon_coords', 'qtype_ddmarker'))),
  48. $shape->get_coords_interpreter_error());
  49. }
  50. public function test_polygon_valdiation_test_repeated_point() {
  51. $shape = new qtype_ddmarker_shape_polygon('70,220;90,200;95,150;120,150;140,200;150,230;'.
  52. '150,230;150,240;120,240;110,240;90,240');
  53. $this->assertEquals(get_string('formerror_repeatedpoint', 'qtype_ddmarker',
  54. array('shape' => 'polygon', 'coordsstring' => get_string('shape_polygon_coords', 'qtype_ddmarker'))),
  55. $shape->get_coords_interpreter_error());
  56. }
  57. public function test_polygon_hit_test() {
  58. $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10; 20, 20; 10, 20');
  59. $this->assertTrue($shape->is_point_in_shape(array(15, 15)));
  60. $this->assertFalse($shape->is_point_in_shape(array(5, 5)));
  61. $this->assertFalse($shape->is_point_in_shape(array(5, 15)));
  62. $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  63. $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
  64. $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
  65. $this->assertTrue($shape->is_point_in_shape(array(19, 19)));
  66. // Test points right on the edge are in.
  67. $this->assertTrue($shape->is_point_in_shape(array(10, 10)));
  68. $this->assertTrue($shape->is_point_in_shape(array(10, 20)));
  69. $this->assertTrue($shape->is_point_in_shape(array(20, 20)));
  70. $this->assertTrue($shape->is_point_in_shape(array(20, 10)));
  71. $this->assertTrue($shape->is_point_in_shape(array(10, 15)));
  72. $this->assertTrue($shape->is_point_in_shape(array(15, 10)));
  73. $this->assertTrue($shape->is_point_in_shape(array(20, 15)));
  74. $this->assertTrue($shape->is_point_in_shape(array(15, 20)));
  75. // Should accept closed polygon coords or unclosed and it will model a closed polygon.
  76. $shape = new qtype_ddmarker_shape_polygon('10, 10; 20, 10; 20, 20; 10, 20; 10, 10');
  77. $this->assertTrue($shape->is_point_in_shape(array(15, 15)));
  78. $this->assertFalse($shape->is_point_in_shape(array(5, 5)));
  79. $this->assertFalse($shape->is_point_in_shape(array(5, 15)));
  80. $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  81. $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
  82. $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
  83. $this->assertTrue($shape->is_point_in_shape(array(19, 19)));
  84. $shape = new qtype_ddmarker_shape_polygon('10, 10; 15, 5; 20, 10; 20, 20; 10, 20');
  85. $this->assertTrue($shape->is_point_in_shape(array(15, 15)));
  86. $this->assertFalse($shape->is_point_in_shape(array(5, 5)));
  87. $this->assertFalse($shape->is_point_in_shape(array(5, 15)));
  88. $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  89. $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
  90. $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
  91. $this->assertTrue($shape->is_point_in_shape(array(19, 19)));
  92. $this->assertTrue($shape->is_point_in_shape(array(15, 9)));
  93. $this->assertTrue($shape->is_point_in_shape(array(15, 10)));
  94. $shape = new qtype_ddmarker_shape_polygon('15, 5; 20, 10; 20, 20; 10, 20; 10, 10');
  95. $this->assertTrue($shape->is_point_in_shape(array(15, 10)));
  96. $shape = new qtype_ddmarker_shape_polygon('15, 5; 20, 10; 20, 20; 10, 20; 10, 10');
  97. $this->assertFalse($shape->is_point_in_shape(array(25, 10)));
  98. $shape = new qtype_ddmarker_shape_polygon('0, 0; 500, 0; 600, 1000; 0, 1200; 10, 10');
  99. $this->assertTrue($shape->is_point_in_shape(array(25, 10)));
  100. }
  101. public function test_circle_valdiation_test() {
  102. $shape = new qtype_ddmarker_shape_circle('10, 10; 10');
  103. $this->assertFalse($shape->get_coords_interpreter_error()); // No errors.
  104. }
  105. public function test_circle_hit_test() {
  106. $shape = new qtype_ddmarker_shape_circle('10, 10; 10');
  107. $this->assertTrue($shape->is_point_in_shape(array(19, 10)));
  108. $this->assertTrue($shape->is_point_in_shape(array(20, 10)));
  109. $this->assertFalse($shape->is_point_in_shape(array(21, 10)));
  110. $this->assertTrue($shape->is_point_in_shape(array(10, 1)));
  111. $this->assertTrue($shape->is_point_in_shape(array(10, 0)));
  112. $this->assertFalse($shape->is_point_in_shape(array(10, -1)));
  113. $this->assertFalse($shape->is_point_in_shape(array(15, 25)));
  114. $this->assertFalse($shape->is_point_in_shape(array(25, 15)));
  115. $this->assertTrue($shape->is_point_in_shape(array(11, 11)));
  116. $this->assertTrue($shape->is_point_in_shape(array(1, 10)));
  117. $this->assertTrue($shape->is_point_in_shape(array(17, 17)));
  118. $this->assertTrue($shape->is_point_in_shape(array(3, 3)));
  119. $this->assertFalse($shape->is_point_in_shape(array(2, 2)));
  120. // Should be exactly on the boundary - 3, 4, 5 right-angled triangle.
  121. $this->assertTrue($shape->is_point_in_shape(array(16, 18)));
  122. }
  123. public function test_rectangle_valdiation_test() {
  124. $shape = new qtype_ddmarker_shape_rectangle('1000, 4000; 500, 400');
  125. $this->assertFalse($shape->get_coords_interpreter_error()); // No errors.
  126. }
  127. public function test_rectangle_hit_test() {
  128. $shape = new qtype_ddmarker_shape_rectangle('1000, 4000; 500, 400');
  129. $this->assertFalse($shape->is_point_in_shape(array(999, 4200)));
  130. $this->assertTrue($shape->is_point_in_shape(array(1000, 4200)));
  131. $this->assertTrue($shape->is_point_in_shape(array(1001, 4200)));
  132. $this->assertTrue($shape->is_point_in_shape(array(1499, 4200)));
  133. $this->assertTrue($shape->is_point_in_shape(array(1500, 4200)));
  134. $this->assertFalse($shape->is_point_in_shape(array(1501, 4200)));
  135. $this->assertFalse($shape->is_point_in_shape(array(1250, 3999)));
  136. $this->assertTrue($shape->is_point_in_shape(array(1250, 4000)));
  137. $this->assertTrue($shape->is_point_in_shape(array(1250, 4400)));
  138. $this->assertFalse($shape->is_point_in_shape(array(1250, 4401)));
  139. }
  140. }