melody.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <cassert>
  2. #include <chrono>
  3. #include "simple/motion/movement.hpp"
  4. #include "simple/motion/melody.hpp"
  5. using namespace std::literals;
  6. using duration = std::chrono::duration<float>;
  7. using simple::support::way;
  8. using simple::motion::melody;
  9. void SharpTurn()
  10. {
  11. // TODO: use rational, can't rely on float
  12. using movement = simple::motion::movement<duration,float,float>;
  13. struct float2 { float x, y; };
  14. float2 block{0,0};
  15. auto sharp_turn = melody(
  16. movement{1250ms, 0,1},
  17. movement{1250ms, 0,1}
  18. );
  19. auto range = sharp_turn.move(std::forward_as_tuple(block.x, block.y), 500ms).updated;
  20. assert( block.x == way(0.f,1.f, 0.5f / 1.25f) );
  21. assert( block.y == 0.f );
  22. assert( range.upper() - range.lower() == 1 );
  23. range = sharp_turn.move(std::forward_as_tuple(block.x, block.y), 500ms).updated;
  24. assert( block.x == way(0.f,1.f, 1.f / 1.25f) );
  25. assert( block.y == 0.f );
  26. assert( range.upper() - range.lower() == 1 );
  27. range = sharp_turn.move(std::forward_as_tuple(block.x, block.y), 500ms).updated;
  28. assert( block.x == way(0.f,1.f, 1.f) );
  29. assert( block.y == way(0.f,1.f, 0.25f / 1.25f) );
  30. assert( range.upper() - range.lower() == 2 );
  31. range = sharp_turn.move(std::forward_as_tuple(block.x, block.y), 500ms).updated;
  32. assert( block.x == way(0.f,1.f, 1.f) );
  33. assert( block.y == way(0.f,1.f, 0.75f / 1.25f) );
  34. assert( range.upper() - range.lower() == 1 );
  35. }
  36. void Advance()
  37. {
  38. // TODO: use rational, can't rely on float
  39. using movement = simple::motion::movement<std::chrono::milliseconds,float,float>;
  40. auto sequence = melody(
  41. movement{10ms, 0,1},
  42. movement{10ms, 0,1},
  43. movement{10ms, 0,1},
  44. movement{10ms, 0,1}
  45. );
  46. auto result = sequence.advance(10ms);
  47. assert( not result.done );
  48. assert( result.updated.lower() == 0 );
  49. assert( result.updated.upper() == 1 );
  50. assert( result.remaining == 0ms );
  51. result = sequence.advance(10ms);
  52. assert( not result.done );
  53. assert( result.updated.lower() == 1 );
  54. assert( result.updated.upper() == 2 );
  55. assert( result.remaining == 0ms );
  56. result = sequence.advance(10ms);
  57. assert( not result.done );
  58. assert( result.updated.lower() == 2 );
  59. assert( result.updated.upper() == 3 );
  60. assert( result.remaining == 0ms );
  61. result = sequence.advance(10ms);
  62. assert( result.done );
  63. assert( result.updated.lower() == 3 );
  64. assert( result.updated.upper() == 4 );
  65. assert( result.remaining == 0ms );
  66. sequence.reset();
  67. result = sequence.advance(15ms);
  68. assert( not result.done );
  69. assert( result.updated.lower() == 0 );
  70. assert( result.updated.upper() == 2 );
  71. assert( result.remaining == 0ms );
  72. result = sequence.advance(15ms);
  73. assert( not result.done );
  74. assert( result.updated.lower() == 1 );
  75. assert( result.updated.upper() == 3 );
  76. assert( result.remaining == 0ms );
  77. result = sequence.advance(15ms);
  78. assert( result.done );
  79. assert( result.updated.lower() == 3 );
  80. assert( result.updated.upper() == 4 );
  81. assert( result.remaining == 5ms );
  82. }
  83. void EmptyIsDone()
  84. {
  85. using movement = simple::motion::movement<std::chrono::milliseconds,float,float>;
  86. using melody_t = melody<movement, movement>;
  87. melody_t m;
  88. assert(m.done());
  89. }
  90. int main()
  91. {
  92. SharpTurn();
  93. Advance();
  94. EmptyIsDone();
  95. return 0;
  96. }