emqx_wdgraph_tests.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_wdgraph_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. empty_test_() ->
  19. G = emqx_wdgraph:new(),
  20. [
  21. ?_assertEqual([], emqx_wdgraph:get_edges(foo, G)),
  22. ?_assertEqual(false, emqx_wdgraph:find_edge(foo, bar, G))
  23. ].
  24. edges_nodes_test_() ->
  25. G1 = emqx_wdgraph:new(),
  26. G2 = emqx_wdgraph:insert_edge(foo, bar, 42, "fancy", G1),
  27. G3 = emqx_wdgraph:insert_edge(bar, baz, 1, "cheapest", G2),
  28. G4 = emqx_wdgraph:insert_edge(bar, foo, 0, "free", G3),
  29. G5 = emqx_wdgraph:insert_edge(foo, bar, 100, "luxury", G4),
  30. [
  31. ?_assertEqual({42, "fancy"}, emqx_wdgraph:find_edge(foo, bar, G2)),
  32. ?_assertEqual({100, "luxury"}, emqx_wdgraph:find_edge(foo, bar, G5)),
  33. ?_assertEqual([{bar, 100, "luxury"}], emqx_wdgraph:get_edges(foo, G5)),
  34. ?_assertEqual({1, "cheapest"}, emqx_wdgraph:find_edge(bar, baz, G5)),
  35. ?_assertEqual([{baz, 1, "cheapest"}, {foo, 0, "free"}], emqx_wdgraph:get_edges(bar, G5))
  36. ].
  37. fold_test_() ->
  38. G1 = emqx_wdgraph:new(),
  39. G2 = emqx_wdgraph:insert_edge(foo, bar, 42, "fancy", G1),
  40. G3 = emqx_wdgraph:insert_edge(bar, baz, 1, "cheapest", G2),
  41. G4 = emqx_wdgraph:insert_edge(bar, foo, 0, "free", G3),
  42. G5 = emqx_wdgraph:insert_edge(foo, bar, 100, "luxury", G4),
  43. [
  44. ?_assertEqual(
  45. % 100 + 0 + 1
  46. 101,
  47. emqx_wdgraph:fold(fun(_From, {_, Weight, _}, Acc) -> Weight + Acc end, 0, G5)
  48. ),
  49. ?_assertEqual(
  50. [bar, baz, foo],
  51. lists:usort(
  52. emqx_wdgraph:fold(fun(From, {To, _, _}, Acc) -> [From, To | Acc] end, [], G5)
  53. )
  54. )
  55. ].
  56. nonexistent_nodes_path_test_() ->
  57. G1 = emqx_wdgraph:new(),
  58. G2 = emqx_wdgraph:insert_edge(foo, bar, 42, "fancy", G1),
  59. G3 = emqx_wdgraph:insert_edge(bar, baz, 1, "cheapest", G2),
  60. [
  61. ?_assertEqual(
  62. {false, nosuchnode},
  63. emqx_wdgraph:find_shortest_path(nosuchnode, baz, G3)
  64. ),
  65. ?_assertEqual(
  66. [],
  67. emqx_wdgraph:find_shortest_path(nosuchnode, nosuchnode, G3)
  68. )
  69. ].
  70. nonexistent_path_test_() ->
  71. G1 = emqx_wdgraph:new(),
  72. G2 = emqx_wdgraph:insert_edge(foo, bar, 42, "fancy", G1),
  73. G3 = emqx_wdgraph:insert_edge(baz, boo, 1, "cheapest", G2),
  74. G4 = emqx_wdgraph:insert_edge(boo, last, 3.5, "change", G3),
  75. [
  76. ?_assertEqual(
  77. {false, last},
  78. emqx_wdgraph:find_shortest_path(baz, foo, G4)
  79. ),
  80. ?_assertEqual(
  81. {false, bar},
  82. emqx_wdgraph:find_shortest_path(foo, last, G4)
  83. )
  84. ].
  85. shortest_path_test() ->
  86. G1 = emqx_wdgraph:new(),
  87. G2 = emqx_wdgraph:insert_edge(foo, bar, 42, "fancy", G1),
  88. G3 = emqx_wdgraph:insert_edge(bar, baz, 1, "cheapest", G2),
  89. G4 = emqx_wdgraph:insert_edge(baz, last, 0, "free", G3),
  90. G5 = emqx_wdgraph:insert_edge(bar, last, 100, "luxury", G4),
  91. G6 = emqx_wdgraph:insert_edge(bar, foo, 0, "comeback", G5),
  92. ?assertEqual(
  93. ["fancy", "cheapest", "free"],
  94. emqx_wdgraph:find_shortest_path(foo, last, G6)
  95. ).