mission_event_batch_test.cpp 1006 B

123456789101112131415161718192021222324
  1. #include <cassert>
  2. #include <vector>
  3. #include "../Sunrise/src/server/activity/mission/mission_script_event_batch.h"
  4. int main() {
  5. using sunrise::server::activity::mission::drain_script_event_batch;
  6. std::vector<unsigned> delivered;
  7. unsigned next = 0;
  8. bool outputPending = false;
  9. const auto ready = [&] { return next < 1000 && !outputPending; };
  10. const auto dispatch = [&] {
  11. delivered.push_back(next++);
  12. if (next == 71) outputPending = true;
  13. };
  14. assert(drain_script_event_batch(ready, dispatch) == 64);
  15. assert(drain_script_event_batch(ready, dispatch) == 7);
  16. assert(drain_script_event_batch(ready, dispatch) == 0);
  17. assert(next == 71); // The callback after a command waits for its commit.
  18. outputPending = false;
  19. unsigned ticks = 0;
  20. while (ready()) { drain_script_event_batch(ready, dispatch); ++ticks; }
  21. assert(ticks == 15 && delivered.size() == 1000);
  22. for (unsigned i = 0; i < delivered.size(); ++i) assert(delivered[i] == i);
  23. }