MovieCamera.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <cstring>
  17. #include "installer/MovieCamera.h"
  18. namespace{
  19. std::vector<uint8_t> Frames(const int count, const int first_with_camera){
  20. std::vector<uint8_t> camera(count * MovieCamera::FRAME_SIZE, 0);
  21. for (int frame = first_with_camera; first_with_camera >= 0 && frame < count; ++ frame)
  22. camera[frame * MovieCamera::FRAME_SIZE] = 1;
  23. // Every frame has the projection, which doesn't count.
  24. for (int frame = 0; frame < count; ++ frame)
  25. camera[frame * MovieCamera::FRAME_SIZE + 36] = 1;
  26. return camera;
  27. }
  28. void Append(std::vector<uint8_t>& data, const void* source, const size_t size){
  29. const uint8_t* bytes = static_cast<const uint8_t*>(source);
  30. data.insert(data.end(), bytes, bytes + size);
  31. }
  32. // An archive with one file.
  33. std::vector<uint8_t> Archive(const std::string& name, const std::vector<uint8_t>& content){
  34. std::vector<uint8_t> archive(12, 0);
  35. const uint32_t count = 1;
  36. Append(archive, &count, 4);
  37. char entry_name[20] = {};
  38. std::memcpy(entry_name, name.c_str(), name.size());
  39. Append(archive, entry_name, 20);
  40. const uint32_t offset = 16 + 27;
  41. Append(archive, &offset, 4);
  42. archive.push_back(0);
  43. archive.push_back(0);
  44. archive.push_back(0);
  45. Append(archive, entry_name, 20);
  46. const uint32_t size = static_cast<uint32_t>(content.size());
  47. Append(archive, &size, 4);
  48. archive.insert(archive.end(), content.begin(), content.end());
  49. return archive;
  50. }
  51. }
  52. BOOST_AUTO_TEST_CASE(TestMovieCameraFirstFrameWithACamera){
  53. BOOST_CHECK_EQUAL(MovieCamera::FirstCameraFrame(Frames(10, 4)), 4);
  54. BOOST_CHECK_EQUAL(MovieCamera::FirstCameraFrame(Frames(10, 0)), 0);
  55. // The projection alone is not a camera.
  56. BOOST_CHECK_EQUAL(MovieCamera::FirstCameraFrame(Frames(10, -1)), -1);
  57. BOOST_CHECK_EQUAL(MovieCamera::FirstCameraFrame({}), -1);
  58. }
  59. BOOST_AUTO_TEST_CASE(TestMovieCameraReadsAFileOfAnArchive){
  60. const std::vector<uint8_t> camera = Frames(5, 3);
  61. const std::vector<uint8_t> archive = Archive("opening.cam", camera);
  62. std::vector<uint8_t> content;
  63. BOOST_REQUIRE(MovieCamera::ReadLgpFile(archive, "opening.cam", content));
  64. BOOST_CHECK(content == camera);
  65. BOOST_CHECK(!MovieCamera::ReadLgpFile(archive, "other.cam", content));
  66. // A truncated archive is not read.
  67. std::vector<uint8_t> truncated(archive.begin(), archive.end() - 10);
  68. BOOST_CHECK(!MovieCamera::ReadLgpFile(truncated, "opening.cam", content));
  69. BOOST_CHECK(!MovieCamera::ReadLgpFile({}, "opening.cam", content));
  70. }