|
| 1 | +/*########################################################################################## |
| 2 | +
|
| 3 | + PAX SAPIENTICA Library 💀🌿🌏 |
| 4 | +
|
| 5 | + [Planning] 2023-2024 As Project |
| 6 | + [Production] 2023-2024 As Project |
| 7 | + [Contact Us] wanotaitei@gmail.com https://github.com/AsPJT/PAX_SAPIENTICA |
| 8 | + [License] Distributed under the CC0 1.0. https://creativecommons.org/publicdomain/zero/1.0/ |
| 9 | +
|
| 10 | +##########################################################################################*/ |
| 11 | + |
| 12 | +#include <gtest/gtest.h> |
| 13 | + |
| 14 | +#include <memory> |
| 15 | +#include <random> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +#include <PAX_SAPIENTICA/Simulation/Manager/SettlementSimulator.hpp> |
| 19 | +#include <PAX_SAPIENTICA/Simulation/Config/SimulationConst.hpp> |
| 20 | +#include <PAX_SAPIENTICA/System/AppConfig.hpp> |
| 21 | +#include <PAX_SAPIENTICA/Utility/MurMur3.hpp> |
| 22 | +#include <PAX_SAPIENTICA/Utility/StringUtils.hpp> |
| 23 | + |
| 24 | +// SettlementSimulator クラスのテスト |
| 25 | + |
| 26 | +class SettlementSimulatorUnitTest : public ::testing::Test { |
| 27 | +protected: |
| 28 | + void SetUp() override { |
| 29 | + // モデル名を設定 |
| 30 | + model_name = "Sample"; |
| 31 | + |
| 32 | + // SimulationConstantsの初期化 |
| 33 | + paxs::SimulationConstants::getInstance(model_name).init(model_name); |
| 34 | + |
| 35 | + // パスを生成(実際のシミュレーションと同じ方法) |
| 36 | + map_list_path = paxs::AppConfig::getInstance().getSettingPath(paxs::MurMur3::calcHash("SimulationXYZTiles")); |
| 37 | + japan_provinces_path = paxs::AppConfig::getInstance().getSettingPath(paxs::MurMur3::calcHash("SimulationProvincesPath")); |
| 38 | + |
| 39 | + paxs::StringUtils::replace(map_list_path, "Sample", model_name); |
| 40 | + paxs::StringUtils::replace(japan_provinces_path, "Sample", model_name); |
| 41 | + |
| 42 | + // テスト用のシミュレーションシードを固定 |
| 43 | + seed = 12345; |
| 44 | + } |
| 45 | + |
| 46 | + std::string model_name; |
| 47 | + std::string map_list_path; |
| 48 | + std::string japan_provinces_path; |
| 49 | + unsigned seed; |
| 50 | +}; |
| 51 | + |
| 52 | +TEST_F(SettlementSimulatorUnitTest, Construction) { |
| 53 | + // Given: パラメータを指定してシミュレータを作成 |
| 54 | + paxs::SettlementSimulator simulator(map_list_path, japan_provinces_path, seed); |
| 55 | + |
| 56 | + // Then: シミュレータが正常に構築される |
| 57 | + EXPECT_EQ(simulator.cgetPopulationNum(), 0); |
| 58 | + EXPECT_EQ(simulator.cgetSettlement(), 0); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(SettlementSimulatorUnitTest, SetEnvironment) { |
| 62 | + // Given: 空のシミュレータ |
| 63 | + paxs::SettlementSimulator simulator; |
| 64 | + |
| 65 | + // When: 環境を設定 |
| 66 | + EXPECT_NO_THROW(simulator.setEnvironment(map_list_path, japan_provinces_path, seed)); |
| 67 | + |
| 68 | + // Then: 正常に設定される |
| 69 | + EXPECT_EQ(simulator.cgetPopulationNum(), 0); |
| 70 | +} |
| 71 | + |
| 72 | +// ======================================== |
| 73 | +// RandomizeSettlements Tests |
| 74 | +// ======================================== |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief randomizeSettlements()の動作を検証するテスト |
| 78 | + * |
| 79 | + * このテストは固定シードで2回実行し、集落配置が決定論的であることを確認します。 |
| 80 | + * リファクタリング前後で同じ結果が得られることを保証します。 |
| 81 | + */ |
| 82 | +TEST_F(SettlementSimulatorUnitTest, RandomizeSettlements_Deterministic) { |
| 83 | + // Given: 同じシードで2つのシミュレータを作成 |
| 84 | + paxs::SettlementSimulator simulator1(map_list_path, japan_provinces_path, seed); |
| 85 | + paxs::SettlementSimulator simulator2(map_list_path, japan_provinces_path, seed); |
| 86 | + |
| 87 | + // When: 両方を初期化 |
| 88 | + simulator1.init(); |
| 89 | + simulator2.init(); |
| 90 | + |
| 91 | + // Then: 同じ人口と集落数が生成される |
| 92 | + EXPECT_EQ(simulator1.cgetPopulationNum(), simulator2.cgetPopulationNum()); |
| 93 | + EXPECT_EQ(simulator1.cgetSettlement(), simulator2.cgetSettlement()); |
| 94 | + |
| 95 | + // 集落の詳細も一致することを確認 |
| 96 | + const auto& grids1 = simulator1.cgetSettlementGrids(); |
| 97 | + const auto& grids2 = simulator2.cgetSettlementGrids(); |
| 98 | + |
| 99 | + EXPECT_EQ(grids1.size(), grids2.size()); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @brief randomizeSettlements()で配置されたエージェントの詳細を検証 |
| 104 | + * |
| 105 | + * 固定シードでエージェントの属性(年齢、ゲノム、文化)が再現可能であることを確認 |
| 106 | + */ |
| 107 | +TEST_F(SettlementSimulatorUnitTest, RandomizeSettlements_AgentDetailsReproducible) { |
| 108 | + // Given: 同じシードで2つのシミュレータを作成 |
| 109 | + const unsigned test_seed = 54321; |
| 110 | + paxs::SettlementSimulator simulator1(map_list_path, japan_provinces_path, test_seed); |
| 111 | + paxs::SettlementSimulator simulator2(map_list_path, japan_provinces_path, test_seed); |
| 112 | + |
| 113 | + // When: 初期化 |
| 114 | + simulator1.init(); |
| 115 | + simulator2.init(); |
| 116 | + |
| 117 | + // Then: 各集落のエージェント詳細が一致 |
| 118 | + const auto& grids1 = simulator1.cgetSettlementGrids(); |
| 119 | + const auto& grids2 = simulator2.cgetSettlementGrids(); |
| 120 | + |
| 121 | + // 集落グリッド数が一致 |
| 122 | + ASSERT_EQ(grids1.size(), grids2.size()); |
| 123 | + |
| 124 | + // 各グリッドの集落を比較 |
| 125 | + for (const auto& [key, grid1] : grids1) { |
| 126 | + const auto* grid2_ptr = grids2.try_get(key); |
| 127 | + ASSERT_NE(grid2_ptr, nullptr) << "Grid key " << key << " not found in simulator2"; |
| 128 | + |
| 129 | + const auto& settlements1 = grid1.cgetSettlements(); |
| 130 | + const auto& settlements2 = grid2_ptr->cgetSettlements(); |
| 131 | + |
| 132 | + EXPECT_EQ(settlements1.size(), settlements2.size()) |
| 133 | + << "Settlement count mismatch in grid " << key; |
| 134 | + |
| 135 | + // 各集落のエージェント数を確認 |
| 136 | + for (std::size_t i = 0; i < settlements1.size() && i < settlements2.size(); ++i) { |
| 137 | + EXPECT_EQ(settlements1[i].getPopulation(), settlements2[i].getPopulation()) |
| 138 | + << "Population mismatch in settlement " << i << " of grid " << key; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * @brief 渡来人配置のテスト |
| 145 | + * |
| 146 | + * randomizeSettlements()が渡来フラグで正しく動作することを確認 |
| 147 | + */ |
| 148 | +TEST_F(SettlementSimulatorUnitTest, RandomizeSettlements_ImmigrationFlag) { |
| 149 | + // Given: シミュレータを作成 |
| 150 | + paxs::SettlementSimulator simulator(map_list_path, japan_provinces_path, seed); |
| 151 | + |
| 152 | + // When: 初期化(在地人配置) |
| 153 | + simulator.init(); |
| 154 | + const std::size_t initial_population = simulator.cgetPopulationNum(); |
| 155 | + |
| 156 | + // Then: 初期人口が設定される |
| 157 | + EXPECT_GT(initial_population, 0) << "Initial population should be greater than 0"; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * @brief 集落配置の空間分布を検証 |
| 162 | + * |
| 163 | + * 集落が可住地にのみ配置されることを確認 |
| 164 | + */ |
| 165 | +TEST_F(SettlementSimulatorUnitTest, RandomizeSettlements_OnlyOnHabitableLand) { |
| 166 | + // Given: シミュレータを作成 |
| 167 | + paxs::SettlementSimulator simulator(map_list_path, japan_provinces_path, seed); |
| 168 | + |
| 169 | + // When: 初期化 |
| 170 | + simulator.init(); |
| 171 | + |
| 172 | + // Then: 全ての集落が有効な位置にある |
| 173 | + const auto& grids = simulator.cgetSettlementGrids(); |
| 174 | + for (const auto& [key, grid] : grids) { |
| 175 | + for (const auto& settlement : grid.cgetSettlements()) { |
| 176 | + // 人口が0より大きい |
| 177 | + EXPECT_GT(settlement.getPopulation(), 0); |
| 178 | + |
| 179 | + // 位置が有効 |
| 180 | + const auto pos = settlement.getPosition(); |
| 181 | + EXPECT_GE(pos.x, 0); |
| 182 | + EXPECT_GE(pos.y, 0); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * @brief 異なるシードで異なる結果が生成されることを確認 |
| 189 | + */ |
| 190 | +TEST_F(SettlementSimulatorUnitTest, RandomizeSettlements_DifferentSeedsDifferentResults) { |
| 191 | + // Given: 異なるシードで2つのシミュレータを作成 |
| 192 | + paxs::SettlementSimulator simulator1(map_list_path, japan_provinces_path, 12345); |
| 193 | + paxs::SettlementSimulator simulator2(map_list_path, japan_provinces_path, 67890); |
| 194 | + |
| 195 | + // When: 両方を初期化 |
| 196 | + simulator1.init(); |
| 197 | + simulator2.init(); |
| 198 | + |
| 199 | + // Then: 人口は同じ設定値だが、集落の詳細配置は異なる可能性が高い |
| 200 | + // (ただし、テストデータや設定によっては偶然一致する可能性もあるため、 |
| 201 | + // ここでは基本的な検証のみ行う) |
| 202 | + |
| 203 | + // 両方とも集落が生成されている |
| 204 | + EXPECT_GT(simulator1.cgetSettlement(), 0); |
| 205 | + EXPECT_GT(simulator2.cgetSettlement(), 0); |
| 206 | +} |
| 207 | + |
| 208 | +// ======================================== |
| 209 | +// Population Calculation Tests |
| 210 | +// ======================================== |
| 211 | + |
| 212 | +TEST_F(SettlementSimulatorUnitTest, CalcPop_UpdatesPopulationAndSettlementCount) { |
| 213 | + // Given: 初期化されたシミュレータ |
| 214 | + paxs::SettlementSimulator simulator(map_list_path, japan_provinces_path, seed); |
| 215 | + simulator.init(); |
| 216 | + |
| 217 | + // When: 人口計算を実行 |
| 218 | + const std::size_t pop_before = simulator.cgetPopulationNum(); |
| 219 | + const std::size_t settlement_before = simulator.cgetSettlement(); |
| 220 | + |
| 221 | + simulator.calcPop(); |
| 222 | + |
| 223 | + // Then: 人口と集落数が計算される(この場合、変化なし) |
| 224 | + EXPECT_EQ(simulator.cgetPopulationNum(), pop_before); |
| 225 | + EXPECT_EQ(simulator.cgetSettlement(), settlement_before); |
| 226 | +} |
| 227 | + |
| 228 | +// ======================================== |
| 229 | +// Migration Count Tests |
| 230 | +// ======================================== |
| 231 | + |
| 232 | +TEST_F(SettlementSimulatorUnitTest, GetMigrationCount_InitiallyZero) { |
| 233 | + // Given: 新しいシミュレータ |
| 234 | + paxs::SettlementSimulator simulator(map_list_path, japan_provinces_path, seed); |
| 235 | + |
| 236 | + // Then: 渡来数は0 |
| 237 | + EXPECT_EQ(simulator.getMigrationCount(), 0); |
| 238 | +} |
0 commit comments