KernelData.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <boost/test/unit_test.hpp>
  16. #include <vector>
  17. #include "installer/extractors/KernelDataParts.h"
  18. #include "installer/extractors/KernelTextList.h"
  19. #include "installer/tables/Characters.h"
  20. /**
  21. * Retrieves the byte that stands for a character in the texts of the kernel.
  22. */
  23. static u8 Encode(const char character){
  24. for (int code = 0; code < 256; code ++)
  25. if (ENGLISH_CHARS[code] == static_cast<unsigned char>(character)) return code;
  26. BOOST_FAIL("Character without a code.");
  27. return 0;
  28. }
  29. BOOST_AUTO_TEST_CASE(TestKernelTextListReadsNamesInOrderAndChangesQuotes){
  30. // Two offsets, and the texts "AB" and a double quote.
  31. const std::vector<u8> section = {
  32. 4, 0, 7, 0, Encode('A'), Encode('B'), 0xFF, Encode('"'), 0xFF
  33. };
  34. KernelTextList names(File(section.data(), 0, section.size()));
  35. BOOST_CHECK_EQUAL(names.NextName(), "AB");
  36. BOOST_CHECK_EQUAL(names.NextName(), "'");
  37. }
  38. BOOST_AUTO_TEST_CASE(TestKernelTextListExpandsCompressedDescriptions){
  39. // "ABCD", and a reference to the four bytes that start four bytes before the 0xF9 byte.
  40. const std::vector<u8> section = {
  41. 2, 0, Encode('A'), Encode('B'), Encode('C'), Encode('D'), 0xF9, 3, 0xFF
  42. };
  43. KernelTextList descriptions(File(section.data(), 0, section.size()));
  44. BOOST_CHECK_EQUAL(descriptions.NextDescription(), "ABCDABCD");
  45. }
  46. BOOST_AUTO_TEST_CASE(TestKernelDataBitsOfElementsAndUsers){
  47. std::vector<int> elements;
  48. KernelData::ReadElements(0x8001, elements);
  49. BOOST_REQUIRE_EQUAL(elements.size(), 2u);
  50. BOOST_CHECK_EQUAL(elements[0], static_cast<int>(KernelData::FIRE));
  51. BOOST_CHECK_EQUAL(elements[1], static_cast<int>(KernelData::HIDDEN));
  52. // All of them means none.
  53. elements.clear();
  54. KernelData::ReadElements(0xFFFF, elements);
  55. BOOST_CHECK(elements.empty());
  56. // Only eleven characters.
  57. std::vector<u8> users;
  58. KernelData::ReadUsers(0xFFFF, users);
  59. BOOST_CHECK_EQUAL(users.size(), 11u);
  60. }
  61. BOOST_AUTO_TEST_CASE(TestKernelDataStatusEffects){
  62. KernelData::StatusEffect cure;
  63. KernelData::ReadStatusEffect(0x40 | 63, 0x8, cure);
  64. BOOST_CHECK_EQUAL(cure.mode, KernelData::CURE);
  65. BOOST_CHECK_EQUAL(cure.chance, 63);
  66. BOOST_REQUIRE_EQUAL(cure.status.size(), 1u);
  67. BOOST_CHECK_EQUAL(cure.status[0], static_cast<int>(KernelData::POISONED));
  68. // Records that don't change statuses have all of them marked.
  69. KernelData::StatusEffect none;
  70. KernelData::ReadStatusEffect(0xFF, 0xFFFFFFFF, none);
  71. BOOST_CHECK(none.status.empty());
  72. }
  73. BOOST_AUTO_TEST_CASE(TestKernelDataTargets){
  74. KernelData::Target target;
  75. // All rows, and not the enemies by default.
  76. KernelData::ReadTarget(0x40, target);
  77. BOOST_CHECK(target.all_rows);
  78. BOOST_CHECK(!target.default_enemy);
  79. KernelData::ReadTarget(0x02, target);
  80. BOOST_CHECK(!target.all_rows);
  81. BOOST_CHECK(target.default_enemy);
  82. }