rotate_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // rotate_test.cpp
  3. // datamodel
  4. //
  5. // Created by Sam Jaffe on 9/30/15.
  6. //
  7. //
  8. #include <gtest/gtest.h>
  9. #include <gmock/gmock.h>
  10. #include "rotate/rotate.hpp"
  11. struct Point {
  12. float x = 0.f, y = 0.f;
  13. Point() = default;
  14. Point(float _x, float _y) : x(_x), y(_y) {}
  15. bool operator==(const Point& other) const {
  16. return !memcmp(this, &other, sizeof(*this));
  17. }
  18. };
  19. struct Dimension {
  20. float width = 0.f, height = 0.f;
  21. Dimension() = default;
  22. Dimension(float w, float h) : width(w), height(h) {}
  23. bool operator==(const Dimension& other) const {
  24. return !memcmp(this, &other, sizeof(*this));
  25. }
  26. };
  27. struct Material {
  28. int id = -1;
  29. Material() = default;
  30. Material(int i) : id(i) {}
  31. bool operator==(const Material& other) const {
  32. return !memcmp(this, &other, sizeof(*this));
  33. }
  34. };
  35. struct RenderInfo {
  36. Point center;
  37. Material material;
  38. Dimension texture_size, object_size;
  39. Point bottom_left, bottom_right, top_right, top_left;
  40. float rotation = 0.0f; // TODO
  41. RenderInfo(Point c, Dimension sz, Material mat = {0}, Dimension tx_sz = {1.f, 1.f})
  42. : center(c)
  43. , material(mat)
  44. , texture_size(tx_sz)
  45. , object_size(sz)
  46. , bottom_left ({c.x - sz.width / 2.f, c.y - sz.height / 2.f})
  47. , bottom_right({c.x - sz.width / 2.f, c.y + sz.height / 2.f})
  48. , top_right ({c.x + sz.width / 2.f, c.y + sz.height / 2.f})
  49. , top_left ({c.x + sz.width / 2.f, c.y - sz.height / 2.f})
  50. {
  51. }
  52. RenderInfo() = default;
  53. bool operator==(const RenderInfo& other) const {
  54. return !memcmp(this, &other, sizeof(*this));
  55. }
  56. };
  57. DEFINE_ROTATE(RenderInfo, (material)(texture_size)(bottom_left)(bottom_right)(top_right)(top_left));
  58. DEFINE_ROTATE_NAME(simple_rotate, RenderInfo, (material)(texture_size)(center)(object_size));
  59. DEFINE_PROXY_NAME(RenderInfo_position_proxy, RenderInfo, (center)(object_size));
  60. DEFINE_PROXY_CONVERSION(RenderInfo, (material)(texture_size)(center)(object_size), (center)(object_size) );
  61. TEST(RotateTest, CanCreateNormalProxyForData) {
  62. RenderInfo_rotate rdata;
  63. RenderInfo info{{5, 6}, {1, 1}};
  64. rdata.push_back(info);
  65. RenderInfo_proxy proxy = rdata.get(0);
  66. EXPECT_THAT(info.texture_size, proxy.texture_size);
  67. EXPECT_THAT(info.bottom_left, proxy.bottom_left);
  68. EXPECT_THAT(info.bottom_right, proxy.bottom_right);
  69. EXPECT_THAT(info.top_left, proxy.top_left);
  70. EXPECT_THAT(info.top_right, proxy.top_right);
  71. }
  72. TEST(RotateTest, CanCreatePartialProxy) {
  73. simple_rotate rdata;
  74. RenderInfo info{{5, 6}, {1, 1}};
  75. rdata.push_back(info);
  76. simple_rotate_proxy proxy = rdata.get(0);
  77. EXPECT_THAT(info.texture_size, proxy.texture_size);
  78. EXPECT_THAT(info.center, proxy.center);
  79. EXPECT_THAT(info.object_size, proxy.object_size);
  80. }
  81. TEST(RotateTest, RotatedDataModelExistsAsStructOfVectors) {
  82. simple_rotate rdata;
  83. RenderInfo infos[3] = {{{1,2},{1,1}},{{0,3},{1,1}},{{4,3.2f},{1,1}}};
  84. rdata.push_back(infos[0]);
  85. rdata.push_back(infos[1]);
  86. rdata.push_back(infos[2]);
  87. EXPECT_THAT(rdata.get(access_t(RenderInfo, center){}),
  88. (std::vector<Point>{{1,2},{0,3},{4,3.2f}}));
  89. }
  90. TEST(RotateTest, CanCreateMultipleProxiesForSameObject) {
  91. simple_rotate rdata;
  92. RenderInfo info{{5, 6}, {1, 1}};
  93. rdata.push_back(info);
  94. RenderInfo_position_proxy proxy =
  95. rdata.getp<rotate_members(RenderInfo, (center)(object_size))>(0);
  96. EXPECT_THAT( sizeof(proxy), sizeof(Point&)+sizeof(Dimension&) );
  97. EXPECT_THAT(info.center, proxy.center);
  98. EXPECT_THAT(info.object_size, proxy.object_size);
  99. }