gflow-source-test.vala 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* -*- Mode: vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
  2. /* GFlowTest
  3. *
  4. * Copyright (C) 2015 Daniel Espinosa <esodan@gmail.com>
  5. *
  6. * librescl is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * librescl is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. using GFlow;
  20. public class GFlowTest.Source : GFlow.SimpleSource
  21. {
  22. public void update ()
  23. {
  24. if (_val.get_boolean ()) _val.set_boolean (false);
  25. else _val.set_boolean (true);
  26. }
  27. public Source () {
  28. Value v = false;
  29. base (v);
  30. }
  31. }
  32. public class GFlowTest.SourceTest
  33. {
  34. public static void add_tests ()
  35. {
  36. Test.add_func ("/gflow/source",
  37. () => {
  38. Value initial = Value(typeof(int));
  39. initial.set_int (1);
  40. var src = new GFlow.SimpleSource (initial);
  41. assert (src.initial != null);
  42. assert (src.val != null);
  43. assert (src.val.holds (typeof (int)));
  44. assert (src.val.get_int () == 1);
  45. assert (!src.is_linked ());
  46. src.val.set_int (10);
  47. assert (src.val.get_int () == 10);
  48. src.val = 0.10;
  49. assert (src.val.get_int () == 10);
  50. });
  51. Test.add_func ("/gflow/source/link",
  52. () => {
  53. var src = new GFlow.SimpleSource (0);
  54. var s = new GFlow.SimpleSink (true);
  55. var s1 = new GFlow.SimpleSink (1);
  56. var s2 = new GFlow.SimpleSink (10);
  57. assert (src.initial != null);
  58. assert (src.val != null);
  59. assert (src.val.holds (typeof (int)));
  60. assert (((int) src.val) == 0);
  61. assert (!src.is_linked ());
  62. bool fail = true;
  63. try { src.link (s); } catch { fail = false; }
  64. if (fail) assert_not_reached ();
  65. try {
  66. assert (!s1.is_linked ());
  67. fail = true;
  68. s1.linked.connect (()=>{
  69. fail = false;
  70. });
  71. src.link (s1);
  72. if (fail) assert_not_reached ();
  73. fail = true;
  74. s2.linked.connect (()=>{
  75. fail = false;
  76. });
  77. src.link (s2);
  78. if (fail) assert_not_reached ();
  79. src.val = 20;
  80. assert (((int) src.val) == 20);
  81. assert (((int) s1.val) == 20);
  82. assert (((int) s2.val) == 20);
  83. } catch { assert_not_reached (); }
  84. });
  85. Test.add_func ("/gflow/source/derived",
  86. () => {
  87. var src = new GFlowTest.Source ();
  88. assert (src.initial != null);
  89. assert (src.val != null);
  90. assert (src.val.holds (typeof (bool)));
  91. assert (!src.val.get_boolean ());
  92. src.update ();
  93. assert (src.val.get_boolean ());
  94. assert (!src.is_linked ());
  95. });
  96. Test.add_func ("/gflow/source/invalidate",
  97. () => {
  98. var src = new GFlow.SimpleSource(0);
  99. assert (!src.valid);
  100. src.set_valid();
  101. assert (src.valid);
  102. src.invalidate();
  103. assert (!src.valid);
  104. });
  105. }
  106. }