Fix rounding in phased_loop

For 1 nanosecond every period, it would CHECK-fail.

Change-Id: I5b9d73eab87f84169697996923edb9e56c85ac2d
diff --git a/aos/util/phased_loop_test.cc b/aos/util/phased_loop_test.cc
index 4a80522..1c766c4 100644
--- a/aos/util/phased_loop_test.cc
+++ b/aos/util/phased_loop_test.cc
@@ -2,13 +2,14 @@
 
 #include "gtest/gtest.h"
 
-#include "aos/testing/test_logging.h"
+#include "aos/time/time.h"
 
 namespace aos {
 namespace time {
 namespace testing {
 
 using ::std::chrono::milliseconds;
+using ::std::chrono::nanoseconds;
 
 typedef ::testing::Test PhasedLoopTest;
 typedef PhasedLoopTest PhasedLoopDeathTest;
@@ -191,6 +192,31 @@
       ".*interval > monotonic_clock::duration\\(0\\).*");
 }
 
+// Tests that every single value within two intervals of 0 works.
+// This is good at finding edge cases in the rounding.
+TEST_F(PhasedLoopTest, SweepingZero) {
+  for (int i = -30; i < -20; ++i) {
+    PhasedLoop loop(nanoseconds(20),
+                    monotonic_clock::epoch() - nanoseconds(30));
+    EXPECT_EQ(1, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
+  }
+  for (int i = -20; i < 0; ++i) {
+    PhasedLoop loop(nanoseconds(20),
+                    monotonic_clock::epoch() - nanoseconds(30));
+    EXPECT_EQ(2, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
+  }
+  for (int i = 0; i < 20; ++i) {
+    PhasedLoop loop(nanoseconds(20),
+                    monotonic_clock::epoch() - nanoseconds(30));
+    EXPECT_EQ(3, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
+  }
+  for (int i = 20; i < 30; ++i) {
+    PhasedLoop loop(nanoseconds(20),
+                    monotonic_clock::epoch() - nanoseconds(30));
+    EXPECT_EQ(4, loop.Iterate(monotonic_clock::epoch() + nanoseconds(i)));
+  }
+}
+
 }  // namespace testing
 }  // namespace time
 }  // namespace aos