ResourceManagerTest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * ResourceManagerTest.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "gtest/gtest.h"
  12. #include "../AI/VCAI/VCAI.h"
  13. #include "../AI/VCAI/Goals/Goals.h"
  14. #include "mock_VCAI_CGoal.h"
  15. #include "mock_VCAI.h"
  16. #include "mock_ResourceManager.h"
  17. #include "../mock/mock_CPSICallback.h"
  18. #include "../lib/CGameInfoCallback.h"
  19. using namespace Goals;
  20. using namespace ::testing;
  21. struct ResourceManagerTest : public Test//, public IResourceManager
  22. {
  23. std::unique_ptr<ResourceManagerMock> rm;
  24. NiceMock<CPSICallbackMock> gcm;
  25. NiceMock<VCAIMock> aim;
  26. TSubgoal invalidGoal, gatherArmy, buildThis, buildAny, recruitHero;
  27. ResourceManagerTest()
  28. {
  29. rm = make_unique<NiceMock<ResourceManagerMock>>(&gcm, &aim);
  30. //note: construct new goal for modifications
  31. invalidGoal = sptr(StrictMock<InvalidGoalMock>());
  32. gatherArmy = sptr(StrictMock<GatherArmyGoalMock>());
  33. buildThis = sptr(StrictMock<BuildThis>());
  34. buildAny = sptr(StrictMock<Build>());
  35. recruitHero = sptr(StrictMock<RecruitHero>());
  36. //auto AI = CDynLibHandler::getNewAI("VCAI.dll");
  37. //SET_GLOBAL_STATE(AI);
  38. //gtest couldn't deduce default return value;
  39. ON_CALL(gcm, getTownsInfo(_))
  40. .WillByDefault(Return(std::vector < const CGTownInstance *>()));
  41. ON_CALL(gcm, getTownsInfo())
  42. .WillByDefault(Return(std::vector < const CGTownInstance *>()));
  43. ON_CALL(aim, getFlaggedObjects())
  44. .WillByDefault(Return(std::vector < const CGObjectInstance *>()));
  45. //enable if get unexpected exceptions
  46. //ON_CALL(gcm, getResourceAmount())
  47. // .WillByDefault(Return(TResources()));
  48. }
  49. };
  50. TEST_F(ResourceManagerTest, canAffordMaths)
  51. {
  52. //mocking cb calls inside canAfford()
  53. ON_CALL(gcm, getResourceAmount())
  54. .WillByDefault(Return(TResources(10, 0, 11, 0, 0, 0, 12345)));
  55. TResources buildingCost(10, 0, 10, 0, 0, 0, 5000);
  56. //EXPECT_CALL(gcm, getResourceAmount()).RetiresOnSaturation();
  57. //EXPECT_CALL(gcm, getTownsInfo(_)).RetiresOnSaturation();
  58. EXPECT_NO_THROW(rm->canAfford(buildingCost));
  59. EXPECT_TRUE(rm->canAfford(buildingCost));
  60. TResources armyCost(0, 0, 0, 0, 0, 0, 54321);
  61. EXPECT_FALSE(rm->canAfford(armyCost));
  62. rm->reserveResources(armyCost, gatherArmy);
  63. EXPECT_FALSE(rm->canAfford(buildingCost)) << "Reserved value should be subtracted from free resources";
  64. }
  65. TEST_F(ResourceManagerTest, notifyGoalImplemented)
  66. {
  67. ASSERT_FALSE(rm->hasTasksLeft());
  68. EXPECT_FALSE(rm->notifyGoalCompleted(invalidGoal));
  69. EXPECT_FALSE(rm->hasTasksLeft());
  70. TResources res(0,0,0,0,0,0,12345);
  71. rm->reserveResources(res, invalidGoal);
  72. ASSERT_FALSE(rm->hasTasksLeft()) << "Can't push Invalid goal";
  73. EXPECT_FALSE(rm->notifyGoalCompleted(invalidGoal));
  74. EXPECT_FALSE(rm->notifyGoalCompleted(gatherArmy)) << "Queue should be empty";
  75. rm->reserveResources(res, gatherArmy);
  76. EXPECT_TRUE(rm->notifyGoalCompleted(gatherArmy)) << "Not implemented"; //TODO: try it with not a copy
  77. EXPECT_FALSE(rm->notifyGoalCompleted(gatherArmy)); //already completed
  78. }
  79. TEST_F(ResourceManagerTest, notifyFulfillsAll)
  80. {
  81. TResources res;
  82. ASSERT_TRUE(buildAny->fulfillsMe(buildThis)) << "Goal dependency implemented incorrectly"; //TODO: goal mock?
  83. rm->reserveResources(res, buildAny);
  84. rm->reserveResources(res, buildAny);
  85. rm->reserveResources(res, buildAny);
  86. ASSERT_TRUE(rm->hasTasksLeft()); //regardless if duplicates are allowed or not
  87. rm->notifyGoalCompleted(buildThis);
  88. ASSERT_FALSE(rm->hasTasksLeft()) << "BuildThis didn't remove Build Any!";
  89. }
  90. TEST_F(ResourceManagerTest, queueOrder)
  91. {
  92. ASSERT_FALSE(rm->hasTasksLeft());
  93. TSubgoal buildLow = sptr(StrictMock<BuildThis>());
  94. TSubgoal buildBit = sptr(StrictMock<BuildThis>());
  95. TSubgoal buildMed = sptr(StrictMock<BuildThis>());
  96. TSubgoal buildHigh = sptr(StrictMock<BuildThis>());
  97. TSubgoal buildVeryHigh = sptr(StrictMock<BuildThis>());
  98. TSubgoal buildExtra = sptr(StrictMock<BuildThis>());
  99. TSubgoal buildNotSoExtra = sptr(StrictMock<BuildThis>());
  100. buildLow->setpriority(0).setbid(1);
  101. buildLow->setpriority(1).setbid(2);
  102. buildMed->setpriority(2).setbid(3);
  103. buildHigh->setpriority(5).setbid(4);
  104. buildVeryHigh->setpriority(10).setbid(5);
  105. TResources price(0, 0, 0, 0, 0, 0, 1000);
  106. rm->reserveResources(price, buildLow);
  107. rm->reserveResources(price, buildHigh);
  108. rm->reserveResources(price, buildMed);
  109. ON_CALL(gcm, getResourceAmount())
  110. .WillByDefault(Return(TResources(0,0,0,0,0,0,4000,0))); //we can afford 4 top goals
  111. auto goal = rm->whatToDo();
  112. EXPECT_EQ(goal->goalType, Goals::BUILD_STRUCTURE);
  113. ASSERT_EQ(rm->whatToDo()->bid, 4);
  114. rm->reserveResources(price, buildBit);
  115. rm->reserveResources(price, buildVeryHigh);
  116. goal = rm->whatToDo();
  117. EXPECT_EQ(goal->goalType, Goals::BUILD_STRUCTURE);
  118. ASSERT_EQ(goal->bid, 5);
  119. buildExtra->setpriority(3).setbid(100);
  120. EXPECT_EQ(rm->whatToDo(price, buildExtra)->bid, 100);
  121. buildNotSoExtra->setpriority(0.7f).setbid(7);
  122. goal = rm->whatToDo(price, buildNotSoExtra);
  123. EXPECT_NE(goal->bid, 7);
  124. EXPECT_EQ(goal->goalType, Goals::COLLECT_RES) << "We can't afford this goal, need to collect resources";
  125. EXPECT_EQ(goal->resID, EGameResID::GOLD) << "We need to collect gold";
  126. goal = rm->whatToDo();
  127. EXPECT_NE(goal->goalType, Goals::COLLECT_RES);
  128. EXPECT_EQ(goal->bid, 5) << "Should return highest-priority goal";
  129. }
  130. TEST_F(ResourceManagerTest, updateGoalImplemented)
  131. {
  132. ASSERT_FALSE(rm->hasTasksLeft());
  133. TResources res;
  134. res[EGameResID::GOLD] = 12345;
  135. buildThis->setpriority(1);
  136. buildThis->bid = 666;
  137. EXPECT_FALSE(rm->updateGoal(buildThis)); //try update with no objectives -> fail
  138. rm->reserveResources(res, buildThis);
  139. ASSERT_TRUE(rm->hasTasksLeft());
  140. buildThis->setpriority(4.444f);
  141. auto buildThis2 = sptr(StrictMock<BuildThis>());
  142. buildThis2->bid = 777;
  143. buildThis2->setpriority(3.33f);
  144. EXPECT_FALSE(rm->updateGoal(buildThis2)) << "Shouldn't update with wrong goal";
  145. EXPECT_TRUE(rm->updateGoal(buildThis)) << "Not implemented"; //try update with copy of reserved goal -> true
  146. EXPECT_FALSE(rm->updateGoal(invalidGoal)) << "Can't update Invalid goal";
  147. }
  148. TEST_F(ResourceManagerTest, complexGoalUpdates)
  149. {
  150. //TODO
  151. ASSERT_FALSE(rm->hasTasksLeft());
  152. }
  153. TEST_F(ResourceManagerTest, tasksLeft)
  154. {
  155. ASSERT_FALSE(rm->hasTasksLeft());
  156. }
  157. TEST_F(ResourceManagerTest, reservedResources)
  158. {
  159. //TOOO, not worth it now
  160. }
  161. TEST_F(ResourceManagerTest, freeResources)
  162. {
  163. ON_CALL(gcm, getResourceAmount()) //in case callback or gs gets crazy
  164. .WillByDefault(Return(TResources(-1, 0, -13, -38763, -93764, -464, -12, -98765)));
  165. auto res = rm->freeResources();
  166. ASSERT_GE(res[EGameResID::WOOD], 0);
  167. ASSERT_GE(res[EGameResID::MERCURY], 0);
  168. ASSERT_GE(res[EGameResID::ORE], 0);
  169. ASSERT_GE(res[EGameResID::SULFUR], 0);
  170. ASSERT_GE(res[EGameResID::CRYSTAL], 0);
  171. ASSERT_GE(res[EGameResID::GEMS], 0);
  172. ASSERT_GE(res[EGameResID::GOLD], 0);
  173. }
  174. TEST_F(ResourceManagerTest, freeResourcesWithManyGoals)
  175. {
  176. ON_CALL(gcm, getResourceAmount())
  177. .WillByDefault(Return(TResources(20, 10, 20, 10, 10, 10, 20000, 0)));
  178. ASSERT_EQ(rm->freeResources(), TResources(20, 10, 20, 10, 10, 10, 20000, 0));
  179. rm->reserveResources(TResources(0, 4, 0, 0, 0, 0, 13000), gatherArmy);
  180. ASSERT_EQ(rm->freeResources(), TResources(20, 6, 20, 10, 10, 10, 7000, 0));
  181. rm->reserveResources(TResources(5, 4, 5, 4, 4, 4, 5000), buildThis);
  182. ASSERT_EQ(rm->freeResources(), TResources(15, 2, 15, 6, 6, 6, 2000, 0));
  183. rm->reserveResources(TResources(0, 0, 0, 0, 0, 0, 2500), recruitHero);
  184. auto res = rm->freeResources();
  185. EXPECT_EQ(res[EGameResID::GOLD], 0) << "We should have 0 gold left";
  186. ON_CALL(gcm, getResourceAmount())
  187. .WillByDefault(Return(TResources(20, 10, 20, 10, 10, 10, 30000, 0))); //+10000 gold
  188. res = rm->freeResources();
  189. EXPECT_EQ(res[EGameResID::GOLD], 9500) << "We should have extra savings now";
  190. }
  191. TEST_F(ResourceManagerTest, freeGold)
  192. {
  193. ON_CALL(gcm, getResourceAmount())
  194. .WillByDefault(Return(TResources(0, 0, 0, 0, 0, 0, 666)));
  195. ASSERT_EQ(rm->freeGold(), 666);
  196. ON_CALL(gcm, getResourceAmount())
  197. .WillByDefault(Return(TResources(0, 0, 0, 0, 0, 0, -3762363)));
  198. ASSERT_GE(rm->freeGold(), 0) << "We should never see negative savings";
  199. }