InstallLogic.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #define BOOST_TEST_MODULE VGearsInstallerLogic
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/filesystem.hpp>
  18. #include <algorithm>
  19. #include <fstream>
  20. #include "installer/InstallLayout.h"
  21. #include "installer/InstallLog.h"
  22. #include "installer/InstallOptions.h"
  23. #include "installer/InstallVerifier.h"
  24. #include "installer/TaskPool.h"
  25. #include "installer/VideoFiles.h"
  26. #include <atomic>
  27. #include <chrono>
  28. #include <set>
  29. namespace fs = boost::filesystem;
  30. namespace{
  31. bool Contains(const std::vector<std::string>& list, const std::string& value){
  32. return std::find(list.begin(), list.end(), value) != list.end();
  33. }
  34. bool StartsWith(const std::string& text, const std::string& prefix){return text.compare(0, prefix.size(), prefix) == 0;}
  35. /**
  36. * A temporary directory that is removed on destruction.
  37. */
  38. struct TempDir{
  39. fs::path path;
  40. TempDir(): path(fs::temp_directory_path() / fs::unique_path("vgears-test-%%%%-%%%%")){
  41. fs::create_directories(path);
  42. }
  43. ~TempDir(){
  44. boost::system::error_code ec;
  45. fs::remove_all(path, ec);
  46. }
  47. std::string Root() const{return path.string() + "/";}
  48. void Touch(const std::string& relative) const{
  49. fs::create_directories((path / relative).parent_path());
  50. std::ofstream(( path / relative).string()) << "x";
  51. }
  52. };
  53. bool HasProblem(const VerifyResult& result, const std::string& group){
  54. for (const VerifyProblem& problem : result.problems) if (problem.group == group) return true;
  55. return false;
  56. }
  57. }
  58. // ---------------------------------------------------------------------------------------------------------------
  59. // Layout: where things go.
  60. // ---------------------------------------------------------------------------------------------------------------
  61. BOOST_AUTO_TEST_SUITE(InstallLayoutTests)
  62. BOOST_AUTO_TEST_CASE(DataAndTempAreInsideTheInstallationDirectory){
  63. BOOST_CHECK_EQUAL(InstallLayout::DataDir("/home/u/.v-gears/"), "/home/u/.v-gears/data/");
  64. BOOST_CHECK_EQUAL(InstallLayout::TempDir("/home/u/.v-gears/"), "/home/u/.v-gears/temp/");
  65. }
  66. BOOST_AUTO_TEST_CASE(DataSubdirsAreRelativeAndUnique){
  67. const auto& dirs = InstallLayout::DataSubdirs();
  68. for (const std::string& dir : dirs){
  69. BOOST_CHECK_MESSAGE(!dir.empty() && dir[0] != '/', "not relative: " << dir);
  70. BOOST_CHECK_MESSAGE(!StartsWith(dir, "data/") && !StartsWith(dir, "temp"), "wrong root: " << dir);
  71. BOOST_CHECK_MESSAGE(std::count(dirs.begin(), dirs.end(), dir) == 1, "duplicated: " << dir);
  72. }
  73. }
  74. BOOST_AUTO_TEST_CASE(EveryDirectoryTheGameReadsIsCreated){
  75. // The game reads "./data/<x>" and "./temp/<x>"; the installer must create them (or be able to write in them).
  76. for (const std::string& resource : InstallLayout::GameResourceDirs()){
  77. if (StartsWith(resource, "data/")){
  78. const std::string dir = resource.substr(5);
  79. bool created = false;
  80. for (const std::string& sub : InstallLayout::DataSubdirs())
  81. created = created || sub == dir || StartsWith(sub, dir + "/");
  82. BOOST_CHECK_MESSAGE(created, "the installer doesn't create " << resource);
  83. }
  84. else{
  85. BOOST_REQUIRE_MESSAGE(StartsWith(resource, "temp/"), "unexpected root: " << resource);
  86. BOOST_CHECK_MESSAGE(Contains(InstallLayout::TempSubdirs(), resource.substr(5)), "not created: " << resource);
  87. }
  88. }
  89. }
  90. BOOST_AUTO_TEST_CASE(CleaningKeepsWhatTheGameNeeds){
  91. const auto removable = InstallLayout::RemovableTempSubdirs();
  92. BOOST_CHECK(!Contains(removable, "world_models"));
  93. for (const std::string& kept : InstallLayout::KeptTempSubdirs()){
  94. BOOST_CHECK(Contains(InstallLayout::TempSubdirs(), kept));
  95. BOOST_CHECK(!Contains(removable, kept));
  96. }
  97. BOOST_CHECK_EQUAL(removable.size() + InstallLayout::KeptTempSubdirs().size(), InstallLayout::TempSubdirs().size());
  98. // The game reads temp/world_models, so it must be a kept directory.
  99. BOOST_CHECK(Contains(InstallLayout::KeptTempSubdirs(), "world_models"));
  100. }
  101. BOOST_AUTO_TEST_CASE(OldSettingsAreMigrated){
  102. // The data directory used to be <installation directory>/data.
  103. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/home/u/.v-gears/data", "", "/def"), "/home/u/.v-gears");
  104. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/home/u/.v-gears/data/", "/other", "/def"), "/home/u/.v-gears");
  105. // A data directory that isn't called "data" is kept as is.
  106. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("/games/ff7", "", "/def"), "/games/ff7");
  107. // Without a data directory the configuration directory is used, and then the default.
  108. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("", "/home/u/.v-gears", "/def"), "/home/u/.v-gears");
  109. BOOST_CHECK_EQUAL(InstallLayout::MigrateInstallDir("", "", "/def"), "/def");
  110. }
  111. BOOST_AUTO_TEST_SUITE_END()
  112. // ---------------------------------------------------------------------------------------------------------------
  113. // Options: dependencies, presets, estimates.
  114. // ---------------------------------------------------------------------------------------------------------------
  115. BOOST_AUTO_TEST_SUITE(InstallOptionsTests)
  116. BOOST_AUTO_TEST_CASE(ModelsNeedTheirMaps){
  117. InstallOptions o;
  118. o.field_models = true;
  119. o.wm_models = true;
  120. InstallRules::ApplyDependencies(o);
  121. BOOST_CHECK(!o.field_models);
  122. BOOST_CHECK(!o.wm_models);
  123. o.fields = o.wm = o.field_models = o.wm_models = true;
  124. InstallRules::ApplyDependencies(o);
  125. BOOST_CHECK(o.field_models);
  126. BOOST_CHECK(o.wm_models);
  127. }
  128. BOOST_AUTO_TEST_CASE(OneMapDoesNotEnableTheOtherModels){
  129. InstallOptions o;
  130. o.fields = true;
  131. o.wm_models = true;
  132. InstallRules::ApplyDependencies(o);
  133. BOOST_CHECK(!o.wm_models);
  134. }
  135. BOOST_AUTO_TEST_CASE(SelectableMatchesDependencies){
  136. // An option is selectable exactly when applying the rules to it keeps it.
  137. for (int bits = 0; bits < (1 << 2); ++ bits){
  138. InstallOptions o;
  139. o.fields = bits & 1;
  140. o.wm = bits & 2;
  141. o.field_models = o.wm_models = true;
  142. const InstallOptions selectable = InstallRules::Selectable(o);
  143. InstallRules::ApplyDependencies(o);
  144. BOOST_CHECK_EQUAL(o.field_models, selectable.field_models);
  145. BOOST_CHECK_EQUAL(o.wm_models, selectable.wm_models);
  146. }
  147. }
  148. BOOST_AUTO_TEST_CASE(PresetsRespectTheRules){
  149. for (InstallPreset preset : {
  150. InstallPreset::EVERYTHING, InstallPreset::FIELDS, InstallPreset::WORLD_MAP, InstallPreset::BATTLE,
  151. InstallPreset::MEDIA, InstallPreset::CUSTOM
  152. }){
  153. for (bool videos : {false, true}){
  154. InstallOptions o = InstallRules::Preset(preset, videos);
  155. InstallOptions checked = o;
  156. InstallRules::ApplyDependencies(checked);
  157. BOOST_CHECK(o.field_models == checked.field_models && o.wm_models == checked.wm_models);
  158. // Videos are only chosen when the disk has them.
  159. if (!videos) BOOST_CHECK(!o.videos);
  160. }
  161. }
  162. }
  163. BOOST_AUTO_TEST_CASE(PresetContents){
  164. const InstallOptions all = InstallRules::Preset(InstallPreset::EVERYTHING, true);
  165. BOOST_CHECK(all.battle_data && all.battle_models && all.kernel && all.images && all.sounds && all.music);
  166. BOOST_CHECK(all.fields && all.field_models && all.wm && all.wm_models && all.videos);
  167. const InstallOptions world = InstallRules::Preset(InstallPreset::WORLD_MAP, true);
  168. BOOST_CHECK(world.wm && world.wm_models);
  169. BOOST_CHECK(!world.fields && !world.field_models && !world.music && !world.videos);
  170. const InstallOptions fields = InstallRules::Preset(InstallPreset::FIELDS, true);
  171. BOOST_CHECK(fields.fields && fields.field_models && !fields.wm);
  172. const InstallOptions none = InstallRules::Preset(InstallPreset::CUSTOM, true);
  173. BOOST_CHECK(!none.fields && !none.wm && !none.music && !none.sounds && !none.videos);
  174. }
  175. BOOST_AUTO_TEST_CASE(ToolsNeededByTheAudio){
  176. InstallOptions none;
  177. BOOST_CHECK(!InstallRules::ToolsNeeded(none).ffmpeg && !InstallRules::ToolsNeeded(none).timidity);
  178. InstallOptions sounds;
  179. sounds.sounds = true;
  180. BOOST_CHECK(InstallRules::ToolsNeeded(sounds).ffmpeg);
  181. BOOST_CHECK(!InstallRules::ToolsNeeded(sounds).timidity);
  182. // The music needs both: TiMidity renders the MIDI and FFmpeg encodes it.
  183. InstallOptions music;
  184. music.music = true;
  185. BOOST_CHECK(InstallRules::ToolsNeeded(music).ffmpeg && InstallRules::ToolsNeeded(music).timidity);
  186. // Nothing else needs them.
  187. InstallOptions other;
  188. other.fields = other.field_models = other.wm = other.battle_data = other.images = other.kernel = true;
  189. BOOST_CHECK(!InstallRules::ToolsNeeded(other).ffmpeg && !InstallRules::ToolsNeeded(other).timidity);
  190. }
  191. BOOST_AUTO_TEST_CASE(WhatIsLostWithoutTheTools){
  192. InstallOptions all;
  193. all.sounds = all.music = true;
  194. // Nothing is lost when the programs work, or when nothing needs them.
  195. BOOST_CHECK(InstallRules::ToolConsequences(all, true, true).empty());
  196. BOOST_CHECK(InstallRules::ToolConsequences(InstallOptions(), false, false).empty());
  197. // Without FFmpeg, both the sounds and the music are lost.
  198. const auto no_ffmpeg = InstallRules::ToolConsequences(all, false, true);
  199. BOOST_REQUIRE_EQUAL(no_ffmpeg.size(), 2u);
  200. BOOST_CHECK(no_ffmpeg[0].find("sound") != std::string::npos);
  201. BOOST_CHECK(no_ffmpeg[1].find("music") != std::string::npos);
  202. // Without TiMidity only the music is lost.
  203. const auto no_timidity = InstallRules::ToolConsequences(all, true, false);
  204. BOOST_REQUIRE_EQUAL(no_timidity.size(), 1u);
  205. BOOST_CHECK(no_timidity[0].find("music") != std::string::npos);
  206. BOOST_CHECK(no_timidity[0].find("TiMidity") != std::string::npos);
  207. // Without both it is not said twice.
  208. BOOST_CHECK_EQUAL(InstallRules::ToolConsequences(all, false, false).size(), 2u);
  209. // Sound effects don't need TiMidity.
  210. InstallOptions sounds;
  211. sounds.sounds = true;
  212. BOOST_CHECK(InstallRules::ToolConsequences(sounds, true, false).empty());
  213. BOOST_CHECK_EQUAL(InstallRules::ToolConsequences(sounds, false, true).size(), 1u);
  214. // Music alone, without FFmpeg.
  215. InstallOptions music;
  216. music.music = true;
  217. const auto music_only = InstallRules::ToolConsequences(music, false, true);
  218. BOOST_REQUIRE_EQUAL(music_only.size(), 1u);
  219. BOOST_CHECK(music_only[0].find("music") != std::string::npos);
  220. }
  221. BOOST_AUTO_TEST_CASE(WorldMapNeedsFields){
  222. InstallOptions o;
  223. o.wm = true;
  224. BOOST_CHECK_EQUAL(InstallRules::Problems(o, false).size(), 1u);
  225. // Fine if the fields are installed now or were installed before.
  226. BOOST_CHECK(InstallRules::Problems(o, true).empty());
  227. o.fields = true;
  228. BOOST_CHECK(InstallRules::Problems(o, false).empty());
  229. // No world map, no problem.
  230. BOOST_CHECK(InstallRules::Problems(InstallOptions(), false).empty());
  231. }
  232. BOOST_AUTO_TEST_CASE(Estimates){
  233. uint64_t temporary = 99;
  234. BOOST_CHECK_EQUAL(InstallRules::EstimateMegabytes(InstallOptions(), &temporary), 0u);
  235. BOOST_CHECK_EQUAL(temporary, 0u);
  236. // Selecting more never makes it smaller.
  237. const InstallOptions all = InstallRules::Preset(InstallPreset::EVERYTHING, false);
  238. uint64_t all_temporary = 0;
  239. const uint64_t all_size = InstallRules::EstimateMegabytes(all, &all_temporary);
  240. for (InstallPreset preset : {InstallPreset::FIELDS, InstallPreset::WORLD_MAP, InstallPreset::BATTLE, InstallPreset::MEDIA}){
  241. uint64_t t = 0;
  242. BOOST_CHECK_LE(InstallRules::EstimateMegabytes(InstallRules::Preset(preset, false), &t), all_size);
  243. BOOST_CHECK_LE(t, all_temporary);
  244. }
  245. // The pointer is optional.
  246. BOOST_CHECK_EQUAL(InstallRules::EstimateMegabytes(all), all_size);
  247. }
  248. BOOST_AUTO_TEST_SUITE_END()
  249. // ---------------------------------------------------------------------------------------------------------------
  250. // Verifier.
  251. // ---------------------------------------------------------------------------------------------------------------
  252. BOOST_AUTO_TEST_SUITE(InstallVerifierTests)
  253. BOOST_AUTO_TEST_CASE(EmptyDirectoryMissesEverything){
  254. TempDir dir;
  255. const VerifyResult result = VerifyInstallation(dir.Root());
  256. BOOST_CHECK(!result.Ok());
  257. for (const char* group : {
  258. "Configuration", "Game files", "Kernel data", "Battle data", "Battle models", "Field maps", "Field models",
  259. "World map", "World map models", "Sound effects", "Music", "Images"
  260. }) BOOST_CHECK_MESSAGE(HasProblem(result, group), "no problem reported for " << group);
  261. BOOST_CHECK_GT(result.checked, 0);
  262. }
  263. BOOST_AUTO_TEST_CASE(ConfigurationAndPlugins){
  264. TempDir dir;
  265. dir.Touch("plugins.cfg");
  266. dir.Touch("resources.cfg");
  267. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Configuration"));
  268. // The plugins have a version suffix.
  269. dir.Touch("RenderSystem_GL.so.1.12.10");
  270. dir.Touch("Codec_STBI.so.1.12.10");
  271. BOOST_CHECK(!HasProblem(VerifyInstallation(dir.Root()), "Configuration"));
  272. }
  273. BOOST_AUTO_TEST_CASE(RootWithoutTrailingSlash){
  274. TempDir dir;
  275. dir.Touch("plugins.cfg");
  276. const VerifyResult with = VerifyInstallation(dir.Root());
  277. const VerifyResult without = VerifyInstallation(dir.path.string());
  278. BOOST_CHECK_EQUAL(with.problems.size(), without.problems.size());
  279. }
  280. BOOST_AUTO_TEST_CASE(FilesOfTheDataFolderMustBeCopied){
  281. TempDir installation;
  282. TempDir reference;
  283. reference.Touch("plugins.cfg");
  284. reference.Touch("data/scripts/a.lua");
  285. BOOST_CHECK(HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  286. installation.Touch("plugins.cfg");
  287. BOOST_CHECK(HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  288. installation.Touch("data/scripts/a.lua");
  289. BOOST_CHECK(!HasProblem(VerifyInstallation(installation.Root(), reference.Root()), "Copied data files"));
  290. // Without a reference there is nothing to compare.
  291. BOOST_CHECK(!HasProblem(VerifyInstallation(installation.Root()), "Copied data files"));
  292. }
  293. BOOST_AUTO_TEST_CASE(CountsFilesInsteadOfJustLookingForOne){
  294. TempDir dir;
  295. for (int i = 0; i < 3; ++ i) dir.Touch("data/fields/f" + std::to_string(i) + "/map.xml");
  296. // Three fields are not an installation of the fields.
  297. BOOST_CHECK(HasProblem(VerifyInstallation(dir.Root()), "Field maps"));
  298. }
  299. BOOST_AUTO_TEST_SUITE_END()
  300. // ---------------------------------------------------------------------------------------------------------------
  301. // Log.
  302. // ---------------------------------------------------------------------------------------------------------------
  303. BOOST_AUTO_TEST_SUITE(InstallLogTests)
  304. BOOST_AUTO_TEST_CASE(Severity){
  305. BOOST_CHECK_EQUAL(InstallLog::Classify("[ERROR] Skip field: x"), InstallLog::ERROR_LEVEL);
  306. BOOST_CHECK_EQUAL(InstallLog::Classify("Exception: Bone not found"), InstallLog::ERROR_LEVEL);
  307. BOOST_CHECK_EQUAL(InstallLog::Classify("[WARNING] Texture not found"), InstallLog::WARNING);
  308. BOOST_CHECK_EQUAL(InstallLog::Classify("Converting field models..."), InstallLog::INFO);
  309. BOOST_CHECK_EQUAL(InstallLog::Classify(""), InstallLog::INFO);
  310. }
  311. BOOST_AUTO_TEST_CASE(CountsFileAndSink){
  312. TempDir dir;
  313. InstallLog& log = InstallLog::Get();
  314. std::vector<std::string> shown;
  315. log.Open(dir.Root() + "installer.log");
  316. log.SetSink([&shown](InstallLog::Level, const std::string& message){shown.push_back(message);});
  317. log.Add("Starting");
  318. log.Add("[WARNING] careful");
  319. log.Add("[ERROR] broken");
  320. // The same problem reported twice in a row counts once.
  321. log.Add("[ERROR] broken");
  322. log.Add("hidden", false);
  323. // What the installer prints is captured too, and only problems reach the sink.
  324. std::cerr << "[ERROR] printed" << std::endl;
  325. std::cout << "plain output" << std::endl;
  326. BOOST_CHECK_EQUAL(log.Count(InstallLog::ERROR_LEVEL), 2u);
  327. BOOST_CHECK_EQUAL(log.Count(InstallLog::WARNING), 1u);
  328. BOOST_CHECK(Contains(shown, "Starting"));
  329. BOOST_CHECK(!Contains(shown, "hidden"));
  330. BOOST_CHECK(!Contains(shown, "plain output"));
  331. BOOST_CHECK(Contains(shown, "[ERROR] printed"));
  332. BOOST_REQUIRE_EQUAL(log.First(InstallLog::ERROR_LEVEL, 1).size(), 1u);
  333. BOOST_CHECK_EQUAL(log.First(InstallLog::ERROR_LEVEL, 1)[0], "[ERROR] broken");
  334. log.SetSink(nullptr);
  335. log.Close();
  336. std::ifstream file(dir.Root() + "installer.log");
  337. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  338. BOOST_CHECK(content.find("careful") != std::string::npos);
  339. BOOST_CHECK(content.find("hidden") != std::string::npos);
  340. BOOST_CHECK(content.find("plain output") != std::string::npos);
  341. }
  342. BOOST_AUTO_TEST_SUITE_END()
  343. // ---------------------------------------------------------------------------------------------------------------
  344. // Steps of the log.
  345. // ---------------------------------------------------------------------------------------------------------------
  346. BOOST_AUTO_TEST_SUITE(InstallLogStepTests)
  347. BOOST_AUTO_TEST_CASE(StepsReportTheirOwnProblems){
  348. TempDir dir;
  349. InstallLog& log = InstallLog::Get();
  350. std::vector<std::string> shown;
  351. log.Open(dir.Root() + "installer.log");
  352. log.SetSink([&shown](InstallLog::Level, const std::string& message){shown.push_back(message);});
  353. log.BeginStep("Clean step");
  354. log.Add("fine");
  355. log.BeginStep("Bad step");
  356. log.Add("[ERROR] one");
  357. log.Add("[ERROR] two");
  358. log.Add("[WARNING] three");
  359. log.BeginStep("Next step");
  360. log.Add("[WARNING] four");
  361. log.EndStep();
  362. log.SetSink(nullptr);
  363. log.Close();
  364. auto summary = [&shown](const std::string& step){
  365. for (const std::string& line : shown) if (line.find("\"" + step + "\" finished") != std::string::npos) return line;
  366. return std::string();
  367. };
  368. // Steps without problems are not mentioned, the others only count their own.
  369. BOOST_CHECK(summary("Clean step").empty());
  370. BOOST_CHECK(summary("Bad step").find("2 error(s) and 1 warning(s)") != std::string::npos);
  371. BOOST_CHECK(summary("Next step").find("0 error(s) and 1 warning(s)") != std::string::npos);
  372. // The summaries don't count as problems themselves.
  373. BOOST_CHECK_EQUAL(InstallLog::Classify(summary("Bad step")), InstallLog::INFO);
  374. }
  375. BOOST_AUTO_TEST_SUITE_END()
  376. // ---------------------------------------------------------------------------------------------------------------
  377. // Task pool.
  378. // ---------------------------------------------------------------------------------------------------------------
  379. BOOST_AUTO_TEST_SUITE(TaskPoolTests)
  380. BOOST_AUTO_TEST_CASE(RunsEveryTask){
  381. std::atomic<int> done(0);
  382. TaskPool pool(4);
  383. for (int i = 0; i < 200; ++ i) pool.Run([&done](){++ done;});
  384. pool.Wait();
  385. BOOST_CHECK_EQUAL(done.load(), 200);
  386. }
  387. BOOST_AUTO_TEST_CASE(WaitWithoutTasksReturns){
  388. TaskPool pool(2);
  389. pool.Wait();
  390. BOOST_CHECK_EQUAL(pool.Threads(), 2u);
  391. }
  392. BOOST_AUTO_TEST_CASE(RunsInParallel){
  393. // Tasks that wait for each other can only finish if they really run at the same time.
  394. std::atomic<int> arrived(0);
  395. TaskPool pool(3);
  396. for (int i = 0; i < 3; ++ i){
  397. pool.Run([&arrived](){
  398. ++ arrived;
  399. const auto limit = std::chrono::steady_clock::now() + std::chrono::seconds(5);
  400. while (arrived < 3 && std::chrono::steady_clock::now() < limit) std::this_thread::yield();
  401. });
  402. }
  403. pool.Wait();
  404. BOOST_CHECK_EQUAL(arrived.load(), 3);
  405. }
  406. BOOST_AUTO_TEST_CASE(UsesSeveralThreads){
  407. std::mutex mutex;
  408. std::set<std::thread::id> threads;
  409. TaskPool pool(4);
  410. for (int i = 0; i < 50; ++ i){
  411. pool.Run([&](){
  412. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  413. std::lock_guard<std::mutex> lock(mutex);
  414. threads.insert(std::this_thread::get_id());
  415. });
  416. }
  417. pool.Wait();
  418. BOOST_CHECK_GT(threads.size(), 1u);
  419. BOOST_CHECK_LE(threads.size(), 4u);
  420. }
  421. BOOST_AUTO_TEST_CASE(DestructorFinishesPendingTasks){
  422. std::atomic<int> done(0);
  423. {
  424. TaskPool pool(2);
  425. for (int i = 0; i < 20; ++ i) pool.Run([&done](){
  426. std::this_thread::sleep_for(std::chrono::milliseconds(2));
  427. ++ done;
  428. });
  429. }
  430. BOOST_CHECK_EQUAL(done.load(), 20);
  431. }
  432. BOOST_AUTO_TEST_CASE(ATaskThatThrowsDoesNotStopThePool){
  433. std::atomic<int> done(0);
  434. TaskPool pool(2);
  435. pool.Run([](){throw std::runtime_error("boom");});
  436. for (int i = 0; i < 10; ++ i) pool.Run([&done](){++ done;});
  437. pool.Wait();
  438. BOOST_CHECK_EQUAL(done.load(), 10);
  439. }
  440. BOOST_AUTO_TEST_CASE(DefaultNumberOfThreadsIsReasonable){
  441. TaskPool pool;
  442. BOOST_CHECK_GE(pool.Threads(), 1u);
  443. BOOST_CHECK_LE(pool.Threads(), 8u);
  444. }
  445. BOOST_AUTO_TEST_SUITE_END()
  446. // ---------------------------------------------------------------------------------------------------------------
  447. // Videos of the game disks.
  448. // ---------------------------------------------------------------------------------------------------------------
  449. BOOST_AUTO_TEST_SUITE(VideoFilesTests)
  450. BOOST_AUTO_TEST_CASE(RecognizesMovies){
  451. std::string name;
  452. BOOST_CHECK(VideoFiles::IsMovie("ff7/movies/bike.avi", &name));
  453. BOOST_CHECK_EQUAL(name, "bike.avi");
  454. // The disks mix the case, and ISO9660 adds version numbers and dots.
  455. BOOST_CHECK(VideoFiles::IsMovie("FF7/MOVIES/SQLOGO.AVI;1", &name));
  456. BOOST_CHECK_EQUAL(name, "sqlogo.avi");
  457. BOOST_CHECK(VideoFiles::IsMovie("/ff7/Movies/Explode.avi", &name));
  458. BOOST_CHECK_EQUAL(name, "explode.avi");
  459. BOOST_CHECK(VideoFiles::IsMovie("ff7\\movies\\ending1.avi", &name));
  460. BOOST_CHECK_EQUAL(name, "ending1.avi");
  461. // Other files of the folder are copied too (the camera data of the movies).
  462. BOOST_CHECK(VideoFiles::IsMovie("ff7/movies/moviecam.lgp", &name));
  463. BOOST_CHECK_EQUAL(name, "moviecam.lgp");
  464. }
  465. BOOST_AUTO_TEST_CASE(IgnoresEverythingElse){
  466. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies"));
  467. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies/"));
  468. BOOST_CHECK(!VideoFiles::IsMovie("ff7/movies/sub/bike.avi"));
  469. BOOST_CHECK(!VideoFiles::IsMovie("ff7/field/char.lgp"));
  470. BOOST_CHECK(!VideoFiles::IsMovie("ff7/moviesx/bike.avi"));
  471. BOOST_CHECK(!VideoFiles::IsMovie("data/movies/bike.avi"));
  472. BOOST_CHECK(!VideoFiles::IsMovie(""));
  473. std::string name = "untouched";
  474. BOOST_CHECK(!VideoFiles::IsMovie("ff7/wm/x.avi", &name));
  475. BOOST_CHECK_EQUAL(name, "untouched");
  476. }
  477. BOOST_AUTO_TEST_CASE(NormalizesPaths){
  478. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("/FF7/Movies/A.AVI;1"), "ff7/movies/a.avi");
  479. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("FF7/MOVIES/A."), "ff7/movies/a");
  480. // A semicolon that is not a version stays.
  481. BOOST_CHECK_EQUAL(VideoFiles::NormalizePath("a;b"), "a;b");
  482. }
  483. BOOST_AUTO_TEST_CASE(LogCanBeAppended){
  484. TempDir dir;
  485. InstallLog& log = InstallLog::Get();
  486. log.Open(dir.Root() + "installer.log");
  487. log.Add("first run");
  488. log.Close();
  489. log.Open(dir.Root() + "installer.log", true);
  490. log.Add("second run");
  491. log.Close();
  492. std::ifstream file(dir.Root() + "installer.log");
  493. std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  494. BOOST_CHECK(content.find("first run") != std::string::npos);
  495. BOOST_CHECK(content.find("second run") != std::string::npos);
  496. // Without append the file starts over.
  497. log.Open(dir.Root() + "installer.log");
  498. log.Close();
  499. std::ifstream again(dir.Root() + "installer.log");
  500. std::string content2((std::istreambuf_iterator<char>(again)), std::istreambuf_iterator<char>());
  501. BOOST_CHECK(content2.empty());
  502. }
  503. BOOST_AUTO_TEST_SUITE_END()