test_astar.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*************************************************************************/
  2. /* test_astar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "test_astar.h"
  31. #include "core/math/a_star.h"
  32. #include "core/os/os.h"
  33. #include <stdio.h>
  34. namespace TestAStar {
  35. class ABCX : public AStar {
  36. public:
  37. enum { A,
  38. B,
  39. C,
  40. X };
  41. ABCX() {
  42. add_point(A, Vector3(0, 0, 0));
  43. add_point(B, Vector3(1, 0, 0));
  44. add_point(C, Vector3(0, 1, 0));
  45. add_point(X, Vector3(0, 0, 1));
  46. connect_points(A, B);
  47. connect_points(A, C);
  48. connect_points(B, C);
  49. connect_points(X, A);
  50. }
  51. // Disable heuristic completely
  52. float _compute_cost(int p_from, int p_to) {
  53. if (p_from == A && p_to == C) {
  54. return 1000;
  55. }
  56. return 100;
  57. }
  58. };
  59. bool test_abc() {
  60. ABCX abcx;
  61. PoolVector<int> path = abcx.get_id_path(ABCX::A, ABCX::C);
  62. bool ok = path.size() == 3;
  63. int i = 0;
  64. ok = ok && path[i++] == ABCX::A;
  65. ok = ok && path[i++] == ABCX::B;
  66. ok = ok && path[i++] == ABCX::C;
  67. return ok;
  68. }
  69. bool test_abcx() {
  70. ABCX abcx;
  71. PoolVector<int> path = abcx.get_id_path(ABCX::X, ABCX::C);
  72. bool ok = path.size() == 4;
  73. int i = 0;
  74. ok = ok && path[i++] == ABCX::X;
  75. ok = ok && path[i++] == ABCX::A;
  76. ok = ok && path[i++] == ABCX::B;
  77. ok = ok && path[i++] == ABCX::C;
  78. return ok;
  79. }
  80. typedef bool (*TestFunc)(void);
  81. TestFunc test_funcs[] = {
  82. test_abc,
  83. test_abcx,
  84. NULL
  85. };
  86. MainLoop *test() {
  87. int count = 0;
  88. int passed = 0;
  89. while (true) {
  90. if (!test_funcs[count])
  91. break;
  92. bool pass = test_funcs[count]();
  93. if (pass)
  94. passed++;
  95. OS::get_singleton()->print("\t%s\n", pass ? "PASS" : "FAILED");
  96. count++;
  97. }
  98. OS::get_singleton()->print("\n");
  99. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  100. return NULL;
  101. }
  102. } // namespace TestAStar