ncompete_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file ncompete_test.cpp
  3. /// \brief Unit tests for nitrogen uptake competition
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2013-10-10 10:20:33 +0200 (Thu, 10 Oct 2013) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "catch.hpp"
  11. #include "ncompete.h"
  12. TEST_CASE("ncompete/single", "Testing a single individual") {
  13. std::vector<NCompetingIndividual> indivs(1);
  14. indivs[0].ndemand = 1;
  15. indivs[0].strength = 1;
  16. // more N available than needed
  17. ncompete(indivs, 2.0);
  18. REQUIRE(indivs[0].fnuptake == Approx(1));
  19. // less N available than needed
  20. ncompete(indivs, 0.5);
  21. REQUIRE(indivs[0].fnuptake == Approx(0.5));
  22. // more N available than needed
  23. ncompete(indivs, 2.0);
  24. REQUIRE(indivs[0].fnuptake == Approx(1));
  25. // less N available than needed
  26. ncompete(indivs, 0.5);
  27. REQUIRE(indivs[0].fnuptake == Approx(0.5));
  28. }
  29. TEST_CASE("ncompete/double", "Testing two individuals") {
  30. std::vector<NCompetingIndividual> indivs(2);
  31. // two equal individuals
  32. indivs[0].ndemand = 1;
  33. indivs[0].strength = 1;
  34. indivs[1] = indivs[0];
  35. // more N available than needed
  36. ncompete(indivs, 3.0);
  37. REQUIRE(indivs[0].fnuptake == Approx(1));
  38. REQUIRE(indivs[1].fnuptake == Approx(1));
  39. // less N available than needed
  40. ncompete(indivs, 1.0);
  41. REQUIRE(indivs[0].fnuptake == Approx(0.5));
  42. REQUIRE(indivs[1].fnuptake == Approx(0.5));
  43. // more N available than needed
  44. ncompete(indivs, 3.0);
  45. REQUIRE(indivs[0].fnuptake == Approx(1));
  46. REQUIRE(indivs[1].fnuptake == Approx(1));
  47. // less N available than needed
  48. ncompete(indivs, 1.0);
  49. REQUIRE(indivs[0].fnuptake == Approx(0.5));
  50. REQUIRE(indivs[1].fnuptake == Approx(0.5));
  51. // make the second indiv twice as strong
  52. indivs[1].strength *= 2;
  53. // less N available than needed
  54. ncompete(indivs, 1.0);
  55. REQUIRE(indivs[0].fnuptake == Approx(1.0/3.0));
  56. REQUIRE(indivs[1].fnuptake == Approx(2.0/3.0));
  57. // reduce the demand for the stronger individual
  58. indivs[1].ndemand = 0.1;
  59. // less N available than needed
  60. ncompete(indivs, 1.0);
  61. REQUIRE(indivs[0].fnuptake == Approx(0.9));
  62. REQUIRE(indivs[1].fnuptake == Approx(1));
  63. }
  64. TEST_CASE("ncompete/triple", "Testing three individuals") {
  65. std::vector<NCompetingIndividual> indivs(3);
  66. indivs[0].ndemand = 1;
  67. indivs[0].strength = 1;
  68. indivs[1].ndemand = 0.01;
  69. indivs[1].strength = 5;
  70. indivs[2].ndemand = 5;
  71. indivs[2].strength = 50;
  72. ncompete(indivs, 5);
  73. REQUIRE(indivs[0].fnuptake == Approx(0.097843137)); // was 0.24
  74. REQUIRE(indivs[1].fnuptake == Approx(1));
  75. REQUIRE(indivs[2].fnuptake == Approx(0.97843137));
  76. indivs[0].ndemand = 0.01;
  77. indivs[0].strength = 5;
  78. indivs[1].ndemand = 5;
  79. indivs[1].strength = 50;
  80. indivs[2].ndemand = 1;
  81. indivs[2].strength = 1;
  82. ncompete(indivs, 5);
  83. REQUIRE(indivs[0].fnuptake == Approx(1));
  84. REQUIRE(indivs[1].fnuptake == Approx(0.97843137));
  85. REQUIRE(indivs[2].fnuptake == Approx(0.097843137));
  86. indivs[0].ndemand = 5;
  87. indivs[0].strength = 50;
  88. indivs[1].ndemand = 1;
  89. indivs[1].strength = 1;
  90. indivs[2].ndemand = 0.01;
  91. indivs[2].strength = 5;
  92. ncompete(indivs, 5);
  93. REQUIRE(indivs[0].fnuptake == Approx(0.97843137));
  94. REQUIRE(indivs[1].fnuptake == Approx(0.097843137));
  95. REQUIRE(indivs[2].fnuptake == Approx(1));
  96. }
  97. TEST_CASE("ncompete/four", "Testing four individuals") {
  98. std::vector<NCompetingIndividual> indivs(4);
  99. indivs[0].ndemand = 0.5;
  100. indivs[0].strength = 2;
  101. indivs[1].ndemand = 8;
  102. indivs[1].strength = 5;
  103. indivs[2].ndemand = 0.5;
  104. indivs[2].strength = 0.1;
  105. indivs[3].ndemand = 2;
  106. indivs[3].strength = 100;
  107. ncompete(indivs, 10);
  108. REQUIRE(indivs[0].fnuptake == Approx(1));
  109. REQUIRE(indivs[1].fnuptake == Approx(0.919118));
  110. REQUIRE(indivs[2].fnuptake == Approx(0.294118));
  111. REQUIRE(indivs[3].fnuptake == Approx(1));
  112. indivs[0].ndemand = 0.5;
  113. indivs[0].strength = 0.1;
  114. indivs[1].ndemand = 2;
  115. indivs[1].strength = 100;
  116. indivs[2].ndemand = 0.5;
  117. indivs[2].strength = 2;
  118. indivs[3].ndemand = 8;
  119. indivs[3].strength = 5;
  120. ncompete(indivs, 10);
  121. REQUIRE(indivs[0].fnuptake == Approx(0.294118));
  122. REQUIRE(indivs[1].fnuptake == Approx(1));
  123. REQUIRE(indivs[2].fnuptake == Approx(1));
  124. REQUIRE(indivs[3].fnuptake == Approx(0.919118));
  125. }